Repository navigation
Expand file tree
/
Copy pathruntime_test.go
More file actions
220 lines (197 loc) · 4.88 KB
/
Copy pathruntime_test.go
File metadata and controls
220 lines (197 loc) · 4.88 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package compiled
import (
"fmt"
"sync"
"testing"
"github.com/roc-ops/gottp/internal/compiler"
"github.com/roc-ops/gottp/internal/parser"
)
func TestStatelessParse(t *testing.T) {
templateText := `
<group name="interfaces">
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
</group>
`
// Parse and compile template
tmpl, err := parser.ParseTemplate(templateText)
if err != nil {
t.Fatalf("Failed to parse template: %v", err)
}
comp := compiler.NewCompiler()
compiled, err := comp.CompileTemplate(tmpl)
if err != nil {
t.Fatalf("Failed to compile template: %v", err)
}
runtime := NewRuntime(compiled)
// Test first parse
data1 := `
interface Loopback0
ip address 192.168.0.1/24
!
`
result1, err := runtime.Parse(map[string]string{"Default_Input": data1}, nil, nil)
if err != nil {
t.Fatalf("First parse failed: %v", err)
}
// Test second parse with different data (no reset needed)
data2 := `
interface Vlan100
ip address 10.0.0.1/24
!
`
result2, err := runtime.Parse(map[string]string{"Default_Input": data2}, nil, nil)
if err != nil {
t.Fatalf("Second parse failed: %v", err)
}
// Verify results are different (check that they're not nil and have content)
if result1 == nil || result2 == nil {
t.Error("Results should not be nil")
}
// Verify stateless - parse first data again should give same result
result3, err := runtime.Parse(map[string]string{"Default_Input": data1}, nil, nil)
if err != nil {
t.Fatalf("Third parse failed: %v", err)
}
// Results should be the same (stateless)
// Note: We can't directly compare interface{} values, but we can check structure
if result3 == nil {
t.Error("Third result should not be nil")
}
// Verify we can parse multiple times without issues
for i := 0; i < 5; i++ {
_, err := runtime.Parse(map[string]string{"Default_Input": data1}, nil, nil)
if err != nil {
t.Fatalf("Parse iteration %d failed: %v", i, err)
}
}
}
func TestConcurrentParse(t *testing.T) {
templateText := `
<group name="test">
{{ value }}
</group>
`
tmpl, err := parser.ParseTemplate(templateText)
if err != nil {
t.Fatalf("Failed to parse template: %v", err)
}
comp := compiler.NewCompiler()
compiled, err := comp.CompileTemplate(tmpl)
if err != nil {
t.Fatalf("Failed to compile template: %v", err)
}
// Test concurrent parsing
runtime := NewRuntime(compiled)
var wg sync.WaitGroup
results := make([]interface{}, 10)
errors := make([]error, 10)
for i := 0; i < 10; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
data := map[string]string{
"Default_Input": fmt.Sprintf("test%d", idx),
}
result, err := runtime.Parse(data, nil, nil)
results[idx] = result
errors[idx] = err
}(i)
}
wg.Wait()
// Check for errors
for i, err := range errors {
if err != nil {
t.Errorf("Parse %d failed: %v", i, err)
}
}
}
func TestParseArguments(t *testing.T) {
tests := []struct {
name string
argStr string
expected []string
}{
{
name: "empty string argument",
argStr: "''",
expected: []string{""},
},
{
name: "empty string with other args",
argStr: "'', '*', '!'",
expected: []string{"", "*", "!"},
},
{
name: "replaceall case - empty, asterisk, exclamation",
argStr: "'', '*', '!'",
expected: []string{"", "*", "!"},
},
{
name: "multiple empty strings",
argStr: "'', '', 'test'",
expected: []string{"", "", "test"},
},
{
name: "regular arguments",
argStr: "'hello', 'world'",
expected: []string{"hello", "world"},
},
{
name: "mixed quotes",
argStr: `"", '*', "!"`,
expected: []string{"", "*", "!"},
},
{
name: "no quotes",
argStr: "hello, world",
expected: []string{"hello", "world"},
},
{
name: "single argument",
argStr: "'test'",
expected: []string{"test"},
},
{
name: "single empty argument",
argStr: "''",
expected: []string{""},
},
// Issue #35: strip('"') must pass a double quote, not an empty string.
{
name: "quoted double quote",
argStr: `'"'`,
expected: []string{`"`},
},
{
name: "quoted single quote",
argStr: `"'"`,
expected: []string{`'`},
},
{
name: "quoted value wrapped in the other quote",
argStr: `'"a"', "'b'"`,
expected: []string{`"a"`, `'b'`},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := parseArguments(tt.argStr, nil)
if len(result) != len(tt.expected) {
t.Errorf("parseArguments() returned %d arguments, want %d", len(result), len(tt.expected))
t.Errorf("Got: %v", result)
t.Errorf("Want: %v", tt.expected)
return
}
for i, expected := range tt.expected {
if i >= len(result) {
t.Errorf("parseArguments() missing argument at index %d", i)
continue
}
if result[i] != expected {
t.Errorf("parseArguments() argument[%d] = %q, want %q", i, result[i], expected)
}
}
})
}
}