Skip to content

Commit

Permalink
Merge pull request #3 from alimyuz/rename
Browse files Browse the repository at this point in the history
Rename. Create README.md
  • Loading branch information
alimyuz committed Oct 26, 2023
2 parents f431d06 + 95c7b4e commit 92b576e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 33 deletions.
20 changes: 10 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import PackageDescription
import CompilerPluginSupport

let package = Package(
name: "TestCasesMacro",
name: "TestCaseKit",
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "TestCasesMacro",
targets: ["TestCasesMacro"]
name: "TestCaseKit",
targets: ["TestCaseKit"]
),
.executable(
name: "TestCasesMacroClient",
targets: ["TestCasesMacroClient"]
name: "TestCaseKitClient",
targets: ["TestCaseKitClient"]
),
],
dependencies: [
Expand All @@ -27,24 +27,24 @@ let package = Package(
// Targets can depend on other targets in this package and products from dependencies.
// Macro implementation that performs the source transformation of a macro.
.macro(
name: "TestCasesMacroMacros",
name: "TestCaseKitMacros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
]
),

// Library that exposes a macro as part of its API, which is used in client programs.
.target(name: "TestCasesMacro", dependencies: ["TestCasesMacroMacros"]),
.target(name: "TestCaseKit", dependencies: ["TestCaseKitMacros"]),

// A client of the library, which is able to use the macro in its own code.
.executableTarget(name: "TestCasesMacroClient", dependencies: ["TestCasesMacro"]),
.executableTarget(name: "TestCaseKitClient", dependencies: ["TestCaseKit"]),

// A test target used to develop the macro implementation.
.testTarget(
name: "TestCasesMacroTests",
name: "TestCaseKitTests",
dependencies: [
"TestCasesMacroMacros",
"TestCaseKitMacros",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
]
),
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# TestCaseKit
TestCaseKit is a Swift Macro that lets you create test cases with arguments, without needing to create separate tests for each set of arguments.

# Example
```swift
@testCase(3, 1, 4)
@testCase(5, 3, 8)
@testCase(6, 12, 18)
func testSumNumbers(_ lhs: Int, _ rhs: Int, _ result: Int) {
XCTAssertEqual(lhs + rhs, result)
}

@testCase("1", "1", "11")
@testCase("5", "3", "53")
@testCase("6", "12", "612")
func testConcatStrings(_ lhs: String, _ rhs: String, _ result: String) {
XCTAssertEqual(lhs + rhs, result)
}

@testCase(nil as String?)
func testOptional(_ x: String?) {
XCTAssertNil(x)
}
```

# Usage

To use TestCaseKit, simply import the library into your test project and add the `@testCase` attribute to your test function. The @testCase attribute takes a variable number of arguments, which will be passed to the test case as arguments.

### Fully integrated in Xcode
<img width="732" alt="Screenshot 2023-10-26 at 22 30 00" src="https://github.com/alimyuz/Swift-TestCasesMacro/assets/45521753/19ce31da-3b15-4b17-b11f-480247b8ca5b">
<img width="237" alt="Screenshot 2023-10-26 at 22 30 28" src="https://github.com/alimyuz/Swift-TestCasesMacro/assets/45521753/795c2774-b757-4260-a958-42959e6c0175">



# Note
- Currently the external parameter name in test functions should be ommitted for the attribute to work (see [Example above](#Example)).
- Do not pass nil without specifying the desired argument type.
```swift
@testCase(nil) // won't work
@testCase(nil as String?) // will work
```
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@attached(peer, names: arbitrary)
public macro testCase<each T>(_: repeat each T) = #externalMacro(module: "TestCasesMacroMacros", type: "TestCaseMacro")
public macro testCase<each T>(_: repeat each T) = #externalMacro(module: "TestCaseKitMacros", type: "TestCaseMacro")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TestCasesMacro
import TestCaseKit


class Example {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SwiftSyntaxMacros
import OSLog

extension String : Error { }

public struct TestCaseMacro: PeerMacro {

public static func expansion<Context, Declaration>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import XCTest
import TestCasesMacroMacros
import TestCasesMacro
import TestCaseKitMacros

let testMacros: [String: Macro.Type] = [
"testCase": TestCaseMacro.self,
]

final class TestCasesMacroTests: XCTestCase {

func testMacroSingleIntArgument() {
assertMacroExpansion(
"""
Expand Down Expand Up @@ -213,23 +213,4 @@ final class TestCasesMacroTests: XCTestCase {
macros: testMacros
)
}

@testCase(3, 1, 4)
@testCase(5, 3, 8)
@testCase(6, 12, 18)
func testRealCodeMacroSum(_ lhs: Int, _ rhs: Int, _ result: Int) {
XCTAssertEqual(lhs + rhs, result)
}

@testCase("1", "1", "11")
@testCase("5", "3", "53")
@testCase("6", "12", "612")
func testRealCodeMacroSumStrings(_ lhs: String, _ rhs: String, _ result: String) {
XCTAssertEqual(lhs + rhs, result)
}

@testCase(nil as String?)
func testRealCodeMacroOptional(_ x: String?) {
XCTAssertNil(x)
}
}
31 changes: 31 additions & 0 deletions Tests/TestCaseKitTests/TestCasesRealCodeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// TestCasesRealCodeTests.swift
//
//
// Created by Alim Yuzbashev on 26.10.2023.
//

import XCTest
import TestCaseKit

final class TestCasesRealCodeTests: XCTestCase {

@testCase(3, 1, 4)
@testCase(5, 3, 8)
@testCase(6, 12, 18)
func testRealCodeMacroSum(_ lhs: Int, _ rhs: Int, _ result: Int) {
XCTAssertEqual(lhs + rhs, result)
}

@testCase("1", "1", "11")
@testCase("5", "3", "53")
@testCase("6", "12", "612")
func testRealCodeMacroSumStrings(_ lhs: String, _ rhs: String, _ result: String) {
XCTAssertEqual(lhs + rhs, result)
}

@testCase(nil as String?)
func testRealCodeMacroOptional(_ x: String?) {
XCTAssertNil(x)
}
}

0 comments on commit 92b576e

Please sign in to comment.