//Action<int> wrappedFunction -- this arg has been removed because I haven't figured out the generics to inline the assertions being tested internal static TypeValuePair GetUnionResult(this object item, UnionMethodInfo unionMethodData) { if (unionMethodData.IsSwitch) { TypeValuePair closureResult = null; void GetSwitch <TValue>(TValue value) => closureResult = new TypeValuePair(typeof(TValue), value); void GetSwitchOptional() => throw new Exception("Optional parameter cannot resolve a value"); var switchMainArgs = unionMethodData.CaseTypes.Select(x => MethodUtils.MakeAction(GetSwitch, x)); var switchFinalArgs = switchMainArgs.Concat( unionMethodData.OptionalLast ? new[] { MethodUtils.MakeAction(GetSwitchOptional) } : new object[0]) .ToArray(); unionMethodData.Method.Invoke(item, switchFinalArgs); return(closureResult); } TypeValuePair GetMatch <TValue>(TValue value) => new TypeValuePair(typeof(TValue), value); TypeValuePair GetMatchOptional() => new TypeValuePair(typeof(None), None.Value); var mainArgs = unionMethodData.CaseTypes.Select(x => MethodUtils.MakeMatchArg(GetMatch, x)); var finalArgs = mainArgs.Concat( unionMethodData.OptionalLast ? new[] { MethodUtils.MakeFunc(GetMatchOptional) } : new object[0]) .ToArray(); return((TypeValuePair)unionMethodData.Method .MakeGenericMethod(typeof(TypeValuePair)) .Invoke(item, finalArgs)); }
internal static TResult CheckItemHelper <TResult>(AssertionScope scope, TypeValuePair typeValuePair, UnionMethodInfo methodInfo) { var itemType = typeValuePair.Type; var expectedType = typeof(TResult); var expectedTypePretty = expectedType.PrettyPrint(); var givenTypes = methodInfo.OptionalLast ? methodInfo.CaseTypes.Concat(new[] { typeof(None) }).ToArray() : methodInfo.CaseTypes; var givenTypesPretty = string.Join(", ", givenTypes.Select(ReflectionUtils.PrettyPrint)); scope .ForCondition(givenTypes.Contains(expectedType)) .FailWith("Value should be one of {0} but found {1} instead.", givenTypesPretty, expectedTypePretty); scope .ForCondition(expectedType.IsAssignableFrom(itemType)) .FailWith("Value should be assignable to {0} but found {1} instead", expectedTypePretty, itemType.PrettyPrint()); return((TResult)typeValuePair.Value); }