Skip to content

Commit

Permalink
Migrate deprecated Nimble types (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbergeron authored and gh-action-runner committed Apr 1, 2024
1 parent 022bbc4 commit 550bf29
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 85 deletions.
10 changes: 5 additions & 5 deletions Tests/ApolloCodegenTests/CodeGenIR/IRFieldCollectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -558,18 +558,18 @@ class IRFieldCollectorTests: XCTestCase {
/// MARK: - Custom Matchers
func equal(
_ expected: ReferencedFields
) -> Nimble.Predicate<ReferencedFields> {
return Predicate.define { actual in
) -> Nimble.Matcher<ReferencedFields> {
return Matcher.define { actual in
let message: ExpectationMessage = .expectedActualValueTo("have fields equal to \(expected)")

guard let actual = try actual.evaluate(),
expected.count == actual.count else {
return PredicateResult(status: .fail, message: message.appended(details: "Fields Did Not Match!"))
return MatcherResult(status: .fail, message: message.appended(details: "Fields Did Not Match!"))
}

for (index, field) in zip(expected, actual).enumerated() {
guard field.0.0 == field.1.0, field.0.1 == field.1.1 else {
return PredicateResult(
return MatcherResult(
status: .fail,
message: message.appended(
details: "Expected fields[\(index)] to equal \(field.0), got \(field.1)."
Expand All @@ -578,7 +578,7 @@ class IRFieldCollectorTests: XCTestCase {
}
}

return PredicateResult(status: .matches, message: message)
return MatcherResult(status: .matches, message: message)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,29 +270,29 @@ extension IR.Entity.Location.FieldComponent {

fileprivate func match(
_ expectedValue: [IR.Entity.Location: IR.Entity]
) -> Nimble.Predicate<[IR.Entity.Location: IR.Entity]> {
return Predicate.define { actual in
) -> Nimble.Matcher<[IR.Entity.Location: IR.Entity]> {
return Matcher.define { actual in
let message: ExpectationMessage = .expectedActualValueTo("equal \(expectedValue)")
guard var actual = try actual.evaluate(),
actual.count == expectedValue.count else {
return PredicateResult(status: .fail, message: message)
return MatcherResult(status: .fail, message: message)
}

for expected in expectedValue {
guard let actual = actual.removeValue(forKey: expected.key) else {
return PredicateResult(status: .fail, message: message)
return MatcherResult(status: .fail, message: message)
}

if expected.value.rootTypePath != actual.rootTypePath ||
expected.value.location != actual.location {
return PredicateResult(status: .fail, message: message)
return MatcherResult(status: .fail, message: message)
}
}

guard actual.isEmpty else {
return PredicateResult(status: .fail, message: message)
return MatcherResult(status: .fail, message: message)
}

return PredicateResult(status: .matches, message: message)
return MatcherResult(status: .matches, message: message)
}
}
76 changes: 38 additions & 38 deletions Tests/ApolloCodegenTests/TestHelpers/IRMatchers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ typealias SelectionMatcherTuple = (fields: [ShallowFieldMatcher],

// MARK - Custom Matchers

func beEmpty<T: SelectionShallowMatchable>() -> Nimble.Predicate<T> {
return Predicate.simple("be empty") { actualExpression in
func beEmpty<T: SelectionShallowMatchable>() -> Nimble.Matcher<T> {
return Matcher.simple("be empty") { actualExpression in
guard let actual = try actualExpression.evaluate() else { return .fail }
return PredicateStatus(bool: actual.isEmpty)
return MatcherStatus(bool: actual.isEmpty)
}
}

Expand All @@ -45,7 +45,7 @@ func beEmpty<T: SelectionShallowMatchable>() -> Nimble.Predicate<T> {
/// checking the `MergedSelections` without having to mock out the entire nested selection sets.
func shallowlyMatch<T: SelectionShallowMatchable>(
_ expectedValue: SelectionMatcherTuple
) -> Nimble.Predicate<T> {
) -> Nimble.Matcher<T> {
return satisfyAllOf([
shallowlyMatch(expectedValue.fields).mappingActualTo { $0?.fields.values },
shallowlyMatch(expectedValue.typeCases).mappingActualTo { $0?.inlineFragments.values.map(\.selectionSet) },
Expand All @@ -55,7 +55,7 @@ func shallowlyMatch<T: SelectionShallowMatchable>(

func shallowlyMatch<T: SelectionShallowMatchable>(
_ expectedValue: [ShallowSelectionMatcher]
) -> Nimble.Predicate<T> {
) -> Nimble.Matcher<T> {
var expectedFields: [ShallowFieldMatcher] = []
var expectedTypeCases: [ShallowInlineFragmentMatcher] = []
var expectedFragments: [ShallowFragmentSpreadMatcher] = []
Expand Down Expand Up @@ -96,13 +96,13 @@ struct SelectionsMatcher {

func shallowlyMatch(
_ expectedValue: SelectionsMatcher
) -> Nimble.Predicate<SelectionSetTestWrapper> {
let directPredicate: Nimble.Predicate<IR.DirectSelections.ReadOnly> = expectedValue.direct == nil
) -> Nimble.Matcher<SelectionSetTestWrapper> {
let directMatcher: Nimble.Matcher<IR.DirectSelections.ReadOnly> = expectedValue.direct == nil
? beNil()
: shallowlyMatch(expectedValue.direct!)

var matchers: [Nimble.Predicate<SelectionSetTestWrapper>] = [
directPredicate.mappingActualTo { $0?.computed.direct },
var matchers: [Nimble.Matcher<SelectionSetTestWrapper>] = [
directMatcher.mappingActualTo { $0?.computed.direct },
]

if !expectedValue.ignoreMergedSelections {
Expand Down Expand Up @@ -175,18 +175,18 @@ struct SelectionSetMatcher {

func shallowlyMatch(
_ expectedValue: SelectionSetMatcher
) -> Nimble.Predicate<SelectionSetTestWrapper> {
) -> Nimble.Matcher<SelectionSetTestWrapper> {
let expectedInclusionConditions = IR.InclusionConditions.allOf(
expectedValue.inclusionConditions ?? []
).conditions

let inclusionPredicate: Nimble.Predicate<IR.InclusionConditions> = expectedInclusionConditions == nil
let inclusionMatcher: Nimble.Matcher<IR.InclusionConditions> = expectedInclusionConditions == nil
? beNil()
: equal(expectedInclusionConditions!)

return satisfyAllOf([
equal(expectedValue.parentType).mappingActualTo { $0?.parentType },
inclusionPredicate.mappingActualTo { $0?.inclusionConditions },
inclusionMatcher.mappingActualTo { $0?.inclusionConditions },
shallowlyMatch(expectedValue.selections)
])
}
Expand Down Expand Up @@ -318,16 +318,16 @@ public struct ShallowFieldMatcher: Equatable, CustomDebugStringConvertible {

public func shallowlyMatch<T: Collection>(
_ expectedValue: [ShallowFieldMatcher]
) -> Nimble.Predicate<T> where T.Element == IR.Field {
return Predicate.define { actual in
) -> Nimble.Matcher<T> where T.Element == IR.Field {
return Matcher.define { actual in
return shallowlyMatch(expected: expectedValue, actual: try actual.evaluate())
}
}

public func shallowlyMatch<T: Collection>(
_ expectedValue: [ShallowSelectionMatcher]
) -> Nimble.Predicate<T> where T.Element == IR.Field {
return Predicate.define { actual in
) -> Nimble.Matcher<T> where T.Element == IR.Field {
return Matcher.define { actual in
let expectedAsFields: [ShallowFieldMatcher] = try expectedValue.map {
guard case let .shallowField(field) = $0 else {
throw TestError("Selection \($0) is not a field!")
Expand All @@ -341,17 +341,17 @@ public func shallowlyMatch<T: Collection>(
public func shallowlyMatch<T: Collection>(
expected: [ShallowFieldMatcher],
actual: T?
) -> PredicateResult where T.Element == IR.Field {
) -> MatcherResult where T.Element == IR.Field {
let message: ExpectationMessage = .expectedActualValueTo("have fields equal to \(expected)")

guard let actual = actual,
expected.count == actual.count else {
return PredicateResult(status: .fail, message: message.appended(details: "Fields Did Not Match!"))
return MatcherResult(status: .fail, message: message.appended(details: "Fields Did Not Match!"))
}

for (index, field) in zip(expected, actual).enumerated() {
guard shallowlyMatch(expected: field.0, actual: field.1) else {
return PredicateResult(
return MatcherResult(
status: .fail,
message: message.appended(
details: "Expected fields[\(index)] to equal \(field.0), got \(field.1)."
Expand All @@ -360,7 +360,7 @@ public func shallowlyMatch<T: Collection>(
}
}

return PredicateResult(status: .matches, message: message)
return MatcherResult(status: .matches, message: message)
}

fileprivate func shallowlyMatch(expected: ShallowFieldMatcher, actual: IR.Field) -> Bool {
Expand Down Expand Up @@ -420,25 +420,25 @@ public struct ShallowInlineFragmentMatcher: Equatable, CustomDebugStringConverti

public func shallowlyMatch<T: Collection>(
_ expectedValue: [ShallowInlineFragmentMatcher]
) -> Nimble.Predicate<T> where T.Element == IR.SelectionSet {
return Predicate.define { actual in
) -> Nimble.Matcher<T> where T.Element == IR.SelectionSet {
return Matcher.define { actual in
return shallowlyMatch(expected: expectedValue, actual: try actual.evaluate())
}
}

fileprivate func shallowlyMatch<T: Collection>(
expected: [ShallowInlineFragmentMatcher],
actual: T?
) -> PredicateResult where T.Element == IR.SelectionSet {
) -> MatcherResult where T.Element == IR.SelectionSet {
let message: ExpectationMessage = .expectedActualValueTo("have typeCases equal to \(expected)")
guard let actual = actual,
expected.count == actual.count else {
return PredicateResult(status: .fail, message: message.appended(details: "Inline Fragments Did Not Match!"))
return MatcherResult(status: .fail, message: message.appended(details: "Inline Fragments Did Not Match!"))
}

for (index, typeCase) in zip(expected, actual).enumerated() {
guard shallowlyMatch(expected: typeCase.0, actual: typeCase.1) else {
return PredicateResult(
return MatcherResult(
status: .fail,
message: message.appended(
details: "Expected typeCases[\(index)] to equal \(typeCase.0), got \(typeCase.1)."
Expand All @@ -447,7 +447,7 @@ fileprivate func shallowlyMatch<T: Collection>(
}
}

return PredicateResult(status: .matches, message: message)
return MatcherResult(status: .matches, message: message)
}

fileprivate func shallowlyMatch(
Expand Down Expand Up @@ -536,33 +536,33 @@ public struct ShallowFragmentSpreadMatcher: Equatable, CustomDebugStringConverti

public func shallowlyMatch<T: Collection>(
_ expectedValue: [ShallowFragmentSpreadMatcher]
) -> Nimble.Predicate<T> where T.Element == IR.NamedFragmentSpread {
return Predicate.define { actual in
) -> Nimble.Matcher<T> where T.Element == IR.NamedFragmentSpread {
return Matcher.define { actual in
return shallowlyMatch(expected: expectedValue, actual: try actual.evaluate())
}
}

public func shallowlyMatch<T: Collection>(
_ expectedValue: [CompilationResult.FragmentDefinition]
) -> Nimble.Predicate<T> where T.Element == IR.NamedFragmentSpread {
return Predicate.define { actual in
) -> Nimble.Matcher<T> where T.Element == IR.NamedFragmentSpread {
return Matcher.define { actual in
return shallowlyMatch(expected: expectedValue.map { .mock($0) }, actual: try actual.evaluate())
}
}

fileprivate func shallowlyMatch<T: Collection>(
expected: [ShallowFragmentSpreadMatcher],
actual: T?
) -> PredicateResult where T.Element == IR.NamedFragmentSpread {
) -> MatcherResult where T.Element == IR.NamedFragmentSpread {
let message: ExpectationMessage = .expectedActualValueTo("have fragments equal to \(expected)")
guard let actual = actual,
expected.count == actual.count else {
return PredicateResult(status: .fail, message: message.appended(details: "Fragments Did Not Match!"))
return MatcherResult(status: .fail, message: message.appended(details: "Fragments Did Not Match!"))
}

for (index, fragment) in zip(expected, actual).enumerated() {
guard shallowlyMatch(expected: fragment.0, actual: fragment.1) else {
return PredicateResult(
return MatcherResult(
status: .fail,
message: message.appended(
details: "Expected fragments[\(index)] to equal \(fragment.0), got \(fragment.1)."
Expand All @@ -571,7 +571,7 @@ fileprivate func shallowlyMatch<T: Collection>(
}
}

return PredicateResult(status: .matches, message: message)
return MatcherResult(status: .matches, message: message)
}

fileprivate func shallowlyMatch(expected: ShallowFragmentSpreadMatcher, actual: IR.NamedFragmentSpread) -> Bool {
Expand All @@ -581,13 +581,13 @@ fileprivate func shallowlyMatch(expected: ShallowFragmentSpreadMatcher, actual:
&& expected.deferCondition == actual.typeInfo.deferCondition
}

// MARK: - Predicate Mapping
// MARK: - Matcher Mapping

extension Nimble.Predicate {
extension Nimble.Matcher {
func mappingActualTo<U>(
_ actualMapper: @escaping ((U?) throws -> T?)
) -> Nimble.Predicate<U> {
Nimble.Predicate<U>.define { (actual: Expression<U>) in
) -> Nimble.Matcher<U> {
Nimble.Matcher<U>.define { (actual: Expression<U>) in
let newActual = actual.cast(actualMapper)
return try self.satisfies(newActual)
}
Expand Down
20 changes: 10 additions & 10 deletions Tests/ApolloCodegenTests/TestHelpers/LineByLineComparison.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public func equalLineByLine(
_ expectedValue: String,
atLine startLine: Int = 1,
ignoringExtraLines: Bool = false
) -> Nimble.Predicate<String> {
return Predicate.define() { actual in
) -> Nimble.Matcher<String> {
return Matcher.define() { actual in
let actualString = try actual.evaluate()

guard let actualLines = actualString?.lines(startingAt: startLine) else {
Expand All @@ -35,7 +35,7 @@ public func equalLineByLine(
let actualLine = actualLines[index]
guard let expectedLine = expectedLinesBuffer.popLast() else {
if ignoringExtraLines {
return PredicateResult(
return MatcherResult(
status: .matches,
message: .expectedTo("be equal")
)
Expand All @@ -56,13 +56,13 @@ public func equalLineByLine(
}

guard expectedLinesBuffer.isEmpty else {
return PredicateResult(
return MatcherResult(
status: .fail,
message: .fail("Expected \(expectedLines.count), actual ended at line \(actualLines.count).")
)
}

return PredicateResult(
return MatcherResult(
status: .matches,
message: .expectedTo("be equal")
)
Expand All @@ -72,12 +72,12 @@ public func equalLineByLine(
fileprivate func PrettyPrintedFailureResult(
actual: String?,
message: ExpectationMessage
) -> PredicateResult {
) -> MatcherResult {
if let actual = actual {
print ("Actual Document:")
print(actual)
}
return PredicateResult(
return MatcherResult(
status: .fail,
message: message
)
Expand All @@ -102,10 +102,10 @@ extension String {
public func equalLineByLine(
toFileAt expectedFileURL: URL,
trimmingImports trimImports: Bool = false
) -> Nimble.Predicate<String> {
return Predicate.define() { actual in
) -> Nimble.Matcher<String> {
return Matcher.define() { actual in
guard ApolloFileManager.default.doesFileExist(atPath: expectedFileURL.path) else {
return PredicateResult(
return MatcherResult(
status: .fail,
message: .fail("File not found at \(expectedFileURL)")
)
Expand Down
8 changes: 4 additions & 4 deletions Tests/ApolloTests/JSONValueMatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import Nimble
import Apollo
import ApolloAPI

public func equalJSONValue(_ expectedValue: JSONEncodable?) -> Predicate<JSONEncodable> {
return Predicate { actual in
public func equalJSONValue(_ expectedValue: JSONEncodable?) -> Matcher<JSONEncodable> {
return Matcher { actual in
let msg = ExpectationMessage.expectedActualValueTo("equal <\(stringify(expectedValue))>")
if let actualValue = try actual.evaluate(), let expectedValue = expectedValue {
return PredicateResult(
return MatcherResult(
bool: actualValue._jsonValue == expectedValue._jsonValue,
message: msg
)
} else {
return PredicateResult(
return MatcherResult(
status: .fail,
message: msg.appendedBeNilHint()
)
Expand Down
Loading

0 comments on commit 550bf29

Please sign in to comment.