Skip to content

Commit 2f79c46

Browse files
committed
more tests
1 parent d9203de commit 2f79c46

File tree

5 files changed

+1829
-0
lines changed

5 files changed

+1829
-0
lines changed

tests/ZLinq.Tests/Linq/CastTest.cs

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Runtime.InteropServices;
3+
using ZLinq.Linq;
4+
5+
namespace ZLinq.Tests.Linq;
6+
7+
public class CastTest
8+
{
9+
[Fact]
10+
public void EmptySource()
11+
{
12+
// Arrange
13+
var empty = Array.Empty<object>();
14+
15+
// Act
16+
var result = empty.AsValueEnumerable().Cast<int>().ToArray();
17+
18+
// Assert
19+
result.ShouldBeEmpty();
20+
}
21+
22+
[Fact]
23+
public void CastCompatibleTypes()
24+
{
25+
// Arrange
26+
var source = new object[] { 1, 2, 3, 4, 5 };
27+
28+
// Act
29+
var result = source.AsValueEnumerable().Cast<int>().ToArray();
30+
31+
// Assert
32+
result.ShouldBe(new[] { 1, 2, 3, 4, 5 });
33+
}
34+
35+
[Fact]
36+
public void CastToBaseType()
37+
{
38+
// Arrange
39+
var source = new[] { "one", "two", "three" };
40+
41+
// Act
42+
var result = source.AsValueEnumerable().Cast<object>().ToArray();
43+
44+
// Assert
45+
result.ShouldBe(new object[] { "one", "two", "three" });
46+
}
47+
48+
[Fact]
49+
public void CastThrowsForIncompatibleTypes()
50+
{
51+
// Arrange
52+
var source = new object[] { "one", 2, "three" }; // Mixed string and int
53+
54+
// Act & Assert
55+
Should.Throw<InvalidCastException>(() =>
56+
{
57+
var enumerable = source.AsValueEnumerable().Cast<int>();
58+
enumerable.ToArray(); // Will throw when trying to cast "one" to int
59+
});
60+
}
61+
62+
[Fact]
63+
public void CastFollowedByLinqOperations()
64+
{
65+
// Arrange
66+
var source = new object[] { 1, 2, 3, 4, 5 };
67+
68+
// Act
69+
var result = source.AsValueEnumerable()
70+
.Cast<int>()
71+
.Where(x => x % 2 == 0)
72+
.Select(x => x * 10)
73+
.ToArray();
74+
75+
// Assert
76+
result.ShouldBe(new[] { 20, 40 });
77+
}
78+
79+
[Fact]
80+
public void TryGetNonEnumeratedCount_ReturnsSourceCount()
81+
{
82+
// Arrange
83+
var source = new object[] { 1, 2, 3 };
84+
var castEnumerable = source.AsValueEnumerable().Cast<int>();
85+
86+
// Act
87+
var result = castEnumerable.TryGetNonEnumeratedCount(out var count);
88+
89+
// Assert
90+
// The Cast enumerator passes through the count from its source
91+
result.ShouldBeTrue();
92+
count.ShouldBe(3);
93+
}
94+
95+
[Fact]
96+
public void TryGetSpan_ReturnsFalse()
97+
{
98+
// Arrange
99+
var source = new object[] { 1, 2, 3 };
100+
var castEnumerable = source.AsValueEnumerable().Cast<int>();
101+
102+
// Act
103+
var result = castEnumerable.TryGetSpan(out var span);
104+
105+
// Assert
106+
result.ShouldBeFalse();
107+
span.IsEmpty.ShouldBeTrue();
108+
}
109+
110+
[Fact]
111+
public void TryCopyTo_ReturnsFalse()
112+
{
113+
// Arrange
114+
var source = new object[] { 1, 2, 3 };
115+
var castEnumerable = source.AsValueEnumerable().Cast<int>();
116+
var destination = new int[3];
117+
118+
// Act
119+
var result = castEnumerable.TryCopyTo(destination);
120+
121+
// Assert
122+
result.ShouldBeFalse();
123+
}
124+
125+
[Fact]
126+
public void Dispose_CallsSourceDispose()
127+
{
128+
// Arrange
129+
var source = new object[] { 1, 2, 3 };
130+
var enumerable = source.AsValueEnumerable().Cast<int>();
131+
132+
// Act & Assert (no exception should be thrown)
133+
enumerable.Dispose();
134+
}
135+
136+
[Fact]
137+
public void CastWithNull()
138+
{
139+
// Arrange
140+
var source = new object?[] { null, 1, null, 2 };
141+
142+
// Act
143+
var result = source.AsValueEnumerable().Cast<int?>().ToArray();
144+
145+
// Assert
146+
result.ShouldBe(new int?[] { null, 1, null, 2 });
147+
}
148+
149+
[Fact]
150+
public void CastPreservesItemsOrder()
151+
{
152+
// Arrange
153+
var source = new object[] { "first", "second", "third", "fourth" };
154+
155+
// Act
156+
var result = source.AsValueEnumerable().Cast<string>().ToArray();
157+
158+
// Assert
159+
result.ShouldBe(new[] { "first", "second", "third", "fourth" });
160+
}
161+
162+
[Fact]
163+
public void TryGetNext_ReturnsItemsInOrder()
164+
{
165+
// Arrange
166+
var source = new object[] { 1, 2, 3, 4, 5 };
167+
var castEnumerable = source.AsValueEnumerable().Cast<int>();
168+
var result = new List<int>();
169+
170+
// Act
171+
using var enumerator = castEnumerable.GetEnumerator();
172+
while (enumerator.MoveNext())
173+
{
174+
result.Add(enumerator.Current);
175+
}
176+
177+
// Assert
178+
result.ShouldBe(new[] { 1, 2, 3, 4, 5 });
179+
}
180+
181+
[Fact]
182+
public void CompareCastWithOfType()
183+
{
184+
// Arrange
185+
var source = new object[] { "one", 2, "three", 4 }; // Mixed string and int
186+
187+
// Act - Cast will throw, OfType will filter
188+
var ofTypeResult = source.AsValueEnumerable().OfType<int>().ToArray();
189+
190+
// Assert
191+
ofTypeResult.ShouldBe(new[] { 2, 4 });
192+
193+
// Act & Assert for Cast (should throw)
194+
Should.Throw<InvalidCastException>(() =>
195+
{
196+
source.AsValueEnumerable().Cast<int>().ToArray();
197+
});
198+
}
199+
}

0 commit comments

Comments
 (0)