Skip to content

Commit 44303b6

Browse files
committed
test: fix + refactor
Signed-off-by: Allain Magyar <[email protected]>
1 parent a44c780 commit 44303b6

File tree

84 files changed

+4944
-2326
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+4944
-2326
lines changed

E2E/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Target
2+
properties.plist
3+
.build
4+
.vscode

E2E/Package.swift

+23-10
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,38 @@ let package = Package(
77
name: "e2e",
88
platforms: [.iOS(.v15), .macOS(.v12)],
99
products: [
10+
.library(
11+
name: "TestFramework",
12+
targets: ["TestFramework"]),
1013
],
1114
dependencies: [
12-
.package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "1.2.1")),
13-
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "1.3.2")),
14-
.package(url: "https://github.com/apple/swift-openapi-urlsession", .upToNextMinor(from: "1.0.1")),
15-
.package(url: "https://github.com/nschum/SwiftHamcrest", .upToNextMajor(from: "2.2.1")),
16-
.package(path: "../")
15+
.package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "1.7.0")),
16+
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "1.8.0")),
17+
.package(url: "https://github.com/apple/swift-openapi-urlsession", .upToNextMinor(from: "1.0.2")),
18+
.package(url: "https://github.com/nschum/SwiftHamcrest", exact: "2.2.1"),
19+
.package(name: "edge-agent", path: "../")
1720
],
1821
targets: [
22+
.target(
23+
name: "TestFramework",
24+
dependencies: [
25+
.product(name: "SwiftHamcrest", package: "SwiftHamcrest")
26+
],
27+
path: "TestFramework",
28+
resources: [
29+
.copy("Resources")
30+
]
31+
),
1932
.testTarget(
20-
name: "e2e",
33+
name: "e2e-tests",
2134
dependencies: [
22-
.product(name: "EdgeAgent", package: "atala-prism-wallet-sdk-swift"),
23-
.product(name: "Domain", package: "atala-prism-wallet-sdk-swift"),
35+
.target(name: "TestFramework"),
36+
.product(name: "EdgeAgent", package: "edge-agent"),
37+
.product(name: "Domain", package: "edge-agent"),
2438
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
2539
.product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"),
26-
.product(name: "SwiftHamcrest", package: "SwiftHamcrest")
2740
],
28-
path: "e2eTests",
41+
path: "Tests",
2942
resources: [
3043
.copy("Resources")
3144
],

E2E/TestFramework/BDD/Feature.swift

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Foundation
2+
import XCTest
3+
import SwiftHamcrest
4+
5+
open class Feature: XCTestCase {
6+
let id: String = UUID().uuidString
7+
open var currentScenario: Scenario? = nil
8+
9+
open func title() -> String {
10+
fatalError("Set feature title")
11+
}
12+
13+
open func description() -> String {
14+
return ""
15+
}
16+
17+
/// our lifecycle starts after xctest is ending
18+
public override func tearDown() async throws {
19+
try await run()
20+
self.currentScenario = nil
21+
try await super.tearDown()
22+
}
23+
24+
public override class func tearDown() {
25+
if (TestConfiguration.started) {
26+
let semaphore = DispatchSemaphore(value: 0)
27+
Task.detached {
28+
try await TestConfiguration.shared().endCurrentFeature()
29+
semaphore.signal()
30+
}
31+
semaphore.wait()
32+
}
33+
super.tearDown()
34+
}
35+
36+
func run() async throws {
37+
// check if we have the scenario
38+
if (currentScenario == nil) {
39+
throw XCTSkip("""
40+
To run the feature you have to setup the scenario for each test case.
41+
Usage:
42+
func testMyScenario() async throws {
43+
scenario = Scenario("description")
44+
.given // ...
45+
}
46+
""")
47+
}
48+
49+
if (currentScenario!.disabled) {
50+
throw XCTSkip("Scenario [\(currentScenario!.title)] is disabled")
51+
}
52+
53+
try await TestConfiguration.setUpInstance()
54+
55+
if (currentScenario! is ParameterizedScenario) {
56+
let parameterizedScenario = currentScenario! as! ParameterizedScenario
57+
for scenario in parameterizedScenario.build() {
58+
try await TestConfiguration.shared().run(self, scenario)
59+
}
60+
} else {
61+
try await TestConfiguration.shared().run(self, currentScenario!)
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Foundation
2+
import XCTest
3+
4+
public class ParameterizedScenario: Scenario {
5+
public var parameters: [[String: String]] = [[:]]
6+
7+
public func parameters(_ parameters: [[String: String]]) -> Scenario {
8+
self.parameters = parameters
9+
return self
10+
}
11+
12+
public func build() -> [Scenario] {
13+
var scenarios: [Scenario] = []
14+
15+
parameters.forEach { parameters in
16+
let scenario = Scenario(replace(line: self.title, parameters: parameters))
17+
scenario.steps = self.steps.map { step in
18+
let newStep = ConcreteStep()
19+
newStep.context = step.context
20+
newStep.action = replace(line: step.action, parameters: parameters)
21+
return newStep
22+
}
23+
scenarios.append(scenario)
24+
}
25+
26+
return scenarios
27+
}
28+
29+
private func replace(line: String, parameters: [String: String]) -> String {
30+
var line = line
31+
for (placeholder, value) in parameters {
32+
line = line.replacingOccurrences(of: "<\(placeholder)>", with: value)
33+
}
34+
return line
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import Foundation
22
import XCTest
33

4-
class Scenario {
4+
public class Scenario {
55
let id = UUID().uuidString
66
var title: String
77
var steps: [ConcreteStep] = []
88
var pass: Bool = false
99
var error: Error? = nil
10+
var disabled: Bool = false
1011

1112
private var lastContext: String = ""
1213

13-
init(_ title: String) {
14+
public init(_ title: String) {
1415
self.title = title
1516
}
1617

17-
func fail(file: StaticString?, line: UInt?, message: String) {
18+
public func fail(file: StaticString?, line: UInt?, message: String) {
1819
if (file != nil) {
1920
XCTFail(message, file: file!, line: line!)
2021
} else {
@@ -29,35 +30,40 @@ class Scenario {
2930
steps.append(stepInstance)
3031
}
3132

32-
func given(_ step: String) -> Scenario {
33+
public func given(_ step: String) -> Scenario {
3334
lastContext = "Given"
3435
addStep(step)
3536
return self
3637
}
3738

38-
func when(_ step: String) -> Scenario {
39+
public func when(_ step: String) -> Scenario {
3940
lastContext = "When"
4041
addStep(step)
4142
return self
4243
}
4344

44-
func then(_ step: String) -> Scenario {
45+
public func then(_ step: String) -> Scenario {
4546
lastContext = "Then"
4647
addStep(step)
4748
return self
4849
}
4950

50-
func but(_ step: String) -> Scenario {
51+
public func but(_ step: String) -> Scenario {
5152
lastContext = "But"
5253
addStep(step)
5354
return self
5455
}
5556

56-
func and(_ step: String) -> Scenario {
57+
public func and(_ step: String) -> Scenario {
5758
if (lastContext.isEmpty) {
5859
fatalError("Trying to add an [and] step without previous context.")
5960
}
6061
addStep(step)
6162
return self
6263
}
64+
65+
public func disable() -> Scenario {
66+
self.disabled = true
67+
return self
68+
}
6369
}

E2E/e2eTests/TestFramework/BDD/Step.swift renamed to E2E/TestFramework/BDD/Step.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import Foundation
33

44
@propertyWrapper
5-
class Step<T> {
5+
public class Step<T> {
66
let file: StaticString
77
let line: UInt
88
var definition: String
9-
var wrappedValue: (T) async throws -> ()
9+
public var wrappedValue: (T) async throws -> ()
1010

11-
init(wrappedValue: @escaping (T) async throws -> (), _ definition: String, file: StaticString = #file, line: UInt = #line) {
11+
public init(wrappedValue: @escaping (T) async throws -> (), _ definition: String, file: StaticString = #file, line: UInt = #line) {
1212
self.file = file
1313
self.line = line
1414
self.wrappedValue = wrappedValue

E2E/e2eTests/TestFramework/Configuration/ITestConfiguration.swift renamed to E2E/TestFramework/Configuration/ITestConfiguration.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
protocol ITestConfiguration {
3+
public protocol ITestConfiguration {
44
static var shared: () -> ITestConfiguration {get}
55
static func createInstance() -> ITestConfiguration
66

E2E/e2eTests/TestFramework/Configuration/Phase.swift renamed to E2E/TestFramework/Configuration/Phase.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
enum Phase {
3+
public enum Phase {
44
case AFTER_FEATURES
55
case BEFORE_FEATURE
66
case AFTER_FEATURE

0 commit comments

Comments
 (0)