Skip to content

Commit

Permalink
No more Cysharp.Text classes used in SmartFormat namespace (axuno#228)
Browse files Browse the repository at this point in the history
Breaking change to version/3.0-alpha.4
* Created ZStringBuilder as a wrapper around Utf16ValueStringBuilder
* Replaced occurrences of Utf16ValueStringBuilder with ZStringBuilder.
* Affects IOutput, NullOutput, StringOutput, TextWriterOoutput, ZStringOutput
  • Loading branch information
axunonb committed Mar 9, 2022
1 parent 5116203 commit 2a3398b
Show file tree
Hide file tree
Showing 19 changed files with 547 additions and 56 deletions.
5 changes: 3 additions & 2 deletions src/Demo.NetFramework/Sample Extensions/RTFOutput.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Drawing;
using Cysharp.Text;
using RTF;
using SmartFormat.Core.Extensions;
using SmartFormat.Core.Output;
using SmartFormat.ZString;

#nullable enable

namespace SmartFormat.Demo.Sample_Extensions
Expand Down Expand Up @@ -58,7 +59,7 @@ public void Write(string text, int startIndex, int length, IFormattingInfo? form
}

///<inheritdoc/>
public void Write(Utf16ValueStringBuilder stringBuilder, IFormattingInfo? formattingInfo)
public void Write(ZStringBuilder stringBuilder, IFormattingInfo? formattingInfo)
{
#if NETSTANDARD2_1
output.Append(stringBuilder.AsSpan());
Expand Down
4 changes: 2 additions & 2 deletions src/Demo/Sample Extensions/RTFOutput.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Drawing;
using Cysharp.Text;
using RTF;
using SmartFormat.Core.Extensions;
using SmartFormat.Core.Output;
using SmartFormat.ZString;

namespace SmartFormat.Demo.Sample_Extensions
{
Expand Down Expand Up @@ -58,7 +58,7 @@ public void Write(string text, int startIndex, int length, IFormattingInfo forma
}

///<inheritdoc/>
public void Write(Utf16ValueStringBuilder stringBuilder, IFormattingInfo formattingInfo)
public void Write(ZStringBuilder stringBuilder, IFormattingInfo formattingInfo)
{
#if NETSTANDARD2_1
output.Append(stringBuilder.AsSpan());
Expand Down
3 changes: 1 addition & 2 deletions src/SmartFormat.Tests/Core/Output/NullOutputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cysharp.Text;
using NUnit.Framework;
using SmartFormat.Core.Output;

Expand All @@ -30,7 +29,7 @@ public void Output_Of_String()
[Test]
public void Output_Of_ValueStringBuilder()
{
using var sb = ZString.CreateStringBuilder();
using var sb = SmartFormat.Utilities.ZStringExtensions.CreateStringBuilder();
sb.Append("text");
var so = new NullOutput();
Assert.DoesNotThrow(() =>so.Write(sb, null));
Expand Down
7 changes: 1 addition & 6 deletions src/SmartFormat.Tests/Core/Output/StringOutputTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cysharp.Text;
using NUnit.Framework;
using SmartFormat.Core.Output;

Expand Down Expand Up @@ -32,7 +27,7 @@ public void Output_Of_String()
public void Output_Of_ValueStringBuilder()
{
var so = new StringOutput();
using var sb = ZString.CreateStringBuilder();
using var sb = SmartFormat.Utilities.ZStringExtensions.CreateStringBuilder();
sb.Append("text");
so.Write(sb, null!);
Assert.AreEqual("text", so.ToString());
Expand Down
6 changes: 1 addition & 5 deletions src/SmartFormat.Tests/Core/Output/TextWriterOutputTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cysharp.Text;
using NUnit.Framework;
using SmartFormat.Core.Output;

Expand Down Expand Up @@ -36,7 +32,7 @@ public void Output_Of_String()
[Test]
public void Output_Of_ValueStringBuilder()
{
using var sb = ZString.CreateStringBuilder();
using var sb = SmartFormat.Utilities.ZStringExtensions.CreateStringBuilder();
sb.Append("text");
var sw = new StringWriter(new StringBuilder());
var two = new TextWriterOutput(sw);
Expand Down
5 changes: 2 additions & 3 deletions src/SmartFormat.Tests/Core/Output/ZStringOutputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cysharp.Text;
using NUnit.Framework;
using SmartFormat.Core.Output;

Expand All @@ -22,7 +21,7 @@ public void Create_With_Capacity()
[Test]
public void Create_With_Other_ValueStringBuilder()
{
using var vsb = ZString.CreateStringBuilder();
using var vsb = SmartFormat.Utilities.ZStringExtensions.CreateStringBuilder();
vsb.Append("text");
using var zStringOutput = new ZStringOutput(vsb);
Assert.That(zStringOutput, Is.Not.Null);
Expand All @@ -49,7 +48,7 @@ public void Output_Of_String()
public void Output_Of_ValueStringBuilder()
{
var so = new ZStringOutput();
using var sb = ZString.CreateStringBuilder();
using var sb = SmartFormat.Utilities.ZStringExtensions.CreateStringBuilder();
sb.Append("text");
so.Write(sb, null);
Assert.AreEqual("text", so.ToString());
Expand Down
5 changes: 3 additions & 2 deletions src/SmartFormat.Tests/Extensions/DefaultSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using SmartFormat.Core.Extensions;
using SmartFormat.Core.Formatting;
using SmartFormat.Extensions;
using SmartFormat.Tests.TestUtils;

namespace SmartFormat.Tests.Extensions
{
Expand All @@ -20,14 +21,14 @@ private class SourceImplementation : Source
public void Call_With_NonNumeric_Argument_Should_Fail()
{
var source = new DefaultSource();
Assert.That(source.TryEvaluateSelector(FormattingInfo.Create("{a}", new List<object?>())), Is.EqualTo(false));
Assert.That(source.TryEvaluateSelector(FormattingInfoExtensions.Create("{a}", new List<object?>())), Is.EqualTo(false));
}

[Test]
public void TryEvaluateSelector_Should_Fail()
{
var source = new SourceImplementation();
Assert.That(source.TryEvaluateSelector(FormattingInfo.Create("{Dummy}", new List<object?>())), Is.EqualTo(false));
Assert.That(source.TryEvaluateSelector(FormattingInfoExtensions.Create("{Dummy}", new List<object?>())), Is.EqualTo(false));
}
}
}
2 changes: 1 addition & 1 deletion src/SmartFormat.Tests/Extensions/NullFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void NullFormatter_Must_Not_Contain_Choose_Options()
public void NullFormatter_Format_Count_Must_Be_1_or_2()
{
var smart = GetFormatter();
Assert.That(() => smart.Format("{0:isnull:1|2|3}", 123), Throws.InstanceOf<FormattingException>(), "No format included");
Assert.That(delegate { return smart.Format("{0:isnull:1|2|3}", 123); }, Throws.InstanceOf<FormattingException>(), "No format included");
}
}
}
3 changes: 2 additions & 1 deletion src/SmartFormat.Tests/Extensions/SubStringFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using SmartFormat.Core.Formatting;
using SmartFormat.Tests.TestUtils;

namespace SmartFormat.Tests.Extensions
{
Expand Down Expand Up @@ -161,7 +162,7 @@ public void ImplicitFormatterEvaluation_With_Wrong_Args_Should_Fail()
var smart = GetFormatter();
Assert.That(
smart.GetFormatterExtension<SubStringFormatter>()!.TryEvaluateFormat(
FormattingInfo.Create("{0::(0,2)}", new List<object?>(new[] {new object()}))), Is.EqualTo(false));
FormattingInfoExtensions.Create("{0::(0,2)}", new List<object?>(new[] {new object()}))), Is.EqualTo(false));
}
}
}
3 changes: 2 additions & 1 deletion src/SmartFormat.Tests/Extensions/TimeFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public void UseTimeFormatter_WithIllegalLanguage()
var timeFormatter = smart.GetFormatterExtension<TimeFormatter>()!;
timeFormatter.FallbackLanguage = string.Empty;

Assert.That(() => smart.Format(CultureInfo.InvariantCulture, "{0:time:noless}", new TimeSpan(1, 2, 3)),
Assert.That(
delegate { return smart.Format(CultureInfo.InvariantCulture, "{0:time:noless}", new TimeSpan(1, 2, 3)); },
Throws.InstanceOf<FormattingException>().And.Message.Contains("TimeTextInfo could not be found"),
"Language as argument");
}
Expand Down
26 changes: 26 additions & 0 deletions src/SmartFormat.Tests/TestUtils/FormattingInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using SmartFormat.Core.Formatting;
using SmartFormat.Core.Output;
using SmartFormat.Core.Settings;

namespace SmartFormat.Tests.TestUtils
{
internal static class FormattingInfoExtensions
{
/// <summary>
/// Creates a new instance of <see cref="FormattingInfo"/>.
/// </summary>
/// <param name="format">The input format string.</param>
/// <param name="data">The data argument.</param>
/// <returns>A new instance of <see cref="FormattingInfo"/>.</returns>
public static FormattingInfo Create(string format, IList<object?> data)
{
var formatter = new SmartFormatter(new SmartSettings());
var formatParsed = formatter.Parser.ParseFormat(format);
// use StringOutput because we don't have to care about disposing.
var formatDetails = new FormatDetails(formatter, formatParsed, data, null, new StringOutput());
return new FormattingInfo(formatDetails, formatDetails.OriginalFormat, data);
}
}
}
Loading

0 comments on commit 2a3398b

Please sign in to comment.