-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerators_test.go
146 lines (134 loc) · 4.31 KB
/
generators_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
/**
* Go Interpreter for Blockly
*
* Copyright 2015 Mark T. Tomczak
* https://github.com/fixermark/goblockly
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package goblockly
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"strings"
"testing"
)
// unittestMainEvaluator evaluates a main unit-test block (which just means it
// evaluates all the statements in the block).
func unittestMainEvaluator(i *Interpreter, b *Block) Value {
tests := b.SingleBlockStatementWithName(i, "DO")
return i.Evaluate(tests)
}
// unittestAssertEqualsEvaluator checks the values in assertion match. If not,
// it outputs to Console an explanation as to why the tests were not equal
// prefixed with "FAIL:"
func unittestAssertEqualsEvaluator(i *Interpreter, b *Block) Value {
actual := i.Evaluate(b.SingleBlockValueWithName(i, "ACTUAL"))
expected := i.Evaluate(b.SingleBlockValueWithName(i, "EXPECTED"))
if !expected.Equals(i, actual) {
msg := b.SingleFieldWithName(i, "MESSAGE")
_, err := fmt.Fprintf(i.Console, "FAIL: %s: Expected %s, got %s\n",
msg, expected.AsString(i), actual.AsString(i))
if err != nil {
panic(err)
}
}
return nilValue
}
// unittestAssertValueEvaluator asserts the specified value is true, false, or
// null. If not, it outputs to Console an explanation as to why the tests were
// not equal prefixed with "FAIL:"
func unittestAssertValueEvaluator(i *Interpreter, b *Block) Value {
actual := i.Evaluate(b.SingleBlockValueWithName(i, "ACTUAL"))
expected := b.SingleFieldWithName(i, "EXPECTED")
var matched bool
switch expected {
case "TRUE":
matched = actual.AsBoolean(i)
case "FALSE":
matched = !actual.AsBoolean(i)
case "NULL":
_, ok := actual.(NilValue)
matched = ok
default:
i.Fail("Unknown value assert expected: '" + expected + "'")
}
if !matched {
msg := b.SingleFieldWithName(i, "MESSAGE")
_, err := fmt.Fprintf(i.Console, "FAIL: %s: Expected %s, got %s\n",
msg, expected, actual.AsString(i))
if err != nil {
panic(err)
}
}
return nilValue
}
// unittestFailEvaluator logs a failure by outputting to Console a message
// prefixed with "FAIL:"
func unittestFailEvaluator(i *Interpreter, b *Block) Value {
msg := b.SingleFieldWithName(i, "MESSAGE")
_, err := fmt.Fprintf(i.Console, "FAIL: %s\n", msg)
if err != nil {
panic(err)
}
return nilValue
}
// Run all of the tests in the xmltests directory, which verify the behavior of
// the Blockly interpreter relative to expectations outlined in the Blockly
// project (https://github.com/google/blockly)
func TestBlocklyGenerators(t *testing.T) {
var i Interpreter
PrepareEvaluators()
i.PrefixHandlers = make(map[string]Evaluator)
i.PrefixHandlers["unittest_main"] = unittestMainEvaluator
i.PrefixHandlers["unittest_assertequals"] = unittestAssertEqualsEvaluator
i.PrefixHandlers["unittest_assertvalue"] = unittestAssertValueEvaluator
i.PrefixHandlers["unittest_fail"] = unittestFailEvaluator
fileInfos, err := ioutil.ReadDir("xmltests")
if err != nil {
panic(err)
}
for _, info := range fileInfos {
if strings.HasSuffix(info.Name(), ".xml") {
runTestsInFile(t, &i, "xmltests/"+info.Name())
}
}
}
// runTestsInFile evaluates all the XML in the file and generates an Errorf
// result if any tests fail.
func runTestsInFile(t *testing.T, i *Interpreter, fname string) {
t.Logf("Testing %s", fname)
xmldata, err := ioutil.ReadFile(fname)
if err != nil {
t.Errorf("Unable to read %s: %v\n", fname, err)
return
}
var blocks BlockXml
err = xml.Unmarshal(xmldata, &blocks)
if err != nil {
t.Errorf("Unable to unmarshal %s: %v\n", fname, err)
return
}
var b bytes.Buffer
i.Console = &b
i.FailHandler = func(reason string) {
t.Errorf("%s: Evaluation failed: %s\n", fname, reason)
}
i.Run(blocks.Blocks)
result := b.String()
if strings.Contains(result, "FAIL:") {
t.Errorf("%s: Test failed: %s\n", fname, result)
}
}