aboutsummaryrefslogtreecommitdiff
path: root/objectid/objectid_test.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-03-04 14:11:50 +0800
committerGravatar Runxi Yu2026-03-04 14:11:50 +0800
commite46ff2047d01de13206a116181df510ceb9535c8 (patch)
treec47783512352295afb41417c37525c163278324d /objectid/objectid_test.go
parentrefstore/reftable: Delete reftable support for now (diff)
signatureNo signature
objectid: Make more tests iterate algorithms
Diffstat (limited to 'objectid/objectid_test.go')
-rw-r--r--objectid/objectid_test.go146
1 files changed, 73 insertions, 73 deletions
diff --git a/objectid/objectid_test.go b/objectid/objectid_test.go
index 1c5f337a..d053aca3 100644
--- a/objectid/objectid_test.go
+++ b/objectid/objectid_test.go
@@ -2,6 +2,7 @@ package objectid_test
import (
"bytes"
+ "strings"
"testing"
"codeberg.org/lindenii/furgit/objectid"
@@ -28,52 +29,37 @@ func TestParseAlgorithm(t *testing.T) {
func TestParseHexRoundtrip(t *testing.T) {
t.Parallel()
- tests := []struct {
- name string
- algo objectid.Algorithm
- hex string
- }{
- {
- name: "sha1",
- algo: objectid.AlgorithmSHA1,
- hex: "0123456789abcdef0123456789abcdef01234567",
- },
- {
- name: "sha256",
- algo: objectid.AlgorithmSHA256,
- hex: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
+ for _, algo := range objectid.SupportedAlgorithms() {
+ t.Run(algo.String(), func(t *testing.T) {
t.Parallel()
- id, err := objectid.ParseHex(tt.algo, tt.hex)
+ hex := strings.Repeat("01", algo.Size())
+
+ id, err := objectid.ParseHex(algo, hex)
if err != nil {
t.Fatalf("ParseHex failed: %v", err)
}
- if got := id.String(); got != tt.hex {
- t.Fatalf("String() = %q, want %q", got, tt.hex)
+ if got := id.String(); got != hex {
+ t.Fatalf("String() = %q, want %q", got, hex)
}
- if got := id.Size(); got != tt.algo.Size() {
- t.Fatalf("Size() = %d, want %d", got, tt.algo.Size())
+ if got := id.Size(); got != algo.Size() {
+ t.Fatalf("Size() = %d, want %d", got, algo.Size())
}
raw := id.Bytes()
- if len(raw) != tt.algo.Size() {
- t.Fatalf("Bytes len = %d, want %d", len(raw), tt.algo.Size())
+ if len(raw) != algo.Size() {
+ t.Fatalf("Bytes len = %d, want %d", len(raw), algo.Size())
}
- id2, err := objectid.FromBytes(tt.algo, raw)
+ id2, err := objectid.FromBytes(algo, raw)
if err != nil {
t.Fatalf("FromBytes failed: %v", err)
}
- if id2.String() != tt.hex {
- t.Fatalf("FromBytes roundtrip = %q, want %q", id2.String(), tt.hex)
+ if id2.String() != hex {
+ t.Fatalf("FromBytes roundtrip = %q, want %q", id2.String(), hex)
}
})
}
@@ -82,24 +68,32 @@ func TestParseHexRoundtrip(t *testing.T) {
func TestParseHexErrors(t *testing.T) {
t.Parallel()
- tests := []struct {
- name string
- algo objectid.Algorithm
- hex string
- }{
- {"unknown algo", objectid.AlgorithmUnknown, "00"},
- {"odd len", objectid.AlgorithmSHA1, "0"},
- {"wrong len", objectid.AlgorithmSHA1, "0123"},
- {"invalid hex", objectid.AlgorithmSHA1, "zz23456789abcdef0123456789abcdef01234567"},
- }
+ t.Run("unknown algo", func(t *testing.T) {
+ t.Parallel()
+
+ _, err := objectid.ParseHex(objectid.AlgorithmUnknown, "00")
+ if err == nil {
+ t.Fatalf("expected ParseHex error")
+ }
+ })
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
+ for _, algo := range objectid.SupportedAlgorithms() {
+ t.Run(algo.String(), func(t *testing.T) {
t.Parallel()
- _, err := objectid.ParseHex(tt.algo, tt.hex)
+ _, err := objectid.ParseHex(algo, strings.Repeat("0", algo.HexLen()-1))
if err == nil {
- t.Fatalf("expected ParseHex error")
+ t.Fatalf("expected ParseHex odd-len error")
+ }
+
+ _, err = objectid.ParseHex(algo, strings.Repeat("0", algo.HexLen()-2))
+ if err == nil {
+ t.Fatalf("expected ParseHex wrong-len error")
+ }
+
+ _, err = objectid.ParseHex(algo, "z"+strings.Repeat("0", algo.HexLen()-1))
+ if err == nil {
+ t.Fatalf("expected ParseHex invalid-hex error")
}
})
}
@@ -113,55 +107,61 @@ func TestFromBytesErrors(t *testing.T) {
t.Fatalf("expected FromBytes unknown algo error")
}
- _, err = objectid.FromBytes(objectid.AlgorithmSHA1, []byte{1, 2})
- if err == nil {
- t.Fatalf("expected FromBytes wrong size error")
+ for _, algo := range objectid.SupportedAlgorithms() {
+ _, err = objectid.FromBytes(algo, []byte{1, 2})
+ if err == nil {
+ t.Fatalf("expected FromBytes wrong size error")
+ }
}
}
func TestBytesReturnsCopy(t *testing.T) {
t.Parallel()
- id, err := objectid.ParseHex(objectid.AlgorithmSHA1, "0123456789abcdef0123456789abcdef01234567")
- if err != nil {
- t.Fatalf("ParseHex failed: %v", err)
- }
+ for _, algo := range objectid.SupportedAlgorithms() {
+ id, err := objectid.ParseHex(algo, strings.Repeat("01", algo.Size()))
+ if err != nil {
+ t.Fatalf("ParseHex failed: %v", err)
+ }
- b1 := id.Bytes()
+ b1 := id.Bytes()
- b2 := id.Bytes()
- if !bytes.Equal(b1, b2) {
- t.Fatalf("Bytes mismatch")
- }
+ b2 := id.Bytes()
+ if !bytes.Equal(b1, b2) {
+ t.Fatalf("Bytes mismatch")
+ }
- b1[0] ^= 0xff
- if bytes.Equal(b1, b2) {
- t.Fatalf("Bytes should return independent copies")
+ b1[0] ^= 0xff
+ if bytes.Equal(b1, b2) {
+ t.Fatalf("Bytes should return independent copies")
+ }
}
}
func TestRawBytesAliasesStorage(t *testing.T) {
t.Parallel()
- id, err := objectid.ParseHex(objectid.AlgorithmSHA1, "0123456789abcdef0123456789abcdef01234567")
- if err != nil {
- t.Fatalf("ParseHex failed: %v", err)
- }
+ for _, algo := range objectid.SupportedAlgorithms() {
+ id, err := objectid.ParseHex(algo, strings.Repeat("01", algo.Size()))
+ if err != nil {
+ t.Fatalf("ParseHex failed: %v", err)
+ }
- b := id.RawBytes()
- if len(b) != id.Size() {
- t.Fatalf("RawBytes len = %d, want %d", len(b), id.Size())
- }
+ b := id.RawBytes()
+ if len(b) != id.Size() {
+ t.Fatalf("RawBytes len = %d, want %d", len(b), id.Size())
+ }
- if cap(b) != len(b) {
- t.Fatalf("RawBytes cap = %d, want %d", cap(b), len(b))
- }
+ if cap(b) != len(b) {
+ t.Fatalf("RawBytes cap = %d, want %d", cap(b), len(b))
+ }
- orig := id.String()
- b[0] ^= 0xff
+ orig := id.String()
+ b[0] ^= 0xff
- if id.String() == orig {
- t.Fatalf("RawBytes should alias object ID storage")
+ if id.String() == orig {
+ t.Fatalf("RawBytes should alias object ID storage")
+ }
}
}