This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecl_test.go
98 lines (73 loc) · 3.05 KB
/
secl_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
package secl
import (
"io/ioutil"
"math/big"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.rls.moe/secl/exec"
"go.rls.moe/secl/parser/context"
"go.rls.moe/secl/types"
)
func TestParseBytes(t *testing.T) {
assert := assert.New(t)
mapList, err := ParseBytes([]byte("( hellO: world false ) 99"))
assert.NoError(err, "No error in parse")
assert.Len(mapList.List, 2)
assert.Len(mapList.Map, 0)
assert.EqualValues(mapList.List[1], &types.Integer{Value: big.NewInt(99)}, "Assert List Entry Equality")
dmp := mapList.List[0].(*types.MapList)
assert.Len(dmp.Map, 1)
assert.EqualValues(&types.String{Value: "world"}, dmp.Map[types.String{Value: "hellO"}])
assert.Len(dmp.List, 1)
assert.EqualValues(&types.Bool{Value: false}, dmp.List[0])
}
func TestParseString(t *testing.T) {
assert := assert.New(t)
mapList, err := ParseString("( hellO: world false ) 99")
assert.NoError(err, "No error in parse")
assert.Len(mapList.List, 2)
assert.Len(mapList.Map, 0)
assert.EqualValues(mapList.List[1], &types.Integer{Value: big.NewInt(99)}, "Assert List Entry Equality")
dmp := mapList.List[0].(*types.MapList)
assert.Len(dmp.Map, 1)
assert.EqualValues(&types.String{Value: "world"}, dmp.Map[types.String{Value: "hellO"}])
assert.Len(dmp.List, 1)
assert.EqualValues(&types.Bool{Value: false}, dmp.List[0])
}
func TestMustParse(t *testing.T) {
assert := require.New(t)
files, err := ioutil.ReadDir("./tests/must-parse")
assert.NoError(err, "Must read test directory")
for _, file := range files {
if filepath.Ext(file.Name()) == ".secl" {
t.Logf("Running test %-45s: ----", file.Name())
fp := filepath.Join("./tests/must-parse", file.Name())
data, err := ioutil.ReadFile(fp)
assert.NoError(err, "Must read test file")
fp2 := filepath.Join("./tests/must-parse", strings.TrimSuffix(file.Name(), ".secl")+".expt")
dataExpected, err := ioutil.ReadFile(fp2)
assert.NoError(err, "Must read expected output file")
ml, err := ParseBytes(data)
assert.NoError(err, "Must parse without error")
t.Logf("Output of Test %-45s: %s", file.Name(), types.PrintDebug(ml))
assert.Equal(string(dataExpected), types.PrintDebug(ml), "Must match expected debug output")
ml2, err := ParseString(types.PrintReproducableValue(ml))
assert.NoError(err)
assert.Equal(types.PrintValue(ml), types.PrintValue(ml2), "Must match reproduced value")
if strings.HasSuffix(file.Name(), ".exec.secl") {
ctx := context.NewParserContext()
ml3, err := exec.Eval(ctx.ToRuntime(), ml)
assert.NoError(err)
t.Logf("Output of Expanded Test %-36s: %s", file.Name(), types.PrintDebug(ml3))
t.Logf("Parsable Output of Expanded Test %-26s: %s", file.Name(), types.PrintReproducableValue(ml3.(*types.MapList)))
fp3 := filepath.Join("./tests/must-parse", strings.TrimSuffix(file.Name(), ".exec.secl")+".expt")
dataExpected, err := ioutil.ReadFile(fp3)
assert.NoError(err, "Must read expected expanded output file")
assert.Equal(string(dataExpected), types.PrintDebug(ml3))
}
}
}
}