Skip to content

Commit

Permalink
fix: Resolves SomethingLike matcher ignoring nested matchers (surpher#93
Browse files Browse the repository at this point in the history
) (surpher#94)

* fix: Resolves SomethingLike matcher ignoring nested matchers (surpher#93)
* fix: Workflow for CI caching on Linux
  • Loading branch information
surpher authored Sep 7, 2022
1 parent b024615 commit 2404e83
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 5 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/build_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
branches:
- 'main'

env:
RUST_TARGET_PATH: pact-reference

jobs:
test:
name: Run on macOS 11
Expand Down Expand Up @@ -57,17 +60,20 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2

- name: Install Protoc
uses: arduino/setup-protoc@v1

- name: "♼ Cache rust binaries"
uses: actions/cache@v3
with:
path: |
${{ env.RUST_TARGET_PATH }}
${{ env.BINARIES_PATH }}
key: build-${{ runner.os }}-rust-${{ hashFiles('**/Package.resolved') }}
key: build-${{ runner.os }}-rust-pactswift-${{ hashFiles('**/Package.resolved') }}
restore-keys: |
build-${{ runner.os }}-rust-pactswift
build-${{ runner.os }}-rust-
- name: Build and Test
- name: "⚗️ Build and Test"
run: |
Scripts/build_test_linux
Expand Down
2 changes: 1 addition & 1 deletion Scripts/build_test_linux
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if [[ "$CI" == true ]]; then
echo "ℹ️ Changing location to ${LIBPACT_FFI_DIR}"
cd $LIBPACT_FFI_DIR
git fetch --all --tags
git checkout tags/libpact_ffi-v0.3.5
git checkout tags/libpact_ffi-v0.3.11
fi

export LD_LIBRARY_PATH="/usr/local/lib"
Expand Down
23 changes: 22 additions & 1 deletion Sources/PactBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,28 @@ private extension PactBuilder {
processedElement = try processMatcher(matcher, at: node)

case let matcher as Matcher.SomethingLike:
processedElement = try processMatcher(matcher, at: node)
// Process the root node
let processedNode = try processMatcher(matcher, at: node)

// Merge any matchers and example generators found in the node's value
switch matcher.value {
case let dict as [String: Any]:
let processedDict = try process(dict, at: node)
processedElement = (
node: AnyEncodable(processedDict.node),
rules: merge(processedNode.rules, with: processedDict.rules),
generators: merge(processedNode.generators, with: processedDict.generators)
)
case let array as [Any]:
let processedArray = try process(array, at: node, isEachLike: false)
processedElement = (
node: AnyEncodable(processedArray.node),
rules: merge(processedNode.rules, with: processedArray.rules),
generators: merge(processedNode.generators, with: processedArray.generators)
)
default:
processedElement = processedNode
}

// MARK: - Process Example generators

Expand Down
66 changes: 66 additions & 0 deletions Tests/Model/PactBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,39 @@ class PactBuilderTests: XCTestCase {
XCTAssertEqual(testResult.match, "type")
}

func testPact_SetsNestedMatchers_in_SomethingLike() throws {
let testBody: Any = [
"body": Matcher.SomethingLike(
[
"refresh_token": Matcher.SomethingLike("xxxxxx"),
"user": Matcher.SomethingLike(
[
"id": Matcher.SomethingLike("xxxxxx-xxxx-xxxx-xxxx-xxxxxxx"),
"email": Matcher.RegexLike(value: "[email protected]", pattern: #"^([-\.\w]+@[-\w]+\.)+[\w-]{2,4}$"#)
]
)
]
)
]

let testPact = prepareTestPact(responseBody: testBody)
let result = try XCTUnwrap(
try JSONDecoder()
.decode(NestedSomethingLikeTestModel.self, from: testPact.data!)
.interactions
.first?
.response
.matchingRules
.body
)

XCTAssertEqual(try XCTUnwrap(result.body.matchers.first).match, "type")
XCTAssertEqual(try XCTUnwrap(result.bodyUser.matchers.first).match, "type")
XCTAssertEqual(try XCTUnwrap(result.bodyUserID.matchers.first).match, "type")
XCTAssertEqual(try XCTUnwrap(result.bodyUserEmail.matchers.first).match, "regex")
XCTAssertEqual(try XCTUnwrap(result.bodyUserEmail.matchers.first).regex, #"^([-\.\w]+@[-\w]+\.)+[\w-]{2,4}$"#)
}

// MARK: - EachLike()

func testPact_SetsDefaultMin_EachLikeMatcher() throws {
Expand Down Expand Up @@ -400,6 +433,39 @@ class PactBuilderTests: XCTestCase {

private extension PactBuilderTests {

struct NestedSomethingLikeTestModel: Decodable {
let interactions: [TestInteractionModel]
struct TestInteractionModel: Decodable {
let response: TestResponseModel
struct TestResponseModel: Decodable {
let matchingRules: TestMatchingRulesModel
struct TestMatchingRulesModel: Decodable {
let body: TestNodeModel
struct TestNodeModel: Decodable {
let body: TestMatchersModel
let bodyUser: TestMatchersModel
let bodyUserID: TestMatchersModel
let bodyUserEmail: TestMatchersModel
enum CodingKeys: String, CodingKey {
case body = "$.body"
case bodyUser = "$.body.user"
case bodyUserID = "$.body.user.id"
case bodyUserEmail = "$.body.user.email"
}
struct TestMatchersModel: Decodable {
let matchers: [TestTypeModel]
let combine: String?
struct TestTypeModel: Decodable {
let match: String
let regex: String?
}
}
}
}
}
}
}

// This test model is tightly coupled with the SomethingLike Matcher for the purpouse of these tests
struct GenericLikeTestModel: Decodable {
let interactions: [TestInteractionModel]
Expand Down

0 comments on commit 2404e83

Please sign in to comment.