Skip to content

Commit bab9607

Browse files
Copilotboxofyellow
andauthored
Address PR review: extend IHeaderStyle, simplify renderer, FontPath, README tidy
Co-authored-by: boxofyellow <54955040+boxofyellow@users.noreply.github.com>
1 parent ae3efc5 commit bab9607

12 files changed

Lines changed: 1341 additions & 103 deletions

File tree

ConsoleMarkdownRenderer.Tests/FigletTextStyleTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,59 @@ public void TextStyle_Justification_IsNullViaIHeaderStyle()
7575
IHeaderStyle plain = new TextStyle(decoration: TextDecoration.Bold, foreground: TextColor.Red);
7676
Assert.IsNull(plain.Justification);
7777
}
78+
79+
[TestMethod]
80+
public void TextStyle_FontPath_IsNullViaIHeaderStyle()
81+
{
82+
// FontPath is explicitly implemented on TextStyle and must always return null --
83+
// custom FIGlet fonts are only meaningful for FigletTextStyle.
84+
IHeaderStyle plain = new TextStyle();
85+
Assert.IsNull(plain.FontPath);
86+
}
87+
88+
[TestMethod]
89+
public void TextStyle_ExposesForegroundBackgroundDecorationViaIHeaderStyle()
90+
{
91+
// Foreground/Background/Decoration on TextStyle are implicit interface members --
92+
// they round-trip through the IHeaderStyle interface unchanged.
93+
IHeaderStyle plain = new TextStyle(
94+
decoration: TextDecoration.Italic,
95+
foreground: TextColor.Red,
96+
background: TextColor.Blue);
97+
98+
Assert.AreEqual(TextDecoration.Italic, plain.Decoration);
99+
Assert.AreEqual(TextColor.Red, plain.Foreground);
100+
Assert.AreEqual(TextColor.Blue, plain.Background);
101+
}
102+
103+
[TestMethod]
104+
public void FigletTextStyle_BackgroundAndDecoration_HardCodedViaIHeaderStyle()
105+
{
106+
// FigletText does not support a background color or text decoration so those
107+
// members on the IHeaderStyle interface are hard-coded.
108+
IHeaderStyle figlet = new FigletTextStyle(foreground: TextColor.Green);
109+
110+
Assert.AreEqual(TextColor.Green, figlet.Foreground);
111+
Assert.IsNull(figlet.Background);
112+
Assert.AreEqual(TextDecoration.None, figlet.Decoration);
113+
}
114+
115+
[TestMethod]
116+
public void FigletTextStyle_FontPath_Preserved()
117+
{
118+
var style = new FigletTextStyle(fontPath: "/some/path/font.flf");
119+
120+
Assert.AreEqual("/some/path/font.flf", style.FontPath);
121+
Assert.AreEqual("/some/path/font.flf", ((IHeaderStyle)style).FontPath);
122+
}
123+
124+
[TestMethod]
125+
public void FigletTextStyle_Equality_DifferentFontPath()
126+
{
127+
var a = new FigletTextStyle(fontPath: "/a.flf");
128+
var b = new FigletTextStyle(fontPath: "/b.flf");
129+
130+
Assert.AreNotEqual(a, b);
131+
}
78132
}
79133
}

ConsoleMarkdownRenderer.Tests/RendererTests.cs

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ public void RendererTests_MarkedTest(bool useCrazy)
162162
[DataRow(false)]
163163
[DataRow(true)]
164164
public void RendererTests_HeaderTest(bool useCrazy)
165-
// The default DisplayOptions configures H1 as a centered FigletTextStyle, so H1's
166-
// literal text ("Level One") is replaced by FIGlet ASCII art and is not asserted
167-
// here. H2 and H3 still fall through to the default Header style.
165+
// The default DisplayOptions configures H1 as a FigletTextStyle, so H1's literal
166+
// text ("Level One") is replaced by FIGlet ASCII art and is not asserted here.
167+
// H2 and H3 still fall through to the default Header style.
168168
=> AssertMarkdownYieldsFormat(
169169
"headingBlock",
170170
text: useCrazy
171-
? "Level Two Level Three"
172-
: "## Level Two ## ### Level Three ###",
171+
? "Level Two with here Level Three with bold word"
172+
: "## Level Two with code here ## ### Level Three with bold word ###",
173173
new Style(decoration: Decoration.Bold | Decoration.Invert | Decoration.Underline),
174174
useCrazy);
175175

@@ -245,9 +245,9 @@ public void RendererTests_FigletHeaderOnlyAppliesToConfiguredLevel()
245245
Assert.DoesNotContain("Level One", output,
246246
$"H1 should be FIGlet rendered:\n{output}");
247247
// H2 and H3 should still be rendered as styled markup wrapped in '#'s.
248-
Assert.Contains("## Level Two ##", output,
248+
Assert.Contains("## Level Two with code here ##", output,
249249
$"H2 should remain styled with '##' wrapping:\n{output}");
250-
Assert.Contains("### Level Three ###", output,
250+
Assert.Contains("### Level Three with bold word ###", output,
251251
$"H3 should remain styled with '###' wrapping:\n{output}");
252252
}
253253

@@ -264,9 +264,56 @@ public void RendererTests_FigletDefaultCanBeOverriddenWithTextStyle()
264264
ConsoleUnderTest.Write(Renderer(GetResourceContent("headingBlock", "md"), options));
265265

266266
var output = ConsoleUnderTest.Output;
267-
Assert.Contains("# Level One #", output, $"H1 should be wrapped with '#':\n{output}");
268-
Assert.Contains("## Level Two ##", output, $"H2 should be wrapped with '##':\n{output}");
269-
Assert.Contains("### Level Three ###", output, $"H3 should be wrapped with '###':\n{output}");
267+
Assert.Contains("# Level One #", output, $"H1 should be wrapped with '#':\n{output}");
268+
Assert.Contains("## Level Two with code here ##", output, $"H2 should be wrapped with '##':\n{output}");
269+
Assert.Contains("### Level Three with bold word ###", output, $"H3 should be wrapped with '###':\n{output}");
270+
}
271+
272+
[TestMethod]
273+
public void RendererTests_FigletEmptyHeadingFallsBackToStyledMarkup()
274+
{
275+
// FigletText cannot render an empty string. When the heading has no text the
276+
// renderer should fall through to the styled-markup path so the level marker
277+
// (e.g. "# #" with WrapHeader=true) is still emitted.
278+
DisplayOptions options = new();
279+
// Sanity check: H1 is configured to use FIGlet by default.
280+
Assert.IsInstanceOfType<FigletTextStyle>(options.EffectiveHeader(1));
281+
282+
ConsoleUnderTest.Write(Renderer("#\n", options));
283+
284+
var output = ConsoleUnderTest.Output;
285+
Assert.Contains("#", output,
286+
$"Empty H1 should fall back to styled '#'-wrapped markup:\n{output}");
287+
}
288+
289+
[TestMethod]
290+
public void RendererTests_FigletFontPathLoadsCustomFont()
291+
{
292+
// When FontPath is set the renderer should load the custom .flf font and use it
293+
// to render the FIGlet text. Compare the output against the default font to make
294+
// sure something actually changed.
295+
const string markdown = "# Hi\n";
296+
var defaultOptions = new DisplayOptions();
297+
ConsoleUnderTest.Write(Renderer(markdown, defaultOptions));
298+
var defaultOutput = ConsoleUnderTest.Output;
299+
300+
NewConsole();
301+
302+
var fontPath = Path.Combine(DataPath, "fonts", "shadow.flf");
303+
Assert.IsTrue(File.Exists(fontPath), $"Test font file should exist at {fontPath}");
304+
305+
var customOptions = new DisplayOptions
306+
{
307+
Headers = new() { new FigletTextStyle(fontPath: fontPath) },
308+
};
309+
ConsoleUnderTest.Write(Renderer(markdown, customOptions));
310+
var customOutput = ConsoleUnderTest.Output;
311+
312+
Assert.AreNotEqual(defaultOutput, customOutput,
313+
$"FIGlet output should differ when a custom FontPath is supplied.\nDefault:\n{defaultOutput}\nCustom:\n{customOutput}");
314+
// FIGlet output should still contain ASCII-art glyph characters.
315+
Assert.IsTrue(customOutput.Contains('|') || customOutput.Contains('\\'),
316+
$"Expected FIGlet glyph characters in custom-font output:\n{customOutput}");
270317
}
271318

272319
[TestMethod]

0 commit comments

Comments
 (0)