Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Interleave/add-tests' into Imrpo…
Browse files Browse the repository at this point in the history
…veInterleaveTests

Conflicts resolved:

- MoreLinq.Test/InterleaveTest.cs
  • Loading branch information
atifaziz committed Jan 14, 2023
2 parents 8164514 + 681f456 commit 739850c
Show file tree
Hide file tree
Showing 211 changed files with 6,727 additions and 3,086 deletions.
4 changes: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"isRoot": true,
"tools": {
"dotnet-t4": {
"version": "2.0.5",
"version": "2.3.0",
"commands": [
"t4"
]
},
"dotnet-reportgenerator-globaltool": {
"version": "4.3.6",
"version": "5.1.11",
"commands": [
"reportgenerator"
]
Expand Down
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ trim_trailing_whitespace = true
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
indent_size = 2

[*.Build.{props,targets}]
indent_size = 2

[*.{sln}]
indent_style = tab

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.opencover.xml
**/TestResults/

### VisualStudio ###
## Ignore Visual Studio temporary files, build results, and
Expand Down
53 changes: 0 additions & 53 deletions .travis.yml

This file was deleted.

7 changes: 7 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>11</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
14 changes: 7 additions & 7 deletions MoreLinq.Test/AcquireTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class AcquireTest
[Test]
public void AcquireAll()
{
Disposable a = null;
Disposable b = null;
Disposable c = null;
Disposable? a = null;
Disposable? b = null;
Disposable? c = null;

var allocators = MoreEnumerable.From(() => a = new Disposable(),
() => b = new Disposable(),
Expand All @@ -48,16 +48,16 @@ public void AcquireAll()
[Test]
public void AcquireSome()
{
Disposable a = null;
Disposable b = null;
Disposable c = null;
Disposable? a = null;
Disposable? b = null;
Disposable? c = null;

var allocators = MoreEnumerable.From(() => a = new Disposable(),
() => b = new Disposable(),
() => throw new TestException(),
() => c = new Disposable());

Assert.Throws<TestException>(() => allocators.Acquire());
Assert.That(allocators.Acquire, Throws.TypeOf<TestException>());

Assert.That(a, Is.Not.Null);
Assert.That(a.Disposed, Is.True);
Expand Down
15 changes: 7 additions & 8 deletions MoreLinq.Test/AggregateRightTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

namespace MoreLinq.Test
{
using System;
using NUnit.Framework;

[TestFixture]
Expand All @@ -28,8 +27,8 @@ public class AggregateRightTest
[Test]
public void AggregateRightWithEmptySequence()
{
Assert.Throws<InvalidOperationException>(
() => new int[0].AggregateRight((a, b) => a + b));
Assert.That(() => new int[0].AggregateRight((a, b) => a + b),
Throws.InvalidOperationException);
}

[Test]
Expand All @@ -49,7 +48,7 @@ public void AggregateRight(SourceKind sourceKind)
{
var enumerable = Enumerable.Range(1, 5).Select(x => x.ToString()).ToSourceKind(sourceKind);

var result = enumerable.AggregateRight((a, b) => string.Format("({0}+{1})", a, b));
var result = enumerable.AggregateRight((a, b) => $"({a}+{b})");

Assert.That(result, Is.EqualTo("(1+(2+(3+(4+5))))"));
}
Expand All @@ -61,7 +60,7 @@ public void AggregateRight(SourceKind sourceKind)
[TestCase(true)]
public void AggregateRightSeedWithEmptySequence(object defaultValue)
{
Assert.That(new int[0].AggregateRight(defaultValue, (a, b) => b), Is.EqualTo(defaultValue));
Assert.That(new int[0].AggregateRight(defaultValue, (_, b) => b), Is.EqualTo(defaultValue));
}

[Test]
Expand All @@ -78,7 +77,7 @@ public void AggregateRightSeedFuncIsNotInvokedOnEmptySequence()
public void AggregateRightSeed()
{
var result = Enumerable.Range(1, 4)
.AggregateRight("5", (a, b) => string.Format("({0}+{1})", a, b));
.AggregateRight("5", (a, b) => $"({a}+{b})");

Assert.That(result, Is.EqualTo("(1+(2+(3+(4+5))))"));
}
Expand All @@ -90,14 +89,14 @@ public void AggregateRightSeed()
[TestCase(true)]
public void AggregateRightResultorWithEmptySequence(object defaultValue)
{
Assert.That(new int[0].AggregateRight(defaultValue, (a, b) => b, a => a == defaultValue), Is.EqualTo(true));
Assert.That(new int[0].AggregateRight(defaultValue, (_, b) => b, a => a == defaultValue), Is.EqualTo(true));
}

[Test]
public void AggregateRightResultor()
{
var result = Enumerable.Range(1, 4)
.AggregateRight("5", (a, b) => string.Format("({0}+{1})", a, b), a => a.Length);
.AggregateRight("5", (a, b) => $"({a}+{b})", a => a.Length);

Assert.That(result, Is.EqualTo("(1+(2+(3+(4+5))))".Length));
}
Expand Down
26 changes: 14 additions & 12 deletions MoreLinq.Test/AggregateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace MoreLinq.Test
using System.Reactive.Linq;
using System.Reflection;
using NUnit.Framework.Interfaces;
using static FuncModule;

[TestFixture]
public class AggregateTest
Expand All @@ -51,8 +52,8 @@ from m in typeof(MoreEnumerable).GetMethods(BindingFlags.Public | BindingFlags.S
Source = source,
Expectation = sum,
Instantiation = m.MakeGenericMethod(Enumerable.Repeat(typeof(int), m.GetGenericArguments().Length - 1)
.Append(typeof(int[])) // TResult
.ToArray()),
.Append(typeof(int[])) // TResult
.ToArray()),
}
into m
let rst = m.Instantiation.GetParameters().Last().ParameterType
Expand All @@ -64,18 +65,19 @@ into m
AccumulatorCount = (m.Instantiation.GetParameters().Length - 2 /* source + resultSelector */) / 2 /* seed + accumulator */,
ResultSelectorType = rst,
Parameters =
rst.GetMethod("Invoke")
.GetParameters()
.Select(p => Expression.Parameter(p.ParameterType))
.ToArray(),
rst.GetMethod("Invoke") is { } invoke
? invoke.GetParameters()
.Select(p => Expression.Parameter(p.ParameterType))
.ToArray()
: throw new Exception("""Method "Invoke" not found."""),
}
into m
let resultSelector =
Expression.Lambda(m.ResultSelectorType,
Expression.NewArrayInit(typeof(int), m.Parameters),
m.Parameters)
.Compile()
let accumulator = new Func<int, int, int>((s, n) => s + n)
let accumulator = Func((int s, int n) => s + n)
select new
{
Name = $"{name}({m.AccumulatorCount})",
Expand All @@ -95,7 +97,7 @@ into t
select new TestCaseData(t.Method, t.Args).SetName(t.Name).Returns(t.Expectation);

[TestCaseSource(nameof(AccumulatorsTestSource), new object[] { nameof(Accumulators), 10 })]
public object Accumulators(MethodInfo method, object[] args) =>
public object? Accumulators(MethodInfo method, object[] args) =>
method.Invoke(null, args);

[Test]
Expand All @@ -110,8 +112,8 @@ public void SevenUniqueAccumulators()
0, (s, e) => s + e.Num,
0, (s, e) => e.Num % 2 == 0 ? s + e.Num : s,
0, (s, _) => s + 1,
(int?)null, (s, e) => s is int n ? Math.Min(n, e.Num) : e.Num,
(int?)null, (s, e) => s is int n ? Math.Max(n, e.Num) : e.Num,
(int?)null, (s, e) => s is {} n ? Math.Min(n, e.Num) : e.Num,
(int?)null, (s, e) => s is {} n ? Math.Max(n, e.Num) : e.Num,
new HashSet<int>(), (s, e) => { s.Add(e.Str.Length); return s; },
new List<(int Num, string Str)>(), (s, e) => { s.Add((e.Num, e.Str)); return s; },
(sum, esum, count, min, max, lengths, items) => new
Expand All @@ -120,8 +122,8 @@ public void SevenUniqueAccumulators()
EvenSum = esum,
Count = count,
Average = (double)sum / count,
Min = min is int mn ? mn : throw new InvalidOperationException(),
Max = max is int mx ? mx : throw new InvalidOperationException(),
Min = min ?? throw new InvalidOperationException(),
Max = max ?? throw new InvalidOperationException(),
UniqueLengths = lengths,
Items = items,
}
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/AppendTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void AppendWithEmptyHeadSequence()
public void AppendWithNullTail()
{
var head = new[] { "first", "second" };
string tail = null;
string? tail = null;
var whole = head.Append(tail);
whole.AssertSequenceEqual("first", "second", null);
}
Expand Down
61 changes: 31 additions & 30 deletions MoreLinq.Test/AssertCountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public class AssertCountTest
public void AssertCountNegativeCount()
{
var source = new object[0];
AssertThrowsArgument.OutOfRangeException("count", () =>
source.AssertCount(-1));
AssertThrowsArgument.OutOfRangeException("count", () =>
source.AssertCount(-1, BreakingFunc.Of<int, int, Exception>()));
Assert.That(() => source.AssertCount(-1),
Throws.ArgumentOutOfRangeException("count"));
Assert.That(() => source.AssertCount(-1, BreakingFunc.Of<int, int, Exception>()),
Throws.ArgumentOutOfRangeException("count"));
}

[Test]
Expand All @@ -42,46 +42,47 @@ public void AssertCountSequenceWithMatchingLength()
[Test]
public void AssertCountShortSequence()
{
Assert.Throws<SequenceException>(() =>
"foo,bar,baz".GenerateSplits(',').AssertCount(4).Consume());
Assert.That(() => "foo,bar,baz".GenerateSplits(',').AssertCount(4).Consume(),
Throws.TypeOf<SequenceException>());
}

[Test]
public void AssertCountLongSequence()
{
Assert.Throws<SequenceException>(() =>
"foo,bar,baz".GenerateSplits(',').AssertCount(2).Consume());
Assert.That(() => "foo,bar,baz".GenerateSplits(',').AssertCount(2).Consume(),
Throws.TypeOf<SequenceException>());
}

[Test]
public void AssertCountDefaultExceptionMessageVariesWithCase()
[TestCase(4, "Sequence contains too few elements when exactly 4 were expected.")]
[TestCase(2, "Sequence contains too many elements when exactly 2 were expected.")]
public void AssertCountDefaultExceptionMessageVariesWithCase(int count, string expectedMessage)
{
var tokens = "foo,bar,baz".GenerateSplits(',');
var e1 = Assert.Throws<SequenceException>(() => tokens.AssertCount(4).Consume());
var e2 = Assert.Throws<SequenceException>(() => tokens.AssertCount(2).Consume());
Assert.That(e1.Message, Is.Not.EqualTo(e2.Message));

Assert.That(() => tokens.AssertCount(count).Consume(),
Throws.TypeOf<SequenceException>().With.Message.EqualTo(expectedMessage));
}

[Test]
public void AssertCountLongSequenceWithErrorSelector()
{
var e =
Assert.Throws<TestException>(() =>
"foo,bar,baz".GenerateSplits(',').AssertCount(2, (cmp, count) => new TestException(cmp, count))
.Consume());
Assert.That(e.Cmp, Is.GreaterThan(0));
Assert.That(e.Count, Is.EqualTo(2));
Assert.That(() =>
"foo,bar,baz".GenerateSplits(',').AssertCount(2, (cmp, count) => new TestException(cmp, count))
.Consume(),
Throws.TypeOf<TestException>()
.With.Property(nameof(TestException.Cmp)).GreaterThan(0)
.And.Count.EqualTo(2));
}

[Test]
public void AssertCountShortSequenceWithErrorSelector()
{
var e =
Assert.Throws<TestException>(() =>
"foo,bar,baz".GenerateSplits(',').AssertCount(4, (cmp, count) => new TestException(cmp, count))
.Consume());
Assert.That(e.Cmp, Is.LessThan(0));
Assert.That(e.Count, Is.EqualTo(4));
Assert.That(() =>
"foo,bar,baz".GenerateSplits(',').AssertCount(4, (cmp, count) => new TestException(cmp, count))
.Consume(),
Throws.TypeOf<TestException>()
.With.Property(nameof(TestException.Cmp)).LessThan(0)
.And.Count.EqualTo(4));
}

sealed class TestException : Exception
Expand All @@ -105,24 +106,24 @@ public void AssertCountIsLazy()
[Test]
public void AssertCountWithCollectionIsLazy()
{
new BreakingCollection<object>(5).AssertCount(0);
new BreakingCollection<int>(new int[5]).AssertCount(0);
}

[Test]
public void AssertCountWithMatchingCollectionCount()
{
var xs = new[] { 123, 456, 789 };
Assert.AreSame(xs, xs.AssertCount(3));
Assert.That(xs, Is.SameAs(xs.AssertCount(3)));
}

[TestCase(3, 2, "Sequence contains too many elements when exactly 2 were expected.")]
[TestCase(3, 4, "Sequence contains too few elements when exactly 4 were expected.")]
public void AssertCountWithMismatchingCollectionCount(int sourceCount, int count, string message)
{
var xs = new int[sourceCount];
var enumerator = xs.AssertCount(count).GetEnumerator();
var e = Assert.Throws<SequenceException>(() => enumerator.MoveNext());
Assert.AreEqual(e.Message, message);
using var enumerator = xs.AssertCount(count).GetEnumerator();
Assert.That(enumerator.MoveNext,
Throws.TypeOf<SequenceException>().With.Message.EqualTo(message));
}

[Test]
Expand Down
Loading

0 comments on commit 739850c

Please sign in to comment.