1 package imported_tests
2
3
4
5
6 import (
7 "fmt"
8 "testing"
9 "time"
10
11 "github.com/pelletier/go-toml/v2"
12 "github.com/stretchr/testify/require"
13 )
14
15 func TestDocMarshal(t *testing.T) {
16 type testDoc struct {
17 Title string `toml:"title"`
18 BasicLists testDocBasicLists `toml:"basic_lists"`
19 SubDocPtrs []*testSubDoc `toml:"subdocptrs"`
20 BasicMap map[string]string `toml:"basic_map"`
21 Subdocs testDocSubs `toml:"subdoc"`
22 Basics testDocBasics `toml:"basic"`
23 SubDocList []testSubDoc `toml:"subdoclist"`
24 err int `toml:"shouldntBeHere"`
25 unexported int `toml:"shouldntBeHere"`
26 Unexported2 int `toml:"-"`
27 }
28
29 var docData = testDoc{
30 Title: "TOML Marshal Testing",
31 unexported: 0,
32 Unexported2: 0,
33 Basics: testDocBasics{
34 Bool: true,
35 Date: time.Date(1979, 5, 27, 7, 32, 0, 0, time.UTC),
36 Float32: 123.4,
37 Float64: 123.456782132399,
38 Int: 5000,
39 Uint: 5001,
40 String: &biteMe,
41 unexported: 0,
42 },
43 BasicLists: testDocBasicLists{
44 Floats: []*float32{&float1, &float2, &float3},
45 Bools: []bool{true, false, true},
46 Dates: []time.Time{
47 time.Date(1979, 5, 27, 7, 32, 0, 0, time.UTC),
48 time.Date(1980, 5, 27, 7, 32, 0, 0, time.UTC),
49 },
50 Ints: []int{8001, 8001, 8002},
51 Strings: []string{"One", "Two", "Three"},
52 UInts: []uint{5002, 5003},
53 },
54 BasicMap: map[string]string{
55 "one": "one",
56 "two": "two",
57 },
58 Subdocs: testDocSubs{
59 First: testSubDoc{"First", 0},
60 Second: &subdoc,
61 },
62 SubDocList: []testSubDoc{
63 {"List.First", 0},
64 {"List.Second", 0},
65 },
66 SubDocPtrs: []*testSubDoc{&subdoc},
67 }
68
69 marshalTestToml := `title = 'TOML Marshal Testing'
70
71 [basic_lists]
72 floats = [12.3, 45.6, 78.9]
73 bools = [true, false, true]
74 dates = [1979-05-27T07:32:00Z, 1980-05-27T07:32:00Z]
75 ints = [8001, 8001, 8002]
76 uints = [5002, 5003]
77 strings = ['One', 'Two', 'Three']
78
79 [[subdocptrs]]
80 name = 'Second'
81
82 [basic_map]
83 one = 'one'
84 two = 'two'
85
86 [subdoc]
87 [subdoc.second]
88 name = 'Second'
89
90 [subdoc.first]
91 name = 'First'
92
93 [basic]
94 uint = 5001
95 bool = true
96 float = 123.4
97 float64 = 123.456782132399
98 int = 5000
99 string = 'Bite me'
100 date = 1979-05-27T07:32:00Z
101
102 [[subdoclist]]
103 name = 'List.First'
104
105 [[subdoclist]]
106 name = 'List.Second'
107 `
108
109 result, err := toml.Marshal(docData)
110 require.NoError(t, err)
111 require.Equal(t, marshalTestToml, string(result))
112 }
113
114 func TestBasicMarshalQuotedKey(t *testing.T) {
115 result, err := toml.Marshal(quotedKeyMarshalTestData)
116 require.NoError(t, err)
117
118 expected := `'Z.string-àéù' = 'Hello'
119 'Yfloat-𝟘' = 3.5
120
121 ['Xsubdoc-àéù']
122 String2 = 'One'
123
124 [['W.sublist-𝟘']]
125 String2 = 'Two'
126
127 [['W.sublist-𝟘']]
128 String2 = 'Three'
129 `
130
131 require.Equal(t, string(expected), string(result))
132
133 }
134
135 func TestEmptyMarshal(t *testing.T) {
136 type emptyMarshalTestStruct struct {
137 Title string `toml:"title"`
138 Bool bool `toml:"bool"`
139 Int int `toml:"int"`
140 String string `toml:"string"`
141 StringList []string `toml:"stringlist"`
142 Ptr *basicMarshalTestStruct `toml:"ptr"`
143 Map map[string]string `toml:"map"`
144 }
145
146 doc := emptyMarshalTestStruct{
147 Title: "Placeholder",
148 Bool: false,
149 Int: 0,
150 String: "",
151 StringList: []string{},
152 Ptr: nil,
153 Map: map[string]string{},
154 }
155 result, err := toml.Marshal(doc)
156 require.NoError(t, err)
157
158 expected := `title = 'Placeholder'
159 bool = false
160 int = 0
161 string = ''
162 stringlist = []
163
164 [map]
165 `
166
167 require.Equal(t, string(expected), string(result))
168 }
169
170 type textMarshaler struct {
171 FirstName string
172 LastName string
173 }
174
175 func (m textMarshaler) MarshalText() ([]byte, error) {
176 fullName := fmt.Sprintf("%s %s", m.FirstName, m.LastName)
177 return []byte(fullName), nil
178 }
179
180 func TestTextMarshaler(t *testing.T) {
181 type wrap struct {
182 TM textMarshaler
183 }
184
185 m := textMarshaler{FirstName: "Sally", LastName: "Fields"}
186
187 t.Run("at root", func(t *testing.T) {
188 _, err := toml.Marshal(m)
189
190 require.Error(t, err)
191 })
192
193 t.Run("leaf", func(t *testing.T) {
194 res, err := toml.Marshal(wrap{m})
195 require.NoError(t, err)
196
197 require.Equal(t, "TM = 'Sally Fields'\n", string(res))
198 })
199 }
200
View as plain text