From a0f76f6c26f54b17b3c3bed09a21ff2c6fd3c25e Mon Sep 17 00:00:00 2001 From: Matt Diephouse Date: Sat, 14 Feb 2015 11:16:08 -0500 Subject: [PATCH] Make TagReference Hashable --- SwiftGit2/References.swift | 10 ++++++++++ SwiftGit2Tests/ReferencesSpec.swift | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/SwiftGit2/References.swift b/SwiftGit2/References.swift index 81229aa..72e13ca 100644 --- a/SwiftGit2/References.swift +++ b/SwiftGit2/References.swift @@ -169,4 +169,14 @@ public enum TagReference: ReferenceType { } } +extension TagReference: Hashable { + public var hashValue: Int { + return longName.hashValue ^ oid.hashValue + } +} + +public func == (lhs: TagReference, rhs: TagReference) -> Bool { + return lhs.longName == rhs.longName + && lhs.oid == rhs.oid +} diff --git a/SwiftGit2Tests/ReferencesSpec.swift b/SwiftGit2Tests/ReferencesSpec.swift index 1564065..2962945 100644 --- a/SwiftGit2Tests/ReferencesSpec.swift +++ b/SwiftGit2Tests/ReferencesSpec.swift @@ -130,5 +130,30 @@ class TagReferenceSpec: QuickSpec { expect(tag).to(beNil()) } } + + describe("==") { + it("should be true with equal tag references") { + let repo = Fixtures.simpleRepository + let tag1 = from_git_reference(repo, "refs/tags/tag-2") { TagReference($0)! } + let tag2 = from_git_reference(repo, "refs/tags/tag-2") { TagReference($0)! } + expect(tag1).to(equal(tag2)) + } + + it("should be false with unequal tag references") { + let repo = Fixtures.simpleRepository + let tag1 = from_git_reference(repo, "refs/tags/tag-1") { TagReference($0)! } + let tag2 = from_git_reference(repo, "refs/tags/tag-2") { TagReference($0)! } + expect(tag1).notTo(equal(tag2)) + } + } + + describe("hashValue") { + it("should be equal with equal references") { + let repo = Fixtures.simpleRepository + let tag1 = from_git_reference(repo, "refs/tags/tag-2") { TagReference($0)! } + let tag2 = from_git_reference(repo, "refs/tags/tag-2") { TagReference($0)! } + expect(tag1.hashValue).to(equal(tag2.hashValue)) + } + } } }