Skip to content

Commit

Permalink
Renamed Pattern to PatternMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Ignition committed Jun 21, 2020
1 parent 2f3e58f commit b72d969
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

/// The kind of pattern for matching request fields such as url and headers
public struct Pattern: Codable, Hashable {
public struct PatternMatch: Codable, Hashable {

// MARK: - Public types

Expand All @@ -26,39 +26,39 @@ public struct Pattern: Codable, Hashable {
self.value = value
}

public static func equal(_ value: String) -> Pattern {
return Pattern(kind: .equal, value: value)
public static func equal(_ value: String) -> PatternMatch {
return PatternMatch(kind: .equal, value: value)
}

public static func wildcard(_ value: String) -> Pattern {
return Pattern(kind: .wildcard, value: value)
public static func wildcard(_ value: String) -> PatternMatch {
return PatternMatch(kind: .wildcard, value: value)
}

public static func regexp(_ value: String) -> Pattern {
return Pattern(kind: .regexp, value: value)
public static func regexp(_ value: String) -> PatternMatch {
return PatternMatch(kind: .regexp, value: value)
}
}


/// Protocol for converting common types to Pattern
public protocol PatternRepresentable {
var pattern: Pattern { get }
var pattern: PatternMatch { get }
}

extension Pattern: PatternRepresentable {
public var pattern: Pattern {
extension PatternMatch: PatternRepresentable {
public var pattern: PatternMatch {
return self
}
}

extension String: PatternRepresentable {
public var pattern: Pattern {
public var pattern: PatternMatch {
return .equal(self)
}
}

extension URL: PatternRepresentable {
public var pattern: Pattern {
public var pattern: PatternMatch {
return .equal(self.absoluteString)
}
}
4 changes: 2 additions & 2 deletions Sources/CatbirdAPI/RequestPattern.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public struct RequestPattern: Codable, Hashable {
public var method: HTTPMethod

/// Request URL.
public var url: Pattern
public var url: PatternMatch

/// Request required headers.
public var headers: [String: Pattern]
public var headers: [String: PatternMatch]

/// A new request pattern.
///
Expand Down
4 changes: 2 additions & 2 deletions Sources/CatbirdApp/Models/Pattern+Match.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import struct CatbirdAPI.Pattern
import struct CatbirdAPI.PatternMatch

extension Pattern {
extension PatternMatch {

func match(_ string: String) -> Bool {
let pattern = value
Expand Down
2 changes: 1 addition & 1 deletion Sources/CatbirdApp/View Model/HeaderItemViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct HeaderItemViewModel: Encodable, Comparable {
let key: String
let value: String

init(_ item: Dictionary<String, Pattern>.Element) {
init(_ item: Dictionary<String, PatternMatch>.Element) {
key = item.key
value = item.value.value
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import struct CatbirdAPI.Pattern
import struct CatbirdAPI.PatternMatch
import XCTest

final class PatternTests: XCTestCase {
final class PatternMatchTests: XCTestCase {

private enum JSONs: String {
case equal = #"{"kind":"equal","value":"some"}"#
Expand All @@ -12,41 +12,41 @@ final class PatternTests: XCTestCase {
}

func testEncodingEqual() throws {
let pattern = Pattern.equal("some")
let pattern = PatternMatch.equal("some")
let data = try JSONEncoder().encode(pattern)
XCTAssertEqual(String(data: data, encoding: .utf8), JSONs.equal.rawValue)
}

func testEncodingWildcard() throws {
let pattern = Pattern.wildcard("some*")
let pattern = PatternMatch.wildcard("some*")
let data = try JSONEncoder().encode(pattern)
XCTAssertEqual(String(data: data, encoding: .utf8), JSONs.wildcard.rawValue)
}

func testEncodingRegexp() throws {
let pattern = Pattern.regexp("^some$")
let pattern = PatternMatch.regexp("^some$")
let data = try JSONEncoder().encode(pattern)
XCTAssertEqual(String(data: data, encoding: .utf8), JSONs.regexp.rawValue)
}

func testDecodingEqual() throws {
let data = JSONs.equal.data
let pattern = try JSONDecoder().decode(Pattern.self, from: data)
let reference = Pattern.equal("some")
let pattern = try JSONDecoder().decode(PatternMatch.self, from: data)
let reference = PatternMatch.equal("some")
XCTAssertEqual(pattern, reference)
}

func testDecodingWildcard() throws {
let data = JSONs.wildcard.data
let pattern = try JSONDecoder().decode(Pattern.self, from: data)
let reference = Pattern.wildcard("some*")
let pattern = try JSONDecoder().decode(PatternMatch.self, from: data)
let reference = PatternMatch.wildcard("some*")
XCTAssertEqual(pattern, reference)
}

func testDecodingRegexp() throws {
let data = JSONs.regexp.data
let pattern = try JSONDecoder().decode(Pattern.self, from: data)
let reference = Pattern.regexp("^some$")
let pattern = try JSONDecoder().decode(PatternMatch.self, from: data)
let reference = PatternMatch.regexp("^some$")
XCTAssertEqual(pattern, reference)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
import CatbirdAPI
import XCTest

final class MatchPatternTests: XCTestCase {
final class PatternMatchTests: XCTestCase {

func testMatchEqual() {
let pattern1 = Pattern.equal("some string")
let pattern1 = PatternMatch.equal("some string")
XCTAssertTrue(pattern1.match("some string"))

let pattern2 = Pattern.equal("some*string")
let pattern2 = PatternMatch.equal("some*string")
XCTAssertTrue(pattern2.match("some*string"))

let pattern3 = Pattern.equal("^some.string$")
let pattern3 = PatternMatch.equal("^some.string$")
XCTAssertTrue(pattern3.match("^some.string$"))
}

func testMatchWildcard() {
let pattern1 = Pattern.wildcard("some?string")
let pattern1 = PatternMatch.wildcard("some?string")
XCTAssertTrue(pattern1.match("some string"))
XCTAssertTrue(pattern1.match("some_string"))
XCTAssertTrue(pattern1.match("some-string"))
XCTAssertFalse(pattern1.match("somestring"))
XCTAssertFalse(pattern1.match("something"))

let pattern2 = Pattern.wildcard("foo{bar,baz}")
let pattern2 = PatternMatch.wildcard("foo{bar,baz}")
XCTAssertTrue(pattern2.match("foobar"))
XCTAssertTrue(pattern2.match("foobaz"))
XCTAssertFalse(pattern2.match("foobuz"))
}

func testMatchRegexp() {
let pattern1 = Pattern.regexp(#"^some[\w\d-_]{1}string"#)
let pattern1 = PatternMatch.regexp(#"^some[\w\d-_]{1}string"#)
XCTAssertTrue(pattern1.match("some-string"))
XCTAssertTrue(pattern1.match("some_string"))
XCTAssertTrue(pattern1.match("some1string"))
Expand Down

0 comments on commit b72d969

Please sign in to comment.