//
//  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])
    }
    
    func testUniquesOrder() {
        let a = Test(string: "a")
        let b = Test(string: "b")
        XCTAssertEqual([a, b].uniques(), [a, b])
    }
    
    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()
        }
    }

}
