Make Reference Hashable

This commit is contained in:
Matt Diephouse 2015-01-30 15:09:04 -05:00
parent d1928ca06c
commit 2a4c231230
2 changed files with 36 additions and 0 deletions

View File

@ -38,6 +38,17 @@ public struct Reference: ReferenceType {
}
}
extension Reference: Hashable {
public var hashValue: Int {
return longName.hashValue ^ oid.hashValue
}
}
public func == (lhs: Reference, rhs: Reference) -> Bool {
return lhs.longName == rhs.longName
&& lhs.oid == rhs.oid
}
/// A git branch.
public struct Branch: ReferenceType {
/// The full name of the reference (e.g., `refs/heads/master`).

View File

@ -34,5 +34,30 @@ class ReferenceSpec: QuickSpec {
expect(ref.oid).to(equal(OID(string: "c4ed03a6b7d7ce837d31d83757febbe84dd465fd")!))
}
}
describe("==") {
it("should be true with equal references") {
let repo = Fixtures.simpleRepository
let ref1 = from_git_reference(repo, "refs/heads/master") { Reference($0) }
let ref2 = from_git_reference(repo, "refs/heads/master") { Reference($0) }
expect(ref1).to(equal(ref2))
}
it("should be false with unequal references") {
let repo = Fixtures.simpleRepository
let ref1 = from_git_reference(repo, "refs/heads/master") { Reference($0) }
let ref2 = from_git_reference(repo, "refs/heads/another-branch") { Reference($0) }
expect(ref1).notTo(equal(ref2))
}
}
describe("hashValue") {
it("should be equal with equal references") {
let repo = Fixtures.simpleRepository
let ref1 = from_git_reference(repo, "refs/heads/master") { Reference($0) }
let ref2 = from_git_reference(repo, "refs/heads/master") { Reference($0) }
expect(ref1.hashValue).to(equal(ref2.hashValue))
}
}
}
}