Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
236 changes: 118 additions & 118 deletions PolyShim.Tests/Net60/EnumerableExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,52 @@ namespace PolyShim.Tests.Net60;

public class EnumerableExtensionsTests
{
[Fact]
public void Chunk_Test()
{
// Arrange
var source = Enumerable.Range(1, 10);

// Act & assert
source
.Chunk(3)
.Should()
.BeEquivalentTo<int[]>([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10],
]);
}

[Fact]
public void DistinctBy_Test()
{
// Arrange
var source = new[]
{
new KeyValuePair<string, int>("Foo", 42),
new KeyValuePair<string, int>("Bar", 13),
new KeyValuePair<string, int>("Foo", 39),
new KeyValuePair<string, int>("Qux", 17),
new KeyValuePair<string, int>("Bar", 15),
new KeyValuePair<string, int>("Baz", 69),
new KeyValuePair<string, int>("Qux", 11),
new KeyValuePair<string, int>("Baz", 54),
};

// Act & assert
source
.DistinctBy(x => x.Key)
.Should()
.Equal(
new KeyValuePair<string, int>("Foo", 42),
new KeyValuePair<string, int>("Bar", 13),
new KeyValuePair<string, int>("Qux", 17),
new KeyValuePair<string, int>("Baz", 69)
);
}

[Fact]
public void ElementAt_Test()
{
Expand All @@ -32,6 +78,30 @@ public void ElementAtOrDefault_Test()
source.ElementAtOrDefault(^10).Should().Be(0);
}

[Fact]
public void ExceptBy_Test()
{
// Arrange
var source = new[]
{
new KeyValuePair<string, int>("Foo", 42),
new KeyValuePair<string, int>("Bar", 13),
new KeyValuePair<string, int>("Baz", 69),
new KeyValuePair<string, int>("Qux", 17),
};

var other = new[] { "Bar", "Qux" };

// Act & assert
source
.ExceptBy(other, x => x.Key)
.Should()
.Equal(
new KeyValuePair<string, int>("Foo", 42),
new KeyValuePair<string, int>("Baz", 69)
);
}

[Fact]
public void FirstOrDefault_Test()
{
Expand All @@ -55,71 +125,63 @@ public void FirstOrDefault_Predicate_Test()
}

[Fact]
public void LastOrDefault_Test()
{
// Act & assert
new[] { 1, 2, 3 }
.LastOrDefault(69)
.Should()
.Be(3);
Array.Empty<int>().LastOrDefault(69).Should().Be(69);
}

[Fact]
public void LastOrDefault_Predicate_Test()
public void IntersectBy_Test()
{
// Arrange
var source = new[] { 1, 2, 3, 4, 5 };
var source = new[]
{
new KeyValuePair<string, int>("Foo", 42),
new KeyValuePair<string, int>("Bar", 13),
new KeyValuePair<string, int>("Baz", 69),
new KeyValuePair<string, int>("Qux", 17),
};

// Act & assert
source.LastOrDefault(x => x % 2 == 0, 69).Should().Be(4);
source.LastOrDefault(x => x > 5, 69).Should().Be(69);
}
var other = new[] { "Bar", "Qux" };

[Fact]
public void SingleOrDefault_Test()
{
// Act & assert
new[] { 1 }
.SingleOrDefault(69)
source
.IntersectBy(other, x => x.Key)
.Should()
.Be(1);
Array.Empty<int>().SingleOrDefault(69).Should().Be(69);
.Equal(
new KeyValuePair<string, int>("Bar", 13),
new KeyValuePair<string, int>("Qux", 17)
);
}

[Fact]
public void SingleOrDefault_Predicate_Test()
public void LastOrDefault_Test()
{
// Arrange
var source = new[] { 1, 2, 3, 4, 5 };

// Act & assert
source.SingleOrDefault(x => x % 3 == 0, 69).Should().Be(3);
source.SingleOrDefault(x => x > 5, 69).Should().Be(69);
new[] { 1, 2, 3 }
.LastOrDefault(69)
.Should()
.Be(3);
Array.Empty<int>().LastOrDefault(69).Should().Be(69);
}

[Fact]
public void Take_Test()
public void LastOrDefault_Predicate_Test()
{
// Arrange
var source = new[] { 1, 2, 3, 4, 5 };

// Act & assert
source.Take(2..^1).Should().Equal(3, 4);
source.LastOrDefault(x => x % 2 == 0, 69).Should().Be(4);
source.LastOrDefault(x => x > 5, 69).Should().Be(69);
}

[Fact]
public void Min_Test()
public void Max_Test()
{
// Arrange
var source = new[] { 1, 2, 3, 4, 5 };

// Act & assert
source.Min(Comparer<int>.Default).Should().Be(1);
source.Max(Comparer<int>.Default).Should().Be(5);
}

[Fact]
public void MinBy_Test()
public void MaxBy_Test()
{
// Arrange
var source = new[]
Expand All @@ -131,25 +193,25 @@ public void MinBy_Test()
};

// Act
var result = source.MinBy(x => x.Value);
var result = source.MaxBy(x => x.Value);

// Assert
result.Key.Should().Be("Bar");
result.Value.Should().Be(13);
result.Key.Should().Be("Baz");
result.Value.Should().Be(69);
}

[Fact]
public void Max_Test()
public void Min_Test()
{
// Arrange
var source = new[] { 1, 2, 3, 4, 5 };

// Act & assert
source.Max(Comparer<int>.Default).Should().Be(5);
source.Min(Comparer<int>.Default).Should().Be(1);
}

[Fact]
public void MaxBy_Test()
public void MinBy_Test()
{
// Arrange
var source = new[]
Expand All @@ -161,87 +223,43 @@ public void MaxBy_Test()
};

// Act
var result = source.MaxBy(x => x.Value);
var result = source.MinBy(x => x.Value);

// Assert
result.Key.Should().Be("Baz");
result.Value.Should().Be(69);
result.Key.Should().Be("Bar");
result.Value.Should().Be(13);
}

[Fact]
public void DistinctBy_Test()
public void SingleOrDefault_Test()
{
// Arrange
var source = new[]
{
new KeyValuePair<string, int>("Foo", 42),
new KeyValuePair<string, int>("Bar", 13),
new KeyValuePair<string, int>("Foo", 39),
new KeyValuePair<string, int>("Qux", 17),
new KeyValuePair<string, int>("Bar", 15),
new KeyValuePair<string, int>("Baz", 69),
new KeyValuePair<string, int>("Qux", 11),
new KeyValuePair<string, int>("Baz", 54),
};

// Act & assert
source
.DistinctBy(x => x.Key)
new[] { 1 }
.SingleOrDefault(69)
.Should()
.Equal(
new KeyValuePair<string, int>("Foo", 42),
new KeyValuePair<string, int>("Bar", 13),
new KeyValuePair<string, int>("Qux", 17),
new KeyValuePair<string, int>("Baz", 69)
);
.Be(1);
Array.Empty<int>().SingleOrDefault(69).Should().Be(69);
}

[Fact]
public void ExceptBy_Test()
public void SingleOrDefault_Predicate_Test()
{
// Arrange
var source = new[]
{
new KeyValuePair<string, int>("Foo", 42),
new KeyValuePair<string, int>("Bar", 13),
new KeyValuePair<string, int>("Baz", 69),
new KeyValuePair<string, int>("Qux", 17),
};

var other = new[] { "Bar", "Qux" };
var source = new[] { 1, 2, 3, 4, 5 };

// Act & assert
source
.ExceptBy(other, x => x.Key)
.Should()
.Equal(
new KeyValuePair<string, int>("Foo", 42),
new KeyValuePair<string, int>("Baz", 69)
);
source.SingleOrDefault(x => x % 3 == 0, 69).Should().Be(3);
source.SingleOrDefault(x => x > 5, 69).Should().Be(69);
}

[Fact]
public void IntersectBy_Test()
public void Take_Test()
{
// Arrange
var source = new[]
{
new KeyValuePair<string, int>("Foo", 42),
new KeyValuePair<string, int>("Bar", 13),
new KeyValuePair<string, int>("Baz", 69),
new KeyValuePair<string, int>("Qux", 17),
};

var other = new[] { "Bar", "Qux" };
var source = new[] { 1, 2, 3, 4, 5 };

// Act & assert
source
.IntersectBy(other, x => x.Key)
.Should()
.Equal(
new KeyValuePair<string, int>("Bar", 13),
new KeyValuePair<string, int>("Qux", 17)
);
source.Take(2..^1).Should().Equal(3, 4);
}

[Fact]
Expand Down Expand Up @@ -275,24 +293,6 @@ public void UnionBy_Test()
);
}

[Fact]
public void Chunk_Test()
{
// Arrange
var source = Enumerable.Range(1, 10);

// Act & assert
source
.Chunk(3)
.Should()
.BeEquivalentTo<int[]>([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10],
]);
}

[Fact]
public void Zip_ThreeSequences_Test()
{
Expand Down
28 changes: 14 additions & 14 deletions PolyShim.Tests/Net80/CollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ public void AddRange_Empty_Test()
list.Should().Equal(1, 2, 3);
}

[Fact]
public void InsertRange_Test()
{
// Arrange
var list = new List<int> { 1, 2, 6 };
var items = (ReadOnlySpan<int>)new[] { 3, 4, 5 };

// Act
list.InsertRange(2, items);

// Assert
list.Should().Equal(1, 2, 3, 4, 5, 6);
}

[Fact]
public void CopyTo_Test()
{
Expand Down Expand Up @@ -78,4 +64,18 @@ public void CopyTo_TooSmall_Test()
// Assert
act.Should().Throw<ArgumentException>();
}

[Fact]
public void InsertRange_Test()
{
// Arrange
var list = new List<int> { 1, 2, 6 };
var items = (ReadOnlySpan<int>)new[] { 3, 4, 5 };

// Act
list.InsertRange(2, items);

// Assert
list.Should().Equal(1, 2, 3, 4, 5, 6);
}
}
Loading
Loading