-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconvert_test.go
144 lines (131 loc) · 3.74 KB
/
convert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package lua
import (
"testing"
"github.com/stretchr/testify/assert"
lua "github.com/yuin/gopher-lua"
)
/*
BenchmarkConvert/table-10 1862884 649.8 ns/op 920 B/op 16 allocs/op
*/
func BenchmarkConvert(b *testing.B) {
b.ReportAllocs()
l := lua.NewState()
defer l.Close()
b.Run("table", func(b *testing.B) {
v := Table{
"hello": String("world"),
"next": Bool(true),
"age": Number(10),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = v.lvalue(l)
}
})
}
func TestValueOf(t *testing.T) {
type myNil struct{}
tests := []struct {
input any
output Value
}{
{input: complex64(1), output: Nil{}},
{input: Number(1), output: Number(1)},
{input: Bool(true), output: Bool(true)},
{input: String("hi"), output: String("hi")},
{input: int(1), output: Number(1)},
{input: int8(1), output: Number(1)},
{input: int16(1), output: Number(1)},
{input: int32(1), output: Number(1)},
{input: int64(1), output: Number(1)},
{input: uint(1), output: Number(1)},
{input: uint8(1), output: Number(1)},
{input: uint16(1), output: Number(1)},
{input: uint32(1), output: Number(1)},
{input: uint64(1), output: Number(1)},
{input: float32(1), output: Number(1)},
{input: float64(1), output: Number(1)},
{input: true, output: Bool(true)},
{input: "hi", output: String("hi")},
{input: []string{"a", "b"}, output: Strings{"a", "b"}},
{input: []int{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []int8{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []int16{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []int32{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []int64{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []uint{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []uint8{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []uint16{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []uint32{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []uint64{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []float32{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []float64{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []bool{false, true}, output: Bools{false, true}},
{input: nil, output: Nil{}},
{input: struct{}{}, output: Nil{}},
{input: myNil{}, output: Nil{}},
{input: Nil{}, output: Nil{}},
{input: map[string]any{
"A": "foo",
"B": "bar",
}, output: Table{
"A": String("foo"),
"B": String("bar"),
}},
}
for _, tc := range tests {
assert.Equal(t, tc.output, ValueOf(tc.input))
assert.NotEmpty(t, tc.output.String())
}
}
func TestResultOfArray(t *testing.T) {
mp := []map[string]any{
{"hello": []float64{1, 2, 3}},
{"next": true},
{"hello": "world"},
}
l := lua.NewState()
val := lvalueOf(l, mp)
res := resultOf(val)
array, ok := res.(Array)
assert.True(t, ok)
assert.Len(t, array, 3)
resMp := array.Native().([]any)
for i, val := range resMp {
assert.Equal(t, mp[i], val.(map[string]any))
}
}
func TestResultComplexMap(t *testing.T) {
mp := []map[string]any{
{"map": []map[string]any{
{"e": "f", "g": "h"}},
},
}
l := lua.NewState()
val := lvalueOf(l, mp)
res := resultOf(val)
array, ok := res.(Array)
assert.True(t, ok)
resMp := array.Native().([]any)
assert.Len(t, resMp, len(mp))
}
func TestResultOfMap(t *testing.T) {
mp := map[string]any{
"string": "aj",
"numbers": []float64{1, 2, 3},
"bool": true,
}
l := lua.NewState()
val := lvalueOf(l, mp)
res := resultOf(val)
table, ok := res.(Table)
assert.True(t, ok)
assert.Len(t, table, 3)
resMp, ok := table.Native().(map[string]any)
assert.True(t, ok)
for _, key := range []string{"string", "numbers", "bool"} {
assert.Equal(t, mp[key], resMp[key])
}
}