-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add unit test project * Add initial unit tests
- Loading branch information
Showing
7 changed files
with
477 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
95 changes: 95 additions & 0 deletions
95
TwitchDownloaderTests/ReadOnlySpanTryReplaceNonEscapedTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using TwitchDownloaderCore.Extensions; | ||
|
||
namespace TwitchDownloaderTests | ||
{ | ||
// ReSharper disable StringLiteralTypo | ||
public class ReadOnlySpanTryReplaceNonEscapedTests | ||
{ | ||
[Fact] | ||
public void ReturnsFalse_WhenDestinationTooShort() | ||
{ | ||
ReadOnlySpan<char> str = @"SORRY FOR TRAFFIC NaM"; | ||
var destination = Array.Empty<char>(); | ||
|
||
var success = str.TryReplaceNonEscaped(destination, 'r', 'w'); | ||
|
||
Assert.False(success); | ||
} | ||
|
||
[Fact] | ||
public void MatchesOriginalString_WhenOldCharNotFound() | ||
{ | ||
ReadOnlySpan<char> str = @"SORRY FOR TRAFFIC NaM"; | ||
var destination = new char[str.Length]; | ||
const string EXPECTED = @"SORRY FOR TRAFFIC NaM"; | ||
|
||
var success = str.TryReplaceNonEscaped(destination, 'r', 'w'); | ||
|
||
Assert.True(success); | ||
Assert.Equal(EXPECTED.AsSpan(), destination); | ||
} | ||
|
||
[Fact] | ||
public void CorrectlyEscapeCharsPrependedByBackslash() | ||
{ | ||
ReadOnlySpan<char> str = @"SO\RRY FO\R TRAFFIC NaM"; | ||
var destination = new char[str.Length]; | ||
const string EXPECTED = @"SO\RWY FO\R TWAFFIC NaM"; | ||
|
||
var success = str.TryReplaceNonEscaped(destination, 'R', 'W'); | ||
|
||
Assert.True(success); | ||
Assert.Equal(EXPECTED.AsSpan(), destination); | ||
} | ||
|
||
[Fact] | ||
public void CorrectlyEscapeSingleCharsContainedWithinQuotes() | ||
{ | ||
ReadOnlySpan<char> str = "SO\"R\"RY FO'R' TRAFFIC NaM"; | ||
var destination = new char[str.Length]; | ||
const string EXPECTED = "SO\"R\"WY FO'R' TWAFFIC NaM"; | ||
|
||
var success = str.TryReplaceNonEscaped(destination, 'R', 'W'); | ||
|
||
Assert.True(success); | ||
Assert.Equal(EXPECTED.AsSpan(), destination); | ||
} | ||
|
||
[Fact] | ||
public void CorrectlyEscapeManyCharsContainedWithinQuotes() | ||
{ | ||
ReadOnlySpan<char> str = "SORRY \"FOR\" 'TRAFFIC' NaM"; | ||
var destination = new char[str.Length]; | ||
const string EXPECTED = "SOWWY \"FOR\" 'TRAFFIC' NaM"; | ||
|
||
var success = str.TryReplaceNonEscaped(destination, 'R', 'W'); | ||
|
||
Assert.True(success); | ||
Assert.Equal(EXPECTED.AsSpan(), destination); | ||
} | ||
|
||
[Fact] | ||
public void CorrectlyEscapesEndQuotesPrependedByBackslashes() | ||
{ | ||
ReadOnlySpan<char> str = @"'It\'s finally over.' It truly is over."; | ||
var destination = new char[str.Length]; | ||
const string EXPECTED = @"'It\'s finally over.' It twuly is ovew."; | ||
|
||
var success = str.TryReplaceNonEscaped(destination, 'r', 'w'); | ||
|
||
Assert.True(success); | ||
Assert.Equal(EXPECTED.AsSpan(), destination); | ||
} | ||
|
||
[Fact] | ||
public void DoesNotEscapeDifferingQuotes() | ||
{ | ||
ReadOnlySpan<char> str = "\"SORRY FOR TRAFFIC NaM.'"; | ||
var destination = new char[str.Length]; | ||
|
||
var success = str.TryReplaceNonEscaped(destination, 'R', 'W'); | ||
|
||
Assert.False(success); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using TwitchDownloaderCore.Extensions; | ||
|
||
namespace TwitchDownloaderTests | ||
{ | ||
// ReSharper disable StringLiteralTypo | ||
public class StringReplaceAnyTests | ||
{ | ||
[Fact] | ||
public void MatchesMultipleStringReplaceUses() | ||
{ | ||
const string STRING = "SORRY FOR TRAFFIC NaM."; | ||
const string OLD_CHARS = "FRM"; | ||
const char NEW_CHAR = 'L'; | ||
|
||
var replaceResult1 = STRING.Replace(OLD_CHARS[0], NEW_CHAR); | ||
var replaceResult2 = replaceResult1.Replace(OLD_CHARS[1], NEW_CHAR); | ||
var replaceResult3 = replaceResult2.Replace(OLD_CHARS[2], NEW_CHAR); | ||
|
||
var replaceAnyResult = replaceResult2.ReplaceAny(OLD_CHARS, NEW_CHAR); | ||
|
||
Assert.Equal(replaceResult3, replaceAnyResult); | ||
} | ||
|
||
[Fact] | ||
public void CorrectlyReplacesAnyCharacter() | ||
{ | ||
const string STRING = "SORRY FOR TRAFFIC NaM."; | ||
const string OLD_CHARS = "FRM"; | ||
const char NEW_CHAR = 'L'; | ||
const string EXPECTED = "SOLLY LOL TLALLIC NaL."; | ||
|
||
var result = STRING.ReplaceAny(OLD_CHARS, NEW_CHAR); | ||
|
||
Assert.Equal(EXPECTED, result); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsOriginalString_WhenEmpty() | ||
{ | ||
const string STRING = ""; | ||
const string OLD_CHARS = ""; | ||
const char NEW_CHAR = 'L'; | ||
|
||
var result = STRING.ReplaceAny(OLD_CHARS, NEW_CHAR); | ||
|
||
Assert.Same(STRING, result); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsOriginalString_WhenOldCharsNotPresent() | ||
{ | ||
const string STRING = "SORRY FOR TRAFFIC NaM."; | ||
const string OLD_CHARS = "PogU"; | ||
const char NEW_CHAR = 'L'; | ||
|
||
var result = STRING.ReplaceAny(OLD_CHARS, NEW_CHAR); | ||
|
||
Assert.Same(STRING, result); | ||
} | ||
} | ||
} |
Oops, something went wrong.