1
- import { ByteBuf } from "./dist/bytebuf.mjs"
1
+ import test , { ExecutionContext } from "ava"
2
+ import { ByteBuf } from "./bytebuf.js"
2
3
3
- const tests = [
4
+ interface TestValue < V > {
5
+ readonly value : V
6
+ readonly bytes : Uint8Array
7
+ readonly littleEndian ?: boolean
8
+ readonly byteLength ?: number
9
+ readonly byteEncoding ?: string
10
+ }
11
+
12
+ interface TestEntry < V > {
13
+ readonly type : string
14
+ readonly values : TestValue < V > [ ]
15
+ getValue ( buffer : ByteBuf , entry : TestValue < V > ) : V
16
+ readValue ( buffer : ByteBuf , entry : TestValue < V > ) : V
17
+ setValue ( buffer : ByteBuf , entry : TestValue < V > ) : any
18
+ writeValue ( buffer : ByteBuf , entry : TestValue < V > ) : any
19
+ }
20
+
21
+ const entries : TestEntry < any > [ ] = [
4
22
{
5
23
type : "Bool" ,
6
24
getValue : function ( buffer ) {
@@ -10,10 +28,10 @@ const tests = [
10
28
return buffer . readBool ( )
11
29
} ,
12
30
setValue : function ( buffer , entry ) {
13
- return buffer . setBool ( 0 , entry . value )
31
+ buffer . setBool ( 0 , entry . value )
14
32
} ,
15
33
writeValue : function ( buffer , entry ) {
16
- return buffer . writeBool ( entry . value )
34
+ buffer . writeBool ( entry . value )
17
35
} ,
18
36
values : [
19
37
{
@@ -373,9 +391,9 @@ const tests = [
373
391
getValue : function ( buffer , entry ) {
374
392
const { value, byteLength } = buffer . getVarInt ( 0 , entry . byteLength )
375
393
376
- console . assert ( byteLength === entry . byteLength ,
377
- { type : "VarInt" , expected : entry . byteLength ,
378
- actual : byteLength , value } )
394
+ if ( entry . byteLength !== byteLength ) {
395
+ throw new RangeError ( "Expected byteLength to be " + entry . byteLength + ", but received " + byteLength )
396
+ }
379
397
380
398
return value
381
399
} ,
@@ -431,9 +449,9 @@ const tests = [
431
449
getValue : function ( buffer , entry ) {
432
450
const { value, byteLength } = buffer . getVarUint ( 0 , entry . byteLength )
433
451
434
- console . assert ( byteLength === entry . byteLength ,
435
- { type : "VarUint" , expected : entry . byteLength ,
436
- actual : byteLength , value } )
452
+ if ( entry . byteLength !== byteLength ) {
453
+ throw new RangeError ( "Expected byteLength to be " + entry . byteLength + ", but received " + byteLength )
454
+ }
437
455
438
456
return value
439
457
} ,
@@ -474,9 +492,9 @@ const tests = [
474
492
getValue : function ( buffer , entry ) {
475
493
const { value, byteLength } = buffer . getVarZint ( 0 , entry . byteLength )
476
494
477
- console . assert ( byteLength === entry . byteLength ,
478
- { type : "VarZint" , expected : entry . byteLength ,
479
- actual : byteLength , value } )
495
+ if ( entry . byteLength !== byteLength ) {
496
+ throw new RangeError ( "Expected byteLength to be " + entry . byteLength + ", but received " + byteLength )
497
+ }
480
498
481
499
return value
482
500
} ,
@@ -523,8 +541,8 @@ const tests = [
523
541
} ,
524
542
values : [
525
543
{
526
- value : new Uint8Array ( [ ] ) ,
527
- bytes : new Uint8Array ( [ ] )
544
+ value : new Uint8Array ( 0 ) ,
545
+ bytes : new Uint8Array ( 0 )
528
546
} ,
529
547
{
530
548
value : new Uint8Array ( [ 0xff ] ) ,
@@ -552,8 +570,8 @@ const tests = [
552
570
} ,
553
571
values : [
554
572
{
555
- value : new Uint16Array ( [ ] ) ,
556
- bytes : new Uint8Array ( [ ] )
573
+ value : new Uint16Array ( 0 ) ,
574
+ bytes : new Uint8Array ( 0 )
557
575
} ,
558
576
{
559
577
value : new Uint16Array ( [ 0xff ] ) ,
@@ -621,9 +639,9 @@ const tests = [
621
639
getValue : function ( buffer , entry ) {
622
640
const { value, byteLength } = buffer . getVarString ( 0 , entry . byteLength )
623
641
624
- console . assert ( byteLength === entry . byteLength ,
625
- { type : "VarString" , expected : entry . byteLength ,
626
- actual : byteLength , value } )
642
+ if ( entry . byteLength !== byteLength ) {
643
+ throw new RangeError ( "Expected byteLength to be " + entry . byteLength + ", but received " + byteLength )
644
+ }
627
645
628
646
return value
629
647
} ,
@@ -656,60 +674,39 @@ const tests = [
656
674
}
657
675
]
658
676
659
- function equals ( a , b ) {
660
- if ( a === b ) return true
661
- if ( ! ArrayBuffer . isView ( a ) || ! ArrayBuffer . isView ( b ) ) return false
662
- if ( a . byteLength !== b . byteLength ) return false
663
- return a . every ( ( val , i ) => val === b [ i ] )
664
- }
677
+ test ( "ByteBuf" , ( ctx : ExecutionContext ) => {
678
+ for ( const entry of entries ) {
679
+ const { type, values, getValue, readValue, setValue, writeValue } = entry
665
680
666
- function testType ( test ) {
667
- const { type, values, getValue, readValue, setValue, writeValue } = test
681
+ for ( const entry of values ) {
682
+ const { value, bytes, byteLength } = entry
683
+ const buffer = ByteBuf . from ( bytes )
684
+
685
+ const valueGet = getValue ( buffer , entry )
686
+ ctx . deepEqual ( value , valueGet ,
687
+ `expected get${ type } () to return ${ value } , but got ${ valueGet } instead.` )
668
688
669
- for ( const entry of values ) {
670
- const { value, bytes, byteLength } = entry
671
- const buffer = ByteBuf . from ( bytes . slice ( ) )
672
-
673
- const decoded = getValue ( buffer , entry )
674
- console . assert ( equals ( value , decoded ) ,
675
- { method : `get${ type } ` , expected : value , actual : decoded , bytes } )
676
-
677
- if ( ! equals ( decoded , readValue ( buffer , entry ) ) ) {
678
- console . warn ( `read${ type } return value does not match get${ type } ` )
679
- }
689
+ const valueRead = readValue ( buffer , entry )
690
+ ctx . deepEqual ( valueGet , valueRead ,
691
+ `expected read${ type } () to return ${ value } , but got ${ valueRead } instead.` )
692
+ ctx . is ( buffer . byteOffset , buffer . byteLength ,
693
+ `expected read${ type } () to offset ${ buffer . byteLength } bytes, but got ${ buffer . byteOffset } instead.` )
694
+
695
+ buffer . reset ( )
696
+ buffer . clear ( )
680
697
681
- if ( buffer . byteOffset !== buffer . byteLength ) {
682
- console . warn ( `read${ type } byte offset does not match ${ buffer . byteLength } ` )
698
+ const lengthSet = setValue ( buffer , entry )
699
+ ctx . is ( lengthSet , byteLength ,
700
+ `expected set${ type } (${ value } ) to return a byte length of ${ byteLength } , but got ${ lengthSet } instead.` )
701
+ const bytesSet = new Uint8Array ( buffer . buffer )
702
+ ctx . deepEqual ( bytesSet , bytes ,
703
+ `expected set${ type } (${ value } ) to encode as ${ bytes } , but got ${ bytesSet } instead.` )
704
+
705
+ writeValue ( buffer , entry )
706
+ ctx . deepEqual ( bytesSet , bytes ,
707
+ `expected write${ type } (${ value } ) to encode as ${ bytes } , but got ${ bytesSet } instead.` )
708
+ ctx . is ( buffer . byteOffset , buffer . byteLength ,
709
+ `expected write${ type } (${ value } ) to offset ${ buffer . byteLength } bytes, but got ${ buffer . byteOffset } instead.` )
683
710
}
684
-
685
- buffer . reset ( )
686
- new Uint8Array ( buffer . buffer ) . set ( new Uint8Array ( buffer . byteLength ) )
687
-
688
- const encodedLength = setValue ( buffer , entry )
689
- console . assert ( byteLength === encodedLength ,
690
- { method : `set${ type } (length)` ,
691
- expected : byteLength , actual : encodedLength , bytes } )
692
-
693
- const encoded = new Uint8Array ( buffer . buffer )
694
- console . assert ( equals ( bytes , encoded ) ,
695
- { method : `set${ type } ` , expected : bytes , actual : encoded , value } )
696
-
697
- writeValue ( buffer , entry )
698
-
699
- if ( ! equals ( bytes , encoded ) ) {
700
- console . warn ( `write${ type } encoding not match set${ type } ` )
701
- }
702
-
703
- if ( buffer . byteOffset !== buffer . byteLength ) {
704
- console . warn ( `write${ type } byte offset does not match ${ buffer . byteLength } ` )
705
- }
706
- }
707
- }
708
-
709
- function testAll ( ) {
710
- for ( const test of tests ) {
711
- testType ( test )
712
711
}
713
- }
714
-
715
- testAll ( )
712
+ } )
0 commit comments