func Test_Or(t *testing.T) { numbers := applyInt(numbers, FilterOr(evenNumbers, oddNumbers)) for i := 0; i < 10; i++ { util.Assert(t, numbers[i] == i+1) } util.Assert(t, len(numbers) == 10) }
func testParentlessCommit(t *testing.T, repo Repository, oid *objects.ObjectId) { rev := oid.String() testObjectExpected(t, repo, rev, oid, objects.ObjectCommit) _, err := ObjectFromRevision(repo, rev+"~1") util.Assert(t, err != nil) _, err = ObjectFromRevision(repo, rev+"^") util.Assert(t, err != nil) }
func Test_Sorting(t *testing.T) { refs := make([]objects.Ref, 3) refs[0] = objects.NewRef("zoo", "", nil, nil) refs[1] = objects.NewRef("yogurt", "", nil, nil) refs[2] = objects.NewRef("xavier", "", nil, nil) sort.Sort(refByName(refs)) util.Assert(t, refs[0].Name() == "xavier") util.Assert(t, refs[1].Name() == "yogurt") util.Assert(t, refs[2].Name() == "zoo") }
func Test_parseInvalidBlobHeader(t *testing.T) { test := func(badBlob string) { p := ObjectParserForString(badBlob) hdr, e := p.ParseHeader() util.Assert(t, e != nil) util.Assert(t, hdr == nil) } test("bad 100\000dsfsdfsdfsdf") test("bl0b 10\000est") test("100\000hehe") test("bad \000hoho") test("no header at all") }
func Test_revParse__zeros(t *testing.T) { testCase := test.Linear repo := Open(testCase.Repo()) info := testCase.Info().(*test.InfoLinear) util.Assert(t, info.N > 0) util.Assert(t, len(info.Commits) == info.N) // test the first, parentless commit for _, c := range info.Commits { oid := objects.OidNow(c.CommitOid) testZeros(t, repo, oid) } }
func Test_revParse__secondAncestor(t *testing.T) { testCase := test.Linear repo := Open(testCase.Repo()) info := testCase.Info().(*test.InfoLinear) util.Assert(t, info.N > 2) util.Assert(t, len(info.Commits) == info.N) // test the first, parentless commit for i, c := range info.Commits[2:] { oid, expOid := objects.OidNow(c.CommitOid), objects.OidNow(info.Commits[i].CommitOid) testSecondAncestor(t, repo, oid, expOid) testSecondAncestorVariations(t, repo, oid, expOid) } }
func Test_ParseObjectId(t *testing.T) { var oid *objects.ObjectId oidStr := "ff6ccb68859fd52216ec8dadf98d2a00859f5369" t1 := ObjectParserForString(oidStr) oid = t1.ParseOid() util.Assert(t, oid.String() == oidStr) }
func Test_revParse__firstParent(t *testing.T) { testCase := test.Linear repo := Open(testCase.Repo()) info := testCase.Info().(*test.InfoLinear) util.Assert(t, info.N > 1) util.Assert(t, len(info.Commits) == info.N) // test the first, parentless commit testParentlessCommit(t, repo, objects.OidNow(info.Commits[0].CommitOid)) for _, c := range info.Commits[1:] { oid, expOid := objects.OidNow(c.CommitOid), objects.OidNow(c.ParentOid) testShortOid(t, repo, oid) testFirstParent(t, repo, oid, expOid) testFirstParentVariations(t, repo, oid, expOid) } }
// Test_readCommits will compare the commit output of // git and ggit for a string of commits. func Test_readTree(t *testing.T) { testCase := test.Tree repo := Open(testCase.Repo()) info := testCase.Info().(*test.InfoTree) f := format.NewStrFormat() var ( oid = objects.OidNow(info.TreeOid) ) o, err := repo.ObjectFromOid(oid) util.AssertNoErr(t, err) // check the id util.Assert(t, o.ObjectId().String() == info.TreeOid) // check the header util.Assert(t, o.Header().Type() == objects.ObjectTree) util.AssertEqualInt(t, int(o.Header().Size()), info.TreeSize) // get the tree // now convert to a tag and check the fields var tree *objects.Tree util.AssertPanicFree(t, func() { tree = o.(*objects.Tree) }) // check entries entries := tree.Entries() util.AssertEqualInt(t, info.N, len(entries)) util.Assert(t, info.N > 2) // check a file file := entries[0] util.AssertEqualString(t, info.File1Oid, file.ObjectId().String()) util.Assert(t, file.Mode() == objects.ModeBlob) // TODO: add checks for type, etc. // check the output // check the whole representation, which will catch // most of the other stuff f.Reset() f.Object(o) util.AssertEqualString(t, info.TreeRepr, f.String()) }
func Test_parseBlobBadSize(t *testing.T) { test := func(contents string) { l := len(contents) size := rand.Intn(l+1) - 1 + 2*rand.Intn(2)*l // never equal to l tb := makeTestBlobWithSize(size, contents) p := ObjectParserForString(tb) hdr, e := p.ParseHeader() util.AssertNoErr(t, e) util.Assert(t, hdr.Type() == objects.ObjectBlob) util.Assert(t, hdr.Size() == int64(size)) // should not be able to parse _, e = p.ParsePayload() util.Assert(t, e != nil) } for _, v := range testCasesBlobs { test(v) } }
func Test_assertFileMode(t *testing.T) { m1 := uint16(0000000) m2 := uint16(0040000) m3 := uint16(0100644) m4 := uint16(0100755) m5 := uint16(0120000) m6 := uint16(0160000) test := func(m uint16, exp objects.FileMode) { mode, ok := assertFileMode(m) util.Assert(t, ok, "could not convert to file mode") util.Assert(t, mode == exp) } test(m1, objects.ModeNew) test(m2, objects.ModeTree) test(m3, objects.ModeBlob) test(m4, objects.ModeBlobExec) test(m5, objects.ModeLink) test(m6, objects.ModeCommit) }
func testPackedTagDerefInfo(t *testing.T, repo Repository, spec string, oid *objects.ObjectId) { ref, err := repo.Ref(spec) util.AssertNoErr(t, err) util.AssertEqualString(t, ref.Name(), spec) // make sure target is symbolic and matches symbolic, target := ref.Target() util.Assert(t, !symbolic) if target == nil || ref.Commit() == nil { t.Fatalf("nil target or commit") } util.AssertEqualString(t, ref.Commit().String(), oid.String()) }
// Test_readCommits will compare the commit output of // git and ggit for a string of commits. func Test_readTree(t *testing.T) { testCase := test.TreeDiff repo := api.Open(testCase.Repo()) info, _ := testCase.Info().(*test.InfoTreeDiff) oid := objects.OidNow(info.TreeOid1) o, err := repo.ObjectFromOid(oid) util.AssertNoErr(t, err) // get the tree tree1, _ := o.(*objects.Tree) oid = objects.OidNow(info.TreeOid2) o, err = repo.ObjectFromOid(oid) util.AssertNoErr(t, err) // get the tree tree2, _ := o.(*objects.Tree) diff, err := NewTreeDiffer(repo, NewBlobComparator()).Diff(tree1, tree2) modified := diff.Modified() if len(modified) != 1 { t.Errorf("expected 1 modification in the tree diff. got %d", len(modified)) } edit := modified[0] before, after := edit.Before, edit.After beforeOid, afterOid := before.ObjectId(), after.ObjectId() beforeName := before.Name() if beforeName != info.ModifiedFileName { t.Errorf("expected modified file name to be [%s], but got [%s]", info.ModifiedFileName, beforeName) } if beforeOid.String() != info.ModifiedFileBeforeOid { t.Errorf("expected modified file's initial id to be [%s], but got [%s]", info.ModifiedFileBeforeOid, beforeOid) } if afterOid.String() != info.ModifiedFileAfterOid { t.Errorf("expected modified file's initial id to be [%s], but got [%s]", info.ModifiedFileAfterOid, afterOid) } deleted := diff.Deleted() util.Assert(t, len(deleted) == 1, fmt.Sprintf("expected 1 file to be deleted, got [%d]", len(deleted))) entry := deleted[0] deletedName, deletedOid := entry.Name(), entry.ObjectId().String() if deletedName != info.RemovedFileName { t.Errorf("expected deleted file's name to be [%s], but got [%s]", info.RemovedFileName, deletedName) } if deletedOid != info.RemovedFileOid { t.Errorf("expected deleted file's id to be [%s], but got [%s]", info.RemovedFileOid, deletedOid) } }
func Test_parseValidFileMode(t *testing.T) { p := ObjectParserForString("0000000\n0040000\n0100644\n0100755\n0120000\n0160000\n") util.Assert(t, p.ParseFileMode(token.LF) == objects.ModeNew) util.Assert(t, p.ParseFileMode(token.LF) == objects.ModeTree) util.Assert(t, p.ParseFileMode(token.LF) == objects.ModeBlob) util.Assert(t, p.ParseFileMode(token.LF) == objects.ModeBlobExec) util.Assert(t, p.ParseFileMode(token.LF) == objects.ModeLink) util.Assert(t, p.ParseFileMode(token.LF) == objects.ModeCommit) util.Assert(t, p.EOF()) }
// parseAndCompareTree will take a string tree representation and compare // it with that tree from the provided repository. A tree representation // has the following format: // // <oid_of_this_tree><LF> // <size><LF> // <output_of_cat_file_p> // // Example: // // `dc5c0113b3d246da13b9d4b54861d2894da45e5a // 348 // 100644 blob 022f0ad6583f99c3c01d0245db452e58bbb8fc8a .gitignore // 100644 blob 4760db634060e0f5c76eddb18031d53bff882a23 README // 040000 tree 888fd8c9a6612c01d8f7c4e71fef114c3d3386d5 api // 040000 tree 7ba590c4aa02ba2268f732f70b6143cd60457782 builtin // 040000 tree bd52eecc257922f4c9008d080ad7dd5f9ac3444e dotfiles // 040000 tree 9ee2de084ec91660183740dacd8988c8170537e3 ggcase // 100644 blob 82a1896a6d17e56220681fb66f94ac8a7e1d9bdc ggit.go // 100644 blob 3b20433fdf8544b2cff26989c5fc63327137e809 templates.go // 040000 tree d42d8e7cf260b951e17687404031b1975ffa0b38 test // 100644 blob 0c01aeb4856324d8487a252e32e04961bf603fad version.go // ` func parseAndCompareTree(t *testing.T, repo Repository, treeRepr string) { p := parse.ObjectParserForString(treeRepr) oid := p.ParseOid() p.ConsumeByte(token.LF) size := p.ParseInt(token.LF, 10, 32) o, err := repo.ObjectFromOid(oid) util.AssertNoErr(t, err) hdr := o.Header() util.Assert(t, hdr.Type() == objects.ObjectTree) util.Assert(t, hdr.Size() == size) tree := o.(*objects.Tree) entries := tree.Entries() for !p.EOF() { mode := p.ParseFileMode(token.SP) otype := objects.ObjectType(p.ConsumeStrings(token.ObjectTypes)) p.ConsumeByte(token.SP) oidStr := p.ParseOid().String() p.ConsumeByte(token.TAB) name := p.ReadString(token.LF) util.Assert(t, len(entries) > 0) entry := entries[0] util.Assert(t, mode == entry.Mode()) util.Assertf(t, name == entry.Name(), "expecting: `%s` got: `%s", name, entry.Name()) util.Assert(t, otype == entry.ObjectType()) util.Assertf(t, oidStr == entry.ObjectId().String(), "expecting: `%s` got: `%s", oidStr, entry.ObjectId()) entries = entries[1:] } util.Assert(t, len(entries) == 0) }
func Test_parseValidBlob(t *testing.T) { for _, v := range testCasesBlobs { tb := makeTestBlob(v) p := ObjectParserForString(tb) hdr, err := p.ParseHeader() util.AssertNoErr(t, err) util.Assert(t, hdr.Type() == objects.ObjectBlob) util.Assert(t, hdr.Size() == int64(len(v))) o, err := p.ParsePayload() util.AssertNoErr(t, err) util.Assert(t, o.Header().Type() == objects.ObjectBlob) util.Assert(t, o.Header().Size() == int64(len(v))) util.Assert(t, o.ObjectId() == nil) // wasn't set in the test scenario var b *objects.Blob util.AssertPanicFree(t, func() { b = o.(*objects.Blob) }) util.AssertEqualString(t, string(b.Data()), v) } }
func assertPeeledRef(t *testing.T, peeledRef objects.Ref, oid *objects.ObjectId) { symbolic, target := peeledRef.Target() util.Assert(t, !symbolic) if target == nil { t.Fatalf("nil target") } util.AssertEqualString(t, target.(*objects.ObjectId).String(), oid.String()) util.AssertEqualString(t, peeledRef.ObjectId().String(), oid.String()) util.AssertPanic(t, func() { s := target.(string) s += "" // for compilation }) }
func Test_NotEven(t *testing.T) { numbers := applyInt(numbers, FilterNot(evenNumbers)) util.Assert(t, numbers[0] == 1) util.Assert(t, numbers[1] == 3) util.Assert(t, numbers[2] == 5) util.Assert(t, numbers[3] == 7) util.Assert(t, numbers[4] == 9) util.Assert(t, len(numbers) == 5) }
func Test_NotOdd(t *testing.T) { numbers := applyInt(numbers, FilterNot(oddNumbers)) util.Assert(t, numbers[0] == 2) util.Assert(t, numbers[1] == 4) util.Assert(t, numbers[2] == 6) util.Assert(t, numbers[3] == 8) util.Assert(t, numbers[4] == 10) util.Assert(t, len(numbers) == 5) }
func Test_EvenNumbers(t *testing.T) { numbers := applyInt(numbers, evenNumbers) util.Assert(t, numbers[0] == 2) util.Assert(t, numbers[1] == 4) util.Assert(t, numbers[2] == 6) util.Assert(t, numbers[3] == 8) util.Assert(t, numbers[4] == 10) util.Assert(t, len(numbers) == 5) }
func Test_OddNumbers(t *testing.T) { numbers := applyInt(numbers, oddNumbers) util.Assert(t, numbers[0] == 1) util.Assert(t, numbers[1] == 3) util.Assert(t, numbers[2] == 5) util.Assert(t, numbers[3] == 7) util.Assert(t, numbers[4] == 9) util.Assert(t, len(numbers) == 5) }
func Test_makeHash(t *testing.T) { testRepo := test.DerefsPacked repo := Open(testRepo.Repo()) info := testRepo.Info().(*test.InfoDerefsPacked) tagOid := objects.OidNow(info.TagOid) o, err := repo.ObjectFromOid(tagOid) if err != nil { panic(err) } if h, err := MakeHash(o); err != nil { panic(err) } else { oid := objects.OidFromHash(h) util.Assert(t, oid.String() == tagOid.String()) } }
func assertSymbolicRef(t *testing.T, symbolicRef objects.Ref, tget string) { symbolic, target := symbolicRef.Target() util.Assert(t, symbolic) if target == nil { t.Fatalf("nil target") } util.AssertEqualString(t, target.(string), tget) util.AssertPanic(t, func() { oid := target.(*objects.ObjectId) oid.String() // for compilation }) util.AssertPanic(t, func() { oid := symbolicRef.ObjectId() oid.String() // for compilation }) }
func assertBadRef(t *testing.T, repo Repository, spec string) { _, err := repo.Ref(spec) util.Assert(t, err != nil) util.Assert(t, IsNoSuchRef(err)) _, err = RefFromSpec(repo, spec) util.Assert(t, err != nil) util.Assert(t, IsNoSuchRef(err)) _, err = PeeledRefFromSpec(repo, spec) util.Assert(t, err != nil) util.Assert(t, IsNoSuchRef(err)) }
// this test implements basic blob reading from // a git repository. func Test_readBlobs(t *testing.T) { testRepo := test.Blobs repo := Open(testRepo.Repo()) info := testRepo.Info().(*test.InfoBlobs) if len(info.Blobs) < 1 { fmt.Println("warning: no blobs to test") } for _, detail := range info.Blobs { // read the blob oid := objects.OidNow(detail.Oid) o, err := repo.ObjectFromOid(oid) util.AssertNoErr(t, err) util.Assert(t, o.Header().Type() == objects.ObjectBlob) b := o.(*objects.Blob) util.AssertEqualString(t, string(b.Data()), detail.Contents) util.AssertEqualInt(t, int(b.Header().Size()), len(detail.Contents)) } }
func Test_NewStrFormat(t *testing.T) { f := NewStrFormat() f.Printf("hello %d", 10) f.Lf() util.Assert(t, f.String() == "hello 10\n") }
// testObjectExpected retrieves the commit with the given revision specification // from the given repository and ensures that this operation went well and the // returned object in fact has the expected oid. func testObjectExpected(t *testing.T, repo Repository, rev string, expOid *objects.ObjectId, expType objects.ObjectType) { parent, err := ObjectFromRevision(repo, rev) util.AssertNoErr(t, err) util.Assert(t, parent.Header().Type() == expType) util.AssertEqualString(t, parent.ObjectId().String(), expOid.String()) }