@@ -16,14 +16,41 @@ function tests(isNative, TextEncoder, TextDecoder) {
1616 const out = dec . decode ( buffer ) ;
1717
1818 assert . equal ( out , s ) ;
19-
2019 } ) ;
2120
2221 suite ( 'decoder' , ( ) => {
2322
2423 test ( 'basic' , ( ) => {
2524 const buffer = new Uint8Array ( [ 104 , 101 , 108 , 108 , 111 ] ) ;
26- assert . equal ( dec . decode ( buffer ) , 'hello' ) ;
25+ assert . equal ( dec . decode ( buffer ) , 'hello' , 'directly Uint8Array' ) ;
26+ assert . equal ( dec . decode ( buffer . buffer ) , 'hello' , 'pass underlying ArrayBuffer' ) ;
27+ } ) ;
28+
29+ test ( 'non-8 backing' , ( ) => {
30+ // If passed a Uint32Array, TextDecoder will still decode the real
31+ // underlying bytes. Source data must be aligned.
32+ const padded = new Uint8Array ( [ 104 , 101 , 108 , 108 , 111 , 33 , 46 , 46 ] ) ;
33+ const u32 = new Uint32Array ( padded . buffer ) ;
34+ assert . equal ( padded . length >> 2 , u32 . length , 'u32 must be 1/4 of real data' ) ;
35+ assert . equal ( dec . decode ( u32 ) , 'hello!..' , 'pass Uint32Array' ) ;
36+ assert . equal ( dec . decode ( u32 . buffer ) , 'hello!..' , 'pass Uint32Array\'s buffer' ) ;
37+
38+ // Ensure that we don't parse larger typed arrays as uint8's. We expect
39+ // nulls here to pad out the remaining three bytes in every word.
40+ const u32raw = new Uint32Array ( [ 104 , 105 , 33 , 46 ] ) ;
41+ assert . equal ( dec . decode ( u32raw ) , 'h\0\0\0i\0\0\0!\0\0\0.\0\0\0' , 'u32 with nulls' ) ;
42+ } ) ;
43+
44+ test ( 'arraylike' , ( ) => {
45+ const arr = [ 104 , 101 , 108 , 108 , 111 ] ;
46+
47+ if ( isNative ) {
48+ // Native can't take Array.
49+ assert . throws ( ( ) => dec . decode ( arr ) ) ;
50+ } else {
51+ // Polyfill can accept Array or array-like.
52+ assert . equal ( dec . decode ( arr ) , 'hello' , 'decode arraylike' ) ;
53+ }
2754 } ) ;
2855
2956 test ( 'constructor' , ( ) => {
0 commit comments