Add unit tests for encodeUTF8 and decodeUTF8

pull/36/head
Samuel Mannehed 5 years ago committed by Lauri Kasanen
parent 789308212b
commit a4c5c38b53

@ -2,6 +2,7 @@
const expect = chai.expect;
import * as Log from '../core/util/logging.js';
import { encodeUTF8, decodeUTF8 } from '../core/util/strings.js';
describe('Utils', function () {
"use strict";
@ -58,8 +59,48 @@ 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 domstring = decodeUTF8(utf8string);
expect(domstring).to.equal("П");
});
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");
});
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("<22>")).to.throw(URIError);
expect(() => decodeUTF8("<22>", true)).to.not.throw(URIError);
// Only URIError should be ignored
expect(() => decodeUTF8(undefVar, true)).to.throw(Error);
});
});
// TODO(directxman12): test the conf_default and conf_defaults methods
// TODO(directxman12): test decodeUTF8
// TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent)
// TODO(directxman12): figure out a good way to test getPosition and getEventPosition
// TODO(directxman12): figure out how to test the browser detection functions properly

Loading…
Cancel
Save