Skip to content

Disallow @Test on member functions of XCTestCase subclasses. #626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Sources/TestingMacros/SuiteDeclarationMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public struct SuiteDeclarationMacro: MemberMacro, PeerMacro, Sendable {
diagnostics += diagnoseIssuesWithLexicalContext(context.lexicalContext, containing: declaration, attribute: suiteAttribute)
diagnostics += diagnoseIssuesWithLexicalContext(declaration, containing: declaration, attribute: suiteAttribute)

// Suites inheriting from XCTestCase are not supported.
// Suites inheriting from XCTestCase are not supported. This check is
// duplicated in TestDeclarationMacro but is not part of
// diagnoseIssuesWithLexicalContext() because it doesn't need to recurse
// across the entire lexical context list, just the innermost type
// declaration.
if let declaration = declaration.asProtocol((any DeclGroupSyntax).self),
declaration.inherits(fromTypeNamed: "XCTestCase", inModuleNamed: "XCTest") {
diagnostics.append(.xcTestCaseNotSupported(declaration, whenUsing: suiteAttribute))
Expand Down
10 changes: 10 additions & 0 deletions Sources/TestingMacros/TestDeclarationMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
// Check if the lexical context is appropriate for a suite or test.
diagnostics += diagnoseIssuesWithLexicalContext(context.lexicalContext, containing: declaration, attribute: testAttribute)

// Suites inheriting from XCTestCase are not supported. We are a bit
// conservative here in this check and only check the immediate context.
// Presumably, if there's an intermediate lexical context that is *not* a
// type declaration, then it must be a function or closure (disallowed
// elsewhere) and thus the test function is not a member of any type.
if let containingTypeDecl = context.lexicalContext.first?.asProtocol((any DeclGroupSyntax).self),
containingTypeDecl.inherits(fromTypeNamed: "XCTestCase", inModuleNamed: "XCTest") {
diagnostics.append(.containingNodeUnsupported(containingTypeDecl, whenUsing: testAttribute, on: declaration))
}

// Only one @Test attribute is supported.
let suiteAttributes = function.attributes(named: "Test")
if suiteAttributes.count > 1 {
Expand Down
4 changes: 4 additions & 0 deletions Tests/TestingMacrosTests/TestDeclarationMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ struct TestDeclarationMacroTests {
"Attribute 'Suite' cannot be applied to a subclass of 'XCTestCase'",
"@Suite final class C: XCTest.XCTestCase {}":
"Attribute 'Suite' cannot be applied to a subclass of 'XCTestCase'",
"final class C: XCTestCase { @Test func f() {} }":
"Attribute 'Test' cannot be applied to a function within class 'C'",
"final class C: XCTest.XCTestCase { @Test func f() {} }":
"Attribute 'Test' cannot be applied to a function within class 'C'",

// Unsupported inheritance
"@Suite protocol P {}":
Expand Down