Skip to content

Commit 026145c

Browse files
committed
feat: more test coverage.
1 parent d19dede commit 026145c

File tree

5 files changed

+109
-51
lines changed

5 files changed

+109
-51
lines changed

Demos/Sharpie.Demos.Font/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4040
using var terminal = new Terminal(CursesBackend.Load(), new(CaretMode: CaretMode.Invisible, AllocateHeader: true));
4141

4242
// Setup the message and a number of rotating styles that will be applied for each letter of the message.
43-
var message = "\x001 Let the ASCII fun begin! \x003";
43+
var message = "A\x0100B";
4444
var styles = Enumerable.Range(0, message.Length)
4545
.Select(i => new Style
4646
{

Sharpie.Tests/DosCp866AsciiFontTests.cs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,36 +54,44 @@ public void HasGlyph_ChecksIfGlyphInRange(int c, bool t)
5454
[TestMethod] public void Layout_ReturnsTheExpectedValue() { _font.Layout.ShouldBe(AsciiFontLayout.FullWidth); }
5555

5656
[TestMethod]
57-
public void GetGlyph_ReturnsTheExpectedGlyph_IfFound()
57+
public void FullWidth_ReturnsAnInstance()
5858
{
59-
var glyph = _font.GetGlyph(new('A'), _style1);
60-
var contents = glyph.GetContents();
61-
62-
var cols = new[,]
63-
{
64-
{ (new('▗'), _style1), (new('█'), _style1), (new('█'), _style1), (new('▀'), _style1) },
65-
{ (new('█'), _style1), (new(' '), _style1), (new('▀'), _style1), (new(' '), _style1) },
66-
{ (new('▖'), _style1), (new('█'), _style1), (new('█'), _style1), (new('▀'), _style1) },
67-
{ (new(' '), _style1), (new(' '), _style1), (new(' '), _style1), (new Rune(' '), _style1) }
68-
};
69-
70-
contents.ShouldBe(cols);
59+
DosCp866AsciiFont.FullWidth.ShouldNotBeNull();
60+
DosCp866AsciiFont.FullWidth.ShouldBe(DosCp866AsciiFont.FullWidth);
7161
}
72-
62+
7363
[TestMethod]
74-
public void GetGlyph_ReturnsTheDefault_IfNotFound()
64+
public void GetGlyphs_Throws_IfSpanIsEmpty()
7565
{
76-
var glyph = _font.GetGlyph(new(256), _style1);
77-
var contents = glyph.GetContents();
66+
Should.Throw<ArgumentException>(() =>
67+
_font.GetGlyphs(Array.Empty<Rune>(), _style1));
68+
}
69+
70+
[TestMethod]
71+
public void GetGlyphs_ReturnsTheExpectedGlyphsIncludingDefault()
72+
{
73+
var k = new[] { new Rune('A'), new Rune(256), new Rune('B') };
74+
75+
var glyphs = _font.GetGlyphs(k, _style1);
76+
var contents = glyphs.GetContents();
7877

7978
var cols = new[,]
8079
{
80+
{ (new('▗'), _style1), (new('█'), _style1), (new('█'), _style1), (new('▀'), _style1) },
81+
{ (new('█'), _style1), (new(' '), _style1), (new('▀'), _style1), (new(' '), _style1) },
82+
{ (new('▖'), _style1), (new('█'), _style1), (new('█'), _style1), (new('▀'), _style1) },
83+
{ (new(' '), _style1), (new(' '), _style1), (new(' '), _style1), (new Rune(' '), _style1) },
8184
{ (new('┌'), _style1), (new('│'), _style1), (new('│'), _style1), (new('└'), _style1) },
8285
{ (new('─'), _style1), (new(' '), _style1), (new(' '), _style1), (new('─'), _style1) },
8386
{ (new('─'), _style1), (new(' '), _style1), (new(' '), _style1), (new('─'), _style1) },
84-
{ (new('┐'), _style1), (new('│'), _style1), (new('│'), _style1), (new Rune('┘'), _style1) }
87+
{ (new('┐'), _style1), (new('│'), _style1), (new('│'), _style1), (new Rune('┘'), _style1) },
88+
{ (new('▜'), _style1), (new('▐'), _style1), (new('▐'), _style1), (new('▀'), _style1) },
89+
{ (new('▛'), _style1), (new('▙'), _style1), (new('▌'), _style1), (new('▀'), _style1) },
90+
{ (new('▜'), _style1), (new('▟'), _style1), (new('▐'), _style1), (new('▀'), _style1) },
91+
{ (new('▖'), _style1), (new('▘'), _style1), (new('▌'), _style1), (new Rune(' '), _style1) }
8592
};
8693

94+
8795
contents.ShouldBe(cols);
8896
}
8997
}

Sharpie.Tests/FigletFontTests.cs

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,58 @@ public void HasGlyph_ReturnsTrue_IfCharDefined()
8585
}
8686

8787
[TestMethod]
88-
public void GetGlyph_ReturnsTheProperGlyph_IfFound()
88+
public void GetGlyphs_Throws_IfSpanIsEmpty()
8989
{
90-
var glyph = _font.GetGlyph(new(1), Style1);
90+
Should.Throw<ArgumentException>(() =>
91+
_font.GetGlyphs(Array.Empty<Rune>(), Style1));
92+
}
93+
94+
[TestMethod]
95+
public void GetGlyphs_ReturnsTheExpectedGlyphsIncludingDefault_GreaterWidth()
96+
{
97+
var k = new[] { new Rune(1), new Rune(256), new Rune(3) };
98+
99+
var glyph = _font.GetGlyphs(k, Style1);
91100
var contents = glyph.GetContents();
92101

93102
var cols = new[,]
94103
{
95104
{ (new Rune('1'), Style1), (new('4'), Style1), (new('7'), Style1) },
96105
{ (new('2'), Style1), (new('5'), Style1), (new('8'), Style1) },
97-
{ (new('3'), Style1), (new('6'), Style1), (new('9'), Style1) }
106+
{ (new('3'), Style1), (new('6'), Style1), (new('9'), Style1) },
107+
108+
{ (new('a'), Style1), (new('d'), Style1), (new('g'), Style1) },
109+
{ (new('b'), Style1), (new('e'), Style1), (new('h'), Style1) },
110+
{ (new('c'), Style1), (new('f'), Style1), (new('i'), Style1) },
111+
{ (new('1'), Style1), (new('2'), Style1), (new('3'), Style1) },
112+
113+
{ (new('1'), Style1), (new(' '), Style1), (new('3'), Style1) }
98114
};
99-
115+
100116
contents.ShouldBe(cols);
101117
}
102-
118+
103119
[TestMethod]
104-
public void GetGlyph_ReturnsTheDefaultGlyph_IfNotFound()
120+
public void GetGlyphs_ReturnsTheExpectedGlyphsIncludingReplacement_GreaterWidth()
105121
{
106-
var glyph = _font.GetGlyph(new(2), Style1);
107-
var contents = glyph.GetContents();
122+
var font = new FigletFont("name", Header, new Dictionary<int, (string[] rows, int width)>()
123+
{ { 1, Char1 }, { 3, Char0 } });
124+
125+
var k = new[] { new Rune(1), new Rune(256), new Rune(3) };
126+
127+
var glyphs = font.GetGlyphs(k, Style1);
128+
var contents = glyphs.GetContents();
108129

109130
var cols = new[,]
110131
{
132+
{ (new Rune('1'), Style1), (new('4'), Style1), (new('7'), Style1) },
133+
{ (new('2'), Style1), (new('5'), Style1), (new('8'), Style1) },
134+
{ (new('3'), Style1), (new('6'), Style1), (new('9'), Style1) },
135+
136+
{ (new('┌'), Style1), (new('│'), Style1), (new('└'), Style1) },
137+
{ (new('─'), Style1), (new(' '), Style1), (new('─'), Style1) },
138+
{ (new('┐'), Style1), (new('│'), Style1), (new Rune('┘'), Style1) },
139+
111140
{ (new Rune('a'), Style1), (new('d'), Style1), (new('g'), Style1) },
112141
{ (new('b'), Style1), (new('e'), Style1), (new('h'), Style1) },
113142
{ (new('c'), Style1), (new('f'), Style1), (new('i'), Style1) },
@@ -118,44 +147,67 @@ public void GetGlyph_ReturnsTheDefaultGlyph_IfNotFound()
118147
}
119148

120149
[TestMethod]
121-
public void GetGlyph_ReturnsReplacesHardBlanksWithWhitespaces()
150+
public void GetGlyphs_ReplacesHardBlanksWithWhitespaces()
122151
{
123-
var glyph = _font.GetGlyph(new(3), Style1);
124-
var contents = glyph.GetContents();
152+
var header = Header with { Height = 1, BaseLine = 1, Attributes = FigletAttribute.HorizontalSmushing };
153+
var font = new FigletFont("name", header, new Dictionary<int, (string[] rows, int width)>
154+
{
155+
{ 1, (new[] { "AB" }, 2) },
156+
{ 2, (new[] { "12" }, 2) }
157+
});
125158

126-
var cols = new[,] { { (new Rune('1'), Style1), (new(' '), Style1), (new('3'), Style1) } };
159+
var k = new[] { new Rune(1), new Rune(2) };
160+
161+
var glyphs = font.GetGlyphs(k, Style1);
162+
var contents = glyphs.GetContents();
163+
164+
var cols = new[,]
165+
{
166+
{ (new Rune('A'), Style1) },
167+
{ (new('1'), Style1) },
168+
{ (new('2'), Style1) }
169+
};
127170

128171
contents.ShouldBe(cols);
129172
}
130-
173+
174+
131175
[TestMethod]
132-
public void GetGlyph_ReturnsReplacementGlyph_IfNotFoundAndNoDefault_WithHeightGreaterThanOne()
176+
public void GetGlyphs_MergesCharactersUsingLayoutRules()
133177
{
134-
var font = new FigletFont("name", Header, new Dictionary<int, (string[] rows, int width)>());
135-
136-
var glyph = font.GetGlyph(new(2), Style1);
178+
var glyph = _font.GetGlyphs(new[] { new Rune(3) }, Style1);
137179
var contents = glyph.GetContents();
138180

139181
var cols = new[,]
140182
{
141-
{ (new('┌'), Style1), (new('│'), Style1), (new('└'), Style1) },
142-
{ (new('─'), Style1), (new(' '), Style1), (new('─'), Style1) },
143-
{ (new('┐'), Style1), (new('│'), Style1), (new Rune('┘'), Style1) }
183+
{ (new Rune('1'), Style1), (new(' '), Style1), (new('3'), Style1) }
144184
};
145185

146186
contents.ShouldBe(cols);
147187
}
148-
188+
149189
[TestMethod]
150-
public void GetGlyph_ReturnsReplacementGlyph_IfNotFoundAndNoDefault_WithWidthOfOne()
190+
public void GetGlyphs_ReturnsChars_WithWidthOfOne()
151191
{
152192
var header = Header with { Height = 1, BaseLine = 1 };
153-
var font = new FigletFont("name", header, new Dictionary<int, (string[] rows, int width)>());
193+
var font = new FigletFont("name", header,
194+
new Dictionary<int, (string[] rows, int width)>()
195+
{
196+
{'A', (new[] { "A" }, 1)},
197+
{'B', (new[] { "B" }, 1)}
198+
} );
154199

155-
var glyph = font.GetGlyph(new(2), Style1);
156-
var contents = glyph.GetContents();
200+
var k = new[] { new Rune('A'), new Rune(256), new Rune('B') };
157201

158-
var cols = new[,] { { (new Rune('□'), Style1) } };
202+
var glyphs = font.GetGlyphs(k, Style1);
203+
var contents = glyphs.GetContents();
204+
205+
var cols = new[,]
206+
{
207+
{ (new Rune('A'), Style1) },
208+
{ (new Rune('□'), Style1) },
209+
{ (new Rune('B'), Style1) },
210+
};
159211

160212
contents.ShouldBe(cols);
161213
}

Sharpie/Abstractions/IAsciiFont.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public interface IAsciiFont
7171
/// <param name="char">The character.</param>
7272
/// <param name="style">The style to apply to the glyph.</param>
7373
/// <returns>The output glyph, if found. Otherwise, the font will substitute the glyph with something else.</returns>
74-
IDrawable GetGlyph(Rune @char, Style style) => GetGlyphs(new[] { @char }, style);
74+
IDrawable GetGlyph(Rune @char, Style style);
7575

7676
/// <summary>
7777
/// Tries to get a drawing for a given list of <paramref name="chars" />.
@@ -89,7 +89,5 @@ public interface IAsciiFont
8989
/// <param name="style">The style to apply to the glyphs.</param>
9090
/// <returns>The output glyphs.</returns>
9191
/// <exception cref="ArgumentException">Thrown if <paramref name="text" /> is empty.</exception>
92-
IDrawable GetGlyphs(string text, Style style) =>
93-
GetGlyphs(text.EnumerateRunes()
94-
.ToArray(), style);
92+
IDrawable GetGlyphs(string text, Style style);
9593
}

Sharpie/Font/DosCp866AsciiFont.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public override IDrawable GetGlyphs(ReadOnlySpan<Rune> chars, Style style)
8888

8989
for (var i = 0; i < chars.Length; i++)
9090
{
91-
DrawGlyphOntoCanvas(canvas, i * Width * 2, chars[i], style);
91+
DrawGlyphOntoCanvas(canvas, i * Width, chars[i], style);
9292
}
9393

9494
return canvas;
@@ -110,7 +110,7 @@ private void DrawGlyphOntoCanvas(Canvas canvas, int offset, Rune @char, Style st
110110
{
111111
for (var y = 0; y < Lines; y++)
112112
{
113-
var ap = new PointF((x + offset) / 2F, y / 2F);
113+
var ap = new PointF((x + offset * 2) / 2F, y / 2F);
114114
if (shape[x, y])
115115
{
116116
canvas.Point(ap, style);

0 commit comments

Comments
 (0)