Assisted-by: GLM 5.3 Flash
This commit is contained in:
+108
@@ -2126,3 +2126,111 @@ func TestUnmarshalWithOptions(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestInlineTag(t *testing.T) {
|
||||
type Inner struct {
|
||||
A int `toml:"a"`
|
||||
B int `toml:"b"`
|
||||
}
|
||||
t.Run("a struct field writes inline", func(t *testing.T) {
|
||||
type Cfg struct {
|
||||
Inner Inner `toml:"inner,inline"`
|
||||
}
|
||||
out, err := Marshal(Cfg{Inner: Inner{1, 2}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(out) != "inner = {a = 1, b = 2}\n" {
|
||||
t.Errorf("output %q", out)
|
||||
}
|
||||
})
|
||||
t.Run("a map field writes inline", func(t *testing.T) {
|
||||
type Cfg struct {
|
||||
Opts map[string]int `toml:"opts,inline"`
|
||||
}
|
||||
out, err := Marshal(Cfg{Opts: map[string]int{"x": 1}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(out) != "opts = {x = 1}\n" {
|
||||
t.Errorf("output %q", out)
|
||||
}
|
||||
})
|
||||
t.Run("a named embedded struct writes inline", func(t *testing.T) {
|
||||
type Cfg struct {
|
||||
Inner `toml:"inner,inline"`
|
||||
}
|
||||
out, err := Marshal(Cfg{Inner: Inner{1, 2}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(out) != "inner = {a = 1, b = 2}\n" {
|
||||
t.Errorf("output %q", out)
|
||||
}
|
||||
})
|
||||
t.Run("an inline field decodes back", func(t *testing.T) {
|
||||
type Cfg struct {
|
||||
Inner Inner `toml:"inner,inline"`
|
||||
}
|
||||
var cfg Cfg
|
||||
if err := Unmarshal([]byte("inner = {a = 3, b = 4}\n"), &cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Inner != (Inner{3, 4}) {
|
||||
t.Errorf("decoded %+v", cfg.Inner)
|
||||
}
|
||||
})
|
||||
t.Run("a forced inline of an array of tables is an error", func(t *testing.T) {
|
||||
type Item struct {
|
||||
N int `toml:"n"`
|
||||
}
|
||||
type Cfg struct {
|
||||
Items []Item `toml:"items,inline"`
|
||||
}
|
||||
if _, err := Marshal(Cfg{Items: []Item{{1}}}); err == nil {
|
||||
t.Error("forced inline of an array of tables succeeded, want an error")
|
||||
}
|
||||
})
|
||||
t.Run("without the tag the header form stands", func(t *testing.T) {
|
||||
type Cfg struct {
|
||||
Inner Inner `toml:"inner"`
|
||||
}
|
||||
out, err := Marshal(Cfg{Inner: Inner{1, 2}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(out) != "[inner]\na = 1\nb = 2\n" {
|
||||
t.Errorf("output %q", out)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestOmitEmptyJSONSemantics(t *testing.T) {
|
||||
type Cfg struct {
|
||||
Empty string `toml:"empty,omitempty"`
|
||||
Full string `toml:"full,omitempty"`
|
||||
Zero int `toml:"zero,omitempty"`
|
||||
One int `toml:"one,omitempty"`
|
||||
Off bool `toml:"off,omitempty"`
|
||||
On bool `toml:"on,omitempty"`
|
||||
Nil *string `toml:"nil,omitempty"`
|
||||
Set *string `toml:"set,omitempty"`
|
||||
Nothing map[string]string `toml:"nothing,omitempty"`
|
||||
Somethg map[string]string `toml:"somethg,omitempty"`
|
||||
}
|
||||
s := "x"
|
||||
out, err := Marshal(Cfg{
|
||||
Full: "y",
|
||||
One: 1,
|
||||
On: true,
|
||||
Set: &s,
|
||||
Somethg: map[string]string{"k": "v"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "full = \"y\"\none = 1\non = true\nset = \"x\"\n\n[somethg]\nk = \"v\"\n"
|
||||
if string(out) != want {
|
||||
t.Errorf("output:\n%q\nwant:\n%q", out, want)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user