/
StringUtilsTest.cs
146 lines (125 loc) · 5.89 KB
/
StringUtilsTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright Bastian Eicher
// Licensed under the MIT License
using System.Security.Cryptography;
using FluentAssertions;
using Xunit;
namespace NanoByte.Common
{
/// <summary>
/// Contains test methods for <see cref="StringUtils"/>.
/// </summary>
public class StringUtilsTest
{
[Fact]
public void TestCompare()
{
StringUtils.EqualsIgnoreCase("abc", "abc").Should().BeTrue();
StringUtils.EqualsIgnoreCase("abc", "ABC").Should().BeTrue();
StringUtils.EqualsIgnoreCase("abc", "123").Should().BeFalse();
StringUtils.EqualsIgnoreCase("abc", "abc ").Should().BeFalse();
}
[Fact]
public void TestContains()
{
"This is a test.".ContainsIgnoreCase("TEST").Should().BeTrue();
"This is a test.".ContainsIgnoreCase("test").Should().BeTrue();
"abc".ContainsIgnoreCase("123").Should().BeFalse();
"test".ContainsIgnoreCase("This is a test.").Should().BeFalse();
}
[Fact]
public void TestCountOccurences()
{
"abc".CountOccurences('/').Should().Be(0);
"ab/c".CountOccurences('/').Should().Be(1);
"ab/c/".CountOccurences('/').Should().Be(2);
}
[Fact]
public void TestSplitMultilineText()
{
"123\nabc".SplitMultilineText().Should().Equal(new[] {"123", "abc"}, because: "Should split Linux-stlye linebreaks");
"123\rabc".SplitMultilineText().Should().Equal(new[] {"123", "abc"}, because: "Should split old Mac-stlye linebreaks");
"123\r\nabc".SplitMultilineText().Should().Equal(new[] {"123", "abc"}, because: "Should split Windows-stlye linebreaks");
}
[Fact]
public void TestJoin()
{
StringUtils.Join(" ", new[] {"part1"}).Should().Be("part1");
StringUtils.Join(" ", new[] {"part1", "part2"}).Should().Be("part1 part2");
new[] {"part1 part2", "part3"}.JoinEscapeArguments().Should().Be("\"part1 part2\" part3");
}
[Fact]
public void TestJoinEscapeArguments()
{
new[] {"part1"}.JoinEscapeArguments().Should().Be("part1");
new[] {"part1", "part2"}.JoinEscapeArguments().Should().Be("part1 part2");
new[] {"part1 \" part2", "part3"}.JoinEscapeArguments().Should().Be("\"part1 \\\" part2\" part3");
}
[Fact]
public void TestGetLeftRightPartChar()
{
const string testString = "text1 text2 text3";
testString.GetLeftPartAtFirstOccurrence(' ').Should().Be("text1");
testString.GetRightPartAtFirstOccurrence(' ').Should().Be("text2 text3");
testString.GetLeftPartAtLastOccurrence(' ').Should().Be("text1 text2");
testString.GetRightPartAtLastOccurrence(' ').Should().Be("text3");
}
[Fact]
public void TestGetLeftRightPartString()
{
const string testString = "text1 - text2 - text3";
testString.GetLeftPartAtFirstOccurrence(" - ").Should().Be("text1");
testString.GetRightPartAtFirstOccurrence(" - ").Should().Be("text2 - text3");
testString.GetLeftPartAtLastOccurrence(" - ").Should().Be("text1 - text2");
testString.GetRightPartAtLastOccurrence(" - ").Should().Be("text3");
}
[Fact]
public void TestStripCharacters() => "a!b?".StripCharacters("!?").Should().Be("ab");
[Fact]
public void TestStripFromEnd() => "abc".StripFromEnd(count: 1).Should().Be("ab");
[Fact]
public void TestEscapeArgument()
{
"".EscapeArgument().Should().Be("\"\"", because: "Empty strings need to be escaped in order not to vanish");
"test".EscapeArgument().Should().Be("test", because: "Simple strings shouldn't be modified");
"test1 test2".EscapeArgument().Should().Be("\"test1 test2\"", because: "Strings with whitespaces should be encapsulated");
"test1 test2\\".EscapeArgument().Should().Be("\"test1 test2\\\\\"", because: "Trailing backslashes should be escaped");
"test1\"test2".EscapeArgument().Should().Be("test1\\\"test2", because: "Quotation marks should be escaped");
"test1\\\\test2".EscapeArgument().Should().Be("test1\\\\test2", because: "Consecutive slashes without quotation marks should not be escaped");
"test1\\\"test2".EscapeArgument().Should().Be("test1\\\\\\\"test2", because: "Slashes with quotation marks should be escaped");
}
[Fact]
public void TestBase64Utf8Encode()
{
"".Base64Utf8Encode().Should().Be("");
"test".Base64Utf8Encode().Should().Be("dGVzdA==");
}
[Fact]
public void TestBase64Utf8Decode()
{
"".Base64Utf8Decode().Should().Be("");
"dGVzdA==".Base64Utf8Decode().Should().Be("test");
}
[Fact]
public void TestBase32Encode() => new byte[] {65, 66}.Base32Encode().Should().Be("IFBA");
[Fact]
public void TestBase16Encode() => new byte[] {65, 66}.Base16Encode().Should().Be("4142");
[Fact]
public void TestBase16Decode() => "4142".Base16Decode().Should().Equal(65, 66);
[Fact]
public void TestHash()
{
const string sha1ForEmptyString = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
"".Hash(SHA1.Create()).Should().Be(sha1ForEmptyString);
}
[Fact]
public void TestGeneratePassword()
{
for (int i = 0; i < 128; i++)
{
string result = StringUtils.GeneratePassword(i);
result.Should().NotContain("=").And.NotContain("l");
result.Length.Should().Be(i);
}
}
}
}