Skip to content
Snippets Groups Projects
Commit 1863c688 authored by Jono Wenger's avatar Jono Wenger
Browse files

Fix headers and add errors_test.go

parent dcd562c1
No related branches found
No related tags found
1 merge request!60Revert "Fail a test to be sure it works"
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx network SEZC //
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
package utils
......
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
package utils
import (
"encoding/json"
"syscall/js"
)
// CopyBytesToGo copies the Uint8Array stored in the js.Value to []byte. This is
// a wrapper for js.CopyBytesToGo to make it more convenient.
func CopyBytesToGo(src js.Value) []byte {
b := make([]byte, src.Length())
js.CopyBytesToGo(b, src)
return b
}
// CopyBytesToJS copies the []byte to a Uint8Array stored in a js.Value. This is
// a wrapper for js.CopyBytesToJS to make it more convenient.
func CopyBytesToJS(src []byte) js.Value {
dst := Uint8Array.New(len(src))
js.CopyBytesToJS(dst, src)
return dst
}
// JsonToJS converts a marshalled JSON bytes to a Javascript object.
func JsonToJS(src []byte) (js.Value, error) {
var inInterface map[string]interface{}
err := json.Unmarshal(src, &inInterface)
if err != nil {
Throw(TypeError, err)
return js.ValueOf(nil), err
}
return js.ValueOf(inInterface), nil
}
// JsToJson converts the Javascript value to JSON.
func JsToJson(value js.Value) string {
return JSON.Call("stringify", value).String()
}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
package utils
import (
"reflect"
"syscall/js"
"testing"
)
func TestCopyBytesToGo(t *testing.T) {
type args struct {
src js.Value
}
tests := []struct {
name string
args args
want []byte
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CopyBytesToGo(tt.args.src); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CopyBytesToGo() = %v, want %v", got, tt.want)
}
})
}
}
func TestCopyBytesToJS(t *testing.T) {
type args struct {
src []byte
}
tests := []struct {
name string
args args
want js.Value
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CopyBytesToJS(tt.args.src); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CopyBytesToJS() = %v, want %v", got, tt.want)
}
})
}
}
func TestJsToJson(t *testing.T) {
type args struct {
value js.Value
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := JsToJson(tt.args.value); got != tt.want {
t.Errorf("JsToJson() = %v, want %v", got, tt.want)
}
})
}
}
func TestJsonToJS(t *testing.T) {
type args struct {
src []byte
}
tests := []struct {
name string
args args
want js.Value
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := JsonToJS(tt.args.src)
if (err != nil) != tt.wantErr {
t.Errorf("JsonToJS() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("JsonToJS() got = %v, want %v", got, tt.want)
}
})
}
}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx network SEZC //
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
package utils
......
......@@ -8,26 +8,33 @@
package utils
import (
"fmt"
"github.com/pkg/errors"
"syscall/js"
"testing"
)
// Tests that TestJsError returns a Javascript Error object with the expected
// message.
func TestJsError(t *testing.T) {
err := errors.Errorf("test error")
jsError := JsError(err)
t.Logf("%+v", jsError)
t.Logf("%+v", jsError.String())
t.Logf("%+v", js.Error{Value: jsError})
err := errors.New("test error")
expectedErr := err.Error()
jsError := JsError(err).Get("message").String()
if jsError != expectedErr {
t.Errorf("Failed to get expected error message."+
"\nexpected: %s\nreceived: %s", expectedErr, jsError)
}
}
// Tests that TestJsTrace returns a Javascript Error object with the expected
// message and stack trace.
func TestJsTrace(t *testing.T) {
}
func TestThrow(t *testing.T) {
}
func Test_throw(t *testing.T) {
err := errors.New("test error")
expectedErr := fmt.Sprintf("%+v", err)
jsError := JsTrace(err).Get("message").String()
if jsError != expectedErr {
t.Errorf("Failed to get expected error message."+
"\nexpected: %s\nreceived: %s", expectedErr, jsError)
}
}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
......@@ -17,7 +10,6 @@
package utils
import (
"encoding/json"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"syscall/js"
......@@ -30,22 +22,6 @@ var (
Uint8Array = js.Global().Get("Uint8Array")
)
// CopyBytesToGo copies the Uint8Array stored in the js.Value to []byte. This is
// a wrapper for js.CopyBytesToGo to make it more convenient.
func CopyBytesToGo(src js.Value) []byte {
b := make([]byte, src.Length())
js.CopyBytesToGo(b, src)
return b
}
// CopyBytesToJS copies the []byte to a Uint8Array stored in a js.Value. This is
// a wrapper for js.CopyBytesToJS to make it more convenient.
func CopyBytesToJS(src []byte) js.Value {
dst := Uint8Array.New(len(src))
js.CopyBytesToJS(dst, src)
return dst
}
// WrapCB wraps a Javascript function in an object so that it can be called
// later with only the arguments and without specifying the function name.
//
......@@ -62,23 +38,6 @@ func WrapCB(parent js.Value, m string) func(args ...interface{}) js.Value {
}
}
// JsonToJS converts a marshalled JSON bytes to a Javascript object.
func JsonToJS(src []byte) (js.Value, error) {
var inInterface map[string]interface{}
err := json.Unmarshal(src, &inInterface)
if err != nil {
Throw(TypeError, err)
return js.ValueOf(nil), err
}
return js.ValueOf(inInterface), nil
}
// JsToJson converts the Javascript value to JSON.
func JsToJson(value js.Value) string {
return JSON.Call("stringify", value).String()
}
type PromiseFn func(resolve, reject func(args ...interface{}) js.Value)
// CreatePromise creates a Javascript promise to return the value of a blocking
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment