-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
153 lines (131 loc) · 4.51 KB
/
file_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
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 efp
import (
"fmt"
"testing"
"github.com/end-r/goutil"
)
func testFile(name string) string {
return fmt.Sprintf("test_files/%s", name)
}
func TestUnknownFile(t *testing.T) {
_, errs := PrototypeFile("not_found.efp")
goutil.Assert(t, errs != nil, "errs should not be nil")
}
func TestEmptyFile(t *testing.T) {
p, errs := PrototypeFile(testFile("empty.efp"))
goutil.Assert(t, errs == nil, "errs should be nil")
goutil.AssertNow(t, p != nil, "p should not be nil")
}
func TestLargeFile(t *testing.T) {
p, errs := PrototypeFile(testFile("large.efp"))
goutil.Assert(t, errs == nil, "errs should be nil")
goutil.AssertNow(t, p != nil, "p should not be nil")
}
func TestLargeFiles(t *testing.T) {
p, errs := PrototypeFile(testFile("large.efp"))
goutil.Assert(t, errs == nil, "errs should be nil")
goutil.AssertNow(t, p != nil, "p should not be nil")
//e, errs := p.ValidateFiles("")
}
func TestVMGenFile(t *testing.T) {
p, errs := PrototypeFile(testFile("vm.efp"))
goutil.Assert(t, errs == nil, "errs should be nil")
goutil.AssertNow(t, p != nil, "p should not be nil")
e, errs := p.ValidateString(`
name = "Example"
author = "[7][7][7]"
instruction("ADD", "01"){
description = "Finds the sum of two numbers."
fuel = 100
}
instruction("PUSH", "02"){
description = "Pushes a number onto the stack."
fuel = 30
}
instruction("TEST", "03"){
description = "Test instruction."
fuel = 30
}
`)
goutil.Assert(t, errs == nil, "errs should be nil")
goutil.AssertNow(t, e != nil, "e should not be nil")
goutil.Assert(t, len(e.Elements("instruction")) == 3, "wrong instruction length")
goutil.Assert(t, e.FirstField("name").Value(0) == "Example",
fmt.Sprintf("wrong param value %s, expected %s\n",
e.FirstField("name").Value(0), "Example"))
goutil.Assert(t, e.FirstElement("instruction").Parameter(0).Value() == "ADD",
fmt.Sprintf("wrong param value %s, expected %s\n",
e.FirstElement("instruction").Parameter(0).Value(), "ADD"))
}
func TestComplexVMGenFile(t *testing.T) {
p, errs := PrototypeFile(testFile("complex_vm.efp"))
goutil.Assert(t, errs == nil, "prototype errs should be nil")
goutil.AssertNow(t, p != nil, "p should not be nil")
goutil.Assert(t, len(p.elements) == 2, "should be two top level prototype elements")
goutil.Assert(t, len(p.fields) == 2, "should be two top level prototype elements")
goutil.Assert(t, p.Element("category") != nil, "should be a top level category element")
goutil.Assert(t, p.Element("instruction") != nil, "should be a top level category element")
goutil.Assert(t, p.Element("category").Element("instruction") != nil, "should be a second level category element")
e, errs := p.ValidateString(`
name = "Example"
author = "[7][7][7]"
instruction("ADD", "01"){
description = "Finds the sum of two numbers."
validate = 2
fuel = 100
}
category("Pushers"){
description = "Things which push in general."
instruction("PUSH", "02"){
description = "Pushes a number onto the stack."
validate = 0
fuel = 10
}
instruction("TEST", "03"){
description = "Test instruction."
validate = 0
fuel = 30
}
}
`)
goutil.Assert(t, errs == nil, "errs should be nil")
goutil.AssertNow(t, e != nil, "e should not be nil")
}
func TestMinMaxFile(t *testing.T) {
p, errs := PrototypeFile(testFile("min_max.efp"))
goutil.Assert(t, errs == nil, "prototype errs should be nil")
goutil.AssertNow(t, p != nil, "p should not be nil")
goutil.Assert(t, len(p.elements) == 1, "should be one top level prototype element")
goutil.Assert(t, len(p.fields) == 0, "should be zero top level prototype elements")
goutil.Assert(t, p.Element("dog") != nil, "should be a top level dog element")
goutil.Assert(t, p.Element("dog").Field("name") != nil, "should be a second level field element")
e, errs := p.ValidateString(`
dog("Alex"){
name = "AC"
name = "Centurion"
}
`)
goutil.Assert(t, errs == nil, "errs should be nil")
goutil.AssertNow(t, e != nil, "e should not be nil")
}
func TestMinMaxFileErrors(t *testing.T) {
p, errs := PrototypeFile(testFile("min_max.efp"))
goutil.Assert(t, errs == nil, "prototype errs should be nil")
goutil.AssertNow(t, p != nil, "p should not be nil")
_, errs = p.ValidateString(`
dog("Alex"){
name = "AC"
}
`)
goutil.Assert(t, errs != nil, "errs should not be nil")
_, errs = p.ValidateString(`
dog("Alex"){
name = "AC"
name = "AC"
name = "AC"
name = "AC"
name = "AC"
}
`)
goutil.Assert(t, errs != nil, "errs should not be nil")
}