2222 * @source : https://github.com/kogmbh/WebODF/
2323 */
2424
25- /*global core, runtime, Runtime*/
25+ /*global core, runtime, Runtime, ArrayBuffer, Uint8Array */
2626
2727/*jslint bitwise: true*/
2828
@@ -102,21 +102,78 @@ core.RuntimeTests = function RuntimeTests(runner) {
102102 } ) ;
103103 }
104104
105- function testUtf8ByteArrayToString ( callback ) {
106- var pre = r . resourcePrefix ( ) ;
107- runtime . read ( pre + "utf8.txt" , 14 , 4 , function ( err , data ) {
108- t . err = err ;
109- r . shouldBeNull ( t , "t.err" ) ;
110- t . data = data ;
111- r . shouldBe ( t , "t.data.length" , "4" ) ;
112- if ( data ) {
113- // we want to test the actual Runtime implementation rather than the nodejs runtime
114- t . data = Runtime . byteArrayToString ( data , "utf8" ) ;
115- }
116- r . shouldBe ( t , "t.data.charCodeAt(0)" , "55378" ) ;
117- r . shouldBe ( t , "t.data.charCodeAt(1)" , "57186" ) ;
118- callback ( ) ;
119- } ) ;
105+ function testUtf8ByteArrayToString_hello_withBOM ( ) {
106+ var bytearray = new Uint8Array ( new ArrayBuffer ( 8 ) ) ;
107+
108+ bytearray [ 0 ] = 0xef ;
109+ bytearray [ 1 ] = 0xbb ;
110+ bytearray [ 2 ] = 0xbf ;
111+ bytearray [ 3 ] = 0x68 ;
112+ bytearray [ 4 ] = 0x65 ;
113+ bytearray [ 5 ] = 0x6C ;
114+ bytearray [ 6 ] = 0x6C ;
115+ bytearray [ 7 ] = 0x6F ;
116+
117+ // we want to test the actual Runtime implementation rather than the nodejs runtime
118+ t . data = Runtime . byteArrayToString ( bytearray , "utf8" ) ;
119+
120+ r . shouldBe ( t , "t.data.charCodeAt(0)" , "0x68" ) ;
121+ r . shouldBe ( t , "t.data.charCodeAt(1)" , "0x65" ) ;
122+ r . shouldBe ( t , "t.data.charCodeAt(2)" , "0x6C" ) ;
123+ r . shouldBe ( t , "t.data.charCodeAt(3)" , "0x6C" ) ;
124+ r . shouldBe ( t , "t.data.charCodeAt(4)" , "0x6F" ) ;
125+ }
126+
127+ function testUtf8ByteArrayToString_hello_noBOM ( ) {
128+ var bytearray = new Uint8Array ( new ArrayBuffer ( 5 ) ) ;
129+
130+ bytearray [ 0 ] = 0x68 ;
131+ bytearray [ 1 ] = 0x65 ;
132+ bytearray [ 2 ] = 0x6C ;
133+ bytearray [ 3 ] = 0x6C ;
134+ bytearray [ 4 ] = 0x6F ;
135+
136+ // we want to test the actual Runtime implementation rather than the nodejs runtime
137+ t . data = Runtime . byteArrayToString ( bytearray , "utf8" ) ;
138+
139+ r . shouldBe ( t , "t.data.charCodeAt(0)" , "0x68" ) ;
140+ r . shouldBe ( t , "t.data.charCodeAt(1)" , "0x65" ) ;
141+ r . shouldBe ( t , "t.data.charCodeAt(2)" , "0x6C" ) ;
142+ r . shouldBe ( t , "t.data.charCodeAt(3)" , "0x6C" ) ;
143+ r . shouldBe ( t , "t.data.charCodeAt(4)" , "0x6F" ) ;
144+ }
145+
146+ function testUtf8ByteArrayToString_surrogates_withBOM ( ) {
147+ var bytearray = new Uint8Array ( new ArrayBuffer ( 7 ) ) ;
148+
149+ bytearray [ 0 ] = 0xef ;
150+ bytearray [ 1 ] = 0xbb ;
151+ bytearray [ 2 ] = 0xbf ;
152+ bytearray [ 3 ] = 0xf0 ;
153+ bytearray [ 4 ] = 0xa4 ;
154+ bytearray [ 5 ] = 0xad ;
155+ bytearray [ 6 ] = 0xa2 ;
156+
157+ // we want to test the actual Runtime implementation rather than the nodejs runtime
158+ t . data = Runtime . byteArrayToString ( bytearray , "utf8" ) ;
159+
160+ r . shouldBe ( t , "t.data.charCodeAt(0)" , "55378" ) ;
161+ r . shouldBe ( t , "t.data.charCodeAt(1)" , "57186" ) ;
162+ }
163+
164+ function testUtf8ByteArrayToString_surrogates_noBOM ( ) {
165+ var bytearray = new Uint8Array ( new ArrayBuffer ( 4 ) ) ;
166+
167+ bytearray [ 0 ] = 0xf0 ;
168+ bytearray [ 1 ] = 0xa4 ;
169+ bytearray [ 2 ] = 0xad ;
170+ bytearray [ 3 ] = 0xa2 ;
171+
172+ // we want to test the actual Runtime implementation rather than the nodejs runtime
173+ t . data = Runtime . byteArrayToString ( bytearray , "utf8" ) ;
174+
175+ r . shouldBe ( t , "t.data.charCodeAt(0)" , "55378" ) ;
176+ r . shouldBe ( t , "t.data.charCodeAt(1)" , "57186" ) ;
120177 }
121178
122179 function testLoadXML ( callback ) {
@@ -137,14 +194,17 @@ core.RuntimeTests = function RuntimeTests(runner) {
137194 t = { } ;
138195 } ;
139196 this . tests = function ( ) {
140- return [
141- ] ;
197+ return r . name ( [
198+ testUtf8ByteArrayToString_hello_withBOM ,
199+ testUtf8ByteArrayToString_hello_noBOM ,
200+ testUtf8ByteArrayToString_surrogates_withBOM ,
201+ testUtf8ByteArrayToString_surrogates_noBOM
202+ ] ) ;
142203 } ;
143204 this . asyncTests = function ( ) {
144205 return r . name ( [
145206 testRead ,
146207 testWrite ,
147- testUtf8ByteArrayToString ,
148208 testLoadXML
149209 ] ) ;
150210 } ;
0 commit comments