Skip to content

Commit

Permalink
Merge pull request #20 from SimplyDanny/attirbutes-on-top
Browse files Browse the repository at this point in the history
Add option to place attributes on top of declarations
  • Loading branch information
giginet authored Jan 5, 2025
2 parents 7622c8b + 34269ba commit 97f4d35
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 11 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ Download `swift-testing-revolutionary.artifactbundle.zip` from [Releases](https:

You can pass some options to the command.

| Option | Description | Default |
|------------------------------------|--------------------------------------------------------------|---------|
| --dry-run | Run as a dry run mode. All test files are not overwritten | |
| --enable/disable-struct-conversion | Whether converting test classes to structs or not | Enabled |
| --enable/disable-strip-test-prefix | Whether stripping `test` prefix from each test method or not | Enabled |
| --enable/disable-adding-suite | Whether adding `@Suite` annotation to each test class or not | Enabled |
| Option | Description | Default |
|------------------------------------------|-----------------------------------------------------------------------------------|---------|
| --dry-run | Run as a dry run mode. All test files are not overwritten | |
| --enable/disable-struct-conversion | Whether converting test classes to structs or not | Enabled |
| --enable/disable-strip-test-prefix | Whether stripping `test` prefix from each test method or not | Enabled |
| --enable/disable-adding-suite | Whether adding `@Suite` annotation to each test class or not | Enabled |
| --enable/disable-attributes-on-same-line | Whether attributes are placed on the same line as the declaration or on top of it | Enabled |

### Struct Conversion

Expand Down
7 changes: 6 additions & 1 deletion Sources/RevolutionKit/GlobalOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ package struct GlobalOptions: Sendable {
/// Whether to add `@Suite` to each test class
var enableAddingSuite: Bool

/// Whether to put attributes on the same line as the declaration or on top of it
var attributesOnSameLine: Bool

package enum BackUpMode {
case disabled
case enabled(URL)
Expand All @@ -23,12 +26,14 @@ package struct GlobalOptions: Sendable {
isDryRunMode: Bool = false,
enableStructConversion: Bool = true,
enableStrippingTestPrefix: Bool = true,
enableAddingSuite: Bool = true
enableAddingSuite: Bool = true,
attributesOnSameLine: Bool = true
) {
self.isDryRunMode = isDryRunMode
self.enableStructConversion = enableStructConversion
self.enableStrippingTestPrefix = enableStrippingTestPrefix
self.enableAddingSuite = enableAddingSuite
self.attributesOnSameLine = attributesOnSameLine
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension XCTestRewriter {
attributeName: IdentifierTypeSyntax(
name: .identifier("Suite")
),
trailingTrivia: .space
trailingTrivia: globalOptions.attributesOnSameLine ? .space : .newlines(1)
)

let newAttributes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ extension XCTestRewriter {
attributeName: IdentifierTypeSyntax(
name: .identifier("Test")
),
trailingTrivia: .space
trailingTrivia: globalOptions.attributesOnSameLine ? .space : .newlines(1)
)

let attributes = {
var attributes = node.attributes
attributes.insert(.attribute(testMacroAttribute), at: attributes.startIndex)
return attributes
.with(\.leadingTrivia, node.leadingTrivia)
.with(\.trailingTrivia, .space)
}()

let newSigniture = node
Expand Down
8 changes: 7 additions & 1 deletion Sources/swift-testing-revolutionary/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ struct SwiftTestingRevolutionaryCommand: AsyncParsableCommand {
@Flag(inversion: .prefixedEnableDisable, help: "Whether adding `@Suite` to each test class")
var addingSuite: Bool = true

@Flag(inversion: .prefixedEnableDisable, help: """
Whether to put attributes on the same line as the declaration or on top of it
""")
var attributesOnSameLine: Bool = true

func run() async throws {
let options = buildRunnerOptions()

Expand All @@ -33,7 +38,8 @@ struct SwiftTestingRevolutionaryCommand: AsyncParsableCommand {
isDryRunMode: dryRun,
enableStructConversion: structConversion,
enableStrippingTestPrefix: stripTestPrefix,
enableAddingSuite: addingSuite
enableAddingSuite: addingSuite,
attributesOnSameLine: attributesOnSameLine
)
}
}
35 changes: 35 additions & 0 deletions Tests/RevolutionKitTests/TestMethodsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,33 @@ private let tearDownConversionFixtures: [ConversionTestFixture] = [
},
]

private let attributesOnTopOfDeclaration: [ConversionTestFixture] = [
Fixture {
"""
func testExample() {
}
"""
"""
@Test
func example() {
}
"""
},
Fixture {
"""
@ExistingAttribute
func testExample() {
}
"""
"""
@Test
@ExistingAttribute
func example() {
}
"""
},
]

@Suite struct TestMethodsTests {
private let emitter = StringEmitter()

Expand All @@ -210,4 +237,12 @@ private let tearDownConversionFixtures: [ConversionTestFixture] = [
let result = try runner.run(for: fixture.source, emitter: StringEmitter())
#expect(result == fixture.expected, sourceLocation: fixture.sourceLocation)
}

@Test("TestMethodsRewriter can put attributes on top of declaration", arguments: attributesOnTopOfDeclaration)
private func rewriteTestCasesWithAttributesOnTopOfDeclaration(_ fixture: ConversionTestFixture) async throws {
let runner = Runner(globalOptions: .init(attributesOnSameLine: false))

let result = try runner.run(for: fixture.source, emitter: StringEmitter())
#expect(result == fixture.expected, sourceLocation: fixture.sourceLocation)
}
}
35 changes: 35 additions & 0 deletions Tests/RevolutionKitTests/TestSuiteClassTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,33 @@ private let structConversionFixturesWithoutSuite: [ConversionTestFixture] = [
},
]

private let attributesOnTopOfDeclaration: [ConversionTestFixture] = [
Fixture {
"""
class HogeTests: XCTestCase {
}
"""
"""
@Suite
struct HogeTests {
}
"""
},
Fixture {
"""
@ExistingAttribute
class HogeTests: XCTestCase {
}
"""
"""
@Suite
@ExistingAttribute
struct HogeTests {
}
"""
},
]

@Suite struct TestSuiteClassTests {
private let emitter = StringEmitter()

Expand All @@ -157,4 +184,12 @@ private let structConversionFixturesWithoutSuite: [ConversionTestFixture] = [
let result = try runner.run(for: fixture.source, emitter: StringEmitter())
#expect(result == fixture.expected, sourceLocation: fixture.sourceLocation)
}

@Test("TestClassRewriter can put attributes on top of the declaration", arguments: attributesOnTopOfDeclaration)
private func rewriterCanPutAttributesOnTopOfDeclaration(_ fixture: ConversionTestFixture) async throws {
let runner = Runner(globalOptions: .init(attributesOnSameLine: false))

let result = try runner.run(for: fixture.source, emitter: StringEmitter())
#expect(result == fixture.expected, sourceLocation: fixture.sourceLocation)
}
}

0 comments on commit 97f4d35

Please sign in to comment.