Make OIDs Equatable and Hashable

This commit is contained in:
Matt Diephouse 2014-11-19 09:07:24 -05:00
parent 692a103ca7
commit 8680038cb1
2 changed files with 41 additions and 0 deletions

View File

@ -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
}

View File

@ -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))
}
}
}
}