-
Notifications
You must be signed in to change notification settings - Fork 0
/
slangroom_test.go
76 lines (68 loc) · 2.61 KB
/
slangroom_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
package slangroom
import (
"encoding/json"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestExecuteSimpleZencode(t *testing.T) {
contract := `Given nothing
Then print the string 'Welcome to slangroom-exec 🥳'`
res, success := SlangroomExec("", contract, "", "", "", "")
assert.JSONEq(t, `{"output":["Welcome_to_slangroom-exec_🥳"]}`, res.Output)
assert.Nil(t, success, "Expected success but got failure")
}
func TestExecuteSimpleSlangroom(t *testing.T) {
contract := `Rule unknown ignore
Given I fetch the local timestamp in seconds and output into 'timestamp'
Given I have a 'number' named 'timestamp'
Then print the 'timestamp'`
res, success := SlangroomExec("", contract, "", "", "", "")
assert.Contains(t, res.Output, "timestamp")
var result map[string]interface{}
if err := json.Unmarshal([]byte(res.Output), &result); err == nil {
ts, ok := result["timestamp"].(float64)
assert.True(t, ok, "Expected timestamp to be present")
assert.True(t, ts == float64(int(ts)), "Expected timestamp to be a number")
} else {
t.Errorf("Failed to unmarshal output: %v", err)
}
assert.Nil(t, success, "Expected success but got failure")
}
func TestFailOnBrokenSlangroom(t *testing.T) {
contract := `Gibberish`
res, success := SlangroomExec("", contract, "", "", "", "")
assert.Contains(t, res.Logs, "Invalid Zencode prefix 1: 'Gibberish'")
assert.NotNil(t, success, "Expected failure but got success")
}
func TestFailOnEmptyContract(t *testing.T) {
contract := ``
res, success := SlangroomExec("", contract, "", "", "", "")
assert.Equal(t, "Malformed input: Slangroom contract is empty\n", res.Logs)
assert.NotNil(t, success, "Expected failure but got success")
}
func TestReadDataCorrectly(t *testing.T) {
os.Setenv("FILES_DIR", ".")
filePath := "test/test.txt"
if _, err := os.Stat(filePath); os.IsNotExist(err) {
t.Fatalf("Test file does not exist: %v", err)
}
contract := `Rule unknown ignore
Given I send path 'filename' and read verbatim file content and output into 'content'
Given I have a 'string' named 'filename'
Given I have a 'string' named 'content'
Then print data`
data := `{
"filename": "` + filePath + `"
}`
res, success := SlangroomExec("", contract, data, "", "", "")
assert.Contains(t, res.Output, "Do you know who greets you? 🥒")
assert.Nil(t, success, "Expected success but got failure")
}
func TestFailOnEmptyOrBrokenContract(t *testing.T) {
contract := ``
conf := `error`
res, success := SlangroomExec(conf, contract, "", "", "", "")
assert.Equal(t, "Malformed input: Slangroom contract is empty\n", res.Logs)
assert.NotNil(t, success, "Expected failure but got success")
}