public static void Behavior <T>(T value, Expression <Func <T, IMaybe> > opExpression, AssertMaybeResult result) { Guard.NotNull(opExpression, "opExpression"); var op = opExpression.Compile(); var body = opExpression.Body; if (body.NodeType == ExpressionType.Convert) { body = ((UnaryExpression)body).Operand; } string expression = body.ToString(); EnsureBehavior(string.Format("{0} with {1}", expression, value != null ? value.ToString() : "{null}"), Maybe.Return(value), () => op(value), result); }
private static void EnsureBehavior <T>(string name, Maybe <T> value, Func <IMaybe> op, AssertMaybeResult expectedResult) { IMaybe result = null; try { result = op(); } catch (Exception) { if (expectedResult != AssertMaybeResult.ThrowExceptionImmediately) { Assert.Fail(string.Format("{0} threw an unexpected immediate exception.", name)); } return; } if (expectedResult == AssertMaybeResult.ThrowExceptionImmediately) { Assert.Fail(string.Format("{0} did not throw an exception immediately.", name)); } if (expectedResult == AssertMaybeResult.ThrowException) { try { var discard = result.HasValue; Assert.Fail(string.Format("{0} did not throw an exception upon evaluation.", name)); } catch (Exception) { return; } } if (expectedResult == AssertMaybeResult.NoValue && result.HasValue) { Assert.Fail(string.Format("{0} did not result in the absence of a value.", name)); } if (expectedResult == AssertMaybeResult.Value && !result.HasValue) { Assert.Fail(string.Format("{0} did not result in a value.", name)); } if (expectedResult == AssertMaybeResult.SameValue && (!result.HasValue || !EqualityComparer <T> .Default.Equals((T)result.Value, value.Value))) { Assert.Fail(string.Format("{0} did not result in the same value.", name)); } }
public static void Behavior <T>(T value, Expression <Func <Maybe <T>, IMaybe> > opExpression, AssertMaybeResult withValue, AssertMaybeResult withNoValue = AssertMaybeResult.NoValue) { Guard.NotNull(opExpression, "opExpression"); var op = opExpression.Compile(); var body = opExpression.Body; if (body.NodeType == ExpressionType.Convert) { body = ((UnaryExpression)body).Operand; } string expression = body.ToString(); var maybeWithValue = Maybe.Return(value); EnsureBehavior(string.Format("{0} with a value", expression), maybeWithValue, () => op(maybeWithValue), withValue); EnsureBehavior(string.Format("{0} with no value", expression), Maybe <T> .NoValue, () => op(Maybe <T> .NoValue), withNoValue); }