-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappend_test.go
114 lines (96 loc) · 1.75 KB
/
append_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
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
package parse
import (
"fmt"
"testing"
)
type spaces struct {
Spaces string `regexp:"[ \\n\\t\\r]*"`
}
type white struct {
White string `regexp:"([ \\t\\r\\n]*|#[^\n]*\n)*"`
}
type config struct {
Sections []section `parse:"*"`
W white
// Eof string `parse:"!" regexp:".|\\n"`
}
type section struct {
White white
Name string `regexp:"[a-zA-Z][a-zA-Z0-9_]*"`
W1 white
_ string `literal:"{"`
Pairs []pair
W2 spaces
_ string `literal:"}"`
}
type pair struct {
White white
Name string `regexp:"[a-zA-Z][a-zA-Z0-9_]*"`
W1 spaces
_ string `literal:"="`
W2 spaces
Value value
W3 spaces
}
type value struct {
FirstOf
Int int64
String string
Bool bool
Array array
RawString string `regexp:"[^\n]*\n"`
}
type array struct {
_ string `literal:"["`
Values []value `delimiter:","`
W2 white
_ string `literal:"]"`
}
func skip(str []byte, loc int) int {
for i := loc; i < len(str); i++ {
if str[i] != ' ' && str[i] != '\t' && str[i] != '\r' {
return i
}
}
return len(str)
}
var test1 = `
Section {
name = 1
name = "String"
name = true
name = Raw string
name = [ 1, 2 ]
}
Section0 {
name1 = [ 1 , 2 , 3 ]
name2 = 2
name3 = "String"
name4 = true
}
Section1 {
int = -5
bool = false
string = "Hello, world!"
raw_string = this is raw string
array = [ 1, 2, false ]
}
Section2 {
test = [ 1, true, [ 2, false ], "The End!" ]
}
`
func TestAppend(t *testing.T) {
var cfg config
nl, err := Parse(&cfg, []byte(test1), &Options{Debug: true})
if err != nil {
fmt.Printf("Error(%d): %v\n", nl, err)
return
}
println(nl)
res, err := Append(nil, cfg)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("CONFIG:\n%s\n", string(res))
}