test: pin the embedded map and map merge rules
Test / test (push) Successful in 1m34s

Assisted-by: GLM 5.3 Flash
This commit is contained in:
2026-09-22 00:23:41 +02:00
parent 10391a090f
commit 13ac6dd521
2 changed files with 59 additions and 2 deletions
+54
View File
@@ -1423,3 +1423,57 @@ func (b *ctxBoth) UnmarshalTOML(data any) error {
b.plainCalled = true
return nil
}
func TestEmbeddedMapRule(t *testing.T) {
t.Run("only the first embedded map takes the leftover keys", func(t *testing.T) {
type ExtraMap map[string]any
type MoreMap map[string]any
type Config struct {
Port int
ExtraMap
MoreMap
}
var cfg Config
in := []byte("port = 8080\nlang = \"cs\"\nregion = \"EU\"\n")
if err := Unmarshal(in, &cfg); err != nil {
t.Fatal(err)
}
if cfg.Port != 8080 {
t.Errorf("port = %d", cfg.Port)
}
if cfg.ExtraMap["lang"] != "cs" || cfg.ExtraMap["region"] != "EU" {
t.Errorf("extra = %v, want the leftover keys", cfg.ExtraMap)
}
if len(cfg.MoreMap) != 0 {
t.Errorf("more = %v, want empty: only the first embedded map is the filler", cfg.MoreMap)
}
})
t.Run("a tagged embedded map is an ordinary field", func(t *testing.T) {
type Config struct {
Extra map[string]any `toml:"extra"`
}
var cfg Config
if err := Unmarshal([]byte("extra = {a = 1}\n"), &cfg); err != nil {
t.Fatal(err)
}
if cfg.Extra["a"] != int64(1) {
t.Errorf("extra = %v", cfg.Extra)
}
})
}
func TestDecodeMergesIntoNonEmptyMap(t *testing.T) {
dst := map[string]any{"keep": "me", "port": 1}
if err := Unmarshal([]byte("port = 8080\nlang = \"cs\"\n"), &dst); err != nil {
t.Fatal(err)
}
if dst["keep"] != "me" {
t.Errorf("keep = %v, want the pre-existing key kept", dst["keep"])
}
if dst["port"] != int64(8080) {
t.Errorf("port = %v, want the document's value to win", dst["port"])
}
if dst["lang"] != "cs" {
t.Errorf("lang = %v, want the key added", dst["lang"])
}
}