Skip to content

Commit

Permalink
Make sure file extension mocks are matching correctly (#95)
Browse files Browse the repository at this point in the history
* Make sure file extension mocks are matching correctly

* Remove whitespace
  • Loading branch information
AvdLee authored Apr 22, 2021
1 parent 05b097b commit d245ac3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
35 changes: 35 additions & 0 deletions MockerTests/MockTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// MockTests.swift
//
//
// Created by Antoine van der Lee on 21/04/2021.
//

import Foundation
import XCTest
@testable import Mocker

final class MockTests: XCTestCase {
override func setUp() {
super.setUp()
Mocker.mode = .optout
}

override func tearDown() {
Mocker.removeAll()
Mocker.mode = .optout
super.tearDown()
}

/// It should match two file extension mocks correctly.
func testFileExtensionMocksComparing() {
let mock200 = Mock(fileExtensions: "png", dataType: .imagePNG, statusCode: 200, data: [.put: Data()])
let secondMock200 = Mock(fileExtensions: "png", dataType: .imagePNG, statusCode: 200, data: [.put: Data()])
let mock400 = Mock(fileExtensions: "png", dataType: .imagePNG, statusCode: 400, data: [.put: Data()])
let mockJPEG = Mock(fileExtensions: "jpeg", dataType: .imagePNG, statusCode: 200, data: [.put: Data()])

XCTAssertEqual(mock200, secondMock200)
XCTAssertEqual(mock200, mock400)
XCTAssertNotEqual(mock200, mockJPEG)
}
}
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let package = Package(name: "Mocker",
],
targets: [
// dev .target(name: "DangerDependencies", dependencies: ["Danger", "WeTransferPRLinter"], path: "Submodules/WeTransfer-iOS-CI/Danger-Swift", sources: ["DangerFakeSource.swift"]),
.target(name: "Mocker", path: "Sources")
.target(name: "Mocker", path: "Sources"),
.testTarget(name: "MockerTests", dependencies: ["Mocker"], path: "MockerTests")
],
swiftLanguageVersions: [.v5])
6 changes: 6 additions & 0 deletions Sources/Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ public struct Mock: Equatable {
public static func == (lhs: Mock, rhs: Mock) -> Bool {
let lhsHTTPMethods: [String] = lhs.data.keys.compactMap { $0.rawValue }
let rhsHTTPMethods: [String] = rhs.data.keys.compactMap { $0.rawValue }

if let lhsFileExtensions = lhs.fileExtensions, let rhsFileExtensions = rhs.fileExtensions, (!lhsFileExtensions.isEmpty || !rhsFileExtensions.isEmpty) {
/// The mocks are targeting file extensions specifically, check on those.
return lhsFileExtensions == rhsFileExtensions && lhsHTTPMethods == rhsHTTPMethods
}

return lhs.request.url!.absoluteString == rhs.request.url!.absoluteString && lhsHTTPMethods == rhsHTTPMethods
}
}
Expand Down

0 comments on commit d245ac3

Please sign in to comment.