SwiftGit2/SwiftGit2Tests/ObjectsSpec.swift
2014-12-06 21:51:46 -05:00

169 lines
5.3 KiB
Swift

//
// ObjectSpec.swift
// SwiftGit2
//
// Created by Matt Diephouse on 12/4/14.
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
//
import LlamaKit
import SwiftGit2
import Nimble
import Quick
func from_git_object<T>(repository: Repository, oid: OID, f: COpaquePointer -> T) -> T{
let repository = repository.pointer
var oid = oid.oid
let pointer = UnsafeMutablePointer<COpaquePointer>.alloc(1)
git_object_lookup(pointer, repository, &oid, GIT_OBJ_ANY)
let result = f(pointer.memory)
git_object_free(pointer.memory)
pointer.dealloc(1)
return result
}
class SignatureSpec: QuickSpec {
override func spec() {
describe("init(signature:)") {
it("should initialize its properties") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let raw_signature = from_git_object(repo, oid) { git_commit_author($0).memory }
let signature = Signature(signature: raw_signature)
expect(signature.name).to(equal("Matt Diephouse"))
expect(signature.email).to(equal("matt@diephouse.com"))
expect(signature.time).to(equal(NSDate(timeIntervalSince1970: 1416186947)))
expect(signature.timeZone.abbreviation).to(equal("GMT-5"))
}
}
describe("==") {
it("should be true with equal objects") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let author1 = from_git_object(repo, oid) { commit in
Signature(signature: git_commit_author(commit).memory)
}
let author2 = author1
expect(author1).to(equal(author2))
}
it("should be false with unequal objects") {
let repo = Fixtures.simpleRepository
let oid1 = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let oid2 = OID(string: "24e1e40ee77525d9e279f079f9906ad6d98c8940")!
let author1 = from_git_object(repo, oid1) { commit in
Signature(signature: git_commit_author(commit).memory)
}
let author2 = from_git_object(repo, oid2) { commit in
Signature(signature: git_commit_author(commit).memory)
}
expect(author1).notTo(equal(author2))
}
}
describe("hashValue") {
it("should be equal with equal objects") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let author1 = from_git_object(repo, oid) { commit in
Signature(signature: git_commit_author(commit).memory)
}
let author2 = author1
expect(author1.hashValue).to(equal(author2.hashValue))
}
}
}
}
class CommitSpec: QuickSpec {
override func spec() {
describe("init(pointer:)") {
it("should initialize its properties") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "24e1e40ee77525d9e279f079f9906ad6d98c8940")!
let commit = from_git_object(repo, oid) { Commit(pointer: $0) }
let author = from_git_object(repo, oid) { commit in
Signature(signature: git_commit_author(commit).memory)
}
let committer = from_git_object(repo, oid) { commit in
Signature(signature: git_commit_committer(commit).memory)
}
let tree = OID(string: "219e9f39c2fb59ed1dfb3e78ed75055a57528f31")!
let parents = [
OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!,
]
expect(commit.oid).to(equal(oid))
expect(commit.tree).to(equal(tree))
expect(commit.parents).to(equal(parents))
expect(commit.message).to(equal("List branches in README\n"))
expect(commit.author).to(equal(author))
expect(commit.committer).to(equal(committer))
}
it("should handle 0 parents") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let commit = from_git_object(repo, oid) { Commit(pointer: $0) }
expect(commit.parents).to(equal([]))
}
it("should handle multiple parents") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "c4ed03a6b7d7ce837d31d83757febbe84dd465fd")!
let commit = from_git_object(repo, oid) { Commit(pointer: $0) }
let parents = [
OID(string: "315b3f344221db91ddc54b269f3c9af422da0f2e")!,
OID(string: "57f6197561d1f99b03c160f4026a07f06b43cf20")!,
]
expect(commit.parents).to(equal(parents))
}
}
describe("==") {
it("should be true with equal objects") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let commit1 = from_git_object(repo, oid) { Commit(pointer: $0) }
let commit2 = commit1
expect(commit1).to(equal(commit2))
}
it("should be false with unequal objects") {
let repo = Fixtures.simpleRepository
let oid1 = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let oid2 = OID(string: "c4ed03a6b7d7ce837d31d83757febbe84dd465fd")!
let commit1 = from_git_object(repo, oid1) { Commit(pointer: $0) }
let commit2 = from_git_object(repo, oid2) { Commit(pointer: $0) }
expect(commit1).notTo(equal(commit2))
}
}
describe("hashValue") {
it("should be equal with equal objects") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let commit1 = from_git_object(repo, oid) { Commit(pointer: $0) }
let commit2 = commit1
expect(commit1.hashValue).to(equal(commit2.hashValue))
}
}
}
}