@@ -12,10 +12,10 @@ import (
1212)
1313
1414type basicMarshalTestStruct struct {
15- String string `toml:"string "`
16- StringList []string `toml:"strlist "`
17- Sub basicMarshalTestSubStruct `toml:"subdoc "`
18- SubList []basicMarshalTestSubStruct `toml:"sublist "`
15+ String string `toml:"Zstring "`
16+ StringList []string `toml:"Ystrlist "`
17+ Sub basicMarshalTestSubStruct `toml:"Xsubdoc "`
18+ SubList []basicMarshalTestSubStruct `toml:"Wsublist "`
1919}
2020
2121type basicMarshalTestSubStruct struct {
@@ -29,16 +29,29 @@ var basicTestData = basicMarshalTestStruct{
2929 SubList : []basicMarshalTestSubStruct {{"Two" }, {"Three" }},
3030}
3131
32- var basicTestToml = []byte (`string = "Hello"
33- strlist = ["Howdy","Hey There"]
32+ var basicTestToml = []byte (`Ystrlist = ["Howdy","Hey There"]
33+ Zstring = "Hello"
3434
35- [subdoc]
35+ [[Wsublist]]
36+ String2 = "Two"
37+
38+ [[Wsublist]]
39+ String2 = "Three"
40+
41+ [Xsubdoc]
3642 String2 = "One"
43+ ` )
3744
38- [[sublist]]
45+ var basicTestTomlOrdered = []byte (`Zstring = "Hello"
46+ Ystrlist = ["Howdy","Hey There"]
47+
48+ [Xsubdoc]
49+ String2 = "One"
50+
51+ [[Wsublist]]
3952 String2 = "Two"
4053
41- [[sublist ]]
54+ [[Wsublist ]]
4255 String2 = "Three"
4356` )
4457
@@ -53,6 +66,18 @@ func TestBasicMarshal(t *testing.T) {
5366 }
5467}
5568
69+ func TestBasicMarshalOrdered (t * testing.T ) {
70+ var result bytes.Buffer
71+ err := NewEncoder (& result ).Order (OrderPreserve ).Encode (basicTestData )
72+ if err != nil {
73+ t .Fatal (err )
74+ }
75+ expected := basicTestTomlOrdered
76+ if ! bytes .Equal (result .Bytes (), expected ) {
77+ t .Errorf ("Bad marshal: expected\n -----\n %s\n -----\n got\n -----\n %s\n -----\n " , expected , result .Bytes ())
78+ }
79+ }
80+
5681func TestBasicMarshalWithPointer (t * testing.T ) {
5782 result , err := Marshal (& basicTestData )
5883 if err != nil {
@@ -64,6 +89,18 @@ func TestBasicMarshalWithPointer(t *testing.T) {
6489 }
6590}
6691
92+ func TestBasicMarshalOrderedWithPointer (t * testing.T ) {
93+ var result bytes.Buffer
94+ err := NewEncoder (& result ).Order (OrderPreserve ).Encode (& basicTestData )
95+ if err != nil {
96+ t .Fatal (err )
97+ }
98+ expected := basicTestTomlOrdered
99+ if ! bytes .Equal (result .Bytes (), expected ) {
100+ t .Errorf ("Bad marshal: expected\n -----\n %s\n -----\n got\n -----\n %s\n -----\n " , expected , result .Bytes ())
101+ }
102+ }
103+
67104func TestBasicUnmarshal (t * testing.T ) {
68105 result := basicMarshalTestStruct {}
69106 err := Unmarshal (basicTestToml , & result )
@@ -78,39 +115,39 @@ func TestBasicUnmarshal(t *testing.T) {
78115
79116type testDoc struct {
80117 Title string `toml:"title"`
81- Basics testDocBasics `toml:"basic"`
82118 BasicLists testDocBasicLists `toml:"basic_lists"`
119+ SubDocPtrs []* testSubDoc `toml:"subdocptrs"`
83120 BasicMap map [string ]string `toml:"basic_map"`
84121 Subdocs testDocSubs `toml:"subdoc"`
122+ Basics testDocBasics `toml:"basic"`
85123 SubDocList []testSubDoc `toml:"subdoclist"`
86- SubDocPtrs []* testSubDoc `toml:"subdocptrs"`
87124 err int `toml:"shouldntBeHere"`
88125 unexported int `toml:"shouldntBeHere"`
89126 Unexported2 int `toml:"-"`
90127}
91128
92129type testDocBasics struct {
130+ Uint uint `toml:"uint"`
93131 Bool bool `toml:"bool"`
94- Date time.Time `toml:"date"`
95132 Float float32 `toml:"float"`
96133 Int int `toml:"int"`
97- Uint uint `toml:"uint"`
98134 String * string `toml:"string"`
135+ Date time.Time `toml:"date"`
99136 unexported int `toml:"shouldntBeHere"`
100137}
101138
102139type testDocBasicLists struct {
140+ Floats []* float32 `toml:"floats"`
103141 Bools []bool `toml:"bools"`
104142 Dates []time.Time `toml:"dates"`
105- Floats []* float32 `toml:"floats"`
106143 Ints []int `toml:"ints"`
107- Strings []string `toml:"strings"`
108144 UInts []uint `toml:"uints"`
145+ Strings []string `toml:"strings"`
109146}
110147
111148type testDocSubs struct {
112- First testSubDoc `toml:"first"`
113149 Second * testSubDoc `toml:"second"`
150+ First testSubDoc `toml:"first"`
114151}
115152
116153type testSubDoc struct {
@@ -174,6 +211,18 @@ func TestDocMarshal(t *testing.T) {
174211 }
175212}
176213
214+ func TestDocMarshalOrdered (t * testing.T ) {
215+ var result bytes.Buffer
216+ err := NewEncoder (& result ).Order (OrderPreserve ).Encode (docData )
217+ if err != nil {
218+ t .Fatal (err )
219+ }
220+ expected , _ := ioutil .ReadFile ("marshal_OrderPreserve_test.toml" )
221+ if ! bytes .Equal (result .Bytes (), expected ) {
222+ t .Errorf ("Bad marshal: expected\n -----\n %s\n -----\n got\n -----\n %s\n -----\n " , expected , result .Bytes ())
223+ }
224+ }
225+
177226func TestDocMarshalPointer (t * testing.T ) {
178227 result , err := Marshal (& docData )
179228 if err != nil {
0 commit comments