|
| 1 | +package gnit |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestNewRepository(t *testing.T) { |
| 6 | + r := NewRepository("foo") |
| 7 | + |
| 8 | + if r.identity.Name != "foo" { |
| 9 | + t.Errorf("expected %s, got %s", "foo", r.identity.Name) |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +func TestCommit(t *testing.T) { |
| 14 | + r := NewRepository("test-repo") |
| 15 | + |
| 16 | + files := map[string][]byte{ |
| 17 | + "main.go": []byte("package main\n\nfunc main() {\n\tprintln(\"Hello, World!\")\n}"), |
| 18 | + "README.md": []byte("# Test Project\n\nThis is a test project."), |
| 19 | + } |
| 20 | + |
| 21 | + commitHash := r.Commit("Initial commit", files) |
| 22 | + |
| 23 | + if commitHash == "" { |
| 24 | + t.Error("expected non-empty commit hash") |
| 25 | + } |
| 26 | + |
| 27 | + commit := r.GetCommit(commitHash) |
| 28 | + if commit == nil { |
| 29 | + t.Error("expected commit to be stored") |
| 30 | + } |
| 31 | + |
| 32 | + if commit.Message != "Initial commit" { |
| 33 | + t.Errorf("expected message 'Initial commit', got %s", commit.Message) |
| 34 | + } |
| 35 | + |
| 36 | + if commit.Hash != commitHash { |
| 37 | + t.Errorf("expected hash %s, got %s", commitHash, commit.Hash) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestGetFile(t *testing.T) { |
| 42 | + r := NewRepository("test-repo") |
| 43 | + |
| 44 | + files := map[string][]byte{ |
| 45 | + "main.go": []byte("package main\n\nfunc main() {\n\tprintln(\"Hello, World!\")\n}"), |
| 46 | + "README.md": []byte("# Test Project\n\nThis is a test project."), |
| 47 | + } |
| 48 | + |
| 49 | + commitHash := r.Commit("Initial commit", files) |
| 50 | + |
| 51 | + mainContent := r.GetFile(commitHash, "main.go") |
| 52 | + if string(mainContent) != string(files["main.go"]) { |
| 53 | + t.Errorf("expected %s, got %s", string(files["main.go"]), string(mainContent)) |
| 54 | + } |
| 55 | + |
| 56 | + readmeContent := r.GetFile(commitHash, "README.md") |
| 57 | + if string(readmeContent) != string(files["README.md"]) { |
| 58 | + t.Errorf("expected %s, got %s", string(files["README.md"]), string(readmeContent)) |
| 59 | + } |
| 60 | + |
| 61 | + nonExistentContent := r.GetFile(commitHash, "nonexistent.txt") |
| 62 | + if nonExistentContent != nil { |
| 63 | + t.Error("expected nil for non-existent file") |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestGetCommitNonExistent(t *testing.T) { |
| 68 | + r := NewRepository("test-repo") |
| 69 | + |
| 70 | + commit := r.GetCommit("nonexistent-hash") |
| 71 | + if commit != nil { |
| 72 | + t.Error("expected nil for non-existent commit") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestMultipleCommits(t *testing.T) { |
| 77 | + r := NewRepository("test-repo") |
| 78 | + |
| 79 | + files1 := map[string][]byte{ |
| 80 | + "file1.txt": []byte("First version"), |
| 81 | + } |
| 82 | + hash1 := r.Commit("First commit", files1) |
| 83 | + |
| 84 | + files2 := map[string][]byte{ |
| 85 | + "file1.txt": []byte("Second version"), |
| 86 | + "file2.txt": []byte("New file"), |
| 87 | + } |
| 88 | + hash2 := r.Commit("Second commit", files2) |
| 89 | + |
| 90 | + if hash1 == hash2 { |
| 91 | + t.Error("expected different hashes for different commits") |
| 92 | + } |
| 93 | + |
| 94 | + content1 := r.GetFile(hash1, "file1.txt") |
| 95 | + if string(content1) != "First version" { |
| 96 | + t.Errorf("expected 'First version', got %s", string(content1)) |
| 97 | + } |
| 98 | + |
| 99 | + content2 := r.GetFile(hash2, "file1.txt") |
| 100 | + if string(content2) != "Second version" { |
| 101 | + t.Errorf("expected 'Second version', got %s", string(content2)) |
| 102 | + } |
| 103 | + |
| 104 | + newFile := r.GetFile(hash2, "file2.txt") |
| 105 | + if string(newFile) != "New file" { |
| 106 | + t.Errorf("expected 'New file', got %s", string(newFile)) |
| 107 | + } |
| 108 | +} |
0 commit comments