Last active
May 22, 2022 22:02
-
-
Save es-repo/082702c366c351cedf8693f38fccf302 to your computer and use it in GitHub Desktop.
better-unit-test-in-c#-article-PutInsideTest-full.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class PutInsideTest | |
{ | |
public sealed record Args | |
{ | |
public Thing Thing { get; init; } = null!; | |
public string Label { get; init; } = ""; | |
} | |
sealed class TestCases : IEnumerable<object[]> | |
{ | |
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | |
public IEnumerator<object[]> GetEnumerator() | |
{ | |
yield return OpenBox_ThingFitIntoBoxAndUniqueLabel_ThingAddedIntoBox_TrueExpected_1(); | |
yield return ClosedBox_ThingFitIntoBoxAndUniqueLabel_ThingNotAddedIntoBox_FalseExpected_2(); | |
} | |
static object[] OpenBox_ThingFitIntoBoxAndUniqueLabel_ThingAddedIntoBox_TrueExpected_1() | |
{ | |
var stateActual = new Box( | |
new Dictionary<string, Thing> | |
{ | |
{ "Label1", new Thing { Size = 10 } }, | |
{ "Label2", new Thing { Size = 20 } }, | |
}) | |
{ | |
Size = 100, | |
IsOpen = true | |
}; | |
var args = new Args | |
{ | |
Thing = new Thing { Size = 30 }, | |
Label = "Label3" | |
}; | |
var stateExpected = new Box(new Dictionary<string, Thing> | |
{ | |
{ "Label1", new Thing { Size = 10 } }, | |
{ "Label2", new Thing { Size = 20 } }, | |
{ "Label3", new Thing { Size = 30 } }, | |
}) | |
{ | |
Size = 100, | |
IsOpen = true | |
}; | |
var expected = true; | |
return new object[] { stateActual, args, stateExpected, expected }; | |
} | |
static object[] ClosedBox_ThingFitIntoBoxAndUniqueLabel_ThingNotAddedIntoBox_FalseExpected_2() | |
{ | |
var stateActual = new Box( | |
new Dictionary<string, Thing> | |
{ | |
{ "Label1", new Thing { Size = 10 } }, | |
{ "Label2", new Thing { Size = 20 } }, | |
}) | |
{ | |
Size = 100, | |
IsOpen = false | |
}; | |
var args = new Args | |
{ | |
Thing = new Thing { Size = 30 }, | |
Label = "Label3" | |
}; | |
var stateExpected = new Box( | |
new Dictionary<string, Thing> | |
{ | |
{ "Label1", new Thing { Size = 10 } }, | |
{ "Label2", new Thing { Size = 20 } }, | |
}) | |
{ | |
Size = 100, | |
IsOpen = false | |
}; | |
var expected = true; | |
return new object[] { stateActual, args, stateExpected, expected }; | |
} | |
} | |
[Theory] | |
[ClassData(typeof(TestCases))] | |
public static void Test( | |
Box stateActual, | |
Args args, | |
Box stateExpected, | |
bool expected) | |
{ | |
var actual = stateActual.PutInside(args.Thing, args.Label); | |
Assert.Equal(stateExpected, stateActual); | |
Assert.Equal(expected, actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment