Skip to content

Commit c97e833

Browse files
committed
fix: IOutput.Write(...) overloads now have argument IFormattingInfo set
1 parent af90b2e commit c97e833

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Text;
4+
using NUnit.Framework;
5+
using SmartFormat.Core.Extensions;
6+
using SmartFormat.Core.Formatting;
7+
using SmartFormat.Core.Output;
8+
using SmartFormat.ZString;
9+
10+
namespace SmartFormat.Tests.Core.Output;
11+
12+
internal class CustomOutput : IOutput
13+
{
14+
private readonly StringBuilder _sb = new(1000);
15+
private const string OutputFormat = "Format: {0}, Selector {1}, Formatted: {2}";
16+
17+
public override string ToString()
18+
{
19+
return _sb.ToString();
20+
}
21+
22+
public void Write(string text, IFormattingInfo? formattingInfo = null)
23+
{
24+
var fi = (FormattingInfo) formattingInfo!;
25+
_sb.AppendFormat(OutputFormat, fi.Format, fi.Selector, text.ToString());
26+
}
27+
28+
public void Write(ReadOnlySpan<char> text, IFormattingInfo? formattingInfo = null)
29+
{
30+
var fi = (FormattingInfo) formattingInfo!;
31+
_sb.AppendFormat(OutputFormat, fi.Format, fi.Selector, text.ToString());
32+
}
33+
34+
public void Write(ZStringBuilder stringBuilder, IFormattingInfo? formattingInfo = null)
35+
{
36+
var fi = (FormattingInfo) formattingInfo!;
37+
_sb.AppendFormat(OutputFormat, fi.Format, fi.Selector, stringBuilder);
38+
}
39+
}
40+
41+
[TestFixture]
42+
internal class CustomOutputTests
43+
{
44+
[Test]
45+
public void CustomOutput_GetsValid_FormattingInfo_Argument()
46+
{
47+
// This test ensures that the IOutput.Write method overloads
48+
// get a valid IFormattingInfo argument.
49+
var smart = Smart.CreateDefaultSmartFormat();
50+
var output = new CustomOutput();
51+
smart.FormatInto(output, CultureInfo.InvariantCulture, "{0:0.0000}", [9m]);
52+
Assert.That(output.ToString(),
53+
Is.EqualTo("Format: 0.0000, Selector 0, Formatted: 9.0000"));
54+
}
55+
}

src/SmartFormat.Tests/Core/Output/StringOutputTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public class StringOutputTests
1212
public void Output_Of_Span()
1313
{
1414
var so = new StringOutput();
15-
so.Write("text".AsSpan(), null!);
15+
so.Write("text".AsSpan(), null);
1616
Assert.That(so.ToString(), Is.EqualTo("text"));
1717
}
1818

1919
[Test]
2020
public void Output_Of_String()
2121
{
2222
var so = new StringOutput(16);
23-
so.Write("text", null!);
23+
so.Write("text", null);
2424
Assert.That(so.ToString(), Is.EqualTo("text"));
2525
}
2626

@@ -30,7 +30,7 @@ public void Output_Of_ValueStringBuilder()
3030
var so = new StringOutput();
3131
using var sb = ZStringBuilderUtilities.CreateZStringBuilder();
3232
sb.Append("text");
33-
so.Write(sb, null!);
33+
so.Write(sb, null);
3434
Assert.That(so.ToString(), Is.EqualTo("text"));
3535
}
3636
}

src/SmartFormat/Core/Formatting/FormattingInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void Write(ReadOnlySpan<char> text)
173173
{
174174
if (Alignment == 0)
175175
{
176-
FormatDetails.Output.Write(text);
176+
FormatDetails.Output.Write(text, this);
177177
FormatDetails.Formatter.Evaluator.OnOutputWritten?.Invoke(this,
178178
new Evaluator.OutputWrittenEventArgs(text.ToString()));
179179
return;
@@ -199,7 +199,7 @@ public void Write(ReadOnlySpan<char> text)
199199
}
200200

201201
// Write the aligned text to the output
202-
FormatDetails.Output.Write(buffer.GetSpan());
202+
FormatDetails.Output.Write(buffer.GetSpan(), this);
203203

204204
FormatDetails.Formatter.Evaluator.OnOutputWritten?.Invoke(this,
205205
new Evaluator.OutputWrittenEventArgs(buffer.ToString()));

0 commit comments

Comments
 (0)