Skip to content

Commit 71405e0

Browse files
committed
namespace Specs { class Cat { void ItMeows() {}}}
1 parent 976c6b1 commit 71405e0

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

MiniSpec/PrivateAPI/TestDiscoverer.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,29 @@ internal TestDiscoverer(Assembly assembly, TestSuite testSuite, TestExecutor tes
2121
_reporter = testReporter;
2222
}
2323

24-
internal List<string> TEST_OBJECT_NAME_PATTERNS = new List<string> { "^[a-zA-Z].*Test", "^[a-zA-Z].*Spec", "^Test", "^Spec" };
25-
internal List<string> TEST_OBJECT_CHILD_TEST_NAME_PATTERNS = new List<string> { "^[a-zA-Z]" };
24+
internal List<string> TEST_OR_TEST_GROUP_NAME_PATTERNS = new List<string> { "^[A-Z].*Test", "^[A-Z].*Spec", "^Test", "^Spec" };
25+
internal List<string> TEST_GROUP_CHILD_TEST_NAME_PATTERNS = new List<string> { "^[A-Z].*Test", "^[A-Z].*Spec", "^Test", "^Spec", "^It", "^Should", "^Can" };
2626

2727
internal void FindAndExecutorTests()
2828
{
2929
foreach (var type in _assembly.GetTypes())
3030
{
3131
var isTestType = false;
32-
foreach (var pattern in TEST_OBJECT_NAME_PATTERNS)
33-
{
34-
var typeMethods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static);
35-
foreach (var method in typeMethods)
36-
{
37-
var methodName = GetTestMethodName(method.Name);
38-
var isTestMethod = false;
39-
foreach (var testObjectPattern in TEST_OBJECT_NAME_PATTERNS) {
40-
if (new Regex(testObjectPattern).IsMatch(methodName)) {
41-
isTestMethod = true;
42-
break;
43-
}
44-
}
45-
if (isTestMethod) {
46-
_reporter.BeforeTest(methodName);
47-
var result = _executor.ExecuteTestMethod(method);
48-
}
32+
foreach (var pattern in TEST_OR_TEST_GROUP_NAME_PATTERNS)
33+
if (new Regex(pattern).IsMatch(type.Name) || new Regex(pattern).IsMatch(type.FullName)) { isTestType = true; break; }
34+
35+
var typeMethods = new List<MethodInfo>(type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance));
36+
typeMethods.AddRange(type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static));
37+
38+
foreach (var method in typeMethods) {
39+
var methodName = GetTestMethodName(method.Name);
40+
var isTestMethod = false;
41+
var patternsToTest = isTestType ? TEST_GROUP_CHILD_TEST_NAME_PATTERNS : TEST_OR_TEST_GROUP_NAME_PATTERNS;
42+
foreach (var pattern in patternsToTest)
43+
if (new Regex(pattern).IsMatch(methodName)) { isTestMethod = true; break; }
44+
if (isTestMethod) {
45+
_reporter.BeforeTest(methodName);
46+
var result = _executor.ExecuteTestMethod(method);
4947
}
5048
}
5149
}

spec/CLI/ListSpecs.Spec.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,52 @@ void UnreleatedFunction2() {}
3232
new Regex($"^{unexpectedTestName}", RegexOptions.Multiline)
3333
.IsMatch(project.RunResult.StandardOutput).Should().BeFalse($"Expected this not to be listed as a test name: {unexpectedTestName}");
3434
}
35+
36+
// [TestCase(framework, TestName = "")] ~ for different framework versions (skip unsupported with Assert.Ignore())
37+
[Test]
38+
public void ListInstanceMethodsOfClass() {
39+
var project = CreateProject(csharp: 9, framework: Project.TargetFrameworks.Net50, type: Project.OutputTypes.Exe);
40+
project.WriteFile("Program.cs", @"
41+
return MiniSpec.Tests.Run(args);
42+
class RegularClass {
43+
void TestOne() {}
44+
void TestTwo() {}
45+
void notATest() {}
46+
void _NotATest() {}
47+
void ItDoesSomething() {} // Not a test because 'It' only works in a test group
48+
}
49+
class DogTest {
50+
void ItBarks() {}
51+
void ShouldBark() {}
52+
void CanBark() {}
53+
void UnrelatedDog() {}
54+
}
55+
namespace Tests {
56+
class Cat {
57+
void ItMeows() {}
58+
void ShouldMeow() {}
59+
void CanMeow() {}
60+
void UnrelatedCat() {}
61+
}
62+
}
63+
");
64+
65+
project.Run("-l");
66+
67+
System.Console.WriteLine($"OUTPUT: {project.RunResult.StandardOutput}");
68+
69+
project.RunResult.StandardError.Should().BeEmpty();
70+
project.RunResult.OK.Should().BeTrue();
71+
72+
foreach (var expectedTestName in new[] { "TestOne", "TestTwo", "ItBarks", "ShouldBark", "CanBark", "ItMeows", "ShouldMeow", "CanMeow" })
73+
new Regex($"^{expectedTestName}", RegexOptions.Multiline)
74+
.IsMatch(project.RunResult.StandardOutput).Should().BeTrue($"Expected listed test name: {expectedTestName}");
75+
76+
foreach (var unexpectedTestName in new[] { "notATest", "_NotATest", "UnrelatedDog", "UnrelatedCat" })
77+
new Regex($"^{unexpectedTestName}", RegexOptions.Multiline)
78+
.IsMatch(project.RunResult.StandardOutput).Should().BeFalse($"Expected this not to be listed as a test name: {unexpectedTestName}");
79+
}
80+
81+
// Next: --list --details (details reporter) for Setup/Teardown/Constructor/Dispose/Group
3582
}
3683
}

0 commit comments

Comments
 (0)