Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests #887

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TwitchDownloaderTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
95 changes: 95 additions & 0 deletions TwitchDownloaderTests/ReadOnlySpanTryReplaceNonEscapedTests.cs
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);
}
}
}
61 changes: 61 additions & 0 deletions TwitchDownloaderTests/StringReplaceAnyTests.cs
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);
}
}
}
Loading
Loading