Skip to content

Commit 1cbe52f

Browse files
authored
Add PassthroughTextEncoder (#109)
* Add some extra unit tests * PassthroughTextEncoder * fix
1 parent f738aa5 commit 1cbe52f

File tree

8 files changed

+280
-68
lines changed

8 files changed

+280
-68
lines changed

Handlebars.Net.Helpers.sln.DotSettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=WASM/@EntryIndexedValue">WASM</s:String>
33
<s:Boolean x:Key="/Default/UserDictionary/Words/=Camelize/@EntryIndexedValue">True</s:Boolean>
44
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dasherize/@EntryIndexedValue">True</s:Boolean>
5+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Formattable/@EntryIndexedValue">True</s:Boolean>
56
<s:Boolean x:Key="/Default/UserDictionary/Words/=handlebarsjs/@EntryIndexedValue">True</s:Boolean>
7+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Passthrough/@EntryIndexedValue">True</s:Boolean>
68
<s:Boolean x:Key="/Default/UserDictionary/Words/=Titlecase/@EntryIndexedValue">True</s:Boolean>
79
<s:Boolean x:Key="/Default/UserDictionary/Words/=Truncator/@EntryIndexedValue">True</s:Boolean>
810
<s:Boolean x:Key="/Default/UserDictionary/Words/=Xeger/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Collections.Generic;
2+
using System.Text;
3+
4+
namespace HandlebarsDotNet.Helpers.IO;
5+
6+
/// <summary>
7+
/// A text encoder that implements <see cref="ITextEncoder"/> and directly passes through the input text to the target without any encoding.
8+
/// </summary>
9+
public class PassthroughTextEncoder : ITextEncoder
10+
{
11+
/// <summary>
12+
/// Writes the given StringBuilder text directly to the target TextWriter.
13+
/// </summary>
14+
/// <param name="text">The StringBuilder containing the text.</param>
15+
/// <param name="target">The TextWriter to write the text to.</param>
16+
public void Encode(StringBuilder text, TextWriter target)
17+
{
18+
target.Write(text);
19+
}
20+
21+
/// <summary>
22+
/// Writes the given string text directly to the target TextWriter.
23+
/// </summary>
24+
/// <param name="text">The string containing the text.</param>
25+
/// <param name="target">The TextWriter to write the text to.</param>
26+
public void Encode(string text, TextWriter target)
27+
{
28+
target.Write(text);
29+
}
30+
31+
/// <summary>
32+
/// Writes the given text enumerator directly to the target TextWriter.
33+
/// </summary>
34+
/// <typeparam name="T">The type of the text enumerator.</typeparam>
35+
/// <param name="text">The enumerator containing the text.</param>
36+
/// <param name="target">The TextWriter to write the text to.</param>
37+
public void Encode<T>(T text, TextWriter target) where T : IEnumerator<char>
38+
{
39+
while (text.MoveNext())
40+
{
41+
target.Write(text.Current);
42+
}
43+
}
44+
}

src/Handlebars.Net.Helpers/Helpers/StringHelpers.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ public string Append(string value, string append)
3939
[HandlebarsWriter(WriterType.String)]
4040
public static string Base64Decode(string value)
4141
{
42-
if (value == null)
43-
{
44-
throw new ArgumentNullException(nameof(value));
45-
}
42+
Guard.NotNull(value);
4643

4744
var base64EncodedBytes = Convert.FromBase64String(value);
4845
return Encoding.UTF8.GetString(base64EncodedBytes);
@@ -51,10 +48,7 @@ public static string Base64Decode(string value)
5148
[HandlebarsWriter(WriterType.String)]
5249
public static string Base64Encode(string value)
5350
{
54-
if (value == null)
55-
{
56-
throw new ArgumentNullException(nameof(value));
57-
}
51+
Guard.NotNull(value);
5852

5953
var plainTextBytes = Encoding.UTF8.GetBytes(value);
6054
return Convert.ToBase64String(plainTextBytes);
@@ -178,6 +172,18 @@ public WrappedString FormatAsString(object? value, string? format = null)
178172
return new WrappedString(Format(value, format ?? string.Empty));
179173
}
180174

175+
[HandlebarsWriter(WriterType.String)]
176+
public string HtmlDecode(string? value)
177+
{
178+
return HtmlUtils.HtmlDecode(value);
179+
}
180+
181+
[HandlebarsWriter(WriterType.String)]
182+
public string HtmlEncode(string? value)
183+
{
184+
return HtmlUtils.HtmlEncode(value);
185+
}
186+
181187
[HandlebarsWriter(WriterType.Value)]
182188
public bool IsNotNullOrEmpty(string value)
183189
{

src/Handlebars.Net.Helpers/Utils/HtmlUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ public static void HtmlDecode(string? value, TextWriter output)
139139
}
140140
}
141141

142-
public static string HtmlDecode(string value)
142+
public static string HtmlDecode(string? value)
143143
{
144144
if (string.IsNullOrEmpty(value))
145145
{
146-
return value;
146+
return string.Empty;
147147
}
148148

149149
var output = new StringWriter(CultureInfo.InvariantCulture);
@@ -198,11 +198,11 @@ public static void HtmlEncode(string? value, TextWriter output)
198198
}
199199
}
200200

201-
public static string HtmlEncode(string value)
201+
public static string HtmlEncode(string? value)
202202
{
203203
if (string.IsNullOrEmpty(value))
204204
{
205-
return value;
205+
return string.Empty;
206206
}
207207

208208
var output = new StringWriter(CultureInfo.InvariantCulture);

test/Handlebars.Net.Helpers.Tests/Helpers/StringHelpersTests.cs

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ namespace HandlebarsDotNet.Helpers.Tests.Helpers;
99

1010
public class StringHelpersTests
1111
{
12-
private readonly Mock<IHandlebars> _contextMock;
13-
1412
private readonly StringHelpers _sut;
1513

1614
public StringHelpersTests()
1715
{
18-
_contextMock = new Mock<IHandlebars>();
19-
_contextMock.SetupGet(c => c.Configuration).Returns(new HandlebarsConfiguration { FormatProvider = CultureInfo.InvariantCulture });
16+
var contextMock = new Mock<IHandlebars>();
17+
contextMock.SetupGet(c => c.Configuration).Returns(new HandlebarsConfiguration { FormatProvider = CultureInfo.InvariantCulture });
2018

21-
_sut = new StringHelpers(_contextMock.Object);
19+
_sut = new StringHelpers(contextMock.Object);
2220
}
2321

2422
[Theory]
@@ -33,6 +31,32 @@ public void Append(string value, string append, string expected)
3331
result.Should().Be(expected);
3432
}
3533

34+
[Theory]
35+
[InlineData("Hello, World!", "SGVsbG8sIFdvcmxkIQ==")]
36+
[InlineData("Test", "VGVzdA==")]
37+
[InlineData("", "")]
38+
public void Base64Encode(string input, string expected)
39+
{
40+
// Act
41+
var result = StringHelpers.Base64Encode(input);
42+
43+
// Assert
44+
result.Should().Be(expected);
45+
}
46+
47+
[Theory]
48+
[InlineData("SGVsbG8sIFdvcmxkIQ==", "Hello, World!")]
49+
[InlineData("VGVzdA==", "Test")]
50+
[InlineData("", "")]
51+
public void Base64Decode(string input, string expected)
52+
{
53+
// Act
54+
var result = StringHelpers.Base64Decode(input);
55+
56+
// Assert
57+
result.Should().Be(expected);
58+
}
59+
3660
[Theory]
3761
[InlineData(null, null)]
3862
[InlineData("", "")]
@@ -102,6 +126,40 @@ public void Ellipsis(string value, int length, string expected)
102126
result.Should().Be(expected);
103127
}
104128

129+
[Theory]
130+
[InlineData("Hello &amp; World", "Hello & World")]
131+
[InlineData("1 &lt; 2 &gt; 0", "1 < 2 > 0")]
132+
[InlineData("Use &quot;quotes&quot;", "Use \"quotes\"")]
133+
[InlineData("&#39;single quotes&#39;", "'single quotes'")]
134+
[InlineData("&#65;", "A")]
135+
[InlineData(null, "")]
136+
[InlineData("", "")]
137+
public void HtmlDecode(string input, string expected)
138+
{
139+
// Act
140+
var result = _sut.HtmlDecode(input);
141+
142+
// Assert
143+
result.Should().Be(expected);
144+
}
145+
146+
[Theory]
147+
[InlineData("Hello & World", "Hello &amp; World")]
148+
[InlineData("1 < 2 > 0", "1 &lt; 2 &gt; 0")]
149+
[InlineData("Use \"quotes\"", "Use &quot;quotes&quot;")]
150+
[InlineData("'single quotes'", "&#39;single quotes&#39;")]
151+
[InlineData("A", "A")]
152+
[InlineData(null, "")]
153+
[InlineData("", "")]
154+
public void HtmlEncode(string input, string expected)
155+
{
156+
// Act
157+
var result = _sut.HtmlEncode(input);
158+
159+
// Assert
160+
result.Should().Be(expected);
161+
}
162+
105163
[Theory]
106164
[InlineData(null, null)]
107165
[InlineData("", "")]
@@ -312,7 +370,7 @@ public void Format_DateTime(string format, string expected)
312370
result1.Should().Be(expected);
313371
result2.Should().Be(expected);
314372
}
315-
373+
316374
[Theory]
317375
[InlineData("N0", "1")]
318376
[InlineData("N1", "1.2")]
@@ -336,7 +394,7 @@ public void Format_NotIFormattableType()
336394
{
337395
// Arrange
338396
var value = new Version(1, 2, 3, 4);
339-
397+
340398
// Act
341399
var result = _sut.Format(value, "should-be-ignored");
342400

@@ -353,7 +411,7 @@ public void Format_Null()
353411
// Assert
354412
result.Should().BeEmpty();
355413
}
356-
414+
357415
[Theory]
358416
[InlineData("", "bar", false)]
359417
[InlineData("foo", "", false)]
@@ -522,7 +580,7 @@ public void Substring_3params(string value, int start, int end, string expected)
522580
// Assert
523581
result.Should().Be(expected);
524582
}
525-
583+
526584
[Theory]
527585
[InlineData(null, 1, 0)]
528586
[InlineData("", 1, 0)]
Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,46 @@
1-
using FluentAssertions;
2-
using HandlebarsDotNet.Helpers.Helpers;
1+
using FluentAssertions;
2+
using HandlebarsDotNet.Helpers.Helpers;
33
using Moq;
4-
using Xunit;
5-
6-
namespace HandlebarsDotNet.Helpers.Tests.Helpers
7-
{
8-
public class UrlHelpersTests
9-
{
10-
private readonly Mock<IHandlebars> _contextMock;
11-
12-
private readonly UrlHelpers _sut;
13-
14-
public UrlHelpersTests()
15-
{
16-
_contextMock = new Mock<IHandlebars>();
17-
18-
_sut = new UrlHelpers(_contextMock.Object);
19-
}
20-
21-
[Theory]
22-
[InlineData(null, null)]
23-
[InlineData("", "")]
24-
[InlineData("arinet%2FHandlebarDocs%2Fblob%2Fmaster%2FcustomHelpers%2Furl.md%23decodeuri", "arinet/HandlebarDocs/blob/master/customHelpers/url.md#decodeuri")]
25-
[InlineData("%2Fsearch%2Finventory%2Fbrand%2FPolaris+Industries%2Fsort%2Fbest-match", "/search/inventory/brand/Polaris Industries/sort/best-match")]
26-
public void DecodeUri(string value, string expected)
27-
{
28-
// Act
29-
var result = _sut.DecodeUri(value);
30-
31-
// Assert
32-
result.Should().Be(expected);
33-
}
34-
35-
[Theory]
36-
[InlineData(null, null)]
37-
[InlineData("", "")]
38-
[InlineData("arinet/HandlebarDocs/blob/master/customHelpers/url.md#decodeuri", "arinet%2FHandlebarDocs%2Fblob%2Fmaster%2FcustomHelpers%2Furl.md%23decodeuri")]
39-
[InlineData("/search/inventory/brand/Polaris Industries/sort/best-match", "%2Fsearch%2Finventory%2Fbrand%2FPolaris+Industries%2Fsort%2Fbest-match")]
40-
public void EncodeUri(string value, string expected)
41-
{
42-
// Act
43-
var result = _sut.EncodeUri(value);
44-
45-
// Assert
46-
result.Should().Be(expected);
47-
}
48-
}
4+
using Xunit;
5+
6+
namespace HandlebarsDotNet.Helpers.Tests.Helpers;
7+
8+
public class UrlHelpersTests
9+
{
10+
private readonly UrlHelpers _sut;
11+
12+
public UrlHelpersTests()
13+
{
14+
var contextMock = new Mock<IHandlebars>();
15+
16+
_sut = new UrlHelpers(contextMock.Object);
17+
}
18+
19+
[Theory]
20+
[InlineData(null, null)]
21+
[InlineData("", "")]
22+
[InlineData("arinet%2FHandlebarDocs%2Fblob%2Fmaster%2FcustomHelpers%2Furl.md%23decodeuri", "arinet/HandlebarDocs/blob/master/customHelpers/url.md#decodeuri")]
23+
[InlineData("%2Fsearch%2Finventory%2Fbrand%2FPolaris+Industries%2Fsort%2Fbest-match", "/search/inventory/brand/Polaris Industries/sort/best-match")]
24+
public void DecodeUri(string value, string expected)
25+
{
26+
// Act
27+
var result = _sut.DecodeUri(value);
28+
29+
// Assert
30+
result.Should().Be(expected);
31+
}
32+
33+
[Theory]
34+
[InlineData(null, null)]
35+
[InlineData("", "")]
36+
[InlineData("arinet/HandlebarDocs/blob/master/customHelpers/url.md#decodeuri", "arinet%2FHandlebarDocs%2Fblob%2Fmaster%2FcustomHelpers%2Furl.md%23decodeuri")]
37+
[InlineData("/search/inventory/brand/Polaris Industries/sort/best-match", "%2Fsearch%2Finventory%2Fbrand%2FPolaris+Industries%2Fsort%2Fbest-match")]
38+
public void EncodeUri(string value, string expected)
39+
{
40+
// Act
41+
var result = _sut.EncodeUri(value);
42+
43+
// Assert
44+
result.Should().Be(expected);
45+
}
4946
}

0 commit comments

Comments
 (0)