1
1
package v8
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"sync"
6
7
"testing"
@@ -56,26 +57,20 @@ func TestAddFunc(t *testing.T) {
56
57
t .Fatal ("Unexpected number of _gov8_testFunc's arguments." , len (args ))
57
58
}
58
59
// First argument
59
- arg := args [0 ]
60
- switch arg .(type ) {
61
- case float64 :
62
- default :
63
- t .Fatal ("Unexpected arg 0 type, expecting float64" )
60
+ var num float64
61
+ err := json .Unmarshal ([]byte (args [0 ].(string )), & num )
62
+ if err != nil {
63
+ t .Fatal ("Error unmarshalling arg 0" , err )
64
64
}
65
- argVal := int (arg .(float64 ))
65
+
66
+ argVal := int (num )
66
67
if argVal != 10 {
67
68
t .Fatal ("Unexpected value for arg 0, expected 10, received:" , argVal )
68
69
}
69
70
70
71
// Second argument
71
- arg = args [1 ]
72
- switch arg .(type ) {
73
- case string :
74
- default :
75
- t .Fatal ("Unexpected arg 1 type, expected string" )
76
- }
77
- argVal2 := arg .(string )
78
- if argVal2 != "Test string" {
72
+ argVal2 := args [1 ].(string )
73
+ if argVal2 != `"Test string"` {
79
74
t .Fatal ("Unexpected value for arg 1, expected Test string, received:" , argVal2 )
80
75
}
81
76
@@ -100,9 +95,21 @@ func TestAddFunc(t *testing.T) {
100
95
func TestAddFuncReturnObject (t * testing.T ) {
101
96
ctx := NewContext ()
102
97
err := ctx .AddFunc ("testFunc" , func (args ... interface {}) interface {} {
98
+ var arg0 float64
99
+ err := json .Unmarshal ([]byte (args [0 ].(string )), & arg0 )
100
+ if err != nil {
101
+ t .Fatal ("Error unmarshalling arg 0" , err )
102
+ }
103
+
104
+ var arg1 string
105
+ err = json .Unmarshal ([]byte (args [1 ].(string )), & arg1 )
106
+ if err != nil {
107
+ t .Fatal ("Error unmarshalling arg 1" , err )
108
+ }
109
+
103
110
return map [string ]interface {}{
104
- "arg0" : int ( args [ 0 ].( float64 )) ,
105
- "arg1" : args [ 1 ].( string ) ,
111
+ "arg0" : arg0 ,
112
+ "arg1" : arg1 ,
106
113
}
107
114
})
108
115
if err != nil {
0 commit comments