2022-11-25 01:58:10 +00:00
|
|
|
package ph7_test
|
|
|
|
|
|
|
|
import (
|
2022-11-25 07:56:56 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-11-25 01:58:10 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
ph7 "git.codemonkeysoftware.net/b/go-ph7"
|
|
|
|
)
|
|
|
|
|
|
|
|
func mustSucceed(t *testing.T, err error) {
|
2022-11-25 04:51:44 +00:00
|
|
|
t.Helper()
|
2022-11-25 01:58:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 04:51:44 +00:00
|
|
|
func mustFail(t *testing.T, err error) {
|
|
|
|
t.Helper()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 01:58:10 +00:00
|
|
|
func mustSucceedF(t *testing.T, f func() error) {
|
2022-11-25 04:51:44 +00:00
|
|
|
t.Helper()
|
2022-11-25 01:58:10 +00:00
|
|
|
err := f()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 23:30:16 +00:00
|
|
|
func TestCore(t *testing.T) {
|
2022-11-25 01:58:10 +00:00
|
|
|
engine, err := ph7.NewEngine()
|
|
|
|
mustSucceed(t, err)
|
|
|
|
defer mustSucceedF(t, engine.Close)
|
|
|
|
|
|
|
|
vm, err := engine.Compile([]byte("<?php echo 'Hello world!'; ?>"), false)
|
|
|
|
mustSucceed(t, err)
|
|
|
|
defer mustSucceedF(t, vm.Close)
|
|
|
|
|
|
|
|
mustSucceed(t, vm.Exec())
|
|
|
|
output, err := vm.ExtractOutput()
|
|
|
|
mustSucceed(t, err)
|
|
|
|
if output != "Hello world!" {
|
|
|
|
t.Fatalf("unexpected output: %s", output)
|
|
|
|
}
|
2022-11-26 23:30:16 +00:00
|
|
|
|
|
|
|
mustSucceed(t, vm.Reset())
|
|
|
|
|
|
|
|
mustSucceed(t, vm.Exec())
|
|
|
|
output, err = vm.ExtractOutput()
|
|
|
|
mustSucceed(t, err)
|
|
|
|
if output != "Hello world!" {
|
|
|
|
t.Fatalf("unexpected output: %s", output)
|
|
|
|
}
|
2022-11-25 01:58:10 +00:00
|
|
|
}
|
2022-11-25 04:51:44 +00:00
|
|
|
|
|
|
|
func TestCompileError(t *testing.T) {
|
|
|
|
engine, err := ph7.NewEngine()
|
|
|
|
mustSucceed(t, err)
|
|
|
|
defer mustSucceedF(t, engine.Close)
|
|
|
|
|
|
|
|
_, err = engine.Compile([]byte("<?php 1234 echo 'Hello world!'; ?>"), false)
|
|
|
|
mustFail(t, err)
|
|
|
|
msg, err := engine.ErrLog()
|
|
|
|
mustSucceed(t, err)
|
|
|
|
if msg == "" {
|
|
|
|
t.Fatalf("expected error message, got nothing")
|
|
|
|
}
|
|
|
|
}
|
2022-11-25 07:56:56 +00:00
|
|
|
|
|
|
|
func TestOutputWriter(t *testing.T) {
|
|
|
|
const source = `<?php
|
|
|
|
for ($x = 0; $x <= 1000; $x++) {
|
|
|
|
echo $x;
|
|
|
|
}
|
|
|
|
?>`
|
|
|
|
var expected bytes.Buffer
|
|
|
|
for i := 0; i <= 1000; i++ {
|
|
|
|
_, err := fmt.Fprint(&expected, i)
|
|
|
|
mustSucceed(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
engine, err := ph7.NewEngine()
|
|
|
|
mustSucceed(t, err)
|
|
|
|
defer mustSucceedF(t, engine.Close)
|
|
|
|
|
|
|
|
vm, err := engine.Compile([]byte(source), false)
|
|
|
|
mustSucceed(t, err)
|
|
|
|
defer vm.Close()
|
|
|
|
|
|
|
|
var actual bytes.Buffer
|
|
|
|
err = vm.SetOutputWriter(&actual)
|
|
|
|
mustSucceed(t, err)
|
|
|
|
|
|
|
|
mustSucceed(t, vm.Exec())
|
|
|
|
|
|
|
|
if !bytes.Equal(expected.Bytes(), actual.Bytes()) {
|
|
|
|
t.Fatalf("expected %s, got %s", expected.String(), actual.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Test failing writer, including vm.OutputWriteError
|
|
|
|
// TODO Test setting writer twice
|