-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoml_test.go
More file actions
153 lines (129 loc) · 3.45 KB
/
Copy pathtoml_test.go
File metadata and controls
153 lines (129 loc) · 3.45 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package contexting
import (
"bytes"
"os"
"path/filepath"
"testing"
"github.com/BurntSushi/toml"
)
type TestConfig struct {
App struct {
Name string
Version string
}
Server struct {
Port int
Host string
}
Ignore []string
}
func TestTOMLDecode(t *testing.T) {
configStr := `
Ignore = [".git", "node_modules", "vendor"]
[App]
Name = "test-app"
Version = "1.0.0"
[Server]
Port = 8080
Host = "localhost"
`
var config TestConfig
metadata, err := toml.Decode(configStr, &config)
if err != nil {
t.Fatalf("Failed to decode TOML: %v", err)
}
t.Logf("Keys: %v", metadata.Keys())
t.Logf("Undecoded: %v", metadata.Undecoded())
if config.App.Name != "test-app" {
t.Errorf("Expected App.Name to be 'test-app', got '%s'", config.App.Name)
}
if config.App.Version != "1.0.0" {
t.Errorf("Expected App.Version to be '1.0.0', got '%s'", config.App.Version)
}
if config.Server.Port != 8080 {
t.Errorf("Expected Server.Port to be 8080, got %d", config.Server.Port)
}
if config.Server.Host != "localhost" {
t.Errorf("Expected Server.Host to be 'localhost', got '%s'", config.Server.Host)
}
if len(config.Ignore) != 3 {
t.Errorf("Expected 3 ignore patterns, got %d", len(config.Ignore))
}
if len(config.Ignore) > 0 && config.Ignore[0] != ".git" {
t.Errorf("Expected first ignore pattern to be '.git', got '%s'", config.Ignore[0])
}
}
func TestTOMLDecodeFromFile(t *testing.T) {
tmpDir := t.TempDir()
configStr := `
[App]
Name = "my-app"
Version = "2.0.0"
[Server]
Port = 3000
Host = "0.0.0.0"
Ignore = ["*.log", "*.tmp"]
`
configPath := filepath.Join(tmpDir, "config.toml")
err := os.WriteFile(configPath, []byte(configStr), 0o644)
if err != nil {
t.Fatalf("Failed to write config file: %v", err)
}
var config TestConfig
_, err = toml.DecodeFile(configPath, &config)
if err != nil {
t.Fatalf("Failed to decode TOML file: %v", err)
}
if config.App.Name != "my-app" {
t.Errorf("Expected App.Name to be 'my-app', got '%s'", config.App.Name)
}
if config.Server.Port != 3000 {
t.Errorf("Expected Server.Port to be 3000, got %d", config.Server.Port)
}
}
func TestTOMLEncode(t *testing.T) {
var config TestConfig
config.App.Name = "encoded-app"
config.App.Version = "1.0.0"
config.Server.Port = 9000
config.Server.Host = "127.0.0.1"
config.Ignore = []string{".gitignore", "*.swp"}
var encoded bytes.Buffer
encoder := toml.NewEncoder(&encoded)
err := encoder.Encode(config)
if err != nil {
t.Fatalf("Failed to encode TOML: %v", err)
}
encodedStr := encoded.String()
if len(encodedStr) == 0 {
t.Error("Expected encoded string to not be empty")
}
var decoded TestConfig
_, err = toml.Decode(encodedStr, &decoded)
if err != nil {
t.Fatalf("Failed to decode encoded TOML: %v", err)
}
if decoded.App.Name != config.App.Name {
t.Errorf("Expected decoded App.Name to be '%s', got '%s'", config.App.Name, decoded.App.Name)
}
if decoded.Server.Port != config.Server.Port {
t.Errorf("Expected decoded Server.Port to be %d, got %d", config.Server.Port, decoded.Server.Port)
}
}
func TestTOMLDecodeEmpty(t *testing.T) {
var config TestConfig
_, err := toml.Decode("", &config)
if err != nil {
t.Fatalf("Failed to decode empty TOML: %v", err)
}
if config.App.Name != "" {
t.Errorf("Expected empty App.Name, got '%s'", config.App.Name)
}
}
func TestTOMLDecodeInvalid(t *testing.T) {
var config TestConfig
_, err := toml.Decode("invalid toml [[[", &config)
if err == nil {
t.Error("Expected error for invalid TOML, got nil")
}
}