18 Star 71 Fork 9

ShirDon-廖显东 / php-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
value_test.go 8.48 KB
一键复制 编辑 原始数据 按行查看 历史
githubvip 提交于 2019-11-29 17:32 . commit
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
// Author:Shirdon <https://www.shirdon.com>
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package php
import (
"reflect"
"testing"
)
func TestValueStart(t *testing.T) {
e, _ = New()
t.SkipNow()
}
var valueNewTests = []struct {
value interface{}
expected interface{}
}{
{
nil,
nil,
},
{
42,
int64(42),
},
{
3.14159,
float64(3.14159),
},
{
true,
true,
},
{
"Hello World",
"Hello World",
},
{
[]string{"Knick", "Knack"},
[]interface{}{"Knick", "Knack"},
},
{
[][]string{{"1", "2"}, {"3"}},
[]interface{}{[]interface{}{"1", "2"}, []interface{}{"3"}},
},
{
map[string]int{"biggs": 23, "wedge": 16},
map[string]interface{}{"biggs": int64(23), "wedge": int64(16)},
},
{
map[int]string{10: "this", 20: "that"},
map[string]interface{}{"10": "this", "20": "that"},
},
{
struct {
I int
S string
B bool
h string
}{66, "wow", true, "hidden"},
map[string]interface{}{"I": int64(66), "S": "wow", "B": true},
},
}
func TestValueNew(t *testing.T) {
c, _ := e.NewContext()
for _, tt := range valueNewTests {
val, err := NewValue(tt.value)
if err != nil {
t.Errorf("NewValue('%v'): %s", tt.value, err)
continue
}
if val == nil {
t.Errorf("NewValue('%v'): No error returned but value is `nil`", tt.value)
continue
}
actual := val.Interface()
if reflect.DeepEqual(actual, tt.expected) == false {
t.Errorf("NewValue('%v'): expected '%#v', actual '%#v'", tt.value, tt.expected, actual)
}
val.Destroy()
}
c.Destroy()
}
var valueNewInvalidTests = []interface{}{
uint(10),
make(chan int),
func() {},
[]interface{}{uint(2)},
map[string]interface{}{"t": make(chan bool)},
map[bool]interface{}{false: true},
struct {
T interface{}
}{func() {}},
}
func TestValueNewInvalid(t *testing.T) {
c, _ := e.NewContext()
for _, value := range valueNewInvalidTests {
val, err := NewValue(value)
if err == nil {
val.Destroy()
t.Errorf("NewValue('%v'): Value is invalid but no error occured", value)
}
}
c.Destroy()
}
var valueKindTests = []struct {
value interface{}
expected ValueKind
}{
{
42,
Long,
},
{
3.14159,
Double,
},
{
true,
Bool,
},
{
"Hello World",
String,
},
{
[]string{"Knick", "Knack"},
Array,
},
{
map[string]int{"t": 1, "c": 2},
Map,
},
{
struct {
I int
S string
}{66, "wow"},
Object,
},
}
func TestValueKind(t *testing.T) {
c, _ := e.NewContext()
for _, tt := range valueKindTests {
val, err := NewValue(tt.value)
if err != nil {
t.Errorf("NewValue('%v'): %s", tt.value, err)
continue
}
actual := val.Kind()
if actual != tt.expected {
t.Errorf("Value.Kind('%v'): expected '%#v', actual '%#v'", tt.value, tt.expected, actual)
}
val.Destroy()
}
c.Destroy()
}
var valueIntTests = []struct {
value interface{}
expected int64
}{
{
42,
int64(42),
},
{
3.14159,
int64(3),
},
{
true,
int64(1),
},
{
"Hello World",
int64(0),
},
{
[]string{"Knick", "Knack"},
int64(1),
},
{
map[string]int{"t": 1, "c": 2},
int64(1),
},
{
struct {
I int
S string
}{66, "wow"},
int64(1),
},
}
func TestValueInt(t *testing.T) {
c, _ := e.NewContext()
for _, tt := range valueIntTests {
val, err := NewValue(tt.value)
if err != nil {
t.Errorf("NewValue('%v'): %s", tt.value, err)
continue
}
actual := val.Int()
if reflect.DeepEqual(actual, tt.expected) == false {
t.Errorf("Value.Int('%v'): expected '%#v', actual '%#v'", tt.value, tt.expected, actual)
}
val.Destroy()
}
c.Destroy()
}
var valueFloatTests = []struct {
value interface{}
expected float64
}{
{
42,
float64(42),
},
{
3.14159,
float64(3.14159),
},
{
true,
float64(1),
},
{
"Hello World",
float64(0),
},
{
[]string{"Knick", "Knack"},
float64(1),
},
{
map[string]int{"t": 1, "c": 2},
float64(1),
},
{
struct {
I int
S string
}{66, "wow"},
float64(1),
},
}
func TestValueFloat(t *testing.T) {
c, _ := e.NewContext()
for _, tt := range valueFloatTests {
val, err := NewValue(tt.value)
if err != nil {
t.Errorf("NewValue('%v'): %s", tt.value, err)
continue
}
actual := val.Float()
if reflect.DeepEqual(actual, tt.expected) == false {
t.Errorf("Value.Float('%v'): expected '%#v', actual '%#v'", tt.value, tt.expected, actual)
}
val.Destroy()
}
c.Destroy()
}
var valueBoolTests = []struct {
value interface{}
expected bool
}{
{
42,
true,
},
{
3.14159,
true,
},
{
true,
true,
},
{
"Hello World",
true,
},
{
[]string{"Knick", "Knack"},
true,
},
{
map[string]int{"t": 1, "c": 2},
true,
},
{
struct {
I int
S string
}{66, "wow"},
true,
},
}
func TestValueBool(t *testing.T) {
c, _ := e.NewContext()
for _, tt := range valueBoolTests {
val, err := NewValue(tt.value)
if err != nil {
t.Errorf("NewValue('%v'): %s", tt.value, err)
continue
}
actual := val.Bool()
if reflect.DeepEqual(actual, tt.expected) == false {
t.Errorf("Value.Bool('%v'): expected '%#v', actual '%#v'", tt.value, tt.expected, actual)
}
val.Destroy()
}
c.Destroy()
}
var valueStringTests = []struct {
value interface{}
expected string
}{
{
42,
"42",
},
{
3.14159,
"3.14159",
},
{
true,
"1",
},
{
"Hello World",
"Hello World",
},
{
[]string{"Knick", "Knack"},
"Array",
},
{
map[string]int{"t": 1, "c": 2},
"Array",
},
{
struct {
I int
S string
}{66, "wow"},
"",
},
}
func TestValueString(t *testing.T) {
c, _ := e.NewContext()
for _, tt := range valueStringTests {
val, err := NewValue(tt.value)
if err != nil {
t.Errorf("NewValue('%v'): %s", tt.value, err)
continue
}
actual := val.String()
if reflect.DeepEqual(actual, tt.expected) == false {
t.Errorf("Value.String('%v'): expected '%#v', actual '%#v'", tt.value, tt.expected, actual)
}
val.Destroy()
}
c.Destroy()
}
var valueSliceTests = []struct {
value interface{}
expected interface{}
}{
{
42,
[]interface{}{int64(42)},
},
{
3.14159,
[]interface{}{float64(3.14159)},
},
{
true,
[]interface{}{true},
},
{
"Hello World",
[]interface{}{"Hello World"},
},
{
[]string{"Knick", "Knack"},
[]interface{}{"Knick", "Knack"},
},
{
map[string]int{"t": 1, "c": 2},
[]interface{}{int64(1), int64(2)},
},
{
struct {
I int
S string
}{66, "wow"},
[]interface{}{int64(66), "wow"},
},
}
func TestValueSlice(t *testing.T) {
c, _ := e.NewContext()
for _, tt := range valueSliceTests {
val, err := NewValue(tt.value)
if err != nil {
t.Errorf("NewValue('%v'): %s", tt.value, err)
continue
}
actual := val.Slice()
if reflect.DeepEqual(actual, tt.expected) == false {
t.Errorf("Value.Slice('%v'): expected '%#v', actual '%#v'", tt.value, tt.expected, actual)
}
val.Destroy()
}
c.Destroy()
}
var valueMapTests = []struct {
value interface{}
expected map[string]interface{}
}{
{
42,
map[string]interface{}{"0": int64(42)},
},
{
3.14159,
map[string]interface{}{"0": float64(3.14159)},
},
{
true,
map[string]interface{}{"0": true},
},
{
"Hello World",
map[string]interface{}{"0": "Hello World"},
},
{
[]string{"Knick", "Knack"},
map[string]interface{}{"0": "Knick", "1": "Knack"},
},
{
map[string]int{"t": 1, "c": 2},
map[string]interface{}{"t": int64(1), "c": int64(2)},
},
{
struct {
I int
S string
}{66, "wow"},
map[string]interface{}{"I": int64(66), "S": "wow"},
},
}
func TestValueMap(t *testing.T) {
c, _ := e.NewContext()
for _, tt := range valueMapTests {
val, err := NewValue(tt.value)
if err != nil {
t.Errorf("NewValue('%v'): %s", tt.value, err)
continue
}
actual := val.Map()
if reflect.DeepEqual(actual, tt.expected) == false {
t.Errorf("Value.Map('%v'): expected '%#v', actual '%#v'", tt.value, tt.expected, actual)
}
val.Destroy()
}
c.Destroy()
}
func TestValuePtr(t *testing.T) {
c, _ := e.NewContext()
defer c.Destroy()
val, err := NewValue(42)
if err != nil {
t.Fatalf("NewValue('%v'): %s", 42, err)
}
if val.Ptr() == nil {
t.Errorf("Value.Ptr('%v'): Unable to create pointer from value", 42)
}
}
func TestValueDestroy(t *testing.T) {
c, _ := e.NewContext()
defer c.Destroy()
val, err := NewValue(42)
if err != nil {
t.Fatalf("NewValue('%v'): %s", 42, err)
}
val.Destroy()
if val.value != nil {
t.Errorf("Value.Destroy(): Did not set internal fields to `nil`")
}
// Attempting to destroy a value twice should be a no-op.
val.Destroy()
}
func TestValueEnd(t *testing.T) {
e.Destroy()
t.SkipNow()
}
Go
1
https://gitee.com/shirdonl/php-go.git
git@gitee.com:shirdonl/php-go.git
shirdonl
php-go
php-go
master

搜索帮助