/
assertion.go
177 lines (150 loc) · 4.3 KB
/
assertion.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package core
import (
"bufio"
"bytes"
"errors"
"strings"
"github.com/inoxlang/inox/internal/globals/globalnames"
"github.com/inoxlang/inox/internal/parse"
pprint "github.com/inoxlang/inox/internal/prettyprint"
"github.com/inoxlang/inox/internal/utils"
)
const ASSERTION_BUFF_WRITER_SIZE = 100
// An AssertionError is raised when an assertion statement fails (false condition).
type AssertionError struct {
msg string
data *AssertionData
isTestAssertion bool
testModule *Module //set if isTestAssertion
}
func (err *AssertionError) ShallowCopy() *AssertionError {
return &AssertionError{
msg: err.msg,
data: err.data,
isTestAssertion: err.isTestAssertion,
testModule: err.testModule,
}
}
func (err AssertionError) IsTestAssertion() bool {
return err.isTestAssertion
}
func (err AssertionError) Error() string {
if err.data == nil || !err.isTestAssertion {
return err.msg
}
buf := bytes.NewBufferString(err.msg)
w := bufio.NewWriterSize(buf, ASSERTION_BUFF_WRITER_SIZE)
err.writeExplanation(w, &PrettyPrintConfig{
PrettyPrintConfig: pprint.PrettyPrintConfig{
MaxDepth: 10,
Colorize: false,
Compact: false,
Indent: []byte{' ', ' '},
},
})
w.Flush()
return buf.String()
}
// writeExplanation attempts to determine an explanation about why the assertion failed,
// if an explanation is found it is written to w.
func (err AssertionError) writeExplanation(w *bufio.Writer, config *PrettyPrintConfig) {
expr := err.data.assertionStatement.Expr
switch node := expr.(type) {
case *parse.BinaryExpression:
leftVal := err.data.intermediaryValues[node.Left]
rightVal := err.data.intermediaryValues[node.Right]
if leftVal == nil || rightVal == nil {
return
}
switch node.Operator {
case
parse.Equal, parse.NotEqual, parse.Is, parse.IsNot,
parse.LessThan, parse.LessOrEqual, parse.GreaterThan, parse.GreaterOrEqual,
parse.Match, parse.Keyof, parse.Substrof:
default:
return
}
lhs := err.stringifyNode(node.Left)
if strings.TrimSpace(lhs) == "" {
return
}
if !parse.NodeIsSimpleValueLiteral(node.Left) {
lhs = lhs + " (" + StringifyWithConfig(leftVal, config) + ")"
}
w.WriteString(": expected ")
w.WriteString(lhs)
switch node.Operator {
case parse.Equal:
w.WriteString(" to be equal to ")
case parse.NotEqual:
w.WriteString(" to not be equal to ")
case parse.Is:
w.WriteString(" to be ")
case parse.IsNot:
w.WriteString(" to not be ")
case parse.LessThan:
w.WriteString(" to be < ")
case parse.LessOrEqual:
w.WriteString(" to be <= ")
case parse.GreaterThan:
w.WriteString(" to be > ")
case parse.GreaterOrEqual:
w.WriteString(" to be >= ")
case parse.Match:
w.WriteString(" to match the patern ")
case parse.Keyof:
w.WriteString(" to be a key of ")
case parse.Substrof:
w.WriteString(" to be a substring of ")
}
rightVal.PrettyPrint(w, config, 0, 0)
}
}
func (err AssertionError) stringifyNode(node parse.Node) string {
if !err.isTestAssertion {
panic(errors.New("node stringification is only supported by test assertion errors"))
}
switch n := node.(type) {
case *parse.Variable:
return "local variable `" + n.Name + "`"
case *parse.GlobalVariable:
return "global variable `" + n.Name + "`"
case *parse.IdentifierLiteral:
return "variable `" + n.Name + "`"
case *parse.CallExpression:
identCallee, ok := n.Callee.(*parse.IdentifierLiteral)
if !ok {
return ""
}
switch identCallee.Name {
case globalnames.LEN_FN:
if len(n.Arguments) != 1 {
return ""
}
actual := err.stringifyNode(n.Arguments[0])
if actual == "" {
return ""
}
return "the length of " + actual
}
}
return parse.SPrint(node, err.testModule.MainChunk.Node, parse.PrintConfig{})
}
func (err AssertionError) PrettyPrint(w *bufio.Writer, config *PrettyPrintConfig) {
w.Write(utils.StringAsBytes(err.msg))
if err.isTestAssertion {
err.writeExplanation(w, config)
}
}
func (err AssertionError) PrettySPrint(config *PrettyPrintConfig) string {
buf := bytes.NewBuffer(nil)
w := bufio.NewWriterSize(buf, ASSERTION_BUFF_WRITER_SIZE)
err.PrettyPrint(w, config)
w.Flush()
return buf.String()
}
// AssertionData is the data recorded about an assertion.
type AssertionData struct {
assertionStatement *parse.AssertionStatement
intermediaryValues map[parse.Node]Value
}