fix: encode mixed arrays with inline table elements
Assisted-by: GLM 5.3 Flash
This commit is contained in:
@@ -504,6 +504,79 @@ func TestMarshalNestedArrays(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalMixedArrayWithInlineTable(t *testing.T) {
|
||||
// Parse accepts a mixed array (TOML allows any value kinds in one array),
|
||||
// so Marshal of the parsed tree must re-emit it. The table element has no
|
||||
// header form inside a value array and renders inline.
|
||||
tree, err := Parse([]byte("arr = [1, {a = 2}, \"x\"]\n"))
|
||||
if err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
out, err := Marshal(tree)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
want := "arr = [1, {a = 2}, \"x\"]\n"
|
||||
if string(out) != want {
|
||||
t.Fatalf("output mismatch:\ngot: %q\nwant: %q", out, want)
|
||||
}
|
||||
re, err := Parse(out)
|
||||
if err != nil {
|
||||
t.Fatalf("re-parse: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(tree, re) {
|
||||
t.Fatalf("round-trip changed the tree:\nwas: %#v\nnow: %#v", tree, re)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalNestedInlineTables(t *testing.T) {
|
||||
tree := map[string]any{
|
||||
"mix": []any{
|
||||
int64(1),
|
||||
map[string]any{"deep": map[string]any{"n": int64(0)}, "list": []any{"a", true}},
|
||||
map[string]any{},
|
||||
},
|
||||
}
|
||||
out, err := Marshal(tree)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
want := "mix = [1, {deep = {n = 0}, list = [\"a\", true]}, {}]\n"
|
||||
if string(out) != want {
|
||||
t.Fatalf("output mismatch:\ngot: %q\nwant: %q", out, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalInlineTableWithDatetime(t *testing.T) {
|
||||
when := time.Date(1979, 5, 27, 7, 32, 0, 0, time.UTC)
|
||||
tree := map[string]any{
|
||||
"mix": []any{when, map[string]any{"t": LocalDateTime{when}}},
|
||||
}
|
||||
out, err := Marshal(tree)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
want := "mix = [1979-05-27T07:32:00Z, {t = 1979-05-27T07:32:00}]\n"
|
||||
if string(out) != want {
|
||||
t.Fatalf("output mismatch:\ngot: %q\nwant: %q", out, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalArrayOfTablesStaysHeaderForm(t *testing.T) {
|
||||
tree, err := Parse([]byte("[[items]]\nname = \"a\"\n\n[[items]]\nname = \"b\"\n"))
|
||||
if err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
out, err := Marshal(tree)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
want := "[[items]]\nname = \"a\"\n\n[[items]]\nname = \"b\"\n"
|
||||
if string(out) != want {
|
||||
t.Fatalf("output mismatch:\ngot: %q\nwant: %q", out, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalFloatExponentNoLeadingZero(t *testing.T) {
|
||||
// strconv.FormatFloat with 'g' would produce "1e+06" (leading zero in
|
||||
// exponent). The encoder must strip it so the output is "1e+6".
|
||||
|
||||
Reference in New Issue
Block a user