-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathEnumerableRowCollectionExtensionsTests.cs
More file actions
119 lines (92 loc) · 4.68 KB
/
Copy pathEnumerableRowCollectionExtensionsTests.cs
File metadata and controls
119 lines (92 loc) · 4.68 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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace System.Data
{
public class EnumerableRowCollectionExtensionsTests
{
public class TestTypedTable<T> : TypedTableBase<T> where T : DataRow
{
public TestTypedTable() : base() { }
}
private class TestDataRowComparer<T> : Comparer<T> where T : DataRow
{
public override int Compare(T x, T y)
{
return int.Parse((string)x.ItemArray[0]).CompareTo(int.Parse((string)y.ItemArray[0]));
}
}
private (TypedTableBase<DataRow> table, DataRow one, DataRow two, DataRow three) InstantiateTable()
{
TypedTableBase<DataRow> table = new TestTypedTable<DataRow>();
table.Columns.Add();
DataRow two = table.Rows.Add(2);
DataRow one = table.Rows.Add(1);
DataRow three = table.Rows.Add(3);
return (table, one, two, three);
}
[Fact]
public void Where_SuccessfullyFindRow()
{
TypedTableBase<DataRow> table = new TestTypedTable<DataRow>();
table.Columns.Add();
DataRow two = table.Rows.Add("two");
EnumerableRowCollection<DataRow> source = table.Cast<DataRow>();
var filtered = source.Where(row => "two".Equals(row.ItemArray[0]));
// Check that only one row matches predicate condition
Assert.Equal(1, filtered.Count());
// Check that matching row is the same object as the second data row
Assert.Same(two, filtered.First());
}
[Fact]
public void OrderBy_AddSortExpressionValidation()
{
var (table, one, two, three) = InstantiateTable();
EnumerableRowCollection<DataRow> source = table.Cast<DataRow>();
var ordered = source.OrderBy(row => int.Parse((string)row.ItemArray[0]));
Assert.Equal(new DataRow[] { one, two, three }, ordered);
DataRow zero = table.Rows.Add(0);
var compared = source.OrderBy((row => row), new TestDataRowComparer<DataRow>());
Assert.Equal(new DataRow[] { zero, one, two, three }, compared);
}
[Fact]
public void OrderByDescending_AddSortExpressionValidation()
{
var (table, one, two, three) = InstantiateTable();
EnumerableRowCollection<DataRow> source = table.Cast<DataRow>();
var orderedBackwards = source.OrderByDescending(row => int.Parse((string)row.ItemArray[0]));
Assert.Equal(new DataRow[] { three, two, one }, orderedBackwards);
DataRow four = table.Rows.Add(4);
var comparedBackwards = source.OrderByDescending((row => row), new TestDataRowComparer<DataRow>());
Assert.Equal(new DataRow[] { four, three, two, one }, comparedBackwards);
}
[Fact]
public void ThenBy_AddSortExpressionValidation()
{
var (table, one, two, three) = InstantiateTable();
// Order the EnumerableRowCollection
OrderedEnumerableRowCollection<DataRow> orderedSource = table.Cast<DataRow>().OrderBy(row => int.Parse((string)row.ItemArray[0]));
DataRow zero = table.Rows.Add(0);
var orderedAgain = orderedSource.ThenBy(row => int.Parse((string)row.ItemArray[0]));
Assert.Equal(new DataRow[] { zero, one, two, three }, orderedAgain);
DataRow negative = table.Rows.Add(-1);
var comparedAgain = orderedSource.ThenBy((row => row), new TestDataRowComparer<DataRow>());
Assert.Equal(new DataRow[] { negative, zero, one, two, three }, comparedAgain);
}
[Fact]
public void ThenByDescending_AddSortExpressionValidation()
{
var (table, one, two, three) = InstantiateTable();
// Order the EnumerableRowCollection
OrderedEnumerableRowCollection<DataRow> orderedSource = table.Cast<DataRow>().OrderByDescending(row => int.Parse((string)row.ItemArray[0]));
DataRow zero = table.Rows.Add(0);
var orderedBackwardsAgain = orderedSource.ThenByDescending(row => int.Parse((string)row.ItemArray[0]));
Assert.Equal(new DataRow[] { three, two, one, zero }, orderedBackwardsAgain);
DataRow negative = table.Rows.Add(-1);
var comparedBackwardsAgain = orderedSource.ThenByDescending((row => row), new TestDataRowComparer<DataRow>());
Assert.Equal(new DataRow[] { three, two, one, zero, negative }, comparedBackwardsAgain);
}
}
}