Skip to content

Commit

Permalink
Update toml-test
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Dec 7, 2023
1 parent c320c2d commit 45e7e49
Show file tree
Hide file tree
Showing 244 changed files with 1,459 additions and 844 deletions.
197 changes: 0 additions & 197 deletions decode_test.go
Expand Up @@ -253,53 +253,6 @@ Number = 123
}
}

func TestDecodeTableArrays(t *testing.T) {
var tomlTableArrays = `
[[albums]]
name = "Born to Run"
[[albums.songs]]
name = "Jungleland"
[[albums.songs]]
name = "Meeting Across the River"
[[albums]]
name = "Born in the USA"
[[albums.songs]]
name = "Glory Days"
[[albums.songs]]
name = "Dancing in the Dark"
`

type Song struct {
Name string
}

type Album struct {
Name string
Songs []Song
}

type Music struct {
Albums []Album
}

expected := Music{[]Album{
{"Born to Run", []Song{{"Jungleland"}, {"Meeting Across the River"}}},
{"Born in the USA", []Song{{"Glory Days"}, {"Dancing in the Dark"}}},
}}
var got Music
if _, err := Decode(tomlTableArrays, &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, got) {
t.Fatalf("\n%#v\n!=\n%#v\n", expected, got)
}
}

func TestDecodePointers(t *testing.T) {
type Object struct {
Type string
Expand Down Expand Up @@ -659,92 +612,6 @@ name = "Rice"

}

func TestDecodeInlineTable(t *testing.T) {
input := `
[CookieJar]
Types = {Chocolate = "yummy", Oatmeal = "best ever"}
[Seasons]
Locations = {NY = {Temp = "not cold", Rating = 4}, MI = {Temp = "freezing", Rating = 9}}
`
type cookieJar struct {
Types map[string]string
}
type properties struct {
Temp string
Rating int
}
type seasons struct {
Locations map[string]properties
}
type wrapper struct {
CookieJar cookieJar
Seasons seasons
}
var got wrapper

meta, err := Decode(input, &got)
if err != nil {
t.Fatal(err)
}
want := wrapper{
CookieJar: cookieJar{
Types: map[string]string{
"Chocolate": "yummy",
"Oatmeal": "best ever",
},
},
Seasons: seasons{
Locations: map[string]properties{
"NY": {
Temp: "not cold",
Rating: 4,
},
"MI": {
Temp: "freezing",
Rating: 9,
},
},
},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("after decode, got:\n\n%#v\n\nwant:\n\n%#v", got, want)
}
if len(meta.keys) != 12 {
t.Errorf("after decode, got %d meta keys; want 12", len(meta.keys))
}
if len(meta.keyInfo) != 12 {
t.Errorf("after decode, got %d meta keyInfo; want 12", len(meta.keyInfo))
}
}

func TestDecodeInlineTableArray(t *testing.T) {
type point struct {
X, Y, Z int
}
var got struct {
Points []point
}
// Example inline table array from the spec.
const in = `
points = [ { x = 1, y = 2, z = 3 },
{ x = 7, y = 8, z = 9 },
{ x = 2, y = 4, z = 8 } ]
`
if _, err := Decode(in, &got); err != nil {
t.Fatal(err)
}
want := []point{
{X: 1, Y: 2, Z: 3},
{X: 7, Y: 8, Z: 9},
{X: 2, Y: 4, Z: 8},
}
if !reflect.DeepEqual(got.Points, want) {
t.Errorf("got %#v; want %#v", got.Points, want)
}
}

type menu struct {
Dishes map[string]dish
}
Expand Down Expand Up @@ -789,70 +656,6 @@ type ingredient struct {
Name string
}

func TestDecodeSlices(t *testing.T) {
type (
T struct {
Arr []string
Tbl map[string]any
}
M map[string]any
)
tests := []struct {
input string
in, want T
}{
{"",
T{}, T{}},

// Leave existing values alone.
{"",
T{[]string{}, M{"arr": []string{}}},
T{[]string{}, M{"arr": []string{}}}},
{"",
T{[]string{"a"}, M{"arr": []string{"b"}}},
T{[]string{"a"}, M{"arr": []string{"b"}}}},

// Empty array always allocates (see #339)
{`arr = []
tbl = {arr = []}`,
T{},
T{[]string{}, M{"arr": []any{}}}},
{`arr = []
tbl = {}`,
T{[]string{}, M{}},
T{[]string{}, M{}}},

{`arr = []`,
T{[]string{"a"}, M{}},
T{[]string{}, M{}}},

{`arr = ["x"]
tbl = {arr=["y"]}`,
T{},
T{[]string{"x"}, M{"arr": []any{"y"}}}},
{`arr = ["x"]
tbl = {arr=["y"]}`,
T{[]string{}, M{}},
T{[]string{"x"}, M{"arr": []any{"y"}}}},
{`arr = ["x"]
tbl = {arr=["y"]}`,
T{[]string{"a", "b"}, M{"arr": []any{"c", "d"}}},
T{[]string{"x"}, M{"arr": []any{"y"}}}},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
_, err := Decode(tt.input, &tt.in)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(tt.in, tt.want) {
t.Errorf("\nhave: %#v\nwant: %#v", tt.in, tt.want)
}
})
}
}

func TestDecodePrimitive(t *testing.T) {
type S struct {
P Primitive
Expand Down
40 changes: 0 additions & 40 deletions encode_test.go
Expand Up @@ -55,46 +55,6 @@ func TestEncodeRoundTrip(t *testing.T) {
}
}

func TestEncodeNestedTableArrays(t *testing.T) {
type song struct {
Name string `toml:"name"`
}
type album struct {
Name string `toml:"name"`
Songs []song `toml:"songs"`
}
type springsteen struct {
Albums []album `toml:"albums"`
}
value := springsteen{
[]album{
{"Born to Run",
[]song{{"Jungleland"}, {"Meeting Across the River"}}},
{"Born in the USA",
[]song{{"Glory Days"}, {"Dancing in the Dark"}}},
},
}
expected := `[[albums]]
name = "Born to Run"
[[albums.songs]]
name = "Jungleland"
[[albums.songs]]
name = "Meeting Across the River"
[[albums]]
name = "Born in the USA"
[[albums.songs]]
name = "Glory Days"
[[albums.songs]]
name = "Dancing in the Dark"
`
encodeExpected(t, "nested table arrays", value, expected, nil)
}

func TestEncodeArrayHashWithNormalHashOrder(t *testing.T) {
type Alpha struct {
V int
Expand Down
10 changes: 5 additions & 5 deletions error_test.go
Expand Up @@ -18,21 +18,21 @@ func TestErrorPosition(t *testing.T) {
tests := []struct {
test, err string
}{
{"array/missing-separator.toml", `
{"array/missing-separator-2.toml", `
toml: error: expected a comma (',') or array terminator (']'), but got '2'
At line 1, column 13:
1 | wrong = [ 1 2 3 ]
^`},

{"array/no-close-2.toml", `
{"array/no-close-1.toml", `
toml: error: expected a comma (',') or array terminator (']'), but got end of file
At line 1, column 10:
At line 1, column 23:
1 | x = [42 #
^`},
1 | no-close-1 = [ 1, 2, 3
^`},

{"array/tables-2.toml", `
toml: error: Key 'fruit.variety' has already been defined.
Expand Down

0 comments on commit 45e7e49

Please sign in to comment.