@@ -7,58 +7,59 @@ import (
7
7
"math"
8
8
"net"
9
9
"os"
10
+ "reflect"
10
11
"strconv"
11
12
"strings"
12
13
"testing"
13
14
"time"
14
15
)
15
16
16
- func TestEncodeRoundTrip (t * testing.T ) {
17
- type Config struct {
18
- Age int
19
- Cats []string
20
- Pi float64
21
- Perfection []int
22
- DOB time.Time
23
- Ipaddress net.IP
24
- }
17
+ func TestRoundtrip (t * testing.T ) {
18
+ type scan struct {
19
+ Age int `toml:"age"`
20
+ Cats []string `toml:"cats"`
21
+ Pi float64 `toml:"pi"`
22
+ Perfection []int `toml:"perfection"`
23
+ DOB time.Time `toml:"dob"`
24
+ IP net.IP `toml:"ip"`
25
+ LargeFloat float64 `toml:"large_float"`
26
+ }
27
+
28
+ doc := `
29
+ age = 13
30
+ cats = ["one", "two", "three"]
31
+ pi = 3.145
32
+ perfection = [11, 2, 3, 4]
33
+ dob = 2012-01-02T15:16:17Z
34
+ ip = "192.168.59.254"
35
+ large_float = 5e+22
36
+ ` [1 :]
25
37
26
- var inputs = Config {
38
+ want := scan {
27
39
Age : 13 ,
28
40
Cats : []string {"one" , "two" , "three" },
29
41
Pi : 3.145 ,
30
42
Perfection : []int {11 , 2 , 3 , 4 },
31
- DOB : time .Now (),
32
- Ipaddress : net .ParseIP ("192.168.59.254" ),
43
+ DOB : time .Date (2012 , 01 , 02 , 15 , 16 , 17 , 0 , time .UTC ),
44
+ IP : net .ParseIP ("192.168.59.254" ),
45
+ LargeFloat : 5e+22 ,
33
46
}
34
47
35
- var (
36
- firstBuffer bytes.Buffer
37
- secondBuffer bytes.Buffer
38
- outputs Config
39
- )
40
- err := NewEncoder (& firstBuffer ).Encode (inputs )
41
- if err != nil {
42
- t .Fatal (err )
43
- }
44
- _ , err = Decode (firstBuffer .String (), & outputs )
48
+ var s scan
49
+ _ , err := Decode (doc , & s )
45
50
if err != nil {
46
- t .Logf ("Could not decode:\n %s\n " , firstBuffer .String ())
47
51
t .Fatal (err )
48
52
}
49
- err = NewEncoder (& secondBuffer ).Encode (outputs )
50
- if err != nil {
51
- t .Fatal (err )
53
+ if ! reflect .DeepEqual (s , want ) {
54
+ t .Errorf ("\n have: %v\n want: %v" , s , want )
52
55
}
53
- if firstBuffer .String () != secondBuffer .String () {
54
- t .Errorf ("%s\n \n IS NOT IDENTICAL TO\n \n %s" , firstBuffer .String (), secondBuffer .String ())
55
- }
56
- out , err := Marshal (inputs )
56
+
57
+ out , err := Marshal (s )
57
58
if err != nil {
58
59
t .Fatal (err )
59
60
}
60
- if firstBuffer . String ( ) != string ( out ) {
61
- t .Errorf ("%s \n \n IS NOT IDENTICAL TO \n \n %s " , firstBuffer . String (), string (out ))
61
+ if string ( out ) != doc {
62
+ t .Errorf ("\n have: \n %v \n want: \n %v " , string (out ), doc )
62
63
}
63
64
}
64
65
0 commit comments