Skip to content

Commit

Permalink
Func file loader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Jun 1, 2024
1 parent 785bbf7 commit 1027c15
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/expressions/funcfile/example.funcfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# test func
double {sumi {0} {0}}
55 changes: 55 additions & 0 deletions pkg/expressions/funcfile/loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package funcfile

import (
"rare/pkg/expressions/stdlib"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLoadDefinitions(t *testing.T) {
data := `# a comment
double {sumi {0} {0}}
# Another comment
quad this equals: \
{multi \
{0} \
{0} \
#Mixed comment
{0} {0}}
`
kb := stdlib.NewStdKeyBuilder()
funcs, err := LoadDefinitions(kb, strings.NewReader(data))
assert.NoError(t, err)
assert.Len(t, funcs, 2)
assert.Contains(t, funcs, "quad")
assert.Contains(t, funcs, "double")

val, err := kb.Compile("{quad 5}")
assert.Nil(t, err)
assert.Equal(t, "this equals: 625", val.BuildKey(nil))
}

func TestLoadDefinitionsErrs(t *testing.T) {
data := `# a comment
unterm unterm {
nofunc
`
kb := stdlib.NewStdKeyBuilder()
funcs, err := LoadDefinitions(kb, strings.NewReader(data))
assert.NotNil(t, err)
assert.Len(t, funcs, 0)
}

func TestLoadFile(t *testing.T) {
kb := stdlib.NewStdKeyBuilder()
funcs, err := LoadDefinitionsFile(kb, "example.funcfile")
assert.NoError(t, err)
assert.Len(t, funcs, 1)

_, err = LoadDefinitionsFile(kb, "notfile.funcfile")
assert.Error(t, err)
}
11 changes: 11 additions & 0 deletions pkg/expressions/funcfile/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ func TestCustomFunc(t *testing.T) {
assert.Equal(t, val, "kd: 250")
}

func TestCustomEdgeCases(t *testing.T) {
k := stdlib.NewStdKeyBuilder()
_, err := createAndAddFunc(k, "err", "{unclosed func")
assert.Error(t, err)

_, err = createAndAddFunc(k, "doublesrc", "{test} {sumi {0} {0}} missing: {1}")
assert.NoError(t, err)
kb, _ := k.Compile("{doublesrc 5}")
assert.Equal(t, "testval 10 missing: ", kb.BuildKey(&testContext))
}

// BenchmarkCustomFunc-4 4767489 244.2 ns/op 16 B/op 2 allocs/op
// BenchmarkCustomFunc-4 7563214 160.4 ns/op 3 B/op 1 allocs/op
func BenchmarkCustomFunc(b *testing.B) {
Expand Down

0 comments on commit 1027c15

Please sign in to comment.