public void Works_Like_Extension_Methods_Values_And_Nones() { var maybes = new List <IMaybe <int> > { AssertionUtilities.DivisionMaybe(4, 2), AssertionUtilities.DivisionMaybe(3, 0), AssertionUtilities.DivisionMaybe(3, 3), AssertionUtilities.DivisionMaybe(4, 0), AssertionUtilities.DivisionMaybe(5, 0), AssertionUtilities.DivisionMaybe(6, 0), AssertionUtilities.DivisionMaybe(7, 0), AssertionUtilities.DivisionMaybe(8, 0), AssertionUtilities.DivisionMaybe(10, 2) }; var matches = maybes .Match(i => i, () => - 1) .ToArray(); Assert.Equal(maybes.Values(), matches.Where(x => x != -1)); Assert.Equal(maybes.Nones(() => - 1), matches.Where(x => x == -1)); }
public static void AssertNotDisposedOrDisposingTest() { var state = new VolatileState(); Assert.That(() => AssertionUtilities.AssertNotDisposedOrDisposing(state), Throws.Nothing ); _ = state.BeginDispose(); Assert.That(() => AssertionUtilities.AssertNotDisposedOrDisposing(state), Configuration.AssertionsEnabled ? Throws.Exception : Throws.Nothing ); state.EndDispose(); Assert.That(() => AssertionUtilities.AssertNotDisposedOrDisposing(state), Configuration.AssertionsEnabled ? Throws.Exception : Throws.Nothing ); }
public async Task Result_With_Error_Flatmaps_Result_with_Error__Expects_Result_With_Error() { var flatSelectorExecuted = false; var errorSelectorExecuted = false; await AssertionUtilities .DivisionAsync(2, 0) .FlatMap(x => { flatSelectorExecuted = true; return(AssertionUtilities.Division(x, 0)); }, s => { errorSelectorExecuted = true; return(s); }) .AssertError("Can not divide '2' with '0'."); Assert.False(errorSelectorExecuted, "Errorselector should not get exeuted since there is an error in the source."); Assert.False(flatSelectorExecuted, "The flatmap selector should not get exectued if the source Result<T, TError> contains error."); }
public async Task Result_With_Error_FlatmapsRS_Result_with_Error__Expects_Result_With_Error() { var flatSelectorExecuted = false; var resultSelectorExectued = false; await AssertionUtilities .DivisionAsync(2, 0) .FlatMapAsync(x => { flatSelectorExecuted = true; return(AssertionUtilities.DivisionAsync(x, 0)); }, (y, x) => { resultSelectorExectued = true; return(y + x); }) .AssertError("Can not divide '2' with '0'."); Assert.False(flatSelectorExecuted, "The flatmapSelector should not get exectued if the source Result<T, TError> contains error."); Assert.False(resultSelectorExectued, "The resultSelector should not get exectued if the source Result<T, TError> contains error."); }
public void Passing_Null_Selector_When_Getting_Errors_From_Maybe_Enumerable() { var maybes = new List <IMaybe <int> > { AssertionUtilities.DivisionMaybe(4, 2), AssertionUtilities.DivisionMaybe(3, 0), AssertionUtilities.DivisionMaybe(3, 3), AssertionUtilities.DivisionMaybe(4, 0), AssertionUtilities.DivisionMaybe(5, 0), AssertionUtilities.DivisionMaybe(6, 0), AssertionUtilities.DivisionMaybe(7, 0), AssertionUtilities.DivisionMaybe(8, 0), AssertionUtilities.DivisionMaybe(10, 2) }; Func <int> selector = null; Assert.Throws <ArgumentNullException>( AssertionUtilities.SelectorName, () => maybes.Nones(selector) ); }
public async Task Result_With_Value_FlatmapsRS_Result_with_Error__Expects_Result_With_Error() { var flatSelectorExecuted = false; var errorSelectorExecuted = false; await AssertionUtilities .DivisionAsync(2, 2) .Flatten(x => { flatSelectorExecuted = true; return(AssertionUtilities.Division(x, 0)); }, s => { errorSelectorExecuted = true; return(s); }) .AssertError("Can not divide '1' with '0'."); Assert.True(errorSelectorExecuted, "Errorselector should get exeuted since the errror came from the result given to the flatselector."); Assert.True(flatSelectorExecuted, "The flatselector should not get executed since flatselector result failed."); }
Result_With_Value_With_Truthy_Predicate__Expects_Predicate_To_Be_Executed_And_ErrorSelector_To_Never_Be_Invoked_With_Parameter_ErrorSelector() { var predicateExectued = false; var errorSelectorExectued = false; await AssertionUtilities .DivisionAsync(10, 2) .Filter(d => { predicateExectued = true; return(true); }, x => { errorSelectorExectued = true; return("This should never happen."); }) .AssertValue(5); Assert.True(predicateExectued, "Should get exectued since there's a value from the result."); Assert.False(errorSelectorExectued, "Should not get exectued since the predicate was falsy."); }
private static int FindMinElementIndex <T>(T[] arr, int startIndex, int endIndex) where T : IComparable <T> { Debug.Assert(0 <= startIndex && startIndex < arr.Length, "Starting index was out of the array boundaries."); Debug.Assert(0 <= endIndex && endIndex < arr.Length, "End index was out of the array boundaries."); Debug.Assert(startIndex <= endIndex, "Starting index was larger than end index"); int minElementIndex = startIndex; for (int i = startIndex + 1; i <= endIndex; i++) { if (arr[i].CompareTo(arr[minElementIndex]) < 0) { minElementIndex = i; } } Debug.Assert(AssertionUtilities.IsMinElementIn(arr[minElementIndex], arr, startIndex, endIndex), "The result of find minimal element index was not the minimal element index"); return(minElementIndex); }
Result_With_Error__Expects_Predicate_Never_To_Be_Executed_And_ErrorSelector_Never_To_Be_Invoked() { var predicateExectued = false; var errorSelectorExectued = false; AssertionUtilities .Division(10, 0) .IsErrorWhen(d => { predicateExectued = true; return(d == 2); }, x => { errorSelectorExectued = true; return("Bad"); }) .AssertError("Can not divide '10' with '0'."); Assert.False(predicateExectued, "Should not get exectued since there's an error before the predicate was applied."); Assert.False(errorSelectorExectued, "Should not get exectued since there's an error before the predicate was applied."); }
Result_With_Value_With_Falsy_Predicate__Expects_Predicate_To_Be_Executed_And_ErrorSelector_To_Never_Be_Invoked() { var predicateExectued = false; var errorSelectorExectued = false; AssertionUtilities .Division(10, 2) .IsErrorWhen(d => { predicateExectued = true; return(false); }, x => { errorSelectorExectued = true; return("Bad"); }) .AssertValue(5); Assert.True(predicateExectued, "Should get exectued since there's a value from the result."); Assert.False(errorSelectorExectued, "Should not get exectued since the predicate was falsy."); }
public void Result_With_Value_FlatmapsRS_Result_with_Value__Expects_Result_With_Value() { var flatSelectorExecuted = false; var resultSelectorExectued = false; var errorSelectorExecuted = false; AssertionUtilities.Division(2, 2).FlatMap(x => { flatSelectorExecuted = true; return(AssertionUtilities.Division(x, 2)); }, (y, x) => { resultSelectorExectued = true; return(y + x); }, s => { errorSelectorExecuted = true; return(s); }).AssertValue(1.5d); Assert.True(flatSelectorExecuted, "Flatmapselecotr should get executed."); Assert.True(resultSelectorExectued, "ResultSelector should get executed since both source and the result from flatmapselector contains values."); Assert.False(errorSelectorExecuted, "Erroselector should not get executed since both source and the result from flatmapselector contains values."); }
public void Result_With_Value_With_Selector() => Assert.Equal(10, AssertionUtilities.Division(10, 2).MapError(x => - 1d).Match(d => d * 2));
public void Result_With_Error() => Assert.Equal(-2, AssertionUtilities.Division(10, 0).MapError(x => - 1d).Match(d => d * 2));
public void Passing_Null_Action_Throws() => Assert.Throws <ArgumentNullException>( AssertionUtilities.ActionParamName, () => AssertionUtilities.Division(10, 0).DoAsync(null) );
public void Passing_Null_ErrorSelector() => Assert.Throws <ArgumentNullException>( AssertionUtilities.ErrorSelectorName, () => AssertionUtilities.DivisionAsync(20, 2) .FlattenAsync(d => AssertionUtilities.DivisionAsync(d, 2), null) );
public async System.Threading.Tasks.Task Result_With_Error__Expects_AsyncResult_With_Error() => await AssertionUtilities.Division(10, 0).ToAsyncResult().AssertError("Can not divide '10' with '0'.");
public void Passing_Null_Selector_Throws() => Assert.Throws <ArgumentNullException>( AssertionUtilities.SelectorName, () => AssertionUtilities.Division(10, 2).FullMap <string, string>(null, s => s) );
public void Result_With_Value() => AssertionUtilities.Division(10, 2).Swap().AssertError(5);
public async System.Threading.Tasks.Task Result_With_Error__Expects_Enumerable_With_No_Element() { var result = (await AssertionUtilities.DivisionAsync(20, 0).ToEnumerable()).ToArray(); Assert.Empty(result); }
public void Hander_WhenCounterDoesNotExistInContext_ThrowsCounterNotFoundExcpetion() => AssertionUtilities.AssertAsyncMethodThrows <CounterNotFoundExcpetion>(_handler.Handle(new GetButtonClickQuery()));
public async Task Result_With_Value_Using_Invalid_Cast_Expects_Cast_Exception() { const int identity = 0; await Assert.ThrowsAsync <InvalidCastException>(async() => await AssertionUtilities.GetGenderAsync(identity).Cast <string>()); }
public void Passing_Null_Selector_With_ResultSelector_Overload_Throws() => Assert.Throws <ArgumentNullException>( AssertionUtilities.SelectorName, () => AssertionUtilities.DivisionAsync(2, 0).FlatMap <double, double>(null, (d, d1) => d + d1));
public void Result_With_Error() => AssertionUtilities.Division(10, 0).Swap().AssertValue("Can not divide '10' with '0'.");
public void Passing_Null_Selector_With_Throws() => Assert.Throws <ArgumentNullException>( AssertionUtilities.SelectorName, () => { AssertionUtilities.DivisionAsync(2, 0).FlatMap <double>(null); });
private static double Division(double left, double second) => AssertionUtilities.Division(left, second).MapError(x => - 1d).Match();
public void Passing_Null_ResultSelector__Throws() => Assert.Throws <ArgumentNullException>( AssertionUtilities.ResultSelector, () => AssertionUtilities.DivisionAsync(2, 0) .FlatMap <double, double>(d => AssertionUtilities.Division(d, 2), null) );
public void Passing_Null_ErrorSelector_Throws() => Assert.Throws <ArgumentNullException>( AssertionUtilities.ErrorSelectorName, () => AssertionUtilities.Division(10, 2).FullMap <string, string>(_ => string.Empty, null) );
public void Passing_Null_Selector_Throws() => Assert.Throws <ArgumentNullException>( AssertionUtilities.SelectorName, () => AssertionUtilities.DivisionAsync(20, 2).FlattenAsync <string>(null) );
public async System.Threading.Tasks.Task Result_With_Value__Expects_AsyncResult_With_Value() => await AssertionUtilities.Division(10, 5).ToAsyncResult().AssertValue(2);
public void Passing_Null_Selector_With_ErrorSelector_Overload_Throws() => Assert.Throws <ArgumentNullException>( AssertionUtilities.SelectorName, () => AssertionUtilities.DivisionAsync(20, 2).FlattenAsync <string, int>(null, i => $"{i}") );