Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
xxdk-wasm
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
xxdk-wasm
Commits
d15828e8
Commit
d15828e8
authored
Sep 10, 2022
by
Jono Wenger
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for array.go
parent
4ec93938
No related branches found
No related tags found
1 merge request
!6
XX-4050 / Send E2E test
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
utils/array.go
+13
-2
13 additions, 2 deletions
utils/array.go
utils/array_test.go
+112
-0
112 additions, 0 deletions
utils/array_test.go
utils/convert_test.go
+3
-10
3 additions, 10 deletions
utils/convert_test.go
with
128 additions
and
12 deletions
utils/array.go
+
13
−
2
View file @
d15828e8
...
...
@@ -33,12 +33,23 @@ func Uint8ArrayToBase64(_ js.Value, args []js.Value) interface{} {
// - Decoded uint8 array (Uint8Array).
// - Throws TypeError if decoding the string fails.
func
Base64ToUint8Array
(
_
js
.
Value
,
args
[]
js
.
Value
)
interface
{}
{
b
,
err
:=
base64
.
StdEncoding
.
DecodeString
(
args
[
0
]
.
String
()
)
b
,
err
:=
base64
ToUint8Array
(
args
[
0
]
)
if
err
!=
nil
{
Throw
(
TypeError
,
err
)
}
return
CopyBytesToJS
(
b
)
return
b
}
// base64ToUint8Array is a helper function that returns an error instead of
// throwing it.
func
base64ToUint8Array
(
base64String
js
.
Value
)
(
js
.
Value
,
error
)
{
b
,
err
:=
base64
.
StdEncoding
.
DecodeString
(
base64String
.
String
())
if
err
!=
nil
{
return
js
.
Value
{},
err
}
return
CopyBytesToJS
(
b
),
nil
}
// Uint8ArrayEquals returns true if the two Uint8Array are equal and false
...
...
This diff is collapsed.
Click to expand it.
utils/array_test.go
0 → 100644
+
112
−
0
View file @
d15828e8
////////////////////////////////////////////////////////////////////////////////
// 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/base64"
"fmt"
"strings"
"syscall/js"
"testing"
)
var
testBytes
=
[][]
byte
{
nil
,
{},
{
0
},
{
0
,
1
,
2
,
3
},
{
214
,
108
,
207
,
78
,
229
,
11
,
42
,
219
,
42
,
87
,
205
,
104
,
252
,
73
,
223
,
229
,
145
,
209
,
79
,
111
,
34
,
96
,
238
,
127
,
11
,
105
,
114
,
62
,
239
,
130
,
145
,
82
,
3
},
}
// Tests that a series of Uint8Array Javascript objects are correctly converted
// to base 64 strings with Uint8ArrayToBase64.
func
TestUint8ArrayToBase64
(
t
*
testing
.
T
)
{
for
i
,
val
:=
range
testBytes
{
// Create Uint8Array and set each element individually
jsBytes
:=
Uint8Array
.
New
(
len
(
val
))
for
j
,
v
:=
range
val
{
jsBytes
.
SetIndex
(
j
,
v
)
}
jsB64
:=
Uint8ArrayToBase64
(
js
.
Value
{},
[]
js
.
Value
{
jsBytes
})
expected
:=
base64
.
StdEncoding
.
EncodeToString
(
val
)
if
expected
!=
jsB64
{
t
.
Errorf
(
"Did not receive expected base64 encoded string (%d)."
+
"
\n
expected: %s
\n
received: %s"
,
i
,
expected
,
jsB64
)
}
}
}
// Tests that Base64ToUint8Array correctly decodes a series of base 64 encoded
// strings into Uint8Array.
func
TestBase64ToUint8Array
(
t
*
testing
.
T
)
{
for
i
,
val
:=
range
testBytes
{
b64
:=
base64
.
StdEncoding
.
EncodeToString
(
val
)
jsArr
,
err
:=
base64ToUint8Array
(
js
.
ValueOf
(
b64
))
if
err
!=
nil
{
t
.
Errorf
(
"Failed to convert js.Value to base 64: %+v"
,
err
)
}
// Generate the expected string to match the output of toString() on a
// Uint8Array
expected
:=
strings
.
ReplaceAll
(
fmt
.
Sprintf
(
"%d"
,
val
),
" "
,
","
)[
1
:
]
expected
=
expected
[
:
len
(
expected
)
-
1
]
// Get the string value of the Uint8Array
jsString
:=
jsArr
.
Call
(
"toString"
)
.
String
()
if
expected
!=
jsString
{
t
.
Errorf
(
"Failed to recevie expected string representation of "
+
"the Uint8Array (%d).
\n
expected: %s
\n
received: %s"
,
i
,
expected
,
jsString
)
}
}
}
// Tests that a base 64 encoded string decoded to Uint8Array via
// Base64ToUint8Array and back to a base 64 encoded string via
// Uint8ArrayToBase64 matches the original.
func
TestBase64ToUint8ArrayUint8ArrayToBase64
(
t
*
testing
.
T
)
{
for
i
,
val
:=
range
testBytes
{
b64
:=
base64
.
StdEncoding
.
EncodeToString
(
val
)
jsArr
,
err
:=
base64ToUint8Array
(
js
.
ValueOf
(
b64
))
if
err
!=
nil
{
t
.
Errorf
(
"Failed to convert js.Value to base 64: %+v"
,
err
)
}
jsB64
:=
Uint8ArrayToBase64
(
js
.
Value
{},
[]
js
.
Value
{
jsArr
})
if
b64
!=
jsB64
{
t
.
Errorf
(
"JSON from Uint8Array does not match original (%d)."
+
"
\n
expected: %s
\n
received: %s"
,
i
,
b64
,
jsB64
)
}
}
}
func
TestUint8ArrayEquals
(
t
*
testing
.
T
)
{
for
i
,
val
:=
range
testBytes
{
// Create Uint8Array and set each element individually
jsBytesA
:=
Uint8Array
.
New
(
len
(
val
))
for
j
,
v
:=
range
val
{
jsBytesA
.
SetIndex
(
j
,
v
)
}
jsBytesB
:=
CopyBytesToJS
(
val
)
if
!
Uint8ArrayEquals
(
js
.
Value
{},
[]
js
.
Value
{
jsBytesA
,
jsBytesB
})
.
(
bool
)
{
t
.
Errorf
(
"Two equal byte slices were found to be different (%d)."
+
"
\n
expected: %s
\n
received: %s"
,
i
,
jsBytesA
.
Call
(
"toString"
)
.
String
(),
jsBytesB
.
Call
(
"toString"
)
.
String
())
}
}
}
This diff is collapsed.
Click to expand it.
utils/convert_test.go
+
3
−
10
View file @
d15828e8
...
...
@@ -23,10 +23,7 @@ import (
// Tests that CopyBytesToGo returns a byte slice that matches the Uint8Array.
func
TestCopyBytesToGo
(
t
*
testing
.
T
)
{
testValues
:=
[][]
byte
{{
0
,
1
,
2
,
3
},
{
0
},
{},
nil
}
for
i
,
val
:=
range
testValues
{
for
i
,
val
:=
range
testBytes
{
// Create Uint8Array and set each element individually
jsBytes
:=
Uint8Array
.
New
(
len
(
val
))
for
j
,
v
:=
range
val
{
...
...
@@ -46,9 +43,7 @@ func TestCopyBytesToGo(t *testing.T) {
// Tests that CopyBytesToJS returns a Javascript Uint8Array with values matching
// the original byte slice.
func
TestCopyBytesToJS
(
t
*
testing
.
T
)
{
testValues
:=
[][]
byte
{{
0
,
1
,
2
,
3
},
{
0
},
{},
nil
}
for
i
,
val
:=
range
testValues
{
for
i
,
val
:=
range
testBytes
{
jsBytes
:=
CopyBytesToJS
(
val
)
// Generate the expected string to match the output of toString() on a
...
...
@@ -70,9 +65,7 @@ func TestCopyBytesToJS(t *testing.T) {
// Tests that a byte slice converted to Javascript via CopyBytesToJS and
// converted back to Go via CopyBytesToGo matches the original.
func
TestCopyBytesToJSCopyBytesToGo
(
t
*
testing
.
T
)
{
testValues
:=
[][]
byte
{{
0
,
1
,
2
,
3
},
{
0
},
{},
nil
}
for
i
,
val
:=
range
testValues
{
for
i
,
val
:=
range
testBytes
{
jsBytes
:=
CopyBytesToJS
(
val
)
goBytes
:=
CopyBytesToGo
(
jsBytes
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment