Files
acp/error_test.go
T
Samuel Cui 4d5dbd0b8c Release ACP v0.2.3
Simplify indexed copy facts and unify staged output completion.
Fix cleanup, rewrite persistence, native paths, and terminal reporting.
Maintain compatibility, upstream notices, and automated release checks.
2026-10-05 16:29:55 +08:00

44 lines
1.2 KiB
Go

package acp
import (
"encoding/json"
"errors"
"testing"
)
func TestErrorUnmarshalJSONNull(t *testing.T) {
for _, initial := range []struct {
name string
value Error
}{
{name: "empty"},
{name: "populated", value: Error{Src: "source", Dst: "target", Err: errors.New("old failure")}},
} {
t.Run(initial.name, func(t *testing.T) {
// A null document clears a reused value without inventing a failure.
decoded := initial.value
if err := json.Unmarshal([]byte("null"), &decoded); err != nil {
t.Fatalf("decode null error: %v", err)
}
if decoded.Src != "" || decoded.Dst != "" || decoded.Err != nil {
t.Fatalf("decoded null = %#v, want an empty error value", decoded)
}
// Re-encoding and decoding must keep the failure absent.
encoded, err := json.Marshal(&decoded)
if err != nil {
t.Fatalf("encode absent error: %v", err)
}
if string(encoded) != "{}" {
t.Fatalf("encoded absent error = %s, want {}", encoded)
}
if err := json.Unmarshal(encoded, &decoded); err != nil {
t.Fatalf("decode absent error: %v", err)
}
if decoded.Err != nil {
t.Fatalf("round-trip error = %v, want nil", decoded.Err)
}
})
}
}