using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UnitTestSharp; namespace TestsForUnitTestSharp { public class ParameterizedAttributeTests : UnitTestSharp.TestFixture { static dynamic[] data = new[] { new { index = 0, expected = false, x = 1, y = 2}, new { index = 1, expected = true, x = 1, y = 1}, }; [Parameterized("data")] public void Test(dynamic row) { CheckEqual(row.expected, row.x == row.y); } [Parameterized("data")] public void Test(dynamic row, int rowIndex) { CheckEqual(row.expected, row.x == row.y); CheckEqual(row.index, rowIndex); } } public class ParameterizedAttribute_FailingTests : UnitTestSharp.TestFixture { [IgnoreFixture] public class FailingAttributeTests : UnitTestSharp.TestFixture { static dynamic[] data = new[] { new { expected = false, x = 1, y = 2}, new { expected = true, x = 1, y = 1}, new { expected = false, x = 1, y = 1}, }; [Parameterized("data")] public void Test(dynamic row) { CheckEqual(!row.expected, row.x == row.y); CheckEqual(!row.expected, row.x == row.y); } } public void FailingFixture() { int totalTests = 0; int failedTests = 0; int failedChecks = 0; TestRunner.RunTestFixture( typeof(FailingAttributeTests), (string line) => { }, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(4, failedChecks); CheckEqual(2, failedTests); CheckEqual(3, totalTests); } } public class ParameterizedAttribute_NotEnumerable : UnitTestSharp.TestFixture { [IgnoreFixture] public class NotEnumerableFixture : UnitTestSharp.TestFixture { #pragma warning disable 414 static int five = 5; #pragma warning disable 414 [Parameterized("five")] public void Test(dynamic row) { CheckEqual(!row.expected, row.x == row.y); } } public void NotEnumerable() { int totalTests = 0; int failedTests = 0; int failedChecks = 0; string message = ""; TestRunner.RunTestFixture( typeof(NotEnumerableFixture), (string line) => { message += line; }, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(1, failedChecks); CheckEqual(1, failedTests); CheckEqual(1, totalTests); Check(message.Contains("'five'")); } } public class ParameterizedAttribute_MissingData : UnitTestSharp.TestFixture { [IgnoreFixture] public class MissingDataFixture : UnitTestSharp.TestFixture { [Parameterized("missing")] public void Test(dynamic row) { CheckEqual(!row.expected, row.x == row.y); } } public void NotEnumerable() { int totalTests = 0; int failedTests = 0; int failedChecks = 0; string message = ""; TestRunner.RunTestFixture( typeof(MissingDataFixture), (string line) => { message += line; }, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(1, failedChecks); CheckEqual(1, failedTests); CheckEqual(1, totalTests); Check(message.Contains("'missing'")); } } public class ParameterizedAttribute_WrongArguments : UnitTestSharp.TestFixture { [IgnoreFixture] public class NoArgumentsFixture : UnitTestSharp.TestFixture { static dynamic[] data = new[] { new { expected = false, x = 1, y = 2}, new { expected = true, x = 1, y = 1}, }; [Parameterized("data")] public void Test() { } } public void NoArguments() { int totalTests = 0; int failedTests = 0; int failedChecks = 0; string message = ""; TestRunner.RunTestFixture( typeof(NoArgumentsFixture), (string line) => { message += line; }, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, failedChecks); CheckEqual(2, failedTests); CheckEqual(2, totalTests); Check(message.Contains("one or two")); } [IgnoreFixture] public class TwoArgumentsFixture : UnitTestSharp.TestFixture { static dynamic[] data = new[] { new { expected = false, x = 1, y = 2}, new { expected = true, x = 1, y = 1}, }; [Parameterized("data")] public void Test(dynamic a, dynamic b, dynamic c) { } } public void ThreeArguments() { int totalTests = 0; int failedTests = 0; int failedChecks = 0; string message = ""; TestRunner.RunTestFixture( typeof(TwoArgumentsFixture), (string line) => { message += line; }, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, failedChecks); CheckEqual(2, failedTests); CheckEqual(2, totalTests); Check(message.Contains("one or two")); } [IgnoreFixture] public class WrongArgumentTypeFixture : UnitTestSharp.TestFixture { static dynamic[] data = new[] { new { expected = false, x = 1, y = 2}, new { expected = true, x = 1, y = 1}, }; [Parameterized("data")] public void Test(int a) { } } public void WrongArgumentType() { int totalTests = 0; int failedTests = 0; int failedChecks = 0; string message = ""; TestRunner.RunTestFixture( typeof(WrongArgumentTypeFixture), (string line) => { message += line; }, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, failedChecks); CheckEqual(2, failedTests); CheckEqual(2, totalTests); CheckEqualRegex("Could not assign from data", message); } [IgnoreFixture] public class NonParameterizedOneArgumentFixture : UnitTestSharp.TestFixture { static dynamic[] data = new[] { new { expected = false, x = 1, y = 2}, new { expected = true, x = 1, y = 1}, }; public void Test(dynamic row) { } } public void NonParameterizedOneArgument() { int totalTests = 0; int failedTests = 0; int failedChecks = 0; string message = ""; TestRunner.RunTestFixture( typeof(NonParameterizedOneArgumentFixture), (string line) => { message += line; }, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(1, failedChecks); CheckEqual(1, failedTests); CheckEqual(1, totalTests); CheckEqualRegex("is not parameterized", message); } } }