Skip to content

Commit 13150fb

Browse files
committed
let's never show sample code that is not runnable...
1 parent 1ce06a4 commit 13150fb

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

terminal/util_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package terminal
2+
3+
import (
4+
"bufio"
5+
"io"
6+
"strings"
7+
"testing"
8+
9+
"github.com/abs-lang/abs/evaluator"
10+
"github.com/abs-lang/abs/lexer"
11+
"github.com/abs-lang/abs/object"
12+
"github.com/abs-lang/abs/parser"
13+
)
14+
15+
func runCode(code string, env *object.Environment) []string {
16+
lex := lexer.New(code)
17+
p := parser.New(lex)
18+
program := p.ParseProgram()
19+
20+
if len(p.Errors()) != 0 {
21+
return p.Errors()
22+
}
23+
24+
evaluated := evaluator.BeginEval(program, env, lex)
25+
26+
if evaluated != nil && evaluated.Type() == object.ERROR_OBJ {
27+
return []string{evaluated.Inspect()}
28+
}
29+
30+
return []string{}
31+
}
32+
33+
func TestAssignStatements(t *testing.T) {
34+
for _, stmt := range exampleStatements {
35+
discard := bufio.NewReadWriter(bufio.NewReader(strings.NewReader("")), bufio.NewWriter(io.Discard))
36+
stdio := &object.Stdio{Stdin: discard, Stdout: discard, Stderr: discard}
37+
errs := runCode(stmt, object.NewEnvironment(stdio, ".", "test", false))
38+
39+
if len(errs) > 0 {
40+
t.Fatalf("%s (code evaluated: %s)", errs[0], stmt)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)