/// <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> /// 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 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)); }
/// <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> /// 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> /// 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)); }
/// <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) { var checker = ExtensibilityHelper.ExtractCodeChecker(check); return(new FluentCheck <T>(checker.Value.Result)); }