1
0
mirror of https://github.com/deuill/go-php.git synced 2024-09-21 00:40:45 +00:00
go-php/context_test.go
Alex Palaistras 6fa93f5160 First version of variable bindings to context.
This commit contains an initial version of variable bindings
to a context, along with tests. Currently supported types are
integers, floating point numbers, and strings.
2015-09-20 01:16:43 +01:00

81 lines
1.5 KiB
Go

package php
import (
"os"
"path"
"strconv"
"testing"
)
var testDir string
var execTests = []struct {
file string // Filename to run
expected string // Expected output
}{
{"echo.php", "Hello World"},
}
func TestContextExec(t *testing.T) {
var w MockWriter
e, _ := New()
ctx, _ := e.NewContext(&w)
defer e.Destroy()
defer ctx.Destroy()
for _, tt := range execTests {
file := path.Join(testDir, tt.file)
if err := ctx.Exec(file); err != nil {
t.Errorf("Context.Exec(%s): %s", tt.file, err)
}
actual := w.String()
w.Reset()
if actual != tt.expected {
t.Errorf("Context.Exec(%s): expected '%s', actual '%s'", tt.file, tt.expected, actual)
}
}
}
var bindTests = []struct {
value interface{} // Value to bind
expected string // Serialized form of value
}{
{42, "i:42;"}, // Integer
{3.14159, "d:3.1415899999999999;"}, // Floating point
{"Such bind", `s:9:"Such bind";`}, // String
}
func TestContextBind(t *testing.T) {
var w MockWriter
e, _ := New()
ctx, _ := e.NewContext(&w)
defer e.Destroy()
defer ctx.Destroy()
for i, tt := range bindTests {
if err := ctx.Bind(strconv.FormatInt(int64(i), 10), tt.value); err != nil {
t.Errorf("Context.Bind(%v): %s", tt.value, err)
}
ctx.Exec(path.Join(testDir, "bind.php"))
actual := w.String()
w.Reset()
if actual != tt.expected {
t.Errorf("Context.Bind(%v): expected '%s', actual '%s'", tt.value, tt.expected, actual)
}
}
}
func init() {
wd, _ := os.Getwd()
testDir = path.Join(wd, "test")
}