-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_LinqExtensionMethods.cs
More file actions
80 lines (68 loc) · 2.24 KB
/
Test_LinqExtensionMethods.cs
File metadata and controls
80 lines (68 loc) · 2.24 KB
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
using CSharpExtender.ExtensionMethods;
namespace Test.CSharpExtender.ExtensionMethods;
public class Test_LinqExtensionMethods
{
[Fact]
public void None_EmptyList_ReturnsTrue()
{
var list = new List<int>();
Assert.True(list.None());
Assert.True(list.None(x => x > 5));
}
[Fact]
public void None_WithCondition_ReturnsExpectedResult()
{
var list = new List<int> { 1, 2, 3, 4, 5 };
Assert.True(list.None(x => x > 5));
Assert.False(list.None(x => x < 5));
}
[Fact]
public void None_WithoutCondition_ReturnsExpectedResult()
{
var list = new List<int> { 1, 2, 3, 4, 5 };
Assert.False(list.None());
}
[Fact]
public void ForEach_IEnumerableAction_AppliesAction()
{
var list = new List<int> { 1, 2, 3, 4, 5 };
int sum = 0;
list.ForEach(x => sum += x);
Assert.Equal(15, sum);
}
[Fact]
public void RandomElement_ReturnsElementInList()
{
var list = new List<int> { 1, 2, 3, 4, 5 };
var randomElement = list.RandomElement();
Assert.Contains(randomElement, list);
}
[Fact]
public void RandomElement_EmptyList_ReturnsDefault()
{
var list = new List<int>();
var randomElement = list.RandomElement();
Assert.Equal(default, randomElement);
}
[Fact]
public void DetectDuplicatePropertyValues()
{
var people = new List<Person>()
{
new() { Id = 1, FirstName = "John", LastName = "Smith" },
new() { Id = 2, FirstName = "Jane", LastName = "SMITH" }
};
Assert.False(people.HasDuplicatePropertyValue(p => p.FirstName));
// Check that the string override of IgnoreCase works
// True, because the default is case-sensitive
Assert.False(people.HasDuplicatePropertyValue(p => p.LastName));
Assert.True(people.HasDuplicatePropertyValue(p => p.LastName, ignoreCase: true));
Assert.False(people.HasDuplicatePropertyValue(p => p.LastName, ignoreCase: false));
}
private class Person
{
public int Id { get; set; }
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
}
}