...
1
2
3
4
5 package toml_test
6
7 import (
8 "encoding/json"
9 "testing"
10
11 "github.com/pelletier/go-toml/v2"
12 "github.com/pelletier/go-toml/v2/internal/testsuite"
13 "github.com/stretchr/testify/require"
14 )
15
16 func testgenInvalid(t *testing.T, input string) {
17 t.Helper()
18 t.Logf("Input TOML:\n%s", input)
19
20 doc := map[string]interface{}{}
21 err := testsuite.Unmarshal([]byte(input), &doc)
22
23 if err == nil {
24 out, err := json.Marshal(doc)
25 if err != nil {
26 panic("could not marshal map to json")
27 }
28 t.Log("JSON output from unmarshal:", string(out))
29 t.Fatalf("test did not fail")
30 }
31 }
32
33 func testgenValid(t *testing.T, input string, jsonRef string) {
34 t.Helper()
35 t.Logf("Input TOML:\n%s", input)
36
37
38 var doc map[string]interface{}
39
40 err := testsuite.Unmarshal([]byte(input), &doc)
41 if err != nil {
42 if de, ok := err.(*toml.DecodeError); ok {
43 t.Logf("%s\n%s", err, de)
44 }
45 t.Fatalf("failed parsing toml: %s", err)
46 }
47 j, err := testsuite.ValueToTaggedJSON(doc)
48 require.NoError(t, err)
49
50 var ref interface{}
51 err = json.Unmarshal([]byte(jsonRef), &ref)
52 require.NoError(t, err)
53
54 var actual interface{}
55 err = json.Unmarshal([]byte(j), &actual)
56 require.NoError(t, err)
57
58 testsuite.CmpJSON(t, "", ref, actual)
59 }
60
View as plain text