diff options
| author | 2025-11-16 00:00:00 +0000 | |
|---|---|---|
| committer | 2025-11-16 00:00:00 +0000 | |
| commit | bad0f9715556a470d0de2a22c7040181e3a033ba (patch) | |
| tree | 21463072ce5bc85682a887ce0cae26d833941af3 /ident_test.go | |
| parent | EntryRecursive should return ErrNotFound instead of nil, nil (diff) | |
| signature | ||
Use actual git for tests and enhance Head
Diffstat (limited to 'ident_test.go')
| -rw-r--r-- | ident_test.go | 123 |
1 files changed, 62 insertions, 61 deletions
diff --git a/ident_test.go b/ident_test.go index 76e1fb04..a3d3d03e 100644 --- a/ident_test.go +++ b/ident_test.go @@ -1,72 +1,73 @@ package furgit import ( - "strings" + "bytes" "testing" ) -func TestParseIdentRoundTrip(t *testing.T) { - line := []byte("Alice Example <alice@example.com> 1700000000 -0700") - id, err := parseIdent(line) - if err != nil { - t.Fatalf("parseIdent error: %v", err) +func TestIdentSerialize(t *testing.T) { + tests := []struct { + name string + ident Ident + }{ + { + name: "positive offset", + ident: Ident{ + Name: []byte("John Doe"), + Email: []byte("john@example.org"), + WhenUnix: 1234567890, + OffsetMinutes: 120, + }, + }, + { + name: "negative offset", + ident: Ident{ + Name: []byte("Jane Smith"), + Email: []byte("jane@example.org"), + WhenUnix: 9876543210, + OffsetMinutes: -300, + }, + }, + { + name: "zero offset", + ident: Ident{ + Name: []byte("UTC User"), + Email: []byte("utc@example.org"), + WhenUnix: 1000000000, + OffsetMinutes: 0, + }, + }, } - if got := string(id.Email); got != "alice@example.com" { - t.Fatalf("email mismatch: %q", got) - } - ids, err := id.Serialize() - if err != nil { - t.Fatalf("Serialize error: %v", err) - } - serialized := string(ids) - if !strings.Contains(serialized, "alice@example.com") { - t.Fatalf("Serialize missing email: %q", serialized) - } - when := id.When() - if when.Unix() != 1700000000 { - t.Fatalf("When unix mismatch: %d", when.Unix()) - } - if _, offset := when.Zone(); offset != -7*3600 { - t.Fatalf("When offset mismatch: %d", offset) - } -} -func TestParseIdentInvalidInputs(t *testing.T) { - cases := []string{ - "MissingEmail 1700000000 +0000", - "Name <email> notanumber +0000", - "Name <email> 1700000000 123", - } - for _, tc := range cases { - if _, err := parseIdent([]byte(tc)); err == nil { - t.Fatalf("expected error for %q", tc) - } - } -} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + serialized, err := tt.ident.Serialize() + if err != nil { + t.Fatalf("Serialize failed: %v", err) + } -func TestIdentSerializeUsesCanonicalSpacing(t *testing.T) { - id := Ident{ - Name: []byte("Bob"), - Email: []byte("bob@example.com"), - WhenUnix: 1000, - OffsetMinutes: 90, - } - ids, err := id.Serialize() - if err != nil { - t.Fatalf("Serialize error: %v", err) - } - got := string(ids) - if !strings.Contains(got, "Bob <bob@example.com>") { - t.Fatalf("unexpected serialize output: %q", got) - } - if !strings.HasSuffix(got, "+0130") { - t.Fatalf("expected timezone in +0130 form: %q", got) - } - loc := id.When() - if loc.Unix() != 1000 { - t.Fatalf("When unix mismatch: %d", loc.Unix()) - } - if _, offset := loc.Zone(); offset != 90*60 { - t.Fatalf("When offset mismatch: %d", offset) + parsed, err := parseIdent(serialized) + if err != nil { + t.Fatalf("parseIdent failed: %v", err) + } + + if !bytes.HasPrefix(parsed.Name, tt.ident.Name) { + t.Errorf("name: got %q, want prefix %q", parsed.Name, tt.ident.Name) + } + if !bytes.Equal(parsed.Email, tt.ident.Email) { + t.Errorf("email: got %q, want %q", parsed.Email, tt.ident.Email) + } + if parsed.WhenUnix != tt.ident.WhenUnix { + t.Errorf("whenUnix: got %d, want %d", parsed.WhenUnix, tt.ident.WhenUnix) + } + if parsed.OffsetMinutes != tt.ident.OffsetMinutes { + t.Errorf("offsetMinutes: got %d, want %d", parsed.OffsetMinutes, tt.ident.OffsetMinutes) + } + + when := tt.ident.When() + if when.Unix() != tt.ident.WhenUnix { + t.Errorf("When().Unix(): got %d, want %d", when.Unix(), tt.ident.WhenUnix) + } + }) } } |
