using UnitTestSharp; using System.Collections.Generic; using System.Reflection; using UnitTestSharp.Extensions; namespace TestsForUnitTestSharp { public class TestFinderTests : TestFixture { public void FindTestsToRun_Test() { System.Type[] types = { this.GetType(), typeof(UnitTestSharp.Assert) }; types = TestFinder.FindFixturesToTest(types); CheckEqual(1, types.Length); CheckEqual(this.GetType(), types[0]); } public void FindTestsToRun_TestZeroLength() { System.Type[] types = { }; types = TestFinder.FindFixturesToTest(types); CheckEqual(0, types.Length); } public void FindTestsToRun_TestNull() { CheckEqual(0, TestFinder.FindFixturesToTest(null).Length); } [IgnoreFixture] class FauxFixture : TestFixture { } class FauxFixtureDerived : FauxFixture { } public void FindTestsToRun_Derived() { System.Type[] types = { typeof(FauxFixtureDerived) }; types = TestFinder.FindFixturesToTest(types); CheckEqual(1, types.Length); CheckEqual(typeof(FauxFixtureDerived), types[0]); } public void FindTestsToRun_IgnoreFixture() { System.Type[] types = { typeof(FauxFixture) }; types = TestFinder.FindFixturesToTest(types); CheckEqual(0, types.Length); } class OverrideFixture : TestFixture { public override void FixtureSetup() { } public override void FixtureTeardown() { } public override void TestSetup() { } public override void TestTeardown() { } public override bool Equals(object obj) { return base.Equals(obj); } public override int GetHashCode() { return base.GetHashCode(); } public override string ToString() { return base.ToString(); } public void Test() { } } public void DoesNotFindBaseOverrides() { int ignored = 0; var methods = TestFinder.FindPublicTestableMethods(typeof(OverrideFixture), ref ignored); CheckEqual(1, methods.Count); CheckEqual("Test", methods[0].Name); } class PropertyFixture : TestFixture { public int Property { get; set; } } public void DoesNotFindProperties() { int ignored = 0; var methods = TestFinder.FindPublicTestableMethods(typeof(PropertyFixture), ref ignored); CheckEqual(0, methods.Count); } class BaseFixture : TestFixture { public virtual void VirtualTest() { } } class DerivedFixture : BaseFixture { public override void VirtualTest() { } } public void FindsDerivedTests() { int ignored = 0; var methods = TestFinder.FindPublicTestableMethods(typeof(DerivedFixture), ref ignored); CheckEqual(1, methods.Count); CheckEqual(typeof(DerivedFixture), methods[0].DeclaringType); } class BaseTest { public void Test() { } } class DerivedTest : BaseTest { } public void FindsInheritedMethods() { List missingIgnoreAttr = new List(); int ignored = 0; var methods = TestFinder.FindPublicTestableMethods(typeof(DerivedTest), ref ignored); CheckEqual(1, methods.Count); CheckEqual("Test", methods[0].Name); CheckEqual(typeof(DerivedTest), methods[0].ReflectedType); CheckEqual(typeof(BaseTest), methods[0].DeclaringType); CheckEqual("TestsForUnitTestSharp::TestFinderTests.DerivedTest::Test", methods[0].ExtractPedigree()); } public class DoesNotFindLambdasTests : UnitTestSharp.TestFixture { public void Test() { System.Func> test = (string key) => { int ignored = 0; return TestFinder.FindPublicTestableMethods(GetType(), ref ignored); }; var toTest = test("foo"); CheckEqual(1, toTest.Count); } } [IgnoreFixture] public class PrivateTestFixture : TestFixture { private void Test() { } } public class FindsPrivateTests : TestFixture { public void Basic() { int ignored = 0; var missingIgnoreAttr = new List(); TestFinder.CheckPrivateMethods(typeof(PrivateTestFixture), ref missingIgnoreAttr, ref ignored); CheckEqual(0, ignored); CheckEqual(1, missingIgnoreAttr.Count); CheckEqual(missingIgnoreAttr[0].Name, "Test"); } } } }