Skip to content

Commit 3210a2d

Browse files
committed
Fix unit tests
Tests hadn't been updated since Go callbacks were made to return JSON strings
1 parent a6b14d0 commit 3210a2d

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

v8.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func _go_v8_callback(contextId uint32, functionName *C.char, v8Objects *C.v8data
126126
}
127127

128128
func init() {
129-
C.v8_callback_init()
129+
C.v8_callback_init()
130130
}
131131

132132
type V8Context struct {

v8_test.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v8
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"sync"
67
"testing"
@@ -56,26 +57,20 @@ func TestAddFunc(t *testing.T) {
5657
t.Fatal("Unexpected number of _gov8_testFunc's arguments.", len(args))
5758
}
5859
// 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)
6464
}
65-
argVal := int(arg.(float64))
65+
66+
argVal := int(num)
6667
if argVal != 10 {
6768
t.Fatal("Unexpected value for arg 0, expected 10, received:", argVal)
6869
}
6970

7071
// 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"` {
7974
t.Fatal("Unexpected value for arg 1, expected Test string, received:", argVal2)
8075
}
8176

@@ -100,9 +95,21 @@ func TestAddFunc(t *testing.T) {
10095
func TestAddFuncReturnObject(t *testing.T) {
10196
ctx := NewContext()
10297
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+
103110
return map[string]interface{}{
104-
"arg0": int(args[0].(float64)),
105-
"arg1": args[1].(string),
111+
"arg0": arg0,
112+
"arg1": arg1,
106113
}
107114
})
108115
if err != nil {

0 commit comments

Comments
 (0)