2023-01-01 15:12:31 -05:00
|
|
|
//
|
|
|
|
// ArrayUniqueTests.swift
|
|
|
|
// TuskerTests
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 1/1/23.
|
|
|
|
// Copyright © 2023 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
@testable import Tusker
|
|
|
|
|
|
|
|
final class ArrayUniqueTests: XCTestCase {
|
|
|
|
|
|
|
|
func testUniquesBy() {
|
|
|
|
let a = Test(string: "test")
|
|
|
|
let b = Test(string: "test")
|
|
|
|
XCTAssertNotEqual(a.id, b.id)
|
|
|
|
XCTAssertNotEqual(a.hashValue, b.hashValue)
|
|
|
|
XCTAssertEqual([a, b].uniques(by: \.string), [a])
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:02:05 -04:00
|
|
|
func testUniquesOrder() {
|
|
|
|
let a = Test(string: "a")
|
|
|
|
let b = Test(string: "b")
|
|
|
|
XCTAssertEqual([a, b].uniques(), [a, b])
|
|
|
|
}
|
|
|
|
|
2023-01-01 15:12:31 -05:00
|
|
|
class Test: NSObject {
|
|
|
|
let id = UUID()
|
|
|
|
let string: String
|
|
|
|
|
|
|
|
init(string: String) {
|
|
|
|
self.string = string
|
|
|
|
}
|
|
|
|
|
|
|
|
override func isEqual(_ object: Any?) -> Bool {
|
|
|
|
guard let other = object as? Self else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return id == other.id && string == other.string
|
|
|
|
}
|
|
|
|
|
|
|
|
override var hash: Int {
|
|
|
|
var hasher = Hasher()
|
|
|
|
hasher.combine(id)
|
|
|
|
hasher.combine(string)
|
|
|
|
return hasher.finalize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|