-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_test.go
178 lines (138 loc) · 4.86 KB
/
object_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package gal_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/seborama/gal/v9"
)
func TestObjectGetProperty(t *testing.T) {
var nilCar *Car
val, ok := gal.ObjectGetProperty(nilCar, "Mileage")
require.False(t, ok)
assert.Equal(t, "undefined: object interface is nil, not a Go value or invalid", val.String())
zeroCar := &Car{}
val, ok = gal.ObjectGetProperty(zeroCar, "Mileage")
require.True(t, ok)
assert.Equal(t, "0", val.String())
myCar := &Car{
Make: "Lotus",
Mileage: gal.NewNumberFromInt(100),
Speed: 50.345,
MaxSpeed: 250,
}
val, ok = gal.ObjectGetProperty(myCar, "Make")
require.True(t, ok)
assert.Equal(t, "Lotus", val.(gal.String).RawString())
val, ok = gal.ObjectGetProperty(myCar, "Mileage")
require.True(t, ok)
assert.Equal(t, gal.NewNumberFromInt(100), val)
// some bothersome floating point issues...
val, ok = gal.ObjectGetProperty(*myCar, "Speed")
require.True(t, ok)
assert.Equal(t, gal.NewNumberFromFloat(50.345).Trunc(1).String(), val.(gal.Number).Trunc(1).String())
val, ok = gal.ObjectGetProperty(complex(10, 3), "Blah")
require.False(t, ok)
assert.Equal(t, "undefined: object is 'complex128' but only 'struct' and '*struct' are currently supported", val.String())
val, ok = gal.ObjectGetProperty(myCar, "ComplexProperty")
require.False(t, ok)
assert.Equal(t, "undefined: object::*gal_test.Car:ComplexProperty - type 'complex128' cannot be mapped to gal.Value", val.String())
val, ok = gal.ObjectGetProperty(myCar, "MaxSpeed")
require.True(t, ok)
assert.Equal(t, gal.NewNumberFromInt(250), val)
}
func TestObjectGetMethod(t *testing.T) {
myCar := &Car{
Make: "Lotus",
Mileage: gal.NewNumberFromInt(100),
Speed: 50.345,
MaxSpeed: 250,
}
var nilCar *Car
val, ok := gal.ObjectGetMethod(nilCar, "Ignite")
require.True(t, ok)
assert.Equal(t, gal.True, val())
val, ok = gal.ObjectGetMethod(myCar, "DoesNotExist!")
require.False(t, ok)
assert.Equal(t, "undefined: type '*gal_test.Car' does not have a method 'DoesNotExist!' (check if it has a pointer receiver)", val().String())
val, ok = gal.ObjectGetMethod(myCar, "Ignite")
require.True(t, ok)
assert.Equal(t, gal.True, val())
val, ok = gal.ObjectGetMethod(myCar, "CurrentSpeed")
require.True(t, ok)
assert.Equal(t, "50.345", val().(gal.Numberer).Number().Trunc(3).String())
val, ok = gal.ObjectGetMethod(myCar, "CurrentSpeed2")
require.True(t, ok)
assert.Equal(t, "50.345", val().(gal.Numberer).Number().Trunc(3).String())
val, ok = gal.ObjectGetMethod(myCar, "SetSpeed")
require.True(t, ok)
got := val(gal.NewNumberFromFloat(76), gal.True, gal.False)
assert.Equal(t, "undefined: invalid function call - object::*gal_test.Car:SetSpeed - wants 1 args, received 3 instead", got.String())
val, ok = gal.ObjectGetMethod(myCar, "SetSpeed")
require.True(t, ok)
got = val(gal.NewNumberFromFloat(86))
assert.Equal(t, "undefined: invalid function call - object::*gal_test.Car:SetSpeed - must return 1 value, returned 0 instead", got.String())
val, ok = gal.ObjectGetMethod(myCar, "SetSpeed2")
require.True(t, ok)
got = val(gal.NewNumberFromFloat(96))
assert.Equal(t, gal.True, got)
assert.Equal(t, "96", myCar.CurrentSpeed().String())
val, ok = gal.ObjectGetMethod(myCar, "SetSpeed3")
require.True(t, ok)
got = val(gal.NewNumberFromFloat(106))
assert.Equal(t, gal.True, got)
assert.Equal(t, "106", myCar.CurrentSpeed().String())
val, ok = gal.ObjectGetMethod(myCar, "SetSpeed4")
require.True(t, ok)
got = val(gal.NewString("blah"))
assert.Equal(t, "undefined: invalid function call - object::*gal_test.Car:SetSpeed4 - invalid argument type passed to function - reflect: Call using gal.String as type gal_test.fancyType", got.String())
}
type Car struct {
Make string
Mileage gal.Number
Speed float32
MaxSpeed int64
ComplexProperty complex128
}
func (c *Car) Ignite() gal.Value {
return gal.True
}
func (c Car) Shutdown() gal.Value { //nolint: gocritic
return gal.True
}
func (c Car) IsRunning() bool {
return true
}
func (c *Car) CurrentSpeed() gal.Value {
return gal.NewNumberFromFloat(float64(c.Speed))
}
func (c *Car) CurrentSpeed2() float32 {
return c.Speed
}
func (c *Car) SetSpeed(speed gal.Number) {
c.Speed = float32(speed.Float64())
}
func (c *Car) SetSpeed2(speed gal.Number) gal.Bool {
c.Speed = float32(speed.Float64())
return gal.True
}
func (c *Car) SetSpeed3(speed float32) gal.Bool {
c.Speed = speed
return gal.True
}
func (c *Car) TillMaxSpeed(speed gal.Number) gal.Number {
return gal.NewNumberFromInt(c.MaxSpeed).Add(speed.Neg()).(gal.Numberer).Number()
}
type fancyType struct {
Speed float32
}
func (c *Car) SetSpeed4(speed fancyType) gal.Bool {
c.Speed = speed.Speed
return gal.True
}
func (c *Car) String() string {
return fmt.Sprint("Car", c.Make, c.Mileage.String(), c.Speed, c.Make)
}
type Road struct {
Type string
}