1
0
mirror of https://github.com/deuill/go-php.git synced 2024-09-21 08:50:45 +00:00
go-php/php_test.go

213 lines
4.2 KiB
Go
Raw Normal View History

// Copyright 2015 Alexander Palaistras. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
2015-10-02 17:07:57 +00:00
package php
import (
"fmt"
"os"
"path"
"strconv"
"testing"
)
var testDir string
2015-09-22 20:55:36 +00:00
type MockWriter struct {
buffer []byte
}
func (m *MockWriter) Write(p []byte) (int, error) {
if m.buffer == nil {
m.buffer = p
} else {
m.buffer = append(m.buffer, p...)
}
return len(p), nil
}
func (m *MockWriter) String() string {
if m.buffer == nil {
return ""
}
return string(m.buffer)
}
func (m *MockWriter) Reset() {
if m.buffer != nil {
m.buffer = m.buffer[:0]
}
}
2015-10-02 17:07:57 +00:00
func TestNewEngineContext(t *testing.T) {
e, err := New()
if err != nil {
t.Errorf("New(): %s", err)
return
2015-10-02 17:07:57 +00:00
}
defer e.Destroy()
_, err = e.NewContext(os.Stdout)
2015-10-02 17:07:57 +00:00
if err != nil {
t.Errorf("NewContext(): %s", err)
}
}
var execTests = []struct {
file string // Filename to run
expected string // Expected output
}{
{"echo.php", "Hello World"},
}
func TestContextExec(t *testing.T) {
var w MockWriter
2015-10-02 17:07:57 +00:00
e, _ := New()
ctx, _ := e.NewContext(&w)
defer e.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)
continue
}
actual := w.String()
w.Reset()
if actual != tt.expected {
t.Errorf("Context.Exec(%s): expected '%s', actual '%s'", tt.file, tt.expected, actual)
}
}
}
var evalTests = []struct {
script string // Script to run
expected string // Expected output
}{
{"echo 'Hello World';", "Hello World"},
{"$i = 10; $d = 20; echo $i + $d;", "30"},
}
func TestContextEval(t *testing.T) {
var w MockWriter
e, _ := New()
ctx, _ := e.NewContext(&w)
defer e.Destroy()
for _, tt := range evalTests {
if _, err := ctx.Eval(tt.script); err != nil {
t.Errorf("Context.Eval(%s): %s", tt.script, err)
continue
}
actual := w.String()
w.Reset()
if actual != tt.expected {
t.Errorf("Context.Eval(%s): expected '%s', actual '%s'", tt.script, tt.expected, actual)
}
}
}
2015-10-02 17:07:57 +00:00
var bindTests = []struct {
value interface{} // Value to bind
expected string // Serialized form of value
}{
2015-10-17 15:00:16 +00:00
// Integer to integer.
2015-10-02 17:07:57 +00:00
{42, "i:42;"},
2015-10-17 15:00:16 +00:00
// Float to double.
2015-10-02 17:07:57 +00:00
{3.14159, "d:3.1415899999999999;"},
2015-10-17 15:00:16 +00:00
// Boolean to boolean.
2015-10-02 17:07:57 +00:00
{true, "b:1;"},
2015-10-17 15:00:16 +00:00
// String to string.
2015-10-02 17:07:57 +00:00
{"Such bind", `s:9:"Such bind";`},
2015-10-17 15:00:16 +00:00
// Simple slice of strings to indexed array.
2015-10-02 17:07:57 +00:00
{[]string{"this", "that"}, `a:2:{i:0;s:4:"this";i:1;s:4:"that";}`},
2015-10-17 15:00:16 +00:00
// Nested slice of integers to indexed array.
2015-10-02 17:07:57 +00:00
{[][]int{[]int{1, 2}, []int{3}}, `a:2:{i:0;a:2:{i:0;i:1;i:1;i:2;}i:1;a:1:{i:0;i:3;}}`},
2015-10-17 15:00:16 +00:00
// Struct to object, with nested struct.
{struct {
I int
C string
F struct {
G bool
}
h bool
}{3, "test", struct {
G bool
}{false}, true}, `O:8:"stdClass":3:{s:1:"I";i:3;s:1:"C";s:4:"test";s:1:"F";O:8:"stdClass":1:{s:1:"G";b:0;}}`},
2015-10-02 17:07:57 +00:00
}
func TestContextBind(t *testing.T) {
var w MockWriter
2015-10-02 17:07:57 +00:00
e, _ := New()
ctx, _ := e.NewContext(&w)
defer e.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)
continue
}
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)
}
}
}
var reverseBindTests = []struct {
script string // Script to run
expected string // Expected value
}{
{"return 'Hello World';", `"Hello World"`},
{"$i = 10; $d = 20; return $i + $d;", `30`},
{"$i = 1.2; $d = 2.4; return $i + $d;", `3.5999999999999996`},
{"$what = true; return $what;", `true`},
{"'This returns nothing';", `<nil>`},
}
func TestContextReverseBind(t *testing.T) {
var w MockWriter
e, _ := New()
ctx, _ := e.NewContext(&w)
defer e.Destroy()
for _, tt := range reverseBindTests {
val, err := ctx.Eval(tt.script)
if err != nil {
t.Errorf("Context.Eval(%s): %s", tt.script, err)
continue
}
actual := fmt.Sprintf("%#v", val.Interface())
if actual != tt.expected {
t.Errorf("Context.Eval(%s): expected '%s', actual '%s'", tt.script, tt.expected, actual)
}
}
}
func init() {
wd, _ := os.Getwd()
2015-10-02 17:07:57 +00:00
testDir = path.Join(wd, ".tests")
}