Skip to content

Commit

Permalink
Fixes bchavez#210. Randomizer.Utf16String generates technically valid…
Browse files Browse the repository at this point in the history
… UTF16 strings. (bchavez#214)

* Fixes bchavez#210. Randomizer.Utf16String generates technically valid UTF16 strings.
  • Loading branch information
bchavez authored Mar 25, 2019
1 parent 19aba84 commit ebcd260
Show file tree
Hide file tree
Showing 4 changed files with 500 additions and 2 deletions.
3 changes: 2 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## v26.0.3 - /master current
Release Date: UNRELEASED
* Added `placeholder.com` image service.
* Issue 210: Added `Randomizer.Utf16String` that generates technically valid Unicode with paired high/low surrogates.
* Added `placeholder.com` image service.

## v26.0.2
Release Date: 2019-03-22
Expand Down
62 changes: 62 additions & 0 deletions Source/Bogus.Tests/RandomizerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
using Z.ExtensionMethods;

namespace Bogus.Tests
{
Expand Down Expand Up @@ -404,5 +405,66 @@ public void can_get_a_weighted_true_value()

truths.Should().BeLessThan(0.25f); // roughly
}


public static IEnumerable<object[]> ExactLenUtf16(int maxTest)
{
return Enumerable.Range(1, maxTest)
.Select(i => new object[] {i, i});
}

public static IEnumerable<object[]> VarLenUtf16(int maxTest)
{
return Enumerable.Range(1, maxTest)
.Select(i => new object[] { i, i+20 });
}

[Theory]
[MemberData(nameof(ExactLenUtf16), parameters: 100)]
[MemberData(nameof(VarLenUtf16), parameters: 100)]
public void can_generate_valid_utf16_string_with_surrogates(int min, int max)
{
var x = r.Utf16String(min, max);

x.Length.Should().BeGreaterOrEqualTo(min)
.And
.BeLessOrEqualTo(max);

for( int i = 0; i < x.Length; i++ )
{
var current = x[i];
if( char.IsSurrogate(current) )
{
char.IsLetterOrDigit(x, i).Should().BeTrue();
i++;
}
else
{
char.IsLetterOrDigit(current).Should().BeTrue();
}
}
}

[Theory]
[MemberData(nameof(ExactLenUtf16), parameters: 100)]
[MemberData(nameof(VarLenUtf16), parameters: 100)]
public void can_generate_valid_utf16_without_surrogates(int min, int max)
{
var x = r.Utf16String(min, max, excludeSurrogates: true);

x.Length.Should().BeGreaterOrEqualTo(min)
.And
.BeLessOrEqualTo(max);

x.Any(char.IsSurrogate).Should().BeFalse();
x.All(char.IsLetterOrDigit).Should().BeTrue();
}

[Fact]
public void static_utf16_tests()
{
r.Utf16String().Should().Be("𦊕𡰴ဩਢۥ侀ゞᜡﷴ𠸮𝒾ⶱﹱຂⱪ𝒟𝚪𝒩𝝵ೡཧΐ뢫ⱜੳ𣽇𐨖ਆ𐀼ฏളᄎ𐏈𦵻𐠸𝕋ꡨ𝔇ずరᢔﬣ𐨟𝔚ઐష");
r.Utf16String().Should().Be("னమወ𡂑ப𐠁ஞ𦂰ઐ𢂍ᬒ𒀓𨴽𝜅𧊆𦑆ආ𝜂ଭ𐀪ಋΊ౦౨ㆳꠁⶺ𝛈𡓎𡯸ᜑ𐁁𝜹લ𩠺ଐ𦕲ﬔჁⶂム𝐾𣭄ງヾ༤𝒪ᙵͼ၅𦛃𩕾ﷸ𝜦ⱶ");
}
}
}
Loading

0 comments on commit ebcd260

Please sign in to comment.