Skip to content

Commit 955e46e

Browse files
committed
Added LinqExtensions tests.
Added StringExtensions tests.
1 parent b77f29e commit 955e46e

3 files changed

Lines changed: 284 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NFluent;
5+
using ReClassNET.Extensions;
6+
using Xunit;
7+
8+
namespace ReClass.NET_Tests.Extensions
9+
{
10+
public class LinqExtensionTests
11+
{
12+
public static TheoryData<IEnumerable<int>, bool> GetTestNoneData => new TheoryData<IEnumerable<int>, bool>
13+
{
14+
{ new int[0], true },
15+
{ new int[1], false },
16+
{ Enumerable.Empty<int>(), true }
17+
};
18+
19+
[Theory]
20+
[MemberData(nameof(GetTestNoneData))]
21+
public void TestNone(IEnumerable<int> sut, bool expected)
22+
{
23+
Check.That(sut.None()).IsEqualTo(expected);
24+
}
25+
26+
public static TheoryData<IEnumerable<int>, Func<int, bool>, bool> GetTestNoneWithSelectorData => new TheoryData<IEnumerable<int>, Func<int, bool>, bool>
27+
{
28+
{ new int[0], i => false, true },
29+
{ new int[0], i => true, true },
30+
{ new [] { 1 }, i => i == 1, false },
31+
{ new [] { 1 }, i => i != 1, true },
32+
{ new [] { 1, 3, 5 }, i => i % 2 == 0, true }
33+
};
34+
35+
[Theory]
36+
[MemberData(nameof(GetTestNoneWithSelectorData))]
37+
public void TestNoneWithSelector(IEnumerable<int> sut, Func<int, bool> selector, bool expected)
38+
{
39+
Check.That(sut.None(selector)).IsEqualTo(expected);
40+
}
41+
42+
public static TheoryData<IEnumerable<int>, Func<int, bool>> GetTestWhereNotData => new TheoryData<IEnumerable<int>, Func<int, bool>>
43+
{
44+
{ new int[0], i => false },
45+
{ new int[0], i => true },
46+
{ new [] { 1 }, i => i == 1 },
47+
{ new [] { 1, 3, 5 }, i => i % 2 == 1 }
48+
};
49+
50+
[Theory]
51+
[MemberData(nameof(GetTestWhereNotData))]
52+
public void TestWhereNot(IEnumerable<int> sut, Func<int, bool> selector)
53+
{
54+
Check.That(sut.WhereNot(selector)).IsEmpty();
55+
}
56+
57+
public static TheoryData<IEnumerable<int>, int, int> GetTestFindIndexData => new TheoryData<IEnumerable<int>, int, int>
58+
{
59+
{ new int[0], 1, -1 },
60+
{ new [] { 1 }, 1, 0 },
61+
{ new [] { 1 }, 2, -1 },
62+
{ new [] { 1, 3, 5 }, 1, 0 },
63+
{ new [] { 1, 3, 5 }, 2, -1 },
64+
{ new [] { 1, 3, 5 }, 3, 1 },
65+
{ new [] { 1, 3, 5 }, 4, -1 },
66+
{ new [] { 1, 3, 5 }, 5, 2 }
67+
};
68+
69+
[Theory]
70+
[MemberData(nameof(GetTestFindIndexData))]
71+
public void TestFindIndex(IEnumerable<int> sut, int item, int expected)
72+
{
73+
Check.That(sut.FindIndex(i => i == item)).IsEqualTo(expected);
74+
}
75+
76+
public static TheoryData<IEnumerable<int>> GetTestForEachData => new TheoryData<IEnumerable<int>>
77+
{
78+
{ Enumerable.Empty<int>() },
79+
{ Enumerable.Repeat(0, 1) },
80+
{ Enumerable.Repeat(0, 2) },
81+
{ Enumerable.Repeat(0, 10) }
82+
};
83+
84+
[Theory]
85+
[MemberData(nameof(GetTestForEachData))]
86+
public void TestForEach(IEnumerable<int> sut)
87+
{
88+
var sutCpy = sut.ToList();
89+
90+
var counter = 0;
91+
sutCpy.ForEach(_ => counter++);
92+
93+
Check.That(counter).IsEqualTo(sutCpy.Count);
94+
}
95+
96+
public static TheoryData<IEnumerable<int>, Func<int, int>, IEnumerable<int>> GetTestDistinctByData => new TheoryData<IEnumerable<int>, Func<int, int>, IEnumerable<int>>
97+
{
98+
{ Enumerable.Empty<int>(), i => i, Enumerable.Empty<int>() },
99+
{ new [] { 1 }, i => i, new [] { 1 } },
100+
{ new [] { 1, 1, 1, 1 }, i => i, new [] { 1 } },
101+
{ new [] { 1, 2, 3, 4 }, i => i, new [] { 1, 2, 3, 4 } },
102+
{ new [] { 1, 1, 2, 2, 3, 3 }, i => i, new [] { 1, 2, 3 } },
103+
{ new [] { 1, 1, 2, 2, 3, 4 }, i => i, new [] { 1, 2, 3, 4 } },
104+
{ new [] { 1, 1, 2, 2, 3, 4 }, i => 0, new [] { 1 } }
105+
};
106+
107+
[Theory]
108+
[MemberData(nameof(GetTestDistinctByData))]
109+
public void TestDistinctBy(IEnumerable<int> sut, Func<int, int> selector, IEnumerable<int> expected)
110+
{
111+
Check.That(sut.DistinctBy(selector)).IsEquivalentTo(expected);
112+
}
113+
114+
public static TheoryData<IEnumerable<int>, IEnumerable<int>, bool> GetTestIsEquivalentToData => new TheoryData<IEnumerable<int>, IEnumerable<int>, bool>
115+
{
116+
{ Enumerable.Empty<int>(), Enumerable.Empty<int>(), true },
117+
{ Enumerable.Empty<int>(), new int[0], true },
118+
{ new [] { 1 }, new int[0], false },
119+
{ new [] { 1 }, new [] { 2 }, false },
120+
{ new [] { 1, 2, 3 }, new [] { 1, 2, 3 }, true },
121+
{ new [] { 1, 2, 3 }, new [] { 3, 1, 2 }, true }
122+
};
123+
124+
[Theory]
125+
[MemberData(nameof(GetTestIsEquivalentToData))]
126+
public void TestIsEquivalentTo(IEnumerable<int> sut, IEnumerable<int> other, bool expected)
127+
{
128+
Check.That(sut.IsEquivalentTo(other)).IsEqualTo(expected);
129+
}
130+
}
131+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NFluent;
5+
using ReClassNET.Extensions;
6+
using Xunit;
7+
8+
namespace ReClass.NET_Tests.Extensions
9+
{
10+
public class StringExtensionTest
11+
{
12+
public static TheoryData<char> GetTestIsPrintableData() => new TheoryData<char>
13+
{
14+
'0', '9', ' ', '\t', '\n', 'a', 'A', 'z', 'Z', '-', '_', '°', '^', '"', '\\', '\"', '&', '@', '€', '$', '|', '<', '>', ';', ',', '.', ':', '#', '*', '+', '~', '`', '´', 'ß', '?', '=', '(', ')', '[', ']', '{', '}'
15+
};
16+
17+
[Theory]
18+
[MemberData(nameof(GetTestIsPrintableData))]
19+
public void TestIsPrintable(char c)
20+
{
21+
Check.That(c.IsPrintable()).IsTrue();
22+
}
23+
24+
public static TheoryData<char> GetTestIsNotPrintableData() => new TheoryData<char>
25+
{
26+
'\u0000','\u0001', '\u0002', '\u009A','\u009B', '\u009C', '\u009D','\u009E', '\u009F'
27+
};
28+
29+
[Theory]
30+
[MemberData(nameof(GetTestIsNotPrintableData))]
31+
public void TestIsNotPrintable(char c)
32+
{
33+
Check.That(c.IsPrintable()).IsFalse();
34+
}
35+
36+
public static TheoryData<string, int, string> GetTestLimitLengthData() => new TheoryData<string, int, string>
37+
{
38+
{ string.Empty, 0, string.Empty },
39+
{ string.Empty, 1, string.Empty },
40+
{ "01234", 0, string.Empty },
41+
{ "01234", 1, "0" },
42+
{ "01234", 5, "01234" },
43+
{ "01234", 10, "01234" }
44+
};
45+
46+
[Theory]
47+
[MemberData(nameof(GetTestLimitLengthData))]
48+
public void TestLimitLength(string sut, int length, string expected)
49+
{
50+
Check.That(sut.LimitLength(length)).IsEqualTo(expected);
51+
}
52+
53+
[Fact]
54+
public void TestLimitLengthThrows()
55+
{
56+
Check.ThatCode(() => "".LimitLength(-1)).Throws<ArgumentOutOfRangeException>();
57+
}
58+
59+
public static TheoryData<IEnumerable<byte>, IEnumerable<char>> GetTestInterpretAsUtf8Data() => new TheoryData<IEnumerable<byte>, IEnumerable<char>>
60+
{
61+
{ new byte[0], string.Empty },
62+
{ new [] { (byte)'t', (byte)'e', (byte)'s', (byte)'t' }, "test".ToArray() /* https://github.com/tpierrain/NFluent/issues/299 */ }
63+
};
64+
65+
[Theory]
66+
[MemberData(nameof(GetTestInterpretAsUtf8Data))]
67+
public void TestInterpretAsUtf8(IEnumerable<byte> sut, IEnumerable<char> expected)
68+
{
69+
Check.That(sut.InterpretAsUtf8()).ContainsExactly(expected);
70+
}
71+
72+
public static TheoryData<IEnumerable<byte>, IEnumerable<char>> GetTestInterpretAsUtf16Data() => new TheoryData<IEnumerable<byte>, IEnumerable<char>>
73+
{
74+
{ new byte[0], string.Empty },
75+
{ new [] { (byte)'t', (byte)0, (byte)'e', (byte)0, (byte)'s', (byte)0, (byte)'t', (byte)0 }, "test".ToArray() /* https://github.com/tpierrain/NFluent/issues/299 */ }
76+
};
77+
78+
[Theory]
79+
[MemberData(nameof(GetTestInterpretAsUtf16Data))]
80+
public void TestInterpretAsUtf16(IEnumerable<byte> sut, IEnumerable<char> expected)
81+
{
82+
Check.That(sut.InterpretAsUtf16()).ContainsExactly(expected);
83+
}
84+
85+
public static TheoryData<IEnumerable<char>, float> GetTestCalculatePrintableDataThresholdData() => new TheoryData<IEnumerable<char>, float>
86+
{
87+
{ new char[0], 0.0f },
88+
{ new [] { '\0' }, 0.0f },
89+
{ new [] { 'a' }, 1.0f },
90+
{ new [] { '\0', 'a' }, 0.0f },
91+
{ new [] { 'a', '\0' }, 0.5f },
92+
{ new [] { '\0', 'a', 'a' }, 0.0f },
93+
{ new [] { 'a', 'a', '\0' }, 2 / 3.0f },
94+
{ new [] { 'a', 'a', '\0', '\0' }, 0.5f },
95+
{ new [] { 'a', 'a', '\0', '\0', '\0' }, 2 / 5.0f }
96+
};
97+
98+
[Theory]
99+
[MemberData(nameof(GetTestCalculatePrintableDataThresholdData))]
100+
public void TestCalculatePrintableDataThreshold(IEnumerable<char> sut, float expected)
101+
{
102+
Check.That(sut.CalculatePrintableDataThreshold()).IsCloseTo(expected, 0.001);
103+
}
104+
105+
[Theory]
106+
[InlineData('a')]
107+
[InlineData('a', 'a')]
108+
[InlineData('a', 'a', 'f')]
109+
[InlineData('#', '+', 'r', '?', 'ß', '%', '&', '§', '_', '0', '/', '(', 'ö')]
110+
public void TestIsPrintableData(params char[] sut)
111+
{
112+
Check.That(sut.IsPrintableData()).IsTrue();
113+
}
114+
115+
[Theory]
116+
[InlineData]
117+
[InlineData('a', '\0')]
118+
[InlineData('\0', 'a')]
119+
[InlineData('a', 'a', '\0')]
120+
[InlineData('a', 'a', 'f', '\0')]
121+
[InlineData('a', 'a', '\0', 'f')]
122+
[InlineData('a', '\0', 'a', 'f')]
123+
[InlineData('\0', 'a', 'a', 'f')]
124+
public void TestIsNotPrintableData(params char[] sut)
125+
{
126+
Check.That(sut.IsPrintableData()).IsFalse();
127+
}
128+
129+
[Theory]
130+
[InlineData('a', 'a', 'f', '\0')]
131+
[InlineData('1', '2', '3', '4', '5', '6', '7', '8', '\0', '\0')]
132+
[InlineData('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', '\0', '\0', '\0', '\0')]
133+
public void TestIsLikelyPrintableData(params char[] sut)
134+
{
135+
Check.That(sut.IsLikelyPrintableData()).IsTrue();
136+
}
137+
138+
[Theory]
139+
[InlineData]
140+
[InlineData('a', '\0')]
141+
[InlineData('\0', 'a')]
142+
[InlineData('a', 'a', '\0')]
143+
[InlineData('a', 'a', '\0', 'f')]
144+
[InlineData('a', 'a', '\0', '\0')]
145+
[InlineData('a', '\0', 'a', 'f')]
146+
public void TestIsNotLikelyPrintableData(params char[] sut)
147+
{
148+
Check.That(sut.IsPrintableData()).IsFalse();
149+
}
150+
}
151+
}

ReClass.NET_Tests/ReClass.NET_Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
<Compile Include="Extensions\DictionaryExtensionTest.cs" />
7272
<Compile Include="Extensions\EncodingExtensionTest.cs" />
7373
<Compile Include="Extensions\FloatingPointExtensionTest.cs" />
74+
<Compile Include="Extensions\LinqExtensionTests.cs" />
75+
<Compile Include="Extensions\StringExtensionTest.cs" />
7476
<Compile Include="Properties\AssemblyInfo.cs" />
7577
<Compile Include="Util\CircularBufferTest.cs" />
7678
<Compile Include="Util\CommandLineArgsTest.cs" />

0 commit comments

Comments
 (0)