Skip to content

Commit

Permalink
Table tests for NewValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Genevieve L'Esperance committed Jan 11, 2022
1 parent f6accef commit 14f7e95
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
1 change: 1 addition & 0 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,7 @@ RtnString ValueToDetailString(ValuePtr ptr) {
}
String::Utf8Value ds(iso, str);
rtn.data = CopyString(ds);
rtn.length = ds.length();
return rtn;
}

Expand Down
6 changes: 2 additions & 4 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func Null(iso *Isolate) *Value {
// string -> V8::String
// int32 -> V8::Integer
// uint32 -> V8::Integer
// bool -> V8::Boolean
// int64 -> V8::BigInt
// uint64 -> V8::BigInt
// bool -> V8::Boolean
Expand Down Expand Up @@ -203,9 +202,8 @@ func (v *Value) DetailString() string {
err := newJSError(rtn.error)
panic(err) // TODO: Return a fallback value
}
s := rtn.data
defer C.free(unsafe.Pointer(s))
return C.GoString(s)
defer C.free(unsafe.Pointer(rtn.data))
return C.GoStringN(rtn.data, rtn.length)
}

// Int32 perform the equivalent of `Number(value)` in JS and convert the result to a
Expand Down
57 changes: 32 additions & 25 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,41 +98,48 @@ func TestValueString(t *testing.T) {
}
}

func TestValueString_GoToJSAndBack(t *testing.T) {
func TestNewValue(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
iso := ctx.Isolate()
defer iso.Dispose()
defer ctx.Close()

str := "s\x00s\x00"
jsStr, err := v8.NewValue(iso, str)
if err != nil {
t.Fatal(err)
tests := []struct {
name string
input interface{}
predicate string
}{
{"string", "s\x00s\x00", `str => str === "s\x00s\x00"`},
{"int32", int32(36), `int => int === 36`},
{"bool", true, `b => b === true`},
}

// Test whether the go->js keeps the null chars
val, err := ctx.RunScript("(str) => { return str === String.fromCharCode(115, 0, 115, 0)}", "test.js")
if err != nil {
t.Fatal(err)
}
fn, err := val.AsFunction()
if err != nil {
t.Fatal(err)
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
val, err := ctx.RunScript(tt.predicate, "test.js")
if err != nil {
t.Fatal(err)
}
fn, err := val.AsFunction()
if err != nil {
t.Fatal(err)
}

result, err := fn.Call(ctx.Global(), jsStr)
if err != nil {
t.Fatal(err)
}
if !result.Boolean() {
t.Fatal("unexpected result: expected true, got false")
}
jsVal, err := v8.NewValue(iso, tt.input)
if err != nil {
t.Fatal(err)
}

// Test whether the js->go keeps the null chars
goStr := jsStr.String()
if goStr != str {
t.Errorf("unexpected result: expected %q, got %q", str, goStr)
result, err := fn.Call(ctx.Global(), jsVal)
if err != nil {
t.Fatal(err)
}
if !result.Boolean() {
t.Fatal("unexpected result: expected true, got false")
}
})
}
}

Expand Down

0 comments on commit 14f7e95

Please sign in to comment.