Test context result methods

This commit is contained in:
Brandon Dyck 2022-11-27 10:56:48 -07:00
parent f2d14f03f1
commit 4b4b9abcbc

View File

@ -31,6 +31,33 @@ func mustSucceedF(t *testing.T, f func() error) {
} }
} }
func setupVM(t *testing.T, script string) (vm *ph7.VM, close func()) {
t.Helper()
engine, err := ph7.NewEngine()
mustSucceed(t, err)
vm, err = engine.Compile([]byte(script), true)
if err != nil {
mustSucceed(t, engine.Close())
mustSucceed(t, err)
}
return vm, func() {
mustSucceed(t, vm.Close())
mustSucceed(t, engine.Close())
}
}
func expectOutput(t *testing.T, vm *ph7.VM, expected string) {
t.Helper()
mustSucceed(t, vm.Exec())
output, err := vm.ExtractOutput()
mustSucceed(t, err)
if output != expected {
t.Fatalf("expected %q, got %q", expected, output)
}
}
func TestCore(t *testing.T) { func TestCore(t *testing.T) {
engine, err := ph7.NewEngine() engine, err := ph7.NewEngine()
mustSucceed(t, err) mustSucceed(t, err)
@ -72,27 +99,21 @@ func TestCompileError(t *testing.T) {
} }
func TestOutputWriter(t *testing.T) { func TestOutputWriter(t *testing.T) {
const source = `<?php const source = `
for ($x = 0; $x <= 1000; $x++) { for ($x = 0; $x <= 1000; $x++) {
echo $x; echo $x;
} }`
?>`
var expected bytes.Buffer var expected bytes.Buffer
for i := 0; i <= 1000; i++ { for i := 0; i <= 1000; i++ {
_, err := fmt.Fprint(&expected, i) _, err := fmt.Fprint(&expected, i)
mustSucceed(t, err) mustSucceed(t, err)
} }
engine, err := ph7.NewEngine() vm, close := setupVM(t, source)
mustSucceed(t, err) defer close()
defer mustSucceedF(t, engine.Close)
vm, err := engine.Compile([]byte(source), false)
mustSucceed(t, err)
defer vm.Close()
var actual bytes.Buffer var actual bytes.Buffer
err = vm.SetOutputWriter(&actual) err := vm.SetOutputWriter(&actual)
mustSucceed(t, err) mustSucceed(t, err)
mustSucceed(t, vm.Exec()) mustSucceed(t, vm.Exec())
@ -104,3 +125,63 @@ func TestOutputWriter(t *testing.T) {
// TODO Test failing writer, including vm.OutputWriteError // TODO Test failing writer, including vm.OutputWriteError
// TODO Test setting writer twice // TODO Test setting writer twice
type CtxFunc func(*ph7.Context) error
func (r CtxFunc) Call(ctx *ph7.Context, _ []*ph7.Value) ph7.ResultCode {
err := r(ctx)
if err != nil {
return r(ctx).(ph7.Error).Code
}
return ph7.ResultCodeOK
}
func testResultCode(t *testing.T, expectedOutput string, f CtxFunc) {
t.Helper()
const script = "echo doStuff();"
vm, close := setupVM(t, script)
defer close()
mustSucceed(t, vm.CreateFunction("doStuff", f))
expectOutput(t, vm, expectedOutput)
}
func TestContextResultInt(t *testing.T) {
testResultCode(t, "42", func(ctx *ph7.Context) error {
return ctx.ResultInt(42)
})
}
func TestContextResultInt64(t *testing.T) {
testResultCode(t, "1234", func(ctx *ph7.Context) error {
return ctx.ResultInt64(1234)
})
}
func TestContextResultBool(t *testing.T) {
testResultCode(t, "TRUE", func(ctx *ph7.Context) error {
return ctx.ResultBool(true)
})
testResultCode(t, "", func(ctx *ph7.Context) error {
return ctx.ResultBool(false)
})
}
func TestContextResultDouble(t *testing.T) {
testResultCode(t, "1.23", func(ctx *ph7.Context) error {
return ctx.ResultDouble(1.23)
})
}
func TestContextResultNull(t *testing.T) {
testResultCode(t, "", (*ph7.Context).ResultNull)
}
func TestContextResultString(t *testing.T) {
testResultCode(t, "hello", func(ctx *ph7.Context) error {
return ctx.ResultString("hello")
})
}
// TODO TestContextResultValue
// TODO TestContextResultResource