-
Notifications
You must be signed in to change notification settings - Fork 23
/
main_test.go
82 lines (76 loc) · 1.78 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"bytes"
"github.com/pelletier/go-toml"
"reflect"
"testing"
)
func TestConfigDataTOML_empty(t *testing.T) {
b, err := toml.Marshal(ConfigData{})
if err != nil {
t.Errorf("failed to marshal")
}
if len(b) > 0 {
t.Errorf("expected no data but got: %s", string(b))
}
}
func TestConfigDataTOML_all(t *testing.T) {
cfg := ConfigData{
URL: "http://127.0.0.1:8040",
Port: "8545",
RPM: 1000,
NoLimit: []string{"test", "test2"},
Allow: []string{"eth_method", "eth_method2"},
}
data := `Allow = [
"eth_method",
"eth_method2",
]
NoLimit = [
"test",
"test2",
]
Port = "8545"
RPM = 1000
URL = "http://127.0.0.1:8040"
`
var buf bytes.Buffer
err := toml.NewEncoder(&buf).ArraysWithOneElementPerLine(true).Encode(cfg)
if err != nil {
t.Errorf("failed to marshal: %v", err)
}
if have := buf.String(); have != data {
t.Errorf("failed\n\twant: %s\n\thave: %s", data, have)
}
var cfg2 ConfigData
if err := toml.Unmarshal([]byte(data), &cfg2); err != nil {
t.Errorf("failed to unmarshal: %v", err)
}
if !reflect.DeepEqual(cfg2, cfg) {
t.Errorf("failed\n\twant: %#v\n\thave: %#v", cfg, cfg2)
}
}
func TestConfigDataTOML_omitempty(t *testing.T) {
cfg := ConfigData{
URL: "ws://127.0.0.1:8041",
Port: "8546",
}
data := `Port = "8546"
URL = "ws://127.0.0.1:8041"
`
var buf bytes.Buffer
err := toml.NewEncoder(&buf).ArraysWithOneElementPerLine(true).Encode(cfg)
if err != nil {
t.Errorf("failed to marshal: %v", err)
}
if have := buf.String(); have != data {
t.Errorf("failed\n\twant: %s\n\thave: %s", data, have)
}
var cfg2 ConfigData
if err := toml.Unmarshal([]byte(data), &cfg2); err != nil {
t.Errorf("failed to unmarshal: %v", err)
}
if !reflect.DeepEqual(cfg2, cfg) {
t.Errorf("failed\n\twant: %#v\n\thave: %#v", cfg, cfg2)
}
}