public void CatchErrorWorksSaltarelleEnumerable() { string errorMessage = null; var enumerable = new TestEnumerable(1, 10) { ThrowOnIndex = 4 }; var result = enumerable.CatchError(ex => errorMessage = ex.Message).ToArray(); Assert.AreEqual(result, new[] { 1, 2, 3, 4, }); Assert.AreEqual(errorMessage, "error"); }
public void MemoizeWorksForSaltarelleEnumerable() { var enumerable = new TestEnumerable(1, 5); var enm = enumerable.Memoize(); enm.Where(i => i % 2 == 0).Force(); enm.Where(i => i % 2 == 0).Force(); Assert.AreEqual(5, enumerable.NumMoveNextCalls); }
public void CanForeachOverLinqJSEnumerable() { var enumerable = new TestEnumerable(1, 3); var result = new List<int>(); foreach (var i in enumerable.Select(i => i * i)) { result.Add(i); } Assert.AreEqual(result, new[] { 1, 4, 9 }, "Result should be correct"); Assert.IsTrue(enumerable.EnumeratorDisposed, "Enumerator should be disposed"); }
private static IEnumerable <int> CreateTestEnumerable() { var foo = new TestEnumerable(new[] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 }, new[] { 7, 8, 9 } }); return(foo.ReadLine()); }
public void Count_With_Enumerable_ReferenceType(int expected) { // Arrange IEnumerable <int> source = TestEnumerable.ReferenceType(expected); // Act var result = source.Count(); // Assert result.Should().Be(expected); }
static void Main(string[] args) { var list = new List <int> { 1, 2, 3, 4, 5 }; var enumerable = new TestEnumerable <int>(list); var predicateAsync = new TestPredicateAsync <int>(i => { switch (i) { case 1: return(Task.Delay(600).ContinueWith(task => true)); case 2: return(Task.Delay(200).ContinueWith(task => true)); case 3: return(Task.Delay(800).ContinueWith(task => true)); case 4: return(Task.Delay(500).ContinueWith(task => true)); case 5: return(Task.Delay(300).ContinueWith(task => true)); default: throw new Exception("Oops!"); } }); var defaultValue = LinqAsyncManager.DefaultValue.OrderByPredicateCompletion; try { LinqAsyncManager.DefaultValue.OrderByPredicateCompletion = false; var result = enumerable.WhereAsync(x => predicateAsync.Predicate(x)).ToList().Result; //// MUST have 5 iterations for enumerable //Assert.AreEqual(4, enumerable.CurrentIndex); //// MUST be ordered with original order //Assert.AreEqual(1, result[0]); //Assert.AreEqual(2, result[1]); //Assert.AreEqual(3, result[2]); //Assert.AreEqual(4, result[3]); //Assert.AreEqual(5, result[4]); } finally { LinqAsyncManager.DefaultValue.OrderByPredicateCompletion = defaultValue; } }
public void First_With_EmptyIEnumerable_Should_Throw() { // Arrange var empty = TestEnumerable.ValueType(0); // Act Action action = () => empty.First(); // Assert action.Should() .ThrowExactly <InvalidOperationException>() .WithMessage("Sequence contains no elements"); }
public void ParallelForEach() { var testEnumerable = new TestEnumerable(); Parallel.ForEach(testEnumerable, i => testEnumerable.Result[i] = Environment.CurrentManagedThreadId); #else Parallel.ForEach(testEnumerable, i => testEnumerable.Result[i] = Thread.CurrentThread.ManagedThreadId); Assert.IsTrue(testEnumerable.Result.All(n => n != 0)); Assert.IsTrue(testEnumerable.EnumeratorDisposed); }
public void Single_With_MoreThanElement_Should_Throw() { // Arrange var empty = TestEnumerable.ValueType(2); // Act Action action = () => empty.SingleOrDefault(); // Assert action.Should() .ThrowExactly <InvalidOperationException>() .WithMessage("Sequence contains more than one element"); }
public void ListSourceConstructorWithEnumerable() { tlog.Debug(tag, $"ListSourceConstructorWithEnumerable START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceConstructorWithEnumerable END (OK)"); }
public void ListSourceCount() { tlog.Debug(tag, $"ListSourceCount START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); Assert.AreEqual(testingTarget.Count, 3, "Count of ListSouce should be equal to 3."); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceCount END (OK)"); }
public void ListSourceSyncRoot() { tlog.Debug(tag, $"ListSourceSyncRoot START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); Assert.IsNotNull(testingTarget.SyncRoot, "SyncRoot of ListSouce should not be null."); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceSyncRoot END (OK)"); }
public void ListSourceGetItem() { tlog.Debug(tag, $"ListSourceGetItem START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); Assert.AreEqual(testingTarget.GetItem(0), 1, "The value of the first item of ListSouce should be 1."); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceGetItem END (OK)"); }
public void ListSourceIsSynchronized() { tlog.Debug(tag, $"ListSourceIsSynchronized START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); tlog.Debug(tag, "IsSynchronized : " + testingTarget.IsSynchronized); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceIsSynchronized END (OK)"); }
public void ListSourceIsFooter() { tlog.Debug(tag, $"ListSourceIsFooter START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); testingTarget.HasFooter = true; tlog.Debug(tag, "IsFooter : " + testingTarget.IsFooter(2)); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceIsFooter END (OK)"); }
public void ListSourceIsHeader() { tlog.Debug(tag, $"ListSourceIsHeader START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); testingTarget.HasHeader = true; Assert.IsTrue(testingTarget.IsHeader(0), "The first item of ListSouce should be header."); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceIsHeader END (OK)"); }
public void ListSourceClear() { tlog.Debug(tag, $"ListSourceClear START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); testingTarget.Clear(); Assert.AreEqual(testingTarget.Count, 0, "The ListSouce should be empty after cleared."); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceClear END (OK)"); }
public void ListSourceContains() { tlog.Debug(tag, $"ListSourceContains START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); var ret = testingTarget.Contains(3); Assert.IsTrue(ret, "The ListSouce should contain 3."); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceContains END (OK)"); }
public void ListSourceIndexOf() { tlog.Debug(tag, $"ListSourceIndexOf START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); var ret = testingTarget.IndexOf(1); Assert.AreEqual(ret, 0, "The index of ListSouce should be 0."); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceIndexOf END (OK)"); }
public async Task ArrayToStream_Cancel() { // arrange var list = new List <object?>(); var asyncEnumerable = new TestEnumerable(_strings); var adapter = new AsyncEnumerableStreamAdapter <string>(asyncEnumerable); var cts = new CancellationTokenSource(); // act await foreach (var item in adapter.WithCancellation(cts.Token)) { list.Add(item); cts.Cancel(); } // assert Assert.Collection(list, s => Assert.Equal(_strings[0], s)); }
public void ListSourceAdd() { tlog.Debug(tag, $"ListSourceAdd START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); var ret = testingTarget.Add(4); Assert.AreEqual(ret, 3, "The index of ListSouce should is 3."); Assert.AreEqual(testingTarget.Count, 4, "The count of ListSouce should be 4."); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceAdd END (OK)"); }
public async Task ArrayToStream() { // arrange var list = new List <object?>(); var asyncEnumerable = new TestEnumerable(_strings); var adapter = new AsyncEnumerableStreamAdapter <string>(asyncEnumerable); // act await foreach (var item in adapter) { list.Add(item); } // assert for (var i = 0; i < list.Count; i++) { Assert.Equal(_strings[i], list[i]); } }
public void ListSourceCopyTo() { tlog.Debug(tag, $"ListSourceCopyTo START"); var para = new TestEnumerable(); var testingTarget = new ListSource(para); Assert.IsNotNull(testingTarget, "should be not null"); Assert.IsInstanceOf <ListSource>(testingTarget, "should be an instance of testing target class!"); Array intArray = Array.CreateInstance(typeof(int), 10); intArray.SetValue(4, 0); testingTarget.CopyTo(intArray, 2); tlog.Debug(tag, "Count : " + testingTarget.Count); testingTarget.Dispose(); tlog.Debug(tag, $"ListSourceCopyTo END (OK)"); }
public void Run() { var maxCount = 2; var count = 0; foreach (var i in SimpleEnumerable()) { if (i < 2) { count++; } Console.WriteLine(i); if (maxCount == count) { break; } } foreach (var item in new TestEnumerableDuckTyping()) { Console.WriteLine(item); } var testEnumerable = new TestEnumerable(); var enumerator = testEnumerable.GetEnumerator().MoveNext(); IEnumerable testEnumerable2 = testEnumerable; var enumerator1 = testEnumerable2.GetEnumerator().MoveNext(); foreach (var item in new TestEnumerable()) { Console.WriteLine(item); } foreach (var number in GetNumbers(15)) { Console.WriteLine(number); } }
public void TraverseDepthFirstWithResultSelectorWithIndexArgWorksForSaltarelleEnumerable() { var enumerable = new TestEnumerable(1, 1); Assert.AreEqual(enumerable.TraverseDepthFirst(x => new[] { x + x }, (x, level) => new { x, level }).Take(5).ToArray(), new[] { new { x = 1, level = 0 }, new { x = 2, level = 1 }, new { x = 4, level = 2 }, new { x = 8, level = 3 }, new { x = 16, level = 4 } }, "Result should be correct"); Assert.IsTrue(enumerable.EnumeratorDisposed, "Enumerator should be disposed"); }
public void TraverseDepthFirstWorksForSaltarelleEnumerable() { var enumerable = new TestEnumerable(1, 1); Assert.AreEqual(enumerable.TraverseDepthFirst(x => new[] { x + x }).Take(5).ToArray(), new[] { 1, 2, 4, 8, 16 }, "Result should be correct"); Assert.IsTrue(enumerable.EnumeratorDisposed, "Enumerator should be disposed"); }
public void WhereWithoutIndexWorksForSaltarelleEnumerable() { var enumerable = new TestEnumerable(1, 5); Assert.AreEqual(enumerable.Where(i => i > 3).ToArray(), new[] { 4, 5 }, "Result should be correct"); Assert.IsTrue(enumerable.EnumeratorDisposed, "Enumerator should be disposed"); }
public void GuardSupportsCollections() { var message = RandomMessage; // Define struct sources. var withoutNullVal = Enumerable.Range('A', 4).Select(i => (char)i).ToList(); var withNullVal = new List <char?>(withoutNullVal.Select(c => c as char?)); var nullIndex = withNullVal.Count - 2; withNullVal.Insert(nullIndex, null); var withNullValEnumerable = new TestEnumerable <char?>(withNullVal); var withNullValEnumerableArg = Guard.Argument(() => withNullValEnumerable); var withNullValCollection = new TestCollection <char?>(withNullVal); var withNullValCollectionArg = Guard.Argument(() => withNullValCollection); var withoutNullValEnumerable = new TestEnumerable <char>(withoutNullVal); var withoutNullValEnumerableArg = Guard.Argument(() => withoutNullValEnumerable); var withoutNullValCollection = new TestCollection <char>(withoutNullVal); var withoutNullValCollectionArg = Guard.Argument(() => withoutNullValCollection); // Define class sources. var withoutNullRef = withoutNullVal.Select(i => i.ToString()).ToList(); var withNullRef = new List <string>(withoutNullRef); withNullRef.Insert(nullIndex, null); var nullRefEnumerable = null as IEnumerable <string>; var nullRefEnumerableArg = Guard.Argument(() => nullRefEnumerable); var emptyRefEnumerable = new TestEnumerable <string>(new string[0]); var emptyRefEnumerableArg = Guard.Argument(() => emptyRefEnumerable); var withNullRefEnumerable = new TestEnumerable <string>(withNullRef); var withNullRefEnumerableArg = Guard.Argument(() => withNullRefEnumerable); var emptyRefCollection = new TestCollection <string>(new string[0]); var emptyRefCollectionArg = Guard.Argument(() => emptyRefCollection); var withNullRefCollection = new TestCollection <string>(withNullRef); var withNullRefCollectionArg = Guard.Argument(() => withNullRefCollection); var validNullableRefIndex = RandomUtils.Current.Next(withNullRef.Count); var validNullableRef = withNullRef[validNullableRefIndex]; var invalidNullableRef = "Z"; var withoutNullRefEnumerable = new TestEnumerable <string>(withoutNullRef); var withoutNullRefEnumerableArg = Guard.Argument(() => withoutNullRefEnumerable); var withoutNullRefCollection = new TestCollection <string>(withoutNullRef); var withoutNullRefCollectionArg = Guard.Argument(() => withoutNullRefCollection); // Empty enumerable. nullRefEnumerableArg.Empty(); emptyRefEnumerableArg.Empty(); CheckAndResetEnumerable(emptyRefEnumerable, 0, true); Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).Empty()); CheckAndResetEnumerable(withNullRefEnumerable, 1); var ex = Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).Empty(e => { Assert.Same(withNullRefEnumerable, e); return(message); })); CheckAndResetEnumerable(withNullRefEnumerable, 1); Assert.StartsWith(message, ex.Message); // Empty collection. emptyRefCollectionArg.Empty(); CheckAndResetCollection(emptyRefCollection, true, false); Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).Empty()); CheckAndResetCollection(withNullRefCollection, true, false); ex = Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).Empty(c => { Assert.Same(withNullRefCollection, c); return(message); })); CheckAndResetCollection(withNullRefCollection, true, false); Assert.StartsWith(message, ex.Message); // Non-empty enumerable. nullRefEnumerableArg.NotEmpty(); withNullRefEnumerableArg.NotEmpty(); CheckAndResetEnumerable(withNullRefEnumerable, 1); Assert.Throws <ArgumentException>( nameof(emptyRefEnumerable), () => Guard.Argument(() => emptyRefEnumerable).NotEmpty()); CheckAndResetEnumerable(emptyRefEnumerable, 0, true); ex = Assert.Throws <ArgumentException>( nameof(emptyRefEnumerable), () => Guard.Argument(() => emptyRefEnumerable).NotEmpty(e => { Assert.Same(emptyRefEnumerable, e); return(message); })); CheckAndResetEnumerable(emptyRefEnumerable, 0, true); Assert.StartsWith(message, ex.Message); // Non-empty collection. withNullRefCollectionArg.NotEmpty(); CheckAndResetCollection(withNullRefCollection, true, false); Assert.Throws <ArgumentException>( nameof(emptyRefCollection), () => Guard.Argument(() => emptyRefCollection).NotEmpty()); CheckAndResetCollection(emptyRefCollection, true, false); ex = Assert.Throws <ArgumentException>( nameof(emptyRefCollection), () => Guard.Argument(() => emptyRefCollection).NotEmpty(c => { Assert.Same(emptyRefCollection, c); return(message); })); CheckAndResetCollection(emptyRefCollection, true, false); Assert.StartsWith(message, ex.Message); // Min count of enumerable. nullRefEnumerableArg.MinCount(withNullRef.Count - 2).MinCount(withNullRef.Count); withNullRefEnumerableArg.MinCount(withNullRef.Count - 2); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count - 2); withNullRefEnumerableArg.MinCount(withNullRef.Count); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).MinCount(withNullRef.Count + 1)); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); ex = Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).MinCount(withNullRef.Count + 1, (e, min) => { Assert.Same(withNullRefEnumerable, e); Assert.Equal(withNullRef.Count + 1, min); return(message); })); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); Assert.StartsWith(message, ex.Message); // Min count of collection. withNullRefCollectionArg.MinCount(withNullRef.Count - 2).MinCount(withNullRef.Count); CheckAndResetCollection(withNullRefCollection, true, false); Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).MinCount(withNullRef.Count + 1)); CheckAndResetCollection(withNullRefCollection, true, false); ex = Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).MinCount(withNullRef.Count + 1, (c, min) => { Assert.Same(withNullRefCollection, c); Assert.Equal(withNullRef.Count + 1, min); return(message); })); CheckAndResetCollection(withNullRefCollection, true, false); Assert.StartsWith(message, ex.Message); // Max count of enumerable. nullRefEnumerableArg.MaxCount(withNullRef.Count).MaxCount(withNullRef.Count + 2); withNullRefEnumerableArg.MaxCount(withNullRef.Count); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); withNullRefEnumerableArg.MaxCount(withNullRef.Count + 2); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).MaxCount(withNullRef.Count - 2)); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count - 1); ex = Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).MaxCount(withNullRef.Count - 2, (e, max) => { Assert.Same(withNullRefEnumerable, e); Assert.Equal(withNullRef.Count - 2, max); return(message); })); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count - 1); Assert.StartsWith(message, ex.Message); // Max count of collection. withNullRefCollectionArg.MaxCount(withNullRef.Count).MaxCount(withNullRef.Count + 2); CheckAndResetCollection(withNullRefCollection, true, false); Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).MaxCount(withNullRef.Count - 1)); CheckAndResetCollection(withNullRefCollection, true, false); ex = Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).MaxCount(withNullRef.Count - 1, (c, max) => { Assert.Same(withNullRefCollection, c); Assert.Equal(withNullRef.Count - 1, max); return(message); })); CheckAndResetCollection(withNullRefCollection, true, false); Assert.StartsWith(message, ex.Message); // Count range of enumerable. nullRefEnumerableArg .CountInRange(0, withNullRef.Count) .CountInRange(withNullRef.Count, withNullRef.Count + 1); withNullRefEnumerableArg.CountInRange(0, withNullRef.Count); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); withNullRefEnumerableArg.CountInRange(withNullRef.Count, withNullRef.Count + 1); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).CountInRange(0, withNullRef.Count - 2)); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count - 1); ex = Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).CountInRange(0, withNullRef.Count - 2, (e, min, max) => { Assert.Same(withNullRefEnumerable, e); Assert.Equal(0, min); Assert.Equal(withNullRef.Count - 2, max); return(message); })); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count - 1); Assert.StartsWith(message, ex.Message); Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).CountInRange(withNullRef.Count + 1, withNullRef.Count + 2)); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); ex = Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).CountInRange(withNullRef.Count + 1, withNullRef.Count + 2, (e, min, max) => { Assert.Same(withNullRefEnumerable, e); Assert.Equal(withNullRef.Count + 1, min); Assert.Equal(withNullRef.Count + 2, max); return(message); })); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); Assert.StartsWith(message, ex.Message); // Count range of collection. withNullRefCollectionArg.CountInRange(0, withNullRef.Count); CheckAndResetCollection(withNullRefCollection, true, false); withNullRefCollectionArg.CountInRange(withNullRef.Count, withNullRef.Count + 1); CheckAndResetCollection(withNullRefCollection, true, false); Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).CountInRange(0, withNullRef.Count - 2)); CheckAndResetCollection(withNullRefCollection, true, false); ex = Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).CountInRange(0, withNullRef.Count - 2, (c, min, max) => { Assert.Same(withNullRefCollection, c); Assert.Equal(0, min); Assert.Equal(withNullRef.Count - 2, max); return(message); })); CheckAndResetCollection(withNullRefCollection, true, false); Assert.StartsWith(message, ex.Message); Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).CountInRange(withNullRef.Count + 1, withNullRef.Count + 2)); CheckAndResetCollection(withNullRefCollection, true, false); ex = Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).CountInRange(withNullRef.Count + 1, withNullRef.Count + 2, (c, min, max) => { Assert.Same(withNullRefCollection, c); Assert.Equal(withNullRef.Count + 1, min); Assert.Equal(withNullRef.Count + 2, max); return(message); })); CheckAndResetCollection(withNullRefCollection, true, false); Assert.StartsWith(message, ex.Message); // Enumerable contains. nullRefEnumerableArg.Contains(validNullableRef); withNullRefEnumerableArg.Contains(validNullableRef); CheckAndResetEnumerable(withNullRefEnumerable, validNullableRefIndex + 1); Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).Contains(invalidNullableRef)); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); ex = Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).Contains(invalidNullableRef, (e, item) => { Assert.Same(withNullRefEnumerable, e); Assert.Equal(invalidNullableRef, item); return(message); })); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); Assert.StartsWith(message, ex.Message); // Collection contains. withNullRefCollectionArg.Contains(validNullableRef); CheckAndResetCollection(withNullRefCollection, false, true); Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).Contains(invalidNullableRef)); CheckAndResetCollection(withNullRefCollection, false, true); ex = Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).Contains(invalidNullableRef, (c, item) => { Assert.Same(withNullRefCollection, c); Assert.Equal(invalidNullableRef, item); return(message); })); CheckAndResetCollection(withNullRefCollection, false, true); Assert.StartsWith(message, ex.Message); // Enumerable does not contain. nullRefEnumerableArg.DoesNotContain(validNullableRef); withNullRefEnumerableArg.DoesNotContain(invalidNullableRef); CheckAndResetEnumerable(withNullRefEnumerable, withNullRef.Count); Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).DoesNotContain(validNullableRef)); CheckAndResetEnumerable(withNullRefEnumerable, validNullableRefIndex + 1); ex = Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).DoesNotContain(validNullableRef, (e, item) => { Assert.Same(withNullRefEnumerable, e); Assert.Equal(validNullableRef, item); return(message); })); CheckAndResetEnumerable(withNullRefEnumerable, validNullableRefIndex + 1); Assert.StartsWith(message, ex.Message); // Collection does not contain. withNullRefCollectionArg.DoesNotContain(invalidNullableRef); CheckAndResetCollection(withNullRefCollection, false, true); Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).DoesNotContain(validNullableRef)); CheckAndResetCollection(withNullRefCollection, false, true); ex = Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).DoesNotContain(validNullableRef, (c, item) => { Assert.Same(withNullRefCollection, c); Assert.Equal(validNullableRef, item); return(message); })); CheckAndResetCollection(withNullRefCollection, false, true); Assert.StartsWith(message, ex.Message); // Enumerable contains null class. nullRefEnumerableArg.ContainsNull(); withNullRefEnumerableArg.ContainsNull(); CheckAndResetEnumerable(withNullRefEnumerable, nullIndex + 1); Assert.Throws <ArgumentException>( nameof(withoutNullRefEnumerable), () => Guard.Argument(() => withoutNullRefEnumerable).ContainsNull()); CheckAndResetEnumerable(withoutNullRefEnumerable, withoutNullRef.Count); ex = Assert.Throws <ArgumentException>( nameof(withoutNullRefEnumerable), () => Guard.Argument(() => withoutNullRefEnumerable).ContainsNull(e => { Assert.Same(withoutNullRefEnumerable, e); return(message); })); CheckAndResetEnumerable(withoutNullRefEnumerable, withoutNullRef.Count); Assert.StartsWith(message, ex.Message); // Enumerable contains null struct. withNullValEnumerableArg.ContainsNull(); CheckAndResetEnumerable(withNullValEnumerable, nullIndex + 1); Assert.Throws <ArgumentException>( nameof(withoutNullValEnumerable), () => Guard.Argument(() => withoutNullValEnumerable).ContainsNull()); CheckAndResetEnumerable(withoutNullValEnumerable, withoutNullVal.Count); ex = Assert.Throws <ArgumentException>( nameof(withoutNullValEnumerable), () => Guard.Argument(() => withoutNullValEnumerable).ContainsNull(e => { Assert.Same(withoutNullValEnumerable, e); return(message); })); CheckAndResetEnumerable(withoutNullValEnumerable, withoutNullVal.Count); Assert.StartsWith(message, ex.Message); // Collection contains null class. withNullRefCollectionArg.ContainsNull(); CheckAndResetCollection(withNullRefCollection, false, true); Assert.Throws <ArgumentException>( nameof(withoutNullRefCollection), () => Guard.Argument(() => withoutNullRefCollection).ContainsNull()); CheckAndResetCollection(withoutNullRefCollection, false, true); ex = Assert.Throws <ArgumentException>( nameof(withoutNullRefCollection), () => Guard.Argument(() => withoutNullRefCollection).ContainsNull(c => { Assert.Same(withoutNullRefCollection, c); return(message); })); CheckAndResetCollection(withoutNullRefCollection, false, true); Assert.StartsWith(message, ex.Message); // Collection contains null struct. withNullValCollectionArg.ContainsNull(); CheckAndResetCollection(withNullValCollection, false, true); Assert.Throws <ArgumentException>( nameof(withoutNullValCollection), () => Guard.Argument(() => withoutNullValCollection).ContainsNull()); CheckAndResetCollection(withoutNullValCollection, false, false); ex = Assert.Throws <ArgumentException>( nameof(withoutNullValCollection), () => Guard.Argument(() => withoutNullValCollection).ContainsNull(c => { Assert.Same(withoutNullValCollection, c); return(message); })); CheckAndResetCollection(withoutNullValCollection, false, false); Assert.StartsWith(message, ex.Message); // Enumerable does not contain null class. nullRefEnumerableArg.DoesNotContainNull(); withoutNullRefEnumerableArg.DoesNotContainNull(); CheckAndResetEnumerable(withoutNullRefEnumerable, withoutNullRef.Count); Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).DoesNotContainNull()); CheckAndResetEnumerable(withNullRefEnumerable, nullIndex + 1); ex = Assert.Throws <ArgumentException>( nameof(withNullRefEnumerable), () => Guard.Argument(() => withNullRefEnumerable).DoesNotContainNull(e => { Assert.Same(withNullRefEnumerable, e); return(message); })); CheckAndResetEnumerable(withNullRefEnumerable, nullIndex + 1); Assert.StartsWith(message, ex.Message); // Enumerable does not contain nullable struct. withoutNullValEnumerableArg.DoesNotContainNull(); CheckAndResetEnumerable(withoutNullValEnumerable, withoutNullVal.Count); Assert.Throws <ArgumentException>( nameof(withNullValEnumerable), () => Guard.Argument(() => withNullValEnumerable).DoesNotContainNull()); CheckAndResetEnumerable(withNullValEnumerable, nullIndex + 1); ex = Assert.Throws <ArgumentException>( nameof(withNullValEnumerable), () => Guard.Argument(() => withNullValEnumerable).DoesNotContainNull(e => { Assert.Same(withNullValEnumerable, e); return(message); })); CheckAndResetEnumerable(withNullValEnumerable, nullIndex + 1); Assert.StartsWith(message, ex.Message); // Collection does not contain null class. withoutNullRefCollectionArg.DoesNotContainNull(); CheckAndResetCollection(withoutNullRefCollection, false, true); Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).DoesNotContainNull()); CheckAndResetCollection(withNullRefCollection, false, true); ex = Assert.Throws <ArgumentException>( nameof(withNullRefCollection), () => Guard.Argument(() => withNullRefCollection).DoesNotContainNull(c => { Assert.Same(withNullRefCollection, c); return(message); })); CheckAndResetCollection(withNullRefCollection, false, true); Assert.StartsWith(message, ex.Message); // Collection does not contain null struct. withoutNullValCollectionArg.DoesNotContainNull(); CheckAndResetCollection(withoutNullValCollection, false, false); Assert.Throws <ArgumentException>( nameof(withNullValCollection), () => Guard.Argument(() => withNullValCollection).DoesNotContainNull()); CheckAndResetCollection(withNullValCollection, false, true); ex = Assert.Throws <ArgumentException>( nameof(withNullValCollection), () => Guard.Argument(() => withNullValCollection).DoesNotContainNull(c => { Assert.Same(withNullValCollection, c); return(message); })); CheckAndResetCollection(withNullValCollection, false, true); Assert.StartsWith(message, ex.Message); // Testing helpers. void CheckAndResetEnumerable <T>(TestEnumerable <T> enumerable, int iterationCount, bool?enumerated = null) { if (!enumerated.HasValue) { enumerated = iterationCount > 0; } Assert.Equal(enumerated, enumerable.Enumerated); Assert.Equal(iterationCount, enumerable.IterationCount); enumerable.Reset(); Assert.False(enumerable.Enumerated); Assert.Equal(0, enumerable.IterationCount); } void CheckAndResetCollection <T>(TestCollection <T> collection, bool countCalled, bool containsCalled) { Assert.Equal(countCalled, collection.CountCalled); Assert.Equal(containsCalled, collection.ContainsCalled); Assert.False(collection.Enumerated); Assert.Equal(0, collection.IterationCount); collection.Reset(); Assert.False(collection.CountCalled); Assert.False(collection.ContainsCalled); } }
public void IsNullOrEmpty_ShouldEnumerateOnce () { // arrange var data = new TestEnumerable<string> (new[] { "a", "b", "c" }); // act var result = data.Enumerate ().IsNullOrEmpty (); // assert result.Should ().BeFalse (); data.EnumeratedCount.Should ().Be (1); }
public void FirstOrNull_ShouldEnumerateOnce () { // arrange var data = new TestEnumerable<int> (new[] { 1, 2, 3 }); // act var result = data.Enumerate ().FirstOrNull (); // assert result.Should ().Be (1); data.EnumeratedCount.Should ().Be (1); }
public void GetValueTest() { // Arrange. var typeConverter = new SafeTypeConverter(); var dataContext = Mock.Create<IDataContext>(Behavior.Loose); var editable1 = Mock.Create<IEditableRoot>(Behavior.Loose); var editable2 = Mock.Create<IEditableRoot>(Behavior.Loose); var testEnumerable = new TestEnumerable(new[] { editable1, editable2 }); var itemValue1 = new TestType(); var itemValue2 = new TestType(); MappingExpression valueExpression = c => { if (c == dataContext) return testEnumerable.Enumerate(); throw new InvalidOperationException(); }; var elementValueCalculator = Mock.Create<IValueCalculator>(Behavior.Loose); Mock.Arrange(() => elementValueCalculator.GetValue(dataContext)).Returns( () => { if (testEnumerable.Current == editable1) return itemValue1; if (testEnumerable.Current == editable2) return itemValue2; throw new InvalidOperationException(); }); var valueCalculator1 = new ArrayValueCalculator(typeof(TestType), valueExpression, elementValueCalculator, typeConverter); var valueCalculator2 = new ArrayValueCalculator(typeof(TestType), valueExpression, null, typeConverter); var valueCalculator3 = new ArrayValueCalculator(typeof(TestType), null, null, typeConverter); var valueCalculator4 = new ArrayValueCalculator(typeof(int), valueExpression, null, typeConverter); // Act. var value1 = valueCalculator1.GetValue(dataContext) as TestType[]; var value2 = valueCalculator2.GetValue(dataContext) as TestType[]; var value3 = valueCalculator3.GetValue(dataContext) as TestType[]; var value4 = valueCalculator4.GetValue(dataContext) as int[]; // Assert. Assert.IsNotNull(value1); Assert.AreEqual(2, value1.Length); Assert.AreSame(itemValue1, value1[0]); Assert.AreSame(itemValue2, value1[1]); Assert.IsNotNull(value2); Assert.AreEqual(2, value2.Length); Assert.IsNull(value2[0]); Assert.IsNull(value2[1]); Assert.IsNull(value3); Assert.IsNotNull(value4); Assert.AreEqual(2, value4.Length); Assert.AreEqual(0, value4[0]); Assert.AreEqual(0, value4[1]); }
public void ToArrayWorksFromSatarelleEnumerable() { var enumerable = new TestEnumerable(1, 3); Assert.AreEqual(enumerable.ToArray(), new[] { 1, 2, 3 }, "Result should be correct"); Assert.IsTrue(enumerable.EnumeratorDisposed, "Enumerator should be disposed"); }
public TestEnumerator(TestEnumerable <T> testTestEnumerable) { TestEnumerable = testTestEnumerable; }
public void TraverseBreadthFirstWithResultSelectorWorksForSaltarelleEnumerable() { var enumerable = new TestEnumerable(1, 1); Assert.AreEqual(enumerable.TraverseBreadthFirst(x => new[] { x + x }, x => x * x).Take(5).ToArray(), new[] { 1, 4, 16, 64, 256 }, "Result should be correct"); Assert.IsTrue(enumerable.EnumeratorDisposed, "Enumerator should be disposed"); }
public Enumerator(TestEnumerable testEnumerable) { _testEnumerable = testEnumerable; _index = 0; _current = 0; }
public void ForceWorksForSaltarelleEnumerable() { var enm = new TestEnumerable(1, 5); enm.Force(); Assert.AreEqual(enm.LastReturnedValue, 5); }
public void TakeWorksForSaltarelleEnumerable() { var enumerable = new TestEnumerable(1, 1000); Assert.AreEqual(enumerable.Take(3).ToArray(), new[] { 1, 2, 3 }, "Result should be correct"); Assert.IsTrue(enumerable.EnumeratorDisposed, "Enumerator should be disposed"); }
public void SelectWithIndexFromSaltarelleEnumerableWorks() { var enumerable = new TestEnumerable(1, 3); Assert.AreEqual(enumerable.Select((i, n) => i * i + n).ToArray(), new[] { 1, 5, 11 }, "Result should be correct"); Assert.IsTrue(enumerable.EnumeratorDisposed, "Enumerator should be disposed"); }