Skip to content

Commit

Permalink
add local mocha test support
Browse files Browse the repository at this point in the history
  • Loading branch information
samthor committed Mar 6, 2020
1 parent 4f814f3 commit 13f1a25
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 71 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test.*
suite.*
2 changes: 1 addition & 1 deletion suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function tests(isNative, TextEncoder, TextDecoder) {

}

if (window.NativeTextEncoder && window.NativeTextDecoder) {
if (typeof NativeTextEncoder !== 'undefined' && typeof NativeTextDecoder !== 'undefined') {
tests(true, NativeTextEncoder, NativeTextDecoder);
}
tests(false, TextEncoder, TextDecoder);
70 changes: 0 additions & 70 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,73 +43,3 @@
<script src="suite.js"></script>
</body>
</html>
<!--
<!DOCTYPE html>
<html>
<head>
<script defer src="https://cdn.rawgit.com/mathiasbynens/utf8.js/5566334e/utf8.js"></script>
<script type="module">
</script>
<script type="module">
import './polyfill.js';
import 'https://cdn.rawgit.com/inexorabletash/text-encoding/b98ab30b/lib/encoding.js';
const polyfill = module.exports;
import './text.js';
const runs = 1;
const dataUrl = './utf8_sequence_0-0xffff_assigned_printable.txt';
(async function() {
let text = await window.fetch(dataUrl).then((data) => data.text());
text = text.substr(0, 10000);
function testEncodeDecode(name, tenc, tdec) {
console.time(name + '.TextEncoder');
let saved;
const encoder = new tenc();
for (let i = 0; i < runs; ++i) {
const out = encoder.encode(text);
saved = out;
}
console.info('got output', saved);
console.timeEnd(name + '.TextEncoder');
console.time(name + '.TextDecoder');
const decoder = new tdec();
for (let i = 0; i < runs; ++i) {
const out = decoder.decode(saved);
output = out;
}
console.timeEnd(name + '.TextDecoder');
}
let saved, output;
console.time('utf8.encode');
for (let i = 0; i < runs; ++i) {
const s = utf8.encode(text);
saved = s;
}
console.timeEnd('utf8.encode');
console.time('utf8.decode');
for (let i = 0; i < runs; ++i) {
const out = utf8.decode(saved);
output = out;
}
console.timeEnd('utf8.decode');
if (output !== text) {
throw new Error('utf8 got wrong answer');
}
testEncodeDecode('native', TextEncoder, TextDecoder);
testEncodeDecode('fast', TextEncoderPolyfill, TextDecoderPolyfill);
testEncodeDecode('polyfill', polyfill.TextEncoder, polyfill.TextDecoder);
}());
</script>
</head>
</html> -->
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @fileoverview Test runner inside a Node environment.
*/

const mocha = require('mocha');
const chai = require('chai');

global.suite = mocha.suite;
global.test = mocha.test;
global.assert = chai.assert;

global.NativeTextDecoder = global.TextDecoder;
global.NativeTextEncoder = global.TextEncoder;
global.TextDecoder = null;
global.TextEncoder = null;

require('./text.js'); // include polyfill
require('./suite.js');

0 comments on commit 13f1a25

Please sign in to comment.