navid/veinsforge / c0db40a1a87977f2b8220db4a3de2bcc14c1d24a798f7c3483450f5a0e036208 / internal/vn/parse_test.go

parse_test.go

← internal/vn · 2882 bytes · raw · download

package vn

import "testing"

func TestParseLog(t *testing.T) {
	text := `  c5b8994084ba  6z2z    2026-11-26 21:53:12  t       <unset> [main]
  26bb37b955b1  fa3e    2026-11-26 21:53:12  t       third (divergent)
@ c47aa877c1e9  o7rn    2026-11-26 21:53:10  A Very Long Author Name  Merge pull request #1: thing
  4a79ee834a67  276x    2026-11-26 21:53:10  t       initial commit
`
	commits := ParseLog(text, 0)
	if len(commits) != 4 {
		t.Fatalf("got %d commits", len(commits))
	}
	if commits[0].Hash != "c5b8994084ba" || commits[0].Author != "t" || commits[0].Message != "<unset> [main]" {
		t.Fatalf("unexpected first commit: %+v", commits[0])
	}
	if !commits[2].Current {
		t.Fatal("expected current marker")
	}
	if commits[2].Author != "A Very Long Author Name" {
		t.Fatalf("author = %q", commits[2].Author)
	}
	if commits[2].Message != "Merge pull request #1: thing" {
		t.Fatalf("message = %q", commits[2].Message)
	}
	if commits[0].Date.Year() != 2026 {
		t.Fatalf("date = %v", commits[0].Date)
	}
	if got := ParseLog(text, 2); len(got) != 2 {
		t.Fatalf("limit returned %d", len(got))
	}
}

func TestParseShow(t *testing.T) {
	text := `commit   4a79ee834a678bbc839bef8cbee761c130a34171ecd7672d302575469838317b
change   276xmffexvvc5pkwauzaj5p4xe
author   Test User <test@example.com>
date     2026-11-26 21:53:10
parents  555c8880e98c
bookmarks [feat]

Add feature file

--- /dev/null
+++ b/b.txt
@@ -0,0 +1,1 @@
+b
`
	d := ParseShow(text)
	if d.Hash != "4a79ee834a678bbc839bef8cbee761c130a34171ecd7672d302575469838317b" {
		t.Fatalf("hash = %q", d.Hash)
	}
	if d.Author != "Test User" || d.Email != "test@example.com" {
		t.Fatalf("author = %q email = %q", d.Author, d.Email)
	}
	if len(d.Parents) != 1 || d.Parents[0] != "555c8880e98c" {
		t.Fatalf("parents = %v", d.Parents)
	}
	if d.Message != "Add feature file" {
		t.Fatalf("message = %q", d.Message)
	}
	if d.Diff == "" {
		t.Fatal("diff was not split")
	}
}

func TestParseBranchesAndConflicts(t *testing.T) {
	branches := ParseBranches("feat      4a79ee834a67  276xmffe  \nmain      b4f9a770b344  fa3elkzy  @\n")
	if len(branches) != 2 || branches[0].Name != "feat" || !branches[1].Current {
		t.Fatalf("branches = %+v", branches)
	}
	if got := ParseBranches("info: no bookmarks\n"); len(got) != 0 {
		t.Fatalf("info line parsed as branches: %+v", got)
	}
	tags := ParseTags("v0.0.1   4a79ee834a67  276xmffe  \nv0.0.10  b4f9a770b344  fa3elkzy  @\n")
	if len(tags) != 2 || tags[0].Name != "v0.0.1" || !tags[1].Current {
		t.Fatalf("tags = %+v", tags)
	}
	if got := ParseTags("info: no tags\n"); len(got) != 0 {
		t.Fatalf("info line parsed as tags: %+v", got)
	}
	conflicts := ParseConflicts("info: no conflicts\n")
	if len(conflicts) != 0 {
		t.Fatalf("unexpected conflicts: %v", conflicts)
	}
	conflicts = ParseConflicts("a.txt\nb/c.txt\n")
	if len(conflicts) != 2 {
		t.Fatalf("conflicts = %v", conflicts)
	}
}