-
Notifications
You must be signed in to change notification settings - Fork 2
/
engine_integration_test.go
65 lines (53 loc) · 1.67 KB
/
engine_integration_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
package easytemplate_test
import (
"context"
"fmt"
"os"
"testing"
"github.com/dop251/goja"
"github.com/speakeasy-api/easytemplate"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEngine_RunScript_Success(t *testing.T) {
files, err := os.ReadDir("./testdata/expected")
require.NoError(t, err)
expectedFiles := make(map[string]string, len(files))
for _, file := range files {
data, err := os.ReadFile("./testdata/expected/" + file.Name())
require.NoError(t, err)
expectedFiles[file.Name()] = string(data)
}
e := easytemplate.New(
easytemplate.WithSearchLocations([]string{"./testdata"}),
easytemplate.WithWriteFunc(func(outFile string, data []byte) error {
expectedData, ok := expectedFiles[outFile]
if ok {
assert.Equal(t, expectedData, string(data))
delete(expectedFiles, outFile)
} else {
require.NoError(t, os.WriteFile("./testdata/expected/"+outFile, data, 0o644))
}
return nil
}),
easytemplate.WithJSFuncs(map[string]func(call easytemplate.CallContext) goja.Value{
"multiply": func(call easytemplate.CallContext) goja.Value {
a := call.Argument(0).ToInteger()
b := call.Argument(1).ToInteger()
return call.VM.ToValue(a * b)
},
}),
easytemplate.WithTemplateFuncs(map[string]any{
"toFloatWithPrecision": func(i int64, precision int) string {
return fmt.Sprintf("%.*f", precision, float64(i))
},
}),
)
err = e.Init(context.Background(), map[string]interface{}{
"Test": "global",
})
require.NoError(t, err)
err = e.RunScript(context.Background(), "scripts/test.js")
require.NoError(t, err)
assert.Empty(t, expectedFiles, "not all expected files were written")
}