/// <summary> /// Tests if an HttpResponseMessage contains the Set-Cookie /// header that would set the cookie with the provided name /// </summary> /// <param name="have"></param> /// <param name="name"></param> /// <param name="customMessageGenerator"></param> /// <returns>Continuation to further test the cookie, if found</returns> public static IMore <Cookie> Cookie( this IHave <HttpResponseMessage> have, string name, Func <string> customMessageGenerator ) { Cookie resolvedCookie = null; have.AddMatcher(actual => { var cookies = actual.Headers.Where( h => h.Key == "Set-Cookie" ).Select(h => h.Value.Select(ParseCookieHeader)) .SelectMany(o => o) .ToArray(); resolvedCookie = cookies.FirstOrDefault(c => c.Name.Equals(name)); var passed = resolvedCookie != null; return(new MatcherResult( passed, FinalMessageFor( () => $"Expected {passed.AsNot()}to find set-cookie for '{name}'", customMessageGenerator ) )); }); return(new Next <Cookie>( () => resolvedCookie, have as IExpectationContext )); }
/// <summary> /// Tests if an HttpResponseMessage contains the Set-Cookie /// header that would set the cookie with the provided name /// </summary> /// <param name="have"></param> /// <param name="name"></param> /// <returns>Continuation to further test the cookie, if found</returns> public static IMore <Cookie> Cookie( this IHave <HttpResponseMessage> have, string name ) { return(have.Cookie(name, NULL_STRING)); }
/// <summary> /// Quick-n-dirty assertion that a controller's method has the required route /// </summary> /// <param name="have"></param> /// <param name="member"></param> /// <param name="expected"></param> /// <returns></returns> public static IMore <Type> Route( this IHave <Type> have, string member, string expected) { have.AddMatcher( actual => { var method = actual.GetMethod(member); if (method == null) { return(new MatcherResult( false, () => $"Expected {false.AsNot()}to find method {actual}.{method}" )); } var attribs = method.GetCustomAttributes(false).OfType <RouteAttribute>(); var passed = attribs.Any(a => a.Template == expected); return(new MatcherResult( passed, () => $"Expected {actual}.{method} {passed.AsNot()}to have route '{expected}'" )); }); return(have.More()); }
/// <summary> /// Tests the string's length against the expected value /// </summary> /// <param name="have">Continuation to operate on</param> /// <param name="expected">Expected string length</param> /// <param name="customMessage">Custom message generator</param> /// <returns>More continuation -- continue with more assertions!</returns> public static IStringMore Length( this IHave <string> have, int expected, string customMessage) { return(have.Length(expected, () => customMessage)); }
/// <summary> /// Tests if the provided type is decorated with TAttribute /// </summary> /// <param name="have"></param> /// <param name="customMessage"></param> /// <typeparam name="TAttribute"></typeparam> /// <returns></returns> public static IMore <Type> Attribute <TAttribute>( this IHave <Type> have, string customMessage ) where TAttribute : Attribute { return(have.Attribute <TAttribute>(() => customMessage)); }
/// <summary> /// Tests if an object has a given property, may continue on to test /// the property value /// </summary> /// <param name="have"></param> /// <param name="property"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IMore <T> Property <T>( this IHave <T> have, string property ) { return(have.Property(property, NULL_STRING)); }
/// <summary> /// Tests if an object has a given property, may continue on to test /// the property value /// </summary> /// <param name="have"></param> /// <param name="property"></param> /// <param name="customMessage"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IMore <T> Property <T>( this IHave <T> have, string property, string customMessage ) { return(have.Property(property, () => customMessage)); }
/// <summary> /// Tests if an object has a given property, may continue on to test /// the property value /// </summary> /// <param name="have"></param> /// <param name="property"></param> /// <param name="customMessageGenerator"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> /// <exception cref="InvalidOperationException"></exception> public static IMore <T> Property <T>( this IHave <T> have, string property, Func <string> customMessageGenerator ) { return((have as ICanAddMatcher <T>).Property(property, customMessageGenerator)); }
/// <summary> /// Tests if an HttpResponseMessage contains the Set-Cookie /// header that would set the cookie with the provided name /// </summary> /// <param name="have"></param> /// <param name="name"></param> /// <param name="customMessage"></param> /// <returns>Continuation to further test the cookie, if found</returns> public static IMore <Cookie> Cookie( this IHave <HttpResponseMessage> have, string name, string customMessage ) { return(have.Cookie(name, () => customMessage)); }
/// <summary> /// Tests if an object or Type has a method with the provided name /// </summary> /// <param name="have"></param> /// <param name="methodName"></param> /// <param name="customMessageGenerator"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IMore <MethodInfo> Method <T>( this IHave <T> have, string methodName, Func <string> customMessageGenerator ) { return(have.Method <T>(methodName, _ => true, customMessageGenerator)); }
internal AndSupportingExtension( IHave <Type> continuation, string member, SupportingExtension supportingExtension) { _continuation = continuation; _member = member; With = supportingExtension; }
/// <summary> /// Returns NSubstitute Received() /// </summary> /// <param name="have"></param> /// <typeparam name="T"></typeparam> public static T Received <T>(this IHave <T> have) where T : class { var actual = have.GetActual(); var context = actual.GetMetadata <IExpectationContext>(Expectations.METADATA_KEY); return(context.IsNegated() ? SubstituteExtensions.DidNotReceive(actual) : SubstituteExtensions.Received(actual)); }
private void InstansiateAchievment(IHave <AchievmentCode> code, IHave <Sprite> sprite) { var achievment = Instantiate(achievmentPrefab, parent); var achievmentData = DB.Data.Find(code.Item); achievment.name = achievmentData.Name; achievment.SetAchievmentData(achievmentData); achievment.AchievmentIcon = sprite.Item; }
/// <summary> /// Tests if an object or Type has a method with the provided name /// </summary> /// <param name="have"></param> /// <param name="methodName"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IMore <MethodInfo> Method <T>( this IHave <T> have, string methodName ) { return(have.Method( methodName, NULL_STRING )); }
public static IMore <T> Color <T>( this IHave <T> have, Colors color ) where T : Animal { have.Compose(animal => { Expect(animal.Colors).To.Contain.Exactly(1).Equal.To(color); }); return(have.More()); }
/// <summary> /// Tests if an object or Type has a method with the provided name /// </summary> /// <param name="have"></param> /// <param name="methodName"></param> /// <param name="customMessage"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IMore <MethodInfo> Method <T>( this IHave <T> have, string methodName, string customMessage ) { return(have.Method( methodName, () => customMessage )); }
public static void Data( this IHave <string> have, byte[] data) { have.Compose(actual => { Expect(actual).To.Exist(); var contents = File.ReadAllBytes(actual); Expect(contents) .To.Equal(data, "Data mismatch"); }); }
/// <summary> /// Returns NSubstitute Received(count) /// </summary> /// <param name="have"></param> /// <param name="count"></param> /// <typeparam name="T"></typeparam> public static T Received <T>(this IHave <T> have, int count) where T : class { var actual = have.GetActual(); var context = actual.GetMetadata <IExpectationContext>(Expectations.METADATA_KEY); if (context.IsNegated()) { throw new NotSupportedException($"Negation of numbered Receive(N) expectations is not supported! (What would it mean, anyway?)"); } return(SubstituteExtensions.Received(have.GetActual(), count)); }
/// <summary> /// Tests for a method by name and discriminator /// - use if you're trying to discern between overloads /// </summary> /// <param name="have"></param> /// <param name="methodName"></param> /// <param name="discriminator"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IMore <MethodInfo> Method <T>( this IHave <T> have, string methodName, Func <MethodInfo, bool> discriminator ) { return(have.Method( methodName, discriminator, NULL_STRING )); }
public static void Child( this IHave <Container> contain, Sub sub) { contain.AddMatcher(actual => { var passed = (actual?.Subs ?? new Sub[0]) .Any(c => c.Name == sub.Name); return(new MatcherResult( passed, $"Expected {actual.Stringify()} {passed.AsNot()}to contain sub {sub.Name}")); }); }
/// <summary> /// Tests for a method by name and discriminator /// - use if you're trying to discern between overloads /// </summary> /// <param name="have"></param> /// <param name="methodName"></param> /// <param name="discriminator"></param> /// <param name="customMessage"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IMore <MethodInfo> Method <T>( this IHave <T> have, string methodName, Func <MethodInfo, bool> discriminator, string customMessage ) { return(have.Method( methodName, discriminator, () => customMessage )); }
/// <summary> /// Asserts that the controller has the expected base route /// </summary> /// <param name="have"></param> /// <param name="expected"></param> /// <returns></returns> public static IMore <Type> Route( this IHave <Type> have, string expected) { have.AddMatcher( actual => { var attribs = actual.GetCustomAttributes(false).OfType <RouteAttribute>(); var passed = attribs.Any(a => a.Template == expected); return(new MatcherResult( passed, () => $"Expected {actual.Name} {passed.AsNot()}to have route '{expected}'" )); }); return(have.More()); }
/// <summary> /// Tests the string's length against the expected value /// </summary> /// <param name="have">Continuation to operate on</param> /// <param name="expected">Expected string length</param> /// <param name="customMessageGenerator">Custom message generator</param> /// <returns>More continuation -- continue with more assertions!</returns> public static IStringMore Length( this IHave <string> have, int expected, Func <string> customMessageGenerator) { have.AddMatcher(actual => { var passed = actual != null && actual.Length == expected; return(new MatcherResult( passed, FinalMessageFor( () => $"Expected {actual.Stringify()} {passed.AsNot()}to have length {expected}", customMessageGenerator) )); }); return(have.More()); }
/// <summary> /// Asserts that the controller has the named method /// </summary> /// <param name="have"></param> /// <param name="method"></param> /// <returns></returns> public static SupportingExtension Method( this IHave <Type> have, string method ) { var result = new SupportingExtension(); have.AddMatcher( actual => { result.Member = method; result.Continuation = have; var passed = actual.GetMethod(method) != null; return(new MatcherResult( passed, () => $"Expected type {actual} to have method {method}")); }); return(result); }
public static WithType Property( this IHave <Type> have, string name) { var result = new WithType(); have.AddMatcher(actual => { result.ParentType = actual; result.PropertyInfo = actual.GetProperties( BindingFlags.Public | BindingFlags.Instance ) .FirstOrDefault(pi => pi.Name == name); var passed = result.PropertyInfo != null; return(new MatcherResult( passed, () => $"Expected {actual} {passed.AsNot()}to have property '{name}'" )); }); return(result); }
/// <summary> /// Tests if the provided type is decorated with TAttribute /// </summary> /// <param name="have"></param> /// <param name="customMessageGenerator"></param> /// <typeparam name="TAttribute"></typeparam> /// <returns></returns> public static IMore <Type> Attribute <TAttribute>( this IHave <Type> have, Func <string> customMessageGenerator ) where TAttribute : Attribute { return(have.AddMatcher(actual => { var attributes = actual?.GetCustomAttributes() .OfType <TAttribute>() .ToArray() ?? new TAttribute[0]; actual.SetMetadata(METADATA_KEY_TYPE_ATTRIBUTES, attributes); var passed = attributes.Any(); return new MatcherResult( passed, FinalMessageFor( () => $"Expected '{actual}' to be decorated with {Attrib<TAttribute>()}", customMessageGenerator ) ); })); }
public static void Data( this IHave <IReadWriteFileResource> have, byte[] expected) { have.AddMatcher(actual => { if (!File.Exists(actual.Path)) { return(new MatcherResult( false, () => $"Expected {false.AsNot()}to find file at: {actual.Path}")); } using (var stream = actual.OpenForRead()) { var data = stream.ReadAllBytes(); var passed = expected.Length == data.Length && data.DeepEquals(expected); return(new MatcherResult( passed, () => { var actualHash = data.ToMD5String(); var expectedHash = expected.ToMD5String(); return $@"Expected { passed.AsNot() } to find data with hash/length { expectedHash }{ expected.Length }, but got { actualHash }/{ data.Length }"; })); } }); }
/// <summary> /// Tests for a method by name and discriminator /// - use if you're trying to discern between overloads /// </summary> /// <param name="have"></param> /// <param name="methodName"></param> /// <param name="discriminator"></param> /// <param name="customMessageGenerator"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IMore <MethodInfo> Method <T>( this IHave <T> have, string methodName, Func <MethodInfo, bool> discriminator, Func <string> customMessageGenerator ) { return(have.AddMatcher(actual => { if (actual is null) { return Fail("actual is null"); } var type = actual as Type ?? typeof(T); var matchesName = type.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static ) .Where(mi => mi.Name == methodName) .ToArray(); var allDiscriminatorMatches = matchesName .Where( mi => mi.Name == methodName && discriminator(mi) ).ToArray(); var methodInfo = allDiscriminatorMatches.FirstOrDefault(); methodInfo.DeleteMetadata(); var passed = methodInfo is not null && allDiscriminatorMatches.Length == 1; return new MatcherResultWithNext <MethodInfo>( passed, () => { var single = matchesName.Length == 1; var moreInfo = matchesName.Any() ? $" (there {(single ? "was" : "were")} {matchesName.Length} match{(single ? "" : "es")} by name '{methodName}')" : ""; if (matchesName.Any() && allDiscriminatorMatches.Length > 1) { moreInfo = moreInfo.RegexReplace("\\)$", " - there were multiple matches for your provided discriminator)"); } return $@"Expected { type.PrettyName() } to have a single matching method for name '{ methodName }' and provided discriminator{ moreInfo }"; }, customMessageGenerator, () => methodInfo ); })); IMatcherResultWithNext <MethodInfo> Fail(string message) { return(new MatcherResultWithNext <MethodInfo>( false, () => message, customMessageGenerator, () => default )); } }
/// <summary> /// Tests if the provided type is decorated with TAttribute /// </summary> /// <param name="have"></param> /// <typeparam name="TAttribute"></typeparam> /// <returns></returns> public static IMore <Type> Attribute <TAttribute>( this IHave <Type> have ) where TAttribute : Attribute { return(have.Attribute <TAttribute>(NULL_STRING)); }
/// <summary> /// Tests the string's length against the expected value /// </summary> /// <param name="have">Continuation to operate on</param> /// <param name="expected">Expected string length</param> /// <returns>More continuation -- continue with more assertions!</returns> public static IStringMore Length( this IHave <string> have, int expected) { return(have.Length(expected, NULL_STRING)); }