mirror of
https://github.com/gosticks/SwiftGit2.git
synced 2025-10-16 11:55:34 +00:00
Make OIDs Equatable and Hashable
This commit is contained in:
parent
692a103ca7
commit
8680038cb1
@ -35,6 +35,11 @@ public struct OID {
|
||||
pointer.dealloc(1)
|
||||
}
|
||||
|
||||
/// Create an instance from a libgit2 `git_oid`.
|
||||
public init(oid: git_oid) {
|
||||
self.oid = oid
|
||||
}
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
public let oid: git_oid
|
||||
@ -50,3 +55,15 @@ extension OID: Printable {
|
||||
return String(bytesNoCopy: string, length: length, encoding: NSASCIIStringEncoding, freeWhenDone: true)!
|
||||
}
|
||||
}
|
||||
|
||||
extension OID: Hashable {
|
||||
public var hashValue: Int {
|
||||
return Int(self.oid.id.0) ^ Int(self.oid.id.1) ^ Int(self.oid.id.2)
|
||||
}
|
||||
}
|
||||
|
||||
public func == (lhs: OID, rhs: OID) -> Bool {
|
||||
var left = lhs.oid
|
||||
var right = rhs.oid
|
||||
return git_oid_cmp(&left, &right) == 0
|
||||
}
|
||||
|
||||
@ -38,5 +38,29 @@ class OIDSpec: QuickSpec {
|
||||
expect(oid.description).to(equal(SHA))
|
||||
}
|
||||
}
|
||||
|
||||
describe("==") {
|
||||
it("should be equal when identical") {
|
||||
let SHA = "1234567890123456789012345678901234567890"
|
||||
let oid1 = OID(string: SHA)!
|
||||
let oid2 = OID(string: SHA)!
|
||||
expect(oid1).to(equal(oid2))
|
||||
}
|
||||
|
||||
it("should be not equal when different") {
|
||||
let oid1 = OID(string: "1234567890123456789012345678901234567890")!
|
||||
let oid2 = OID(string: "0000000000000000000000000000000000000000")!
|
||||
expect(oid1).notTo(equal(oid2))
|
||||
}
|
||||
}
|
||||
|
||||
describe("hashValue") {
|
||||
it("should be equal when OIDs are equal") {
|
||||
let SHA = "1234567890123456789012345678901234567890"
|
||||
let oid1 = OID(string: SHA)!
|
||||
let oid2 = OID(string: SHA)!
|
||||
expect(oid1.hashValue).to(equal(oid2.hashValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user