diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml new file mode 100644 index 0000000..7219c22 --- /dev/null +++ b/.github/workflows/swift.yml @@ -0,0 +1,22 @@ +name: Swift + +on: + push: + branches: [ "develop" ] + pull_request: + branches: [ "develop" ] + +jobs: + build: + runs-on: macos-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup Swift + uses: fwal/setup-swift@v2 + with: + swift-version: '6.0.0' + - name: Build + run: swift build -v + - name: Run tests + run: swift test -v diff --git a/README.md b/README.md index 2b9dce5..9d0b5d3 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # EasyMock -**Simulate. Test. Verify.** -A lightweight and expressive library for unit testing in Swift — supporting `async/await`, delays, error simulation, and call tracking. +[![Swift](https://github.com/EasyPackages/EasyMock/actions/workflows/swift.yml/badge.svg)](https://github.com/EasyPackages/EasyMock/actions/workflows/swift.yml) + +## Simulate. Test. Verify ---- +A lightweight and expressive library for unit testing in Swift — supporting `async/await`, delays, error simulation, and call tracking. ## Overview @@ -19,8 +20,6 @@ It’s ideal for testing interactions, async flows, and error handling — witho - ❗ Error simulation (`throw`) - 🧪 Designed for clarity in unit tests ---- - ## Why Use EasyMock? ### Replace This: @@ -61,8 +60,6 @@ struct AuthenticatorMock: Authenticator { } ``` ---- - ## Installation ### Using Swift Package Manager @@ -87,19 +84,6 @@ In your target: ) ``` ---- - -## Library Structure - -| Type | Name | Purpose | -|--------------------|-----------------------|----------------------------------------| -| Mock | `Mock` | Basic sync mock | -| AsyncMock | `AsyncMock` | Mock with `async/await` | -| ThrowableMock | `ThrowableMock` | Mock that can simulate thrown errors | -| AsyncThrowableMock | `AsyncThrowableMock` | Full-featured async mock with error and delay support | - ---- - ## Examples ### Basic Mock @@ -144,16 +128,12 @@ let value = try await asyncMock.synchronize("ping") XCTAssertEqual(value, 42) ``` ---- - ## Supported Platforms - iOS 13+ - macOS 10.15+ - Swift 5.9+ ---- - ## Author Created by [Paolo Prodossimo Lopes](https://github.com/PaoloProdossimoLopes) diff --git a/Tests/EasyMockTests/AsyncMockTests.swift b/Tests/EasyMockTests/AsyncMockTests.swift index 15cb33a..be6c133 100644 --- a/Tests/EasyMockTests/AsyncMockTests.swift +++ b/Tests/EasyMockTests/AsyncMockTests.swift @@ -154,6 +154,7 @@ struct AsyncMockTests { @Test("should wait for the configured delay before completing") func testsynchronizeAppliesDelay() async { let delay = 0.2 + let tolerance = 0.05 let clock = ContinuousClock() let start = clock.now let sut = makeSut() @@ -162,8 +163,13 @@ struct AsyncMockTests { await sut.synchronize("any-input") let duration = start.duration(to: clock.now) - #expect(duration >= .seconds(delay)) - #expect(duration <= .seconds(delay + (delay * 0.1))) + let minimumExpected = delay + let maximumExpected = delay + tolerance + + #expect( + duration >= .seconds(minimumExpected) && duration <= .seconds(maximumExpected), + "Duration (\(duration)) was not within the expected range (\(minimumExpected)s to \(maximumExpected)s)" + ) } @Test("should not trigger observers when mocking delay") diff --git a/Tests/EasyMockTests/AsyncThrowableMockTests.swift b/Tests/EasyMockTests/AsyncThrowableMockTests.swift index 2d4778a..1100935 100644 --- a/Tests/EasyMockTests/AsyncThrowableMockTests.swift +++ b/Tests/EasyMockTests/AsyncThrowableMockTests.swift @@ -192,6 +192,7 @@ struct AsyncThrowableMockTests { @Test("should wait for the configured delay before completing") func testsynchronizeAppliesDelay() async throws { let delay = 0.2 + let tolerance = 0.05 let clock = ContinuousClock() let start = clock.now let sut = makeSut() @@ -200,8 +201,13 @@ struct AsyncThrowableMockTests { try await sut.synchronize("any-input") let duration = start.duration(to: clock.now) - #expect(duration >= .seconds(delay)) - #expect(duration <= .seconds(delay + (delay * 0.1))) + let minimumExpected = delay + let maximumExpected = delay + tolerance + + #expect( + duration >= .seconds(minimumExpected) && duration <= .seconds(maximumExpected), + "Duration (\(duration)) was not within the expected range (\(minimumExpected)s to \(maximumExpected)s)" + ) } @Test("should not trigger observers when mocking delay")