fix(decode): report the smallest unknown field deterministically
Assisted-by: GLM 5.3
This commit is contained in:
@@ -5,6 +5,7 @@ package interpres
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -316,6 +317,26 @@ func TestDisallowUnknownFields(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisallowUnknownFieldsReportsSmallestKey(t *testing.T) {
|
||||
// Map iteration order is random, so the reported key must be chosen
|
||||
// deterministically: the smallest unknown key, whichever order the map
|
||||
// iterates in.
|
||||
type C struct {
|
||||
Known string `toml:"known"`
|
||||
}
|
||||
data := []byte("known = \"x\"\nzeta = 1\nalpha = 2\nmu = 3\n")
|
||||
for range 20 {
|
||||
var c C
|
||||
err := NewDecoder().DisallowUnknownFields().Decode(data, &c)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unknown fields")
|
||||
}
|
||||
if !strings.Contains(err.Error(), `unknown field "alpha"`) {
|
||||
t.Fatalf("err = %v, want the smallest unknown key alpha", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkippedFieldTag(t *testing.T) {
|
||||
type C struct {
|
||||
Keep string `toml:"keep"`
|
||||
|
||||
Reference in New Issue
Block a user