Last active
January 24, 2021 07:29
-
-
Save V8tr/4507110d40e0b62fb09f1600bd992a96 to your computer and use it in GitHub Desktop.
Sample code for my blog post Reflection and Mirror in Swift http://www.vadimbulavin.com/2018-03-09-reflection-and-mirror-in-swift/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import XCTest | |
protocol AutoEquatable: Equatable {} | |
extension AutoEquatable { | |
static func ==(lhs: Self, rhs: Self) -> Bool { | |
var lhsDump = String() | |
dump(lhs, to: &lhsDump) | |
var rhsDump = String() | |
dump(rhs, to: &rhsDump) | |
return rhsDump == lhsDump | |
} | |
} | |
protocol AutoHashable: Hashable {} | |
extension AutoHashable { | |
var hashValue: Int { | |
var buf = String() | |
dump(self, to: &buf) | |
return buf.hashValue | |
} | |
} | |
struct Order { | |
let uid: UUID | |
let count: Int | |
let orderedAt: Date | |
let item: Item | |
} | |
struct Item { | |
let uid: UUID | |
let title: String | |
let description: String? | |
let priceUSD: Double | |
} | |
struct Person { | |
let name: String | |
} | |
extension Order: AutoEquatable {} | |
extension Person: AutoEquatable {} | |
class AutoEquatableTests: XCTestCase { | |
let coffee = Item(uid: UUID(), title: "Coffee", description: "Nescafe Original", priceUSD: 5) | |
lazy var twoCoffees: Order = { Order(uid: UUID(), count: 2, orderedAt: Date(), item: coffee) }() | |
func test_isEqual_samePersons_areEqual() | |
{ | |
XCTAssertEqual(Person(name: "name"), Person(name: "name")) | |
} | |
func test_notEqual_personsWithDifferentNames_areNotEqual() | |
{ | |
XCTAssertNotEqual(Person(name: "name"), Person(name: "anotherName")) | |
} | |
func test_isEqual_sameOrders_areEqual() | |
{ | |
XCTAssertEqual(twoCoffees, twoCoffees) | |
} | |
func test_notEqual_differentOrders_areNotEqual() | |
{ | |
let sandwich = Item(uid: UUID(), title: "Sandwich", description: nil, priceUSD: 5) | |
let oneSandwich = Order(uid: UUID(), count: 1, orderedAt: Date(), item: sandwich) | |
XCTAssertNotEqual(twoCoffees, oneSandwich) | |
} | |
} | |
extension Order: AutoHashable {} | |
extension Person: AutoHashable {} | |
class AutoHashableTests: XCTestCase { | |
let coffee = Item(uid: UUID(), title: "Coffee", description: "Nescafe Original", priceUSD: 5) | |
lazy var twoCoffees: Order = { Order(uid: UUID(), count: 2, orderedAt: Date(), item: coffee) }() | |
func test_hashValue_personsWithEqualNames_haveEqualHash() | |
{ | |
XCTAssertEqual(Person(name: "name").hashValue, Person(name: "name").hashValue) | |
} | |
func test_hashValue_personsWithDifferentNames_haveDifferentHash() | |
{ | |
XCTAssertNotEqual(Person(name: "name").hashValue, Person(name: "anotherName").hashValue) | |
} | |
func test_hashValue_personsWithEqualNames_replacedInSet() | |
{ | |
var set = Set<Person>() | |
set.insert(Person(name: "name")) | |
set.insert(Person(name: "name")) | |
XCTAssertEqual(set.count, 1) | |
} | |
func test_hashValue_personsWithDifferentNames_notReplacedInSet() | |
{ | |
var set = Set<Person>() | |
set.insert(Person(name: "name")) | |
set.insert(Person(name: "anotherName")) | |
XCTAssertEqual(set.count, 2) | |
} | |
func test_hashValue_sameOrders_haveEqualHash() | |
{ | |
XCTAssertEqual(twoCoffees.hashValue, twoCoffees.hashValue) | |
} | |
func test_hashValue_differentOrders_haveDifferentHash() | |
{ | |
let sandwich = Item(uid: UUID(), title: "Sandwich", description: nil, priceUSD: 5) | |
let oneSandwich = Order(uid: UUID(), count: 1, orderedAt: Date(), item: sandwich) | |
XCTAssertNotEqual(twoCoffees.hashValue, oneSandwich.hashValue) | |
} | |
} | |
AutoEquatableTests.defaultTestSuite.run() | |
AutoHashableTests.defaultTestSuite.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment