diff options
author | Samuel Mannehed <samuel@cendio.se> | 2020-01-03 10:41:34 +0100 |
---|---|---|
committer | Samuel Mannehed <samuel@cendio.se> | 2020-01-03 10:41:34 +0100 |
commit | 64fdd336a03105db8d21c896e83de97e84971a86 (patch) | |
tree | 99dd74d8fcf9343ccd74eb98a03aac90003d0f60 | |
parent | 80c72e92d2b8b1ac7cac552dd6bd431cfedbd3bb (diff) | |
download | novnc-64fdd336a03105db8d21c896e83de97e84971a86.tar.gz |
Simplify encodeUTF8/decodeUTF8 unittests
-rw-r--r-- | tests/test.util.js | 33 |
1 files changed, 6 insertions, 27 deletions
diff --git a/tests/test.util.js b/tests/test.util.js index 1f670bd..fb1d037 100644 --- a/tests/test.util.js +++ b/tests/test.util.js @@ -61,9 +61,7 @@ describe('Utils', function () { describe('string functions', function () { it('should decode UTF-8 to DOMString correctly', function () { - // The capital cyrillic letter 'PE' has the hexcode 'd0 9f' in UTF-8 - const utf8string = String.fromCodePoint(parseInt("d0", 16), - parseInt("9f", 16)); + const utf8string = '\xd0\x9f'; const domstring = decodeUTF8(utf8string); expect(domstring).to.equal("П"); }); @@ -71,32 +69,13 @@ describe('Utils', function () { it('should encode DOMString to UTF-8 correctly', function () { const domstring = "åäöa"; const utf8string = encodeUTF8(domstring); - - // The hexcode in UTF-8 for 'å' is 'c3 a5' - expect(utf8string.codePointAt(0).toString(16)).to.equal("c3"); - expect(utf8string.codePointAt(1).toString(16)).to.equal("a5"); - // ä - expect(utf8string.codePointAt(2).toString(16)).to.equal("c3"); - expect(utf8string.codePointAt(3).toString(16)).to.equal("a4"); - // ö - expect(utf8string.codePointAt(4).toString(16)).to.equal("c3"); - expect(utf8string.codePointAt(5).toString(16)).to.equal("b6"); - // a - expect(utf8string.codePointAt(6).toString(16)).to.equal("61"); + expect(utf8string).to.equal('\xc3\xa5\xc3\xa4\xc3\xb6\x61'); }); - it('should keep the string intact if encoding to UTF-8 and then decoding', function() { - const domstring = "κόσμε"; - const utf8string = encodeUTF8(domstring); - expect(decodeUTF8(utf8string)).to.equal(domstring); - }); - - it('should ignore URIErrors when UTF-8 decoding if allowLatin1 is set', function() { - expect(() => decodeUTF8("�")).to.throw(URIError); - expect(() => decodeUTF8("�", true)).to.not.throw(URIError); - - // Only URIError should be ignored - expect(() => decodeUTF8(undefVar, true)).to.throw(Error); + it('should allow Latin-1 strings if allowLatin1 is set when decoding', function () { + const latin1string = '\xe5\xe4\xf6'; + expect(() => decodeUTF8(latin1string)).to.throw(Error); + expect(decodeUTF8(latin1string, true)).to.equal('åäö'); }); }); |