Skip to content

Commit

Permalink
Merge c783ad6 into 2ce0954
Browse files Browse the repository at this point in the history
  • Loading branch information
samal-rasmussen committed Apr 1, 2020
2 parents 2ce0954 + c783ad6 commit 7dd098e
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 34 deletions.
175 changes: 158 additions & 17 deletions tests/tests.js
Expand Up @@ -50,64 +50,75 @@
{
'codePoint': 0x0000,
'decoded': '\0',
'encoded': '\0'
'encoded': '\0',
'encodedArray': [0x00]
},
{
'codePoint': 0x005C,
'decoded': '\x5C',
'encoded': '\x5C'
'encoded': '\x5C',
'encodedArray': [0x5C]
},
{
'codePoint': 0x007F,
'decoded': '\x7F',
'encoded': '\x7F'
'encoded': '\x7F',
'encodedArray': [0x7F]
},

// 2-byte
{
'codePoint': 0x0080,
'decoded': '\x80',
'encoded': '\xC2\x80'
'encoded': '\xC2\x80',
'encodedArray': [0xC2,0x80]
},
{
'codePoint': 0x05CA,
'decoded': '\u05CA',
'encoded': '\xD7\x8A'
'encoded': '\xD7\x8A',
'encodedArray': [0xD7,0x8A]
},
{
'codePoint': 0x07FF,
'decoded': '\u07FF',
'encoded': '\xDF\xBF',
'encodedArray': [0xDF,0xBF]
},

// 3-byte
{
'codePoint': 0x0800,
'decoded': '\u0800',
'encoded': '\xE0\xA0\x80',
'encodedArray': [0xE0,0xA0,0x80]
},
{
'codePoint': 0x2C3C,
'decoded': '\u2C3C',
'encoded': '\xE2\xB0\xBC'
'encoded': '\xE2\xB0\xBC',
'encodedArray': [0xE2,0xB0,0xBC]
},
{
'codePoint': 0xFFFF,
'decoded': '\uFFFF',
'encoded': '\xEF\xBF\xBF'
'encoded': '\xEF\xBF\xBF',
'encodedArray': [0xEF,0xBF,0xBF]
},
// unmatched surrogate halves
// high surrogates: 0xD800 to 0xDBFF
{
'codePoint': 0xD800,
'decoded': '\uD800',
'encoded': '\xED\xA0\x80',
'encodedArray': [0xED,0xA0,0x80],
'error': true
},
{
'description': 'High surrogate followed by another high surrogate',
'decoded': '\uD800\uD800',
'encoded': '\xED\xA0\x80\xED\xA0\x80',
'encodedArray': [0xED,0xA0,0x80,0xED,0xA0,0x80],
'error': true
},
{
Expand All @@ -120,31 +131,36 @@
'description': 'Unmatched high surrogate, followed by a surrogate pair, followed by an unmatched high surrogate',
'decoded': '\uD800\uD834\uDF06\uD800',
'encoded': '\xED\xA0\x80\xF0\x9D\x8C\x86\xED\xA0\x80',
'encodedArray': [0xED,0xA0,0x80,0xF0,0x9D,0x8C,0x86,0xED,0xA0,0x80],
'error': true
},
{
'codePoint': 0xD9AF,
'decoded': '\uD9AF',
'encoded': '\xED\xA6\xAF',
'encodedArray': [0xED,0xA6,0xAF],
'error': true
},
{
'codePoint': 0xDBFF,
'decoded': '\uDBFF',
'encoded': '\xED\xAF\xBF',
'encodedArray': [0xED,0xAF,0xBF],
'error': true
},
// low surrogates: 0xDC00 to 0xDFFF
{
'codePoint': 0xDC00,
'decoded': '\uDC00',
'encoded': '\xED\xB0\x80',
'encodedArray': [0xED,0xB0,0x80],
'error': true
},
{
'description': 'Low surrogate followed by another low surrogate',
'decoded': '\uDC00\uDC00',
'encoded': '\xED\xB0\x80\xED\xB0\x80',
'encodedArray': [0xED,0xB0,0x80,0xED,0xB0,0x80],
'error': true
},
{
Expand All @@ -157,36 +173,48 @@
'description': 'Unmatched low surrogate, followed by a surrogate pair, followed by an unmatched low surrogate',
'decoded': '\uDC00\uD834\uDF06\uDC00',
'encoded': '\xED\xB0\x80\xF0\x9D\x8C\x86\xED\xB0\x80',
'encodedArray': [0xED,0xB0,0x80,0xF0,0x9D,0x8C,0x86,0xED,0xB0,0x80],
'error': true
},
{
'codePoint': 0xDEEE,
'decoded': '\uDEEE',
'encoded': '\xED\xBB\xAE',
'encodedArray': [0xED,0xBB,0xAE],
'error': true
},
{
'codePoint': 0xDFFF,
'decoded': '\uDFFF',
'encoded': '\xED\xBF\xBF',
'encodedArray': [0xED,0xBF,0xBF],
'error': true
},

// 4-byte
{
'codePoint': 0x010000,
'decoded': '\uD800\uDC00',
'encoded': '\xF0\x90\x80\x80'
'encoded': '\xF0\x90\x80\x80',
'encodedArray': [0xF0,0x90,0x80,0x80]
},
{
'codePoint': 0x01D306,
'decoded': '\uD834\uDF06',
'encoded': '\xF0\x9D\x8C\x86'
'encoded': '\xF0\x9D\x8C\x86',
'encodedArray': [0xF0,0x9D,0x8C,0x86]
},
{
'codePoint': 0x10FFF,
'decoded': '\uDBFF\uDFFF',
'encoded': '\xF4\x8F\xBF\xBF'
'encoded': '\xF4\x8F\xBF\xBF',
'encodedArray': [0xF4,0x8F,0xBF,0xBF]
},
{
'codePoint': 0x1F602,
'decoded': '😂',
'encoded': '\xF0\x9F\x98\x82',
'encodedArray': [0xF0,0x9F,0x98,0x82]
}
];

Expand All @@ -206,30 +234,71 @@
var description = object.description || 'U+' + object.codePoint.toString(16).toUpperCase();
;
if (object.error) {
raises(
function() {
utf8.encode(object.decoded);
},
Error,
'Expecting non-scalar value detected error on encode'
);
raises(
function() {
utf8.decode(object.encoded);
},
Error,
'Error: non-scalar value detected'
'Expect non-scalar value detected error on decode'
);
raises(
function() {
utf8.encode(object.decoded);
utf8.encodeToArray(object.decoded);
},
Error,
'Error: non-scalar value detected'
'Expect non-scalar value detected error on encodeToArray'
);
raises(
function() {
utf8.encodeToUint8Array(object.decoded);
},
Error,
'Expect non-scalar value detected error on encodeToUint8Array'
);
raises(
function() {
utf8.decodeArray(object.encodedArray);
},
Error,
'Expect non-scalar value detected error on decodeArray'
);
} else {
equal(
object.encoded,
utf8.encode(object.decoded),
'Encoding: ' + description
object.encoded,
'Encoding to byte string:\t' + description
);
equal(
object.decoded,
utf8.decode(object.encoded),
'Decoding: ' + description
object.decoded,
'Decoding from byte string:\t' + description
);
deepEqual(
utf8.encodeToArray(object.decoded),
object.encodedArray,
'Encoding to byte array:\t\t' + description
);
deepEqual(
utf8.encodeToUint8Array(object.decoded),
Uint8Array.from(object.encodedArray),
'Encoding to Uint8Array:\t\t' + description
);
equal(
utf8.decodeArray(object.encodedArray),
object.decoded,
'Decoding from byte array:\t' + description
);
equal(
utf8.decodeArray(Uint8Array.from(object.encodedArray)),
object.decoded,
'Decoding from byte array:\t' + description
);
}
});
Expand Down Expand Up @@ -263,6 +332,78 @@
Error,
'Error: invalid byte index'
);

// Argument type checking
raises(
function() {
utf8.decode(123);
},
Error,
'Expect error when trying to call decode with a number'
);
raises(
function() {
utf8.decode([123]);
},
Error,
'Expect error when trying to call decode with an array'
);
raises(
function() {
utf8.decodeArray('123');
},
Error,
'Expect error when trying to call decodeArray with a string'
);
raises(
function() {
utf8.decodeArray(123);
},
Error,
'Expect error when trying to call decodeArray with a number'
);
raises(
function() {
utf8.encode(123);
},
Error,
'Expect error when trying to call encode with a number'
);
raises(
function() {
utf8.encode([123]);
},
Error,
'Expect error when trying to call encode with an array'
);
raises(
function() {
utf8.encodeToArray(123);
},
Error,
'Expect error when trying to call encodeToArray with a number'
);
raises(
function() {
utf8.encodeToArray([123]);
},
Error,
'Expect error when trying to call encodeToArray with an array'
);
raises(
function() {
utf8.encodeToUint8Array(123);
},
Error,
'Expect error when trying to call encodeToUint8Array with a number'
);
raises(
function() {
utf8.encodeToUint8Array([123]);
},
Error,
'Expect error when trying to call encodeToUint8Array with an array'
);
});

/*--------------------------------------------------------------------------*/
Expand Down

0 comments on commit 7dd098e

Please sign in to comment.