public static void ObjectEquality <T>(FailableFunc <T> failableFunc, [CanBeNull] object obj, bool expectedEquality = true) { Asserter.Against(failableFunc) .WithHeading($"[{failableFunc}] should {(expectedEquality ? "be" : "not be")} equal to [{obj}]") .And(it => it.Equals(obj), Is.EqualTo(expectedEquality)) .And(it => Equals(it, obj), Is.EqualTo(expectedEquality)) .Invoke(); }
public static void FailedFailable <T>(FailableFunc <T> failableFunc) { Asserter.Against(failableFunc) .And(Has.Property(nameof(failableFunc.HasValue)).False) .And(Has.Property(nameof(failableFunc.Failed)).True) .And(it => it.Value, Throws.InvalidOperationException) .Invoke(); }
public void SuccessRate(int iterations, double successRate) { var fickleTimes = MethodTimer.MeasureExecution(() => FickleFunc(successRate), iterations); Asserter.Against(fickleTimes) .And(Has.Property(nameof(fickleTimes.SuccessRate)).CloseTo(successRate, 0.005)) .And(Has.Property(nameof(fickleTimes.Iterations)).EqualTo(iterations)) .Invoke(); }
public static void IsEquivalentTo <T>(this ISaveFile <T> actual, ISaveFile <T> expected) where T : ISaveData { Asserter.Against(actual) .WithHeading($"First {actual.GetType().Prettify()} must be equivalent to the second {expected.GetType().Prettify()}") .And(Has.Property(nameof(actual.Nickname)).EqualTo(expected.Nickname)) .And(Has.Property(nameof(actual.TimeStamp)).EqualTo(expected.TimeStamp)) .And(Has.Property(nameof(actual.Data)).EqualTo(expected.Data)) .Invoke(); }
public void Trim_Regex(string input, string trimPattern, string expected_end, string expected_start, string expected_both) { var pattern = new Regex(trimPattern); Asserter.Against(input) .And(it => it.TrimEnd(pattern), Is.EqualTo(expected_end)) .And(it => it.TrimStart(pattern), Is.EqualTo(expected_start)) .And(it => it.Trim(pattern), Is.EqualTo(expected_both)) .Invoke(); }
private static IMultipleAsserter ComparingFallbacks <T>(Fallback <T> actual, Fallback <T> expected, [CanBeNull] PrettificationSettings asserterPrettificationSettings = default) { return(Asserter.Against(actual) .WithHeading($"Comparing {nameof(Fallback<T>)}s") .WithPrettificationSettings(asserterPrettificationSettings) .And( Is.EqualTo(expected) .Using(new FallbackComparer()), () => typeof(FallbackComparer).Prettify(asserterPrettificationSettings) )); }
public void EmptyEqualsEmpty() { var a = new Optional <int>(); var b = new Optional <int>(); Asserter.Against(a) .And(Is.EqualTo(b), "a IsEqualTo b") .And(Is.EqualTo(new Optional <int>())) .And(Is.EqualTo(default(Optional <int>))) .And(it => it == b, Is.True, "a == b") .And(it => it.Equals(b), Is.True, "a.Equals(b)") .Invoke(); }
public Asserter <AggregateExecutionComparison.TimeComparison> AssertComparison( AggregateExecutionComparison.TimeComparison results, int expectedComparison ) { Constraint ratioConstraint = expectedComparison switch { -1 => Is.Positive.And.LessThan(1), 0 => Is.EqualTo(1), 1 => Is.Positive.And.GreaterThan(1), _ => throw new ArgumentOutOfRangeException(nameof(expectedComparison)) }; return(Asserter.Against(results) .And(it => it.First.CompareTo(it.Second), Is.EqualTo(expectedComparison)) .And(it => it.Difference.Sign(), Is.EqualTo(expectedComparison)) .And(it => it.Ratio, ratioConstraint)); }
public void SaveToSlot() { var saveSlot = GetUniqueSlot(); var nickname = nameof(SaveToSlot) + Guid.NewGuid(); var data = new TestSaveData(nickname); var startTime = DateTime.Today; for (int i = 0; i < 5; i++) { var now = startTime.AddDays(i); data.Counter = i; var saved = saveSlot.Save(data, now); var latest = saveSlot.LatestFile(); latest?.Load(); // expectations Asserter.Against(saveSlot) .WithHeading($"Save Slot iteration #{i}") .And(Has.Property(nameof(saveSlot.SaveFileCount)).EqualTo(i + 1)) .And( Asserter.Against(latest) .WithHeading($"Latest File #{i}") .And(Is.Not.Null) .Exists() .Nicknamed(saveSlot.Nickname) .TimeStamped(now) .And(Has.Property(nameof(latest.Data)).Not.Null) .And(it => it.Data?.Counter, Is.EqualTo(i)) ) .And( Asserter.Against(saved) .WithHeading($"Returned file from {nameof(saveSlot)}.{nameof(saveSlot.Save)}") .And(Is.Not.Null) .Exists() .Nicknamed(saveSlot.Nickname) .TimeStamped(now) .And(Has.Property(nameof(saved.Data)).Not.Null) .And(it => it.Data?.Counter, Is.EqualTo(i)) .And(it => it.Data, Is.EqualTo(latest.Data)) ) .Invoke(); } }
public void SingleActionTime() { Action action = Quick; //stopwatch version var stopwatch = Stopwatch.StartNew(); action(); stopwatch.Stop(); Console.WriteLine($"Stopwatch version: {stopwatch.Elapsed:g}"); var exTime = new ExecutionTime(action); Console.WriteLine($"{nameof(exTime)}: {exTime}"); Asserter.Against(exTime) .And(Has.Property(nameof(exTime.Duration)).CloseTo(SlowSpan, TimeSpan.FromSeconds(0.01))) .And(it => it.Execution.Failed, Is.EqualTo(false)) .And(Has.Property(nameof(exTime.Duration)).CloseTo(stopwatch.Elapsed)) .Invoke(); }
public void Copy_Array() { var original = new int[] { 1, 2, 3 }; var dupe = original.Copy(); Asserter.Against(dupe) .And(Is.EqualTo(original)) .And(Is.Not.SameAs(original)) .Invoke(); original[0] = 99; dupe[dupe.Length - 1] = -99; Asserter.Against(dupe) .And(original, Is.EqualTo(new[] { 99, 2, 3 })) .And(Is.EqualTo(new[] { 1, 2, -99 })) .And(Is.Not.EqualTo(original)) .And(Is.Not.SameAs(original)) .Invoke(); }
public void ListVsSet() { const int iterations = 1000; var items = MakeHugeCollection(iterations).ToArray(); var toFind = items.Random(); // Using their respecting .Contains() methods var list = items.ToList(); var set = items.ToHashSet(); var listTimes = MethodTimer.MeasureExecution(() => ListContains(list, toFind), iterations); var setTimes = MethodTimer.MeasureExecution(() => SetContains(set, toFind), iterations); Console.WriteLine($"list: {listTimes}"); Console.WriteLine($"set: {setTimes}"); // Using the extension Enumerable.Contains() list.RandomizeEntries(); set.RandomizeEntries(); var listableTimes = MethodTimer.MeasureExecution(() => EnumerableContains(list, toFind), iterations); var settableTimes = MethodTimer.MeasureExecution(() => EnumerableContains(set, toFind), iterations); Console.WriteLine($"{nameof(listableTimes)}: {listableTimes}"); Console.WriteLine($"{nameof(settableTimes)}: {settableTimes}"); // Actually doing a comparison var comparison = new AggregateExecutionComparison(listTimes, setTimes); Console.WriteLine(comparison); Asserter.Against(comparison) .And(AssertComparison(comparison.Average, 1)) .And(AssertComparison(comparison.Total, 1)) .Invoke(); }
public void SaveSlotNewFile() { var saveSlot = GetUniqueSlot(); var nickname = nameof(SaveSlotNewFile) + Guid.NewGuid(); var data = new TestSaveData(nickname); Assert.That(saveSlot, Has.Property(nameof(saveSlot.SaveFileCount)).EqualTo(0)); // save to the slot for the first time var now = DateTime.Today; var firstSave = saveSlot.Save(data, now); Asserter.WithHeading("After the first save") .And( Asserter.Against(saveSlot) .And(Has.Property(nameof(saveSlot.SaveFileCount)).EqualTo(1)) .And( it => it.EnumerateSaveFiles().Select(f => f.TimeStamp), Contains.Item(now) ) ) .And( Asserter.Against(firstSave) .Exists() .Nicknamed(saveSlot.Nickname) .TimeStamped(now) .And(Has.Property(nameof(firstSave.Data)).EqualTo(data)) ) .And( Asserter.Against(saveSlot.LatestFile()?.Load()) .And(it => it.IsEquivalentTo(firstSave)) ) .Invoke(); // save to the slot a _second_ time now += TimeSpan.FromDays(1); var secondSave = saveSlot.Save(data, now); Asserter.WithHeading("After second save") .And( Asserter.Against(saveSlot) .And(Has.Property(nameof(saveSlot.SaveFileCount)).EqualTo(2)) .And( it => it.EnumerateSaveFiles().Select(f => f.TimeStamp), Contains.Item(now) ) ) .And( Asserter.Against(secondSave) .Exists() .Nicknamed(saveSlot.Nickname) .TimeStamped(now) .And(Has.Property(nameof(secondSave.Data)).EqualTo(data)) .And(Has.Property(nameof(secondSave.Data)).EqualTo(firstSave.Data)) .And(Has.Property(nameof(secondSave.TimeStamp)).Not.EqualTo(firstSave.TimeStamp)) ) .And( Asserter.Against(saveSlot.LatestFile()?.Load()) .Exists() .And(it => it.IsEquivalentTo(secondSave)) ) .Invoke(); }