Automate testing of the examples directory

This commit is contained in:
Brian Picciano 2023-11-02 17:19:01 +01:00
parent 4cde5179f1
commit a7a5018f38

53
examples/examples_test.go Normal file
View File

@ -0,0 +1,53 @@
package examples_test
import (
"embed"
"fmt"
"testing"
"code.betamike.com/mediocregopher/ginger/gg"
"code.betamike.com/mediocregopher/ginger/vm"
"github.com/stretchr/testify/assert"
)
//go:embed *.gg
var examplesFS embed.FS
func TestAllExamples(t *testing.T) {
tests := []struct {
path string
in vm.Value
exp vm.Value
}{
{
path: "fib.gg",
in: vm.Value{Value: gg.Number(5)},
exp: vm.Value{Value: gg.Number(5)},
},
{
path: "fib.gg",
in: vm.Value{Value: gg.Number(10)},
exp: vm.Value{Value: gg.Number(55)},
},
{
path: "fib.gg",
in: vm.Value{Value: gg.Number(69)},
exp: vm.Value{Value: gg.Number(117669030460994)},
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s_%v", test.path, test.in), func(t *testing.T) {
f, err := examplesFS.Open(test.path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
got, err := vm.EvaluateSource(f, test.in, vm.GlobalScope)
assert.NoError(t, err)
assert.True(t, test.exp.Equal(got), "%v != %v", test.exp, got)
})
}
}