Skip to content

Commit

Permalink
Merge pull request #61 from MetalheadSanya/55-verify-doesnt-fail-the-…
Browse files Browse the repository at this point in the history
…test

Append SwiftMockConfiguration module for correct engine configuration
  • Loading branch information
MetalheadSanya authored Nov 20, 2023
2 parents b7d3aee + e5a7121 commit 0e851a6
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 26 deletions.
8 changes: 8 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ let package = Package(
name: "SwiftMock",
targets: ["SwiftMock"]
),
.library(
name: "SwiftMockConfiguration",
targets: ["SwiftMockConfiguration"]
),
.executable(
name: "SwiftMockClient",
targets: ["SwiftMockClient"]
Expand All @@ -37,6 +41,9 @@ let package = Package(
// Library that exposes a macro as part of its API, which is used in client programs.
.target(name: "SwiftMock", dependencies: ["SwiftMockMacros"]),

// Library that contains configuration of engine
.target(name: "SwiftMockConfiguration", dependencies: ["SwiftMock"]),

// A client of the library, which is able to use the macro in its own code.
.executableTarget(name: "SwiftMockClient", dependencies: [
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
Expand All @@ -49,6 +56,7 @@ let package = Package(
dependencies: [
"SwiftMock",
"SwiftMockMacros",
"SwiftMockConfiguration",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
]
),
Expand Down
25 changes: 25 additions & 0 deletions Sources/SwiftMock/Documentation.docc/Usage/Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ public protocol SomeProtocol {

This macro will generate a new public type with the protocol name and the suffix `Mock`. In our example, the type name will be `SomeProtocolMock`.

### Test setup

In test case you should perform some actions for correct working of engine.

First of all you shoud import `SwiftMockConfiguration` module.

You should override `setUp()` and `tearDown()` methods. In `setUp()` method you must set `continueAfterFailure` to false and call `setUp()` function of `SwiftMockConfiguration` module. In `tearDown()` you must call `tearDown()` function of `SwiftMockConfiguration` module.

```swift
final class Tests: XCTestCase {
override func setUp() {
continueAfterFailure = false
SwiftMockConfiguration.setUp()
}

override func tearDown() {
SwiftMockConfiguration.tearDown()
super.tearDown()
}

...
```

> Important: `continueAfterFailure` is important for correct running of test.

### Stubbing basics

Let's add the `getAlbumName()` method to our protocol, which returns the name of the album with the `String` type. And let's move on to writing the test.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftMock/SwiftMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
public macro Mock() = #externalMacro(module: "SwiftMockMacros", type: "MockMacro")

public var testFailureReport: (String) -> Void = { _ in

assertionFailure("Please import SwiftMockConfiguration module and call 'SwiftMockConfiguration.setUp()' in 'setUp()' method of your XCTestCase subclass.")
}

public func cleanUpMock() {
Expand Down
21 changes: 21 additions & 0 deletions Sources/SwiftMockConfiguration/Configuration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Configuration.swift
//
//
// Created by Alexandr Zalutskiy on 20/11/2023.
//

import SwiftMock
import XCTest

public enum SwiftMockConfiguration {
public static func setUp() {
testFailureReport = {
XCTFail($0)
}
}

public static func tearDown() {
cleanUpMock()
}
}
7 changes: 3 additions & 4 deletions Tests/SwiftMockTests/ArgumentMatcherTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftMock
import SwiftMockConfiguration
import XCTest

@Mock
Expand All @@ -9,13 +10,11 @@ protocol ArgumentMatcherTestProtocol {
final class ArgumentMatcherTests: XCTestCase {
override func setUp() {
continueAfterFailure = false
testFailureReport = { message in
XCTFail(message)
}
SwiftMockConfiguration.setUp()
}

override func tearDown() {
cleanUpMock()
SwiftMockConfiguration.tearDown()
super.tearDown()
}

Expand Down
7 changes: 3 additions & 4 deletions Tests/SwiftMockTests/AssociatedTypeStubbingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation
import SwiftMock
import SwiftMockConfiguration
import XCTest

@Mock
Expand All @@ -26,13 +27,11 @@ final class AssociatedTypeStubbingTests: XCTestCase {
override func setUp() {
super.setUp()
continueAfterFailure = false
testFailureReport = { message in
XCTFail(message)
}
SwiftMockConfiguration.setUp()
}

override func tearDown() {
cleanUpMock()
SwiftMockConfiguration.tearDown()
super.tearDown()
}

Expand Down
7 changes: 3 additions & 4 deletions Tests/SwiftMockTests/InOrderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@

import Foundation
import SwiftMock
import SwiftMockConfiguration
import XCTest

final class InOrderTests: XCTestCase {
override func setUp() {
super.setUp()
continueAfterFailure = false
testFailureReport = { message in
XCTFail(message)
}
SwiftMockConfiguration.setUp()
}

override func tearDown() {
cleanUpMock()
SwiftMockConfiguration.tearDown()
super.tearDown()
}

Expand Down
10 changes: 7 additions & 3 deletions Tests/SwiftMockTests/MethodStubbingTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftMock
import SwiftMockConfiguration
import XCTest

enum CustomError: Error {
Expand Down Expand Up @@ -63,9 +64,12 @@ protocol MethodProtocol {
final class MethodStubbingTests: XCTestCase {
override func setUp() {
continueAfterFailure = false
testFailureReport = { message in
XCTFail(message)
}
SwiftMockConfiguration.setUp()
}

override func tearDown() {
SwiftMockConfiguration.tearDown()
super.tearDown()
}

func testEmptyProtocol() throws {
Expand Down
9 changes: 6 additions & 3 deletions Tests/SwiftMockTests/PropertyStubbingTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftMock
import SwiftMockConfiguration
import XCTest

@Mock
Expand Down Expand Up @@ -29,9 +30,11 @@ protocol AsyncThrowsGetPropertyProtocol {
final class PropertyStubbingTests: XCTestCase {
override func setUp() {
continueAfterFailure = false
testFailureReport = { message in
XCTFail(message)
}
SwiftMockConfiguration.setUp()
}

override func tearDown() {
SwiftMockConfiguration.tearDown()
}

func testGetProperty() {
Expand Down
7 changes: 3 additions & 4 deletions Tests/SwiftMockTests/SubscriptStubbingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import SwiftMock
import SwiftMockConfiguration
import XCTest

@Mock
Expand All @@ -18,13 +19,11 @@ protocol SubscriptProtocol {
final class SubscriptStubbingTests: XCTestCase {
override func setUp() {
continueAfterFailure = false
testFailureReport = { message in
XCTFail(message)
}
SwiftMockConfiguration.setUp()
}

override func tearDown() {
cleanUpMock()
SwiftMockConfiguration.tearDown()
super.tearDown()
}

Expand Down
10 changes: 7 additions & 3 deletions Tests/SwiftMockTests/VerifyTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftMock
import SwiftMockConfiguration
import XCTest

@Mock
Expand All @@ -9,9 +10,12 @@ protocol VerifyTestsProtocol {
final class VerifyTests: XCTestCase {
override func setUp() {
continueAfterFailure = false
testFailureReport = { message in
XCTFail(message)
}
SwiftMockConfiguration.setUp()
}

override func tearDown() {
SwiftMockConfiguration.tearDown()
super.tearDown()
}

func testDefaultsCountDefaultArguments() {
Expand Down

0 comments on commit 0e851a6

Please sign in to comment.