aboutsummaryrefslogtreecommitdiff
path: root/ident_test.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2025-11-11 00:00:00 +0000
committerGravatar Runxi Yu2025-11-13 00:00:00 +0000
commit15855e3249754ab7dc07183c9383f8a8e8c26af2 (patch)
tree83b32bdd63f7e672152f07d89268e9b268d1f3f5 /ident_test.go
signature
Initial commit
Diffstat (limited to 'ident_test.go')
-rw-r--r--ident_test.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/ident_test.go b/ident_test.go
new file mode 100644
index 00000000..032dee4c
--- /dev/null
+++ b/ident_test.go
@@ -0,0 +1,64 @@
+package furgit
+
+import (
+ "strings"
+ "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)
+ }
+ if got := string(id.Email); got != "alice@example.com" {
+ t.Fatalf("email mismatch: %q", got)
+ }
+ serialized := string(id.Serialize())
+ 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)
+ }
+ }
+}
+
+func TestIdentSerializeUsesCanonicalSpacing(t *testing.T) {
+ id := Ident{
+ Name: []byte("Bob"),
+ Email: []byte("bob@example.com"),
+ WhenUnix: 1000,
+ OffsetMinutes: 90,
+ }
+ got := string(id.Serialize())
+ 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)
+ }
+}