/// <summary> /// Checks that the execution time is below a specified threshold. /// </summary> /// <typeparam name="T">Type of the checked type.</typeparam> /// <param name="check">The fluent check to be extended. /// </param> /// <param name="threshold"> /// The threshold. /// </param> /// <param name="timeUnit"> /// The time unit of the given threshold. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// Execution was strictly above limit. /// </exception> public static ICheckLink <ICodeCheck <T> > LastsLessThan <T>( this ICodeCheck <T> check, double threshold, TimeUnit timeUnit) where T : RunTrace { var checker = ExtensibilityHelper.ExtractCodeChecker(check); var comparand = new Duration(checker.Value.ExecutionTime, timeUnit); var durationThreshold = new Duration(threshold, timeUnit); checker.ExecuteCheck( () => { if (comparand > durationThreshold) { var message = checker.BuildMessage( "The checked code took too much time to execute.") .For(LabelForExecTime) .Expected(durationThreshold) .Comparison(LabelForLessThan) .ToString(); throw new FluentCheckException(message); } }, checker.BuildMessage("The checked code took too little time to execute.").For(LabelForExecTime).Expected(durationThreshold).Comparison(LabelForMoreThan).ToString()); return(new CheckLink <ICodeCheck <T> >(check)); }
/// <summary> /// Checks that the CPU time is below a specified threshold. /// </summary> /// <typeparam name="T">Type of the checked type.</typeparam> /// <param name="check">The fluent check to be extended. /// </param> /// <param name="threshold"> /// The threshold. /// </param> /// <param name="timeUnit"> /// The time unit of the given threshold. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// Execution was strictly above limit. /// </exception> public static ICheckLink <ICodeCheck <T> > ConsumesLessThan <T>( this ICodeCheck <T> check, double threshold, TimeUnit timeUnit) where T : RunTrace { var checker = ExtensibilityHelper.ExtractCodeChecker(check); var comparand = new Duration(checker.Value.TotalProcessorTime, timeUnit); var durationThreshold = new Duration(threshold, timeUnit); checker.ExecuteCheck( () => { if (comparand > durationThreshold) { var message = FluentMessage.BuildMessage( "The checked code consumed too much CPU time.") .For("cpu time") .On(comparand) .And.Expected(durationThreshold) .Comparison("less than") .ToString(); throw new FluentCheckException(message); } }, FluentMessage.BuildMessage("The checked code took too little cpu time to execute.").For("cpu time").On(comparand).And.Expected(durationThreshold).Comparison("more than").ToString()); return(new CheckLink <ICodeCheck <T> >(check)); }
/// <summary> /// Builds a check /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TV"></typeparam> /// <param name="check"></param> /// <param name="extractor"></param> /// <returns></returns> public static ICheck <TV> BuildCheck <T, TV>(ICodeCheck <T> check, Func <T, TV> extractor) where T : RunTrace { var extract = (FluentSut <T>)check; var value = extractor(ExtractCodeChecker(check).Value); return(!string.IsNullOrEmpty(extract.CustomMessage) ? Check.WithCustomMessage(extract.CustomMessage).That(value) : Check.That(value)); }
/// <summary> /// Checks that the code did throw an exception of a specified type. /// </summary> /// <typeparam name="T">Expected exception type.</typeparam> /// <param name="check">The fluent check to be extended. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException">The code did not raised an exception of the specified type, or did not raised an exception at all.</exception> public static ILambdaExceptionCheck <T> Throws <T>(this ICodeCheck <RunTrace> check) where T : Exception { var exc = CheckExceptionType(check, typeof(T)); return(new LambdaExceptionCheck <T>((T)exc, ((INegated)check).Negated)); }
/// <summary> /// Verify that the code results in a failed NFluent check with a specified message. /// </summary> /// <param name="check"></param> /// <param name="lines"></param> /// <returns>A link check</returns> public static ICheckLink <ICodeCheck <RunTrace> > IsAFailingCheckWithMessage(this ICodeCheck <RunTrace> check, params string[] lines) { ExtensibilityHelper.BeginCheck(check) .SetSutName("fluent check") .CheckSutAttributes((sut) => sut.RaisedException, "raised exception") .FailIfNull("The check succeeded whereas it should have failed.") .FailWhen((sut) => !ExceptionHelper.IsFailedException(sut), $"The exception raised is not of the expected type.") .CheckSutAttributes((sut) => sut.Message.SplitAsLines(), "error message") .Analyze((messageLines, test) => { var expectedLines = (lines.Length == 1) ? lines[0].SplitAsLines() : lines; for (var i = 0; i < expectedLines.Count; i++) { if (expectedLines[i] == "*") { //any line continue; } if (messageLines.Count <= i) { test.Fail($"Lines are missing in the error message starting at #{i}"); break; } if (expectedLines[i].StartsWith("#")) { if (!Regex.IsMatch(messageLines[i], expectedLines[i].Substring(1))) { test.Fail($"Line {i} is different from what is expected" + Environment.NewLine + "Act:" + messageLines[i].DoubleCurlyBraces() + Environment.NewLine + "Exp (regex):" + expectedLines[i].DoubleCurlyBraces() ); break; } } else if (messageLines[i] != expectedLines[i]) { test.Fail($"Line {i} is different from what is expected" + Environment.NewLine + "Act:" + messageLines[i].DoubleCurlyBraces() + Environment.NewLine + "Exp:" + expectedLines[i].DoubleCurlyBraces()); break; } } if (messageLines.Count > expectedLines.Count) { test.Fail($"Too many lines in the error message starting at #{expectedLines.Count}"); } }). DefineExpectedValue(lines). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the code did throw an exception of a specified type. /// </summary> /// <param name="check">The fluent check to be extended.</param> /// <param name="exceptionType">Expected exception type.</param> /// <returns>A check link.</returns> public static ILambdaExceptionCheck <Exception> ThrowsType(this ICodeCheck <RunTrace> check, Type exceptionType) { CheckExceptionType(check, exceptionType); var checker = ExtensibilityHelper.ExtractCodeChecker(check); return(checker.Negated ? (ILambdaExceptionCheck <Exception>) new NegatedLambdaExceptionCheck <Exception>() : new LambdaExceptionCheck <Exception>(checker.Value.RaisedException)); }
private static void CheckExceptionType(ICodeCheck <RunTrace> check, Type expecting) { ExtensibilityHelper.BeginCheck(check).SutNameIs("code") .GetSutProperty((sut) => sut.RaisedException, "raised exception") .ExpectingType(expecting, expectedLabel: "", negatedLabel: "should not be") .FailsIfNull("The checked code did not raise an exception, whereas it must.") .FailsIf((sut) => !expecting.IsInstanceOfType(sut), "The {0} is of a different type than expected.") .Negates("The {0} raised an exception of the forbidden type.") .EndCheck(); }
/// <summary> /// Checks that the code did throw an exception of a specified type. /// </summary> /// <typeparam name="T">Expected exception type.</typeparam> /// <param name="check">The fluent check to be extended. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException">The code did not raised an exception of the specified type, or did not raised an exception at all.</exception> public static ILambdaExceptionCheck <T> Throws <T>(this ICodeCheck <RunTrace> check) where T : Exception { CheckExceptionType(check, typeof(T)); var checker = ExtensibilityHelper.ExtractCodeChecker(check); return(checker.Negated ? (ILambdaExceptionCheck <T>) new NegatedLambdaExceptionCheck <T>() : new LambdaExceptionCheck <T>((T)checker.Value.RaisedException)); }
/// <summary> /// Check that the code does not throw an exception. /// </summary> /// <param name="check">The fluent check to be extended. /// </param> /// <typeparam name="T">Inferred type of the code.</typeparam> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException">The code raised an exception.</exception> public static ICheckLink <ICodeCheck <T> > DoesNotThrow <T>(this ICodeCheck <T> check) where T : RunTrace { ExtensibilityHelper.BeginCheck(check). SetSutName("code"). CheckSutAttributes(sut => sut.RaisedException, "raised exception"). FailWhen(sut => sut != null, "The checked code raised an exception, whereas it must not."). OnNegate("The checked code did not raise an exception, whereas it must.", MessageOption.NoCheckedBlock). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Verify that the code results in a failed NFluent check with a specified message. /// </summary> /// <param name="check"></param> /// <returns>A link check</returns> public static ICheckLink <ICodeCheck <RunTrace> > IsAFaillingCheck(this ICodeCheck <RunTrace> check) { ExtensibilityHelper.BeginCheck(check) .GetSutProperty((sut) => sut.RaisedException, "raised exception") .SutNameIs("fluent check") .FailsIfNull() .FailsIf((sut) => !ExceptionHelper.IsFailedException(sut), "The exception raised is not of the expected type"). ExpectingType(ExceptionHelper.BuildException(string.Empty).GetType(), "an instance of:", "an instance of a different type"). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the code did throw an exception of a specified type. /// </summary> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// The code did not raised an exception of the specified type, or did not raised an exception at all. /// </exception> public static ILambdaExceptionCheck <Exception> ThrowsAny(this ICodeCheck <RunTrace> check) { ExtensibilityHelper.BeginCheck(check) .OnNegate("The checked code raised an exception, whereas it must not.") .SetSutName("code") .CheckSutAttributes((sut) => sut.RaisedException, "raised exception") .FailIfNull("The checked code did not raise an exception, whereas it must.") .EndCheck(); var checker = ExtensibilityHelper.ExtractCodeChecker(check); return(new LambdaExceptionCheck <Exception>(checker.Value.RaisedException, ((INegated)check).Negated)); }
/// <summary> /// Verify that the code results in a failed NFluent check with a specified message. /// </summary> /// <param name="check"></param> /// <returns>A link check</returns> public static ICheckLink <ICodeCheck <RunTrace> > IsAFaillingCheck(this ICodeCheck <RunTrace> check) { ExtensibilityHelper.BeginCheck(check) .SetSutName("fluent check") .CheckSutAttributes((sut) => sut.RaisedException, "raised exception") .FailIfNull("The fluent check did not raise an exception, where as it must.") .FailWhen((sut) => !ExceptionHelper.IsFailedException(sut), "The exception raised is not of the expected type"). DefineExpectedType(ExceptionHelper.BuildException(string.Empty).GetType()). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the execution time is below a specified threshold. /// </summary> /// <typeparam name="T">Type of the checked type.</typeparam> /// <param name="check">The fluent check to be extended. /// </param> /// <param name="threshold"> /// The threshold. /// </param> /// <param name="timeUnit"> /// The time unit of the given threshold. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// Execution was strictly above limit. /// </exception> public static ICheckLink <ICodeCheck <T> > LastsLessThan <T>( this ICodeCheck <T> check, double threshold, TimeUnit timeUnit) where T : RunTrace { var durationThreshold = new Duration(threshold, timeUnit); ExtensibilityHelper.BeginCheck(check).FailsIf((sut) => new Duration(sut.ExecutionTime, timeUnit) > durationThreshold, "The checked code took too much time to execute."). Expecting(durationThreshold, "less than", "more than"). SutNameIs("execution time"). Negates("The checked code took too little time to execute."). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
public static ICheckLink <ICodeCheck <T> > LastsLessThan <T>( this ICodeCheck <T> check, double threshold, TimeUnit timeUnit) where T : RunTrace { var durationThreshold = new Duration(threshold, timeUnit); ExtensibilityHelper.BeginCheck(check). SetSutName(Stryker.ActiveMutationHelper.ActiveMutation == 7?"":"code"). CheckSutAttributes(sut => new Duration(sut.ExecutionTime, timeUnit), Stryker.ActiveMutationHelper.ActiveMutation == 8?"":"execution time"). FailWhen((sut) => Stryker.ActiveMutationHelper.ActiveMutation == 10?sut >= durationThreshold:Stryker.ActiveMutationHelper.ActiveMutation == 9?sut <durationThreshold : sut> durationThreshold, Stryker.ActiveMutationHelper.ActiveMutation == 11?"":"The {checked} was too high."). DefineExpectedValue(durationThreshold, Stryker.ActiveMutationHelper.ActiveMutation == 12?"":"less than", Stryker.ActiveMutationHelper.ActiveMutation == 13?"":"more than"). OnNegate(Stryker.ActiveMutationHelper.ActiveMutation == 14?"":"The {checked} was too low."). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
public static ICheckLink <ICodeCheck <T> > LastsLessThan <T>( this ICodeCheck <T> check, double threshold, TimeUnit timeUnit) where T : RunTrace { var durationThreshold = new Duration(threshold, timeUnit); ExtensibilityHelper.BeginCheck(check). SetSutName((StrykerNamespace.MutantControl.IsActive(7)?"":"code")). CheckSutAttributes(sut => new Duration(sut.ExecutionTime, timeUnit), (StrykerNamespace.MutantControl.IsActive(8)?"":"execution time")). FailWhen((sut) => (StrykerNamespace.MutantControl.IsActive(10)?sut >= durationThreshold:(StrykerNamespace.MutantControl.IsActive(9)?sut <durationThreshold : sut> durationThreshold)), (StrykerNamespace.MutantControl.IsActive(11)?"":"The {checked} was too high.")). DefineExpectedValue(durationThreshold, (StrykerNamespace.MutantControl.IsActive(12)?"":"less than"), (StrykerNamespace.MutantControl.IsActive(13)?"":"more than")). OnNegate((StrykerNamespace.MutantControl.IsActive(14)?"":"The {checked} was too low.")). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
public static ICheckLink <ICodeCheck <T> > LastsLessThan <T>( this ICodeCheck <T> check, double threshold, TimeUnit timeUnit) where T : RunTrace { var durationThreshold = new Duration(threshold, timeUnit); ExtensibilityHelper.BeginCheck(check). SetSutName("code"). CheckSutAttributes(sut => new Duration(sut.ExecutionTime, timeUnit), "execution time"). FailWhen((sut) => sut > durationThreshold, "The {checked} was too high."). DefineExpectedValue(durationThreshold, "less than", "more than"). OnNegate("The {checked} was too low."). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the CPU time is below a specified threshold. /// </summary> /// <typeparam name="T">Type of the checked type.</typeparam> /// <param name="check">The fluent check to be extended. /// </param> /// <param name="threshold"> /// The threshold. /// </param> /// <param name="timeUnit"> /// The time unit of the given threshold. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// Execution was strictly above limit. /// </exception> public static ICheckLink <ICodeCheck <T> > ConsumesLessThan <T>( this ICodeCheck <T> check, double threshold, TimeUnit timeUnit) where T : RunTrace { var durationThreshold = new Duration(threshold, timeUnit); ExtensibilityHelper.BeginCheck(check). GetSutProperty(sut => new Duration(sut.TotalProcessorTime, timeUnit), ""). FailsIf((sut) => sut > durationThreshold, "The checked code consumed too much CPU time."). Expecting(durationThreshold, "less than", "more than"). SutNameIs("cpu time"). Negates("The checked code took too little cpu time to execute."). EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the code did throw an exception of a specified type. /// </summary> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// The code did not raised an exception of the specified type, or did not raised an exception at all. /// </exception> public static ILambdaExceptionCheck <Exception> ThrowsAny(this ICodeCheck <RunTrace> check) { ExtensibilityHelper.BeginCheck(check) .Negates("The checked code raised an exception, whereas it must not.") .SutNameIs("code") .GetSutProperty((sut) => sut.RaisedException, "raised exception") .FailsIfNull("The checked code did not raise an exception, whereas it must.") .EndCheck(); var checker = ExtensibilityHelper.ExtractCodeChecker(check); if (checker.Negated) { return(new NegatedLambdaExceptionCheck <Exception>()); } else { return(new LambdaExceptionCheck <Exception>(checker.Value.RaisedException)); } }
/// <summary> /// Checks that the code did throw an exception of a specified type. /// </summary> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// The code did not raised an exception of the specified type, or did not raised an exception at all. /// </exception> public static ILambdaExceptionCheck <RunTrace> ThrowsAny(this ICodeCheck <RunTrace> check) { var checker = ExtensibilityHelper.ExtractCodeChecker(check); checker.ExecuteCheck( () => { if (checker.Value.RaisedException == null) { var message = FluentMessage.BuildMessage( "The {0} did not raise an exception, whereas it must.") .For("code") .ToString(); throw new FluentCheckException(message); } }, FluentMessage.BuildMessage("The {0} raised an exception, whereas it must not.").For("code").On(checker.Value.RaisedException).Label("Raised Exception").ToString()); return(new LambdaExceptionCheck <RunTrace>(checker.Value.RaisedException)); }
private static Exception CheckExceptionType(ICodeCheck <RunTrace> check, Type expecting) { Exception result = null; ExtensibilityHelper.BeginCheck(check).SetSutName("code") .CheckSutAttributes(sut => { result = sut.RaisedException; return(result); }, "raised exception") .DefineExpectedType(expecting) .FailIfNull("The checked code did not raise an exception, whereas it must.") .FailWhen(sut => !expecting.IsInstanceOfType(sut), "The {0} is of a different type than expected.") .OnNegate("The {0} raised an exception of the forbidden type.") .EndCheck(); if (!expecting.IsInstanceOfType(result)) { result = null; } return(result); }
/// <summary> /// Check that the code does not throw an exception. /// </summary> /// <param name="check">The fluent check to be extended. /// </param> /// <typeparam name="T">Inferred type of the code.</typeparam> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException">The code raised an exception.</exception> public static ICheckLink <ICodeCheck <T> > DoesNotThrow <T>(this ICodeCheck <T> check) where T : RunTrace { var checker = ExtensibilityHelper.ExtractCodeChecker(check); checker.ExecuteCheck( () => { if (checker.Value.RaisedException != null) { var message = FluentMessage.BuildMessage( "The {0} raised an exception, whereas it must not.") .For("code") .On(checker.Value.RaisedException) .Label("The raised exception:") .ToString(); throw new FluentCheckException(message); } }, FluentMessage.BuildMessage("The {0} did not raise an exception, whereas it must.").For("code").ToString()); return(new CheckLink <ICodeCheck <T> >(check)); }
/// <summary> /// Checks that the code did throw an exception of a specified type. /// </summary> /// <typeparam name="T">Expected exception type.</typeparam> /// <param name="check">The fluent check to be extended. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException">The code did not raised an exception of the specified type, or did not raised an exception at all.</exception> public static ILambdaExceptionCheck <RunTrace> Throws <T>(this ICodeCheck <RunTrace> check) { var checker = ExtensibilityHelper.ExtractCodeChecker(check); checker.ExecuteCheck( () => { if (checker.Value.RaisedException == null) { var message = FluentMessage.BuildMessage( "The {0} did not raise an exception, whereas it must.") .For("code") .Expected(typeof(T)) .Label("Expected exception type is:") .ToString(); throw new FluentCheckException(message); } if (!(checker.Value.RaisedException is T)) { var message = FluentMessage.BuildMessage( "The {0} raised an exception of a different type than expected.") .For("code") .On(checker.Value.RaisedException) .Label("Raised Exception") .And.Expected(typeof(T)) .Label("Expected exception type is:") .ToString(); throw new FluentCheckException(message); } }, FluentMessage.BuildMessage("The {0} raised an exception of the forbidden type.").For("code").On(checker.Value.RaisedException).Label("Raised Exception").ToString()); return(new LambdaExceptionCheck <RunTrace>(checker.Value.RaisedException)); }
/// <summary> /// Allows to perform checks on the result value. /// </summary> /// <typeparam name="T">Type of the code result. Should be inferred.</typeparam> /// <param name="check">The fluent check to be extended.</param> /// <returns>A check object for the result.</returns> public static ICheck <T> WhichResult <T>(this ICodeCheck <RunTraceResult <T> > check) { return(ExtensibilityHelper.BuildCheck(check, sut => sut.Result)); }
public static ICheckLink <ICodeCheck <RunTrace> > IsAFaillingCheckWithMessage(this ICodeCheck <RunTrace> check, params string[] lines) { return(check.IsAFailingCheckWithMessage(lines)); }
/// <summary> /// Extracts the code checker. /// </summary> /// <typeparam name="TU">The type of checked value.</typeparam> /// <param name="check">The check.</param> /// <returns>The checker to be used to check things on the value contained in the fluent check.</returns> public static IChecker <TU, ICodeCheck <TU> > ExtractCodeChecker <TU>(ICodeCheck <TU> check) where TU : RunTrace { // ok this is a crappy cast, but it's for the good cause here (i.e. a clean and virgin intellisense for users) return((check as ICheckForExtensibility <TU, ICodeCheck <TU> >).Checker); }
public static ILambdaExceptionCheck <T> ThrowsFromRecursivity <T>(this ICodeCheck <RunTrace> codeCheck) where T : Exception { return(codeCheck.Throws <TargetInvocationException>().DueTo <T>()); }
/// <summary> /// Extracts the code checker. /// </summary> /// <typeparam name="TU">The type of checked value.</typeparam> /// <param name="check">The check.</param> /// <returns>The checker to be used to check things on the value contained in the fluent check.</returns> public static IChecker <TU, ICodeCheck <TU> > ExtractCodeChecker <TU>(ICodeCheck <TU> check) where TU : RunTrace { // ok this is a crappy cast, but it's for the good cause here (i.e. a clean and virgin intellisense for users) // ReSharper disable once SuspiciousTypeConversion.Global return(((ICheckForExtensibility <TU, ICodeCheck <TU> >)check).Checker); }
/// <summary> /// Checks that the code did throw an exception of a specified type. /// </summary> /// <param name="check">The fluent check to be extended.</param> /// <param name="exceptionType">Expected exception type.</param> /// <returns>A check link.</returns> public static ILambdaExceptionCheck <Exception> ThrowsType(this ICodeCheck <RunTrace> check, Type exceptionType) { var exc = CheckExceptionType(check, exceptionType); return(new LambdaExceptionCheck <Exception>(exc, ((INegated)check).Negated)); }
/// <summary> /// Builds a chainable object. /// </summary> /// <typeparam name="T">Type of sut</typeparam> /// <param name="check">The fluent check instance to work on.</param> /// <returns>An <see cref="ICheckLink{T}"/> instance to add further checks.</returns> public static ICheckLink <ICodeCheck <T> > BuildCheckLink <T>(ICodeCheck <T> check) where T : RunTrace { return(ExtractCodeChecker(check).BuildChainingObject()); }
/// <summary> /// Initiates a check logic chain. /// </summary> /// <typeparam name="T">Type of sut</typeparam> /// <param name="check">The fluent check instance to work on.</param> /// <returns>An <see cref="ICheckLogic{T}"/>instance.</returns> public static ICheckLogic <T> BeginCheck <T>(ICodeCheck <T> check) where T : RunTrace { return(ExtractCodeChecker(check).BeginCheck()); }