comunicationLeap/node_modules/utf8/tests/tests.js
node_modules/utf8/tests/tests.jsRaw
1;(function(root) {
2 'use strict';
3
4 /** Use a single `load` function */
5 var load = typeof require == 'function' ? require : root.load;
6
7 /** The unit testing framework */
8 var QUnit = (function() {
9 var noop = Function.prototype;
10 return root.QUnit || (
11 root.addEventListener || (root.addEventListener = noop),
12 root.setTimeout || (root.setTimeout = noop),
13 root.QUnit = load('../node_modules/qunitjs/qunit/qunit.js') || root.QUnit,
14 (load('../node_modules/qunit-clib/qunit-clib.js') || { 'runInContext': noop }).runInContext(root),
15 addEventListener === noop && delete root.addEventListener,
16 root.QUnit
17 );
18 }());
19
20 /** The `utf8` object to test */
21 var utf8 = root.utf8 || (root.utf8 = (
22 utf8 = load('../utf8.js') || root.utf8,
23 utf8 = utf8.utf8 || utf8
24 ));
25
26 /*--------------------------------------------------------------------------*/
27
28 function forEach(array, fn) {
29 var index = -1;
30 var length = array.length;
31 while (++index < length) {
32 fn(array[index]);
33 }
34 }
35
36 // Quick and dirty test to see if we’re in Node & need extended tests
37 var runExtendedTests = (function() {
38 try {
39 return process.argv[0] == 'node' && process.argv[2] == '--extended';
40 } catch(error) { }
41 }());
42
43 var data = [
44 // 1-byte
45 {
46 'codePoint': 0x0000,
47 'decoded': '\0',
48 'encoded': '\0'
49 },
50 {
51 'codePoint': 0x005C,
52 'decoded': '\x5C',
53 'encoded': '\x5C'
54 },
55 {
56 'codePoint': 0x007F,
57 'decoded': '\x7F',
58 'encoded': '\x7F'
59 },
60
61 // 2-byte
62 {
63 'codePoint': 0x0080,
64 'decoded': '\x80',
65 'encoded': '\xC2\x80'
66 },
67 {
68 'codePoint': 0x05CA,
69 'decoded': '\u05CA',
70 'encoded': '\xD7\x8A'
71 },
72 {
73 'codePoint': 0x07FF,
74 'decoded': '\u07FF',
75 'encoded': '\xDF\xBF',
76 },
77
78 // 3-byte
79 {
80 'codePoint': 0x0800,
81 'decoded': '\u0800',
82 'encoded': '\xE0\xA0\x80',
83 },
84 {
85 'codePoint': 0x2C3C,
86 'decoded': '\u2C3C',
87 'encoded': '\xE2\xB0\xBC'
88 },
89 {
90 'codePoint': 0xFFFF,
91 'decoded': '\uFFFF',
92 'encoded': '\xEF\xBF\xBF'
93 },
94 // unmatched surrogate halves
95 // high surrogates: 0xD800 to 0xDBFF
96 {
97 'codePoint': 0xD800,
98 'decoded': '\uD800',
99 'encoded': '\xED\xA0\x80'
100 },
101 {
102 'description': 'High surrogate followed by another high surrogate',
103 'decoded': '\uD800\uD800',
104 'encoded': '\xED\xA0\x80\xED\xA0\x80'
105 },
106 {
107 'description': 'High surrogate followed by a symbol that is not a surrogate',
108 'decoded': '\uD800A',
109 'encoded': '\xED\xA0\x80A'
110 },
111 {
112 'description': 'Unmatched high surrogate, followed by a surrogate pair, followed by an unmatched high surrogate',
113 'decoded': '\uD800\uD834\uDF06\uD800',
114 'encoded': '\xED\xA0\x80\xF0\x9D\x8C\x86\xED\xA0\x80'
115 },
116 {
117 'codePoint': 0xD9AF,
118 'decoded': '\uD9AF',
119 'encoded': '\xED\xA6\xAF'
120 },
121 {
122 'codePoint': 0xDBFF,
123 'decoded': '\uDBFF',
124 'encoded': '\xED\xAF\xBF'
125 },
126 // low surrogates: 0xDC00 to 0xDFFF
127 {
128 'codePoint': 0xDC00,
129 'decoded': '\uDC00',
130 'encoded': '\xED\xB0\x80'
131 },
132 {
133 'description': 'Low surrogate followed by another low surrogate',
134 'decoded': '\uDC00\uDC00',
135 'encoded': '\xED\xB0\x80\xED\xB0\x80'
136 },
137 {
138 'description': 'Low surrogate followed by a symbol that is not a surrogate',
139 'decoded': '\uDC00A',
140 'encoded': '\xED\xB0\x80A'
141 },
142 {
143 'description': 'Unmatched low surrogate, followed by a surrogate pair, followed by an unmatched low surrogate',
144 'decoded': '\uDC00\uD834\uDF06\uDC00',
145 'encoded': '\xED\xB0\x80\xF0\x9D\x8C\x86\xED\xB0\x80'
146 },
147 {
148 'codePoint': 0xDEEE,
149 'decoded': '\uDEEE',
150 'encoded': '\xED\xBB\xAE'
151 },
152 {
153 'codePoint': 0xDFFF,
154 'decoded': '\uDFFF',
155 'encoded': '\xED\xBF\xBF'
156 },
157
158 // 4-byte
159 {
160 'codePoint': 0x010000,
161 'decoded': '\uD800\uDC00',
162 'encoded': '\xF0\x90\x80\x80'
163 },
164 {
165 'codePoint': 0x01D306,
166 'decoded': '\uD834\uDF06',
167 'encoded': '\xF0\x9D\x8C\x86'
168 },
169 {
170 'codePoint': 0x10FFF,
171 'decoded': '\uDBFF\uDFFF',
172 'encoded': '\xF4\x8F\xBF\xBF'
173 }
174 ];
175
176 if (runExtendedTests) {
177 data = data.concat(require('./data.json'));
178 }
179
180 // `throws` is a reserved word in ES3; alias it to avoid errors
181 var raises = QUnit.assert['throws'];
182
183 // explicitly call `QUnit.module()` instead of `module()`
184 // in case we are in a CLI environment
185 QUnit.module('utf8.js');
186
187 test('encode/decode', function() {
188 forEach(data, function(object) {
189 var description = object.description || 'U+' + object.codePoint.toString(16).toUpperCase();
190 ;
191 equal(
192 object.encoded,
193 utf8.encode(object.decoded),
194 'Encoding: ' + description
195 );
196 equal(
197 object.decoded,
198 utf8.decode(object.encoded),
199 'Decoding: ' + description
200 );
201 });
202
203 // Error handling
204 raises(
205 function() {
206 utf8.decode('\uFFFF');
207 },
208 Error,
209 'Error: invalid UTF-8 detected'
210 );
211 raises(
212 function() {
213 utf8.decode('\xE9\x00\x00');
214 },
215 Error,
216 'Error: invalid continuation byte (4-byte sequence expected)'
217 );
218 raises(
219 function() {
220 utf8.decode('\xC2\uFFFF');
221 },
222 Error,
223 'Error: invalid continuation byte'
224 );
225 raises(
226 function() {
227 utf8.decode('\xF0\x9D');
228 },
229 Error,
230 'Error: invalid byte index'
231 );
232 });
233
234 /*--------------------------------------------------------------------------*/
235
236 // configure QUnit and call `QUnit.start()` for
237 // Narwhal, Node.js, PhantomJS, Rhino, and RingoJS
238 if (!root.document || root.phantom) {
239 QUnit.config.noglobals = true;
240 QUnit.start();
241 }
242}(typeof global == 'object' && global || this));