[Pkg-javascript-commits] [node-string-decoder] 03/27: current version + tests

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 11 15:12:30 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-string-decoder.

commit c355459ca15e2d9576356c580da660791f34c4df
Author: Rod Vagg <rod at vagg.org>
Date:   Fri Jan 17 21:27:58 2014 +1100

    current version + tests
---
 index.js                               |   2 -
 package.json                           |   4 +-
 test/common.js                         | 200 +++++++++++++++++++++++++++++++++
 test/simple/test-string-decoder-end.js |  75 +++++++++++++
 test/simple/test-string-decoder.js     | 163 +++++++++++++++++++++++++++
 5 files changed, 440 insertions(+), 4 deletions(-)

diff --git a/index.js b/index.js
index 80edb05..6b1e308 100644
--- a/index.js
+++ b/index.js
@@ -19,8 +19,6 @@
 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 // USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-var Buffer = require('buffer').Buffer;
-
 function assertEncoding(encoding) {
   if (encoding && !Buffer.isEncoding(encoding)) {
     throw new Error('Unknown encoding: ' + encoding);
diff --git a/package.json b/package.json
index 8c80f3e..968f1d2 100644
--- a/package.json
+++ b/package.json
@@ -5,10 +5,10 @@
   "main": "index.js",
   "dependencies": {},
   "devDependencies": {
-    "tape": "~1.0.4"
+    "tap": "~0.4.8"
   },
   "scripts": {
-    "test": "tape test/*.js"
+    "test": "tap test/simple/*.js"
   },
   "repository": {
     "type": "git",
diff --git a/test/common.js b/test/common.js
new file mode 100644
index 0000000..ed5ff08
--- /dev/null
+++ b/test/common.js
@@ -0,0 +1,200 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var path = require('path');
+var assert = require('assert');
+
+exports.testDir = path.dirname(__filename);
+exports.fixturesDir = path.join(exports.testDir, 'fixtures');
+exports.libDir = path.join(exports.testDir, '../lib');
+exports.tmpDir = path.join(exports.testDir, 'tmp');
+exports.PORT = +process.env.NODE_COMMON_PORT || 12346;
+
+if (process.platform === 'win32') {
+  exports.PIPE = '\\\\.\\pipe\\libuv-test';
+} else {
+  exports.PIPE = exports.tmpDir + '/test.sock';
+}
+
+var util = require('util');
+for (var i in util) exports[i] = util[i];
+//for (var i in exports) global[i] = exports[i];
+
+function protoCtrChain(o) {
+  var result = [];
+  for (; o; o = o.__proto__) { result.push(o.constructor); }
+  return result.join();
+}
+
+exports.indirectInstanceOf = function(obj, cls) {
+  if (obj instanceof cls) { return true; }
+  var clsChain = protoCtrChain(cls.prototype);
+  var objChain = protoCtrChain(obj);
+  return objChain.slice(-clsChain.length) === clsChain;
+};
+
+
+exports.ddCommand = function(filename, kilobytes) {
+  if (process.platform === 'win32') {
+    var p = path.resolve(exports.fixturesDir, 'create-file.js');
+    return '"' + process.argv[0] + '" "' + p + '" "' +
+           filename + '" ' + (kilobytes * 1024);
+  } else {
+    return 'dd if=/dev/zero of="' + filename + '" bs=1024 count=' + kilobytes;
+  }
+};
+
+
+exports.spawnCat = function(options) {
+  var spawn = require('child_process').spawn;
+
+  if (process.platform === 'win32') {
+    return spawn('more', [], options);
+  } else {
+    return spawn('cat', [], options);
+  }
+};
+
+
+exports.spawnPwd = function(options) {
+  var spawn = require('child_process').spawn;
+
+  if (process.platform === 'win32') {
+    return spawn('cmd.exe', ['/c', 'cd'], options);
+  } else {
+    return spawn('pwd', [], options);
+  }
+};
+
+
+// Turn this off if the test should not check for global leaks.
+exports.globalCheck = true;
+
+process.on('exit', function() {
+  if (!exports.globalCheck) return;
+  var knownGlobals = [setTimeout,
+                      setInterval,
+                      typeof setImmediate == 'undefined' ? null : setImmediate,
+                      clearTimeout,
+                      clearInterval,
+                      typeof clearImmediate == 'undefined' ? null : clearImmediate,
+                      console,
+                      Buffer,
+                      process,
+                      global].filter(Boolean);
+
+  if (global.gc) {
+    knownGlobals.push(gc);
+  }
+
+  if (global.DTRACE_HTTP_SERVER_RESPONSE) {
+    knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE);
+    knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST);
+    knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE);
+    knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST);
+    knownGlobals.push(DTRACE_NET_STREAM_END);
+    knownGlobals.push(DTRACE_NET_SERVER_CONNECTION);
+    knownGlobals.push(DTRACE_NET_SOCKET_READ);
+    knownGlobals.push(DTRACE_NET_SOCKET_WRITE);
+  }
+  if (global.COUNTER_NET_SERVER_CONNECTION) {
+    knownGlobals.push(COUNTER_NET_SERVER_CONNECTION);
+    knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE);
+    knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST);
+    knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE);
+    knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST);
+    knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE);
+  }
+
+  if (global.ArrayBuffer) {
+    knownGlobals.push(ArrayBuffer);
+    knownGlobals.push(Int8Array);
+    knownGlobals.push(Uint8Array);
+    knownGlobals.push(Uint8ClampedArray);
+    knownGlobals.push(Int16Array);
+    knownGlobals.push(Uint16Array);
+    knownGlobals.push(Int32Array);
+    knownGlobals.push(Uint32Array);
+    knownGlobals.push(Float32Array);
+    knownGlobals.push(Float64Array);
+    knownGlobals.push(DataView);
+  }
+
+  for (var x in global) {
+    var found = false;
+
+    for (var y in knownGlobals) {
+      if (global[x] === knownGlobals[y]) {
+        found = true;
+        break;
+      }
+    }
+
+    if (!found) {
+      console.error('Unknown global: %s', x);
+      assert.ok(false, 'Unknown global found');
+    }
+  }
+});
+
+
+var mustCallChecks = [];
+
+
+function runCallChecks(exitCode) {
+  if (exitCode !== 0) return;
+
+  var failed = mustCallChecks.filter(function(context) {
+    return context.actual !== context.expected;
+  });
+
+  failed.forEach(function(context) {
+    console.log('Mismatched %s function calls. Expected %d, actual %d.',
+                context.name,
+                context.expected,
+                context.actual);
+    console.log(context.stack.split('\n').slice(2).join('\n'));
+  });
+
+  if (failed.length) process.exit(1);
+}
+
+
+exports.mustCall = function(fn, expected) {
+  if (typeof expected !== 'number') expected = 1;
+
+  var context = {
+    expected: expected,
+    actual: 0,
+    stack: (new Error).stack,
+    name: fn.name || '<anonymous>'
+  };
+
+  // add the exit listener only once to avoid listener leak warnings
+  if (mustCallChecks.length === 0) process.on('exit', runCallChecks);
+
+  mustCallChecks.push(context);
+
+  return function() {
+    context.actual++;
+    return fn.apply(this, arguments);
+  };
+};
diff --git a/test/simple/test-string-decoder-end.js b/test/simple/test-string-decoder-end.js
new file mode 100644
index 0000000..869a411
--- /dev/null
+++ b/test/simple/test-string-decoder-end.js
@@ -0,0 +1,75 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+// verify that the string decoder works getting 1 byte at a time,
+// the whole buffer at once, and that both match the .toString(enc)
+// result of the entire buffer.
+
+var assert = require('assert');
+var SD = require('../../').StringDecoder;
+var encodings = ['base64', 'hex', 'utf8', 'utf16le', 'ucs2'];
+
+var bufs = [ '☃💩', 'asdf' ].map(function(b) {
+  return new Buffer(b);
+});
+
+// also test just arbitrary bytes from 0-15.
+for (var i = 1; i <= 16; i++) {
+  var bytes = new Array(i).join('.').split('.').map(function(_, j) {
+    return j + 0x78;
+  });
+  bufs.push(new Buffer(bytes));
+}
+
+encodings.forEach(testEncoding);
+
+console.log('ok');
+
+function testEncoding(encoding) {
+  bufs.forEach(function(buf) {
+    testBuf(encoding, buf);
+  });
+}
+
+function testBuf(encoding, buf) {
+  console.error('# %s', encoding, buf);
+
+  // write one byte at a time.
+  var s = new SD(encoding);
+  var res1 = '';
+  for (var i = 0; i < buf.length; i++) {
+    res1 += s.write(buf.slice(i, i + 1));
+  }
+  res1 += s.end();
+
+  // write the whole buffer at once.
+  var res2 = '';
+  var s = new SD(encoding);
+  res2 += s.write(buf);
+  res2 += s.end();
+
+  // .toString() on the buffer
+  var res3 = buf.toString(encoding);
+
+  console.log('expect=%j', res3);
+  assert.equal(res1, res3, 'one byte at a time should match toString');
+  assert.equal(res2, res3, 'all bytes at once should match toString');
+}
diff --git a/test/simple/test-string-decoder.js b/test/simple/test-string-decoder.js
new file mode 100644
index 0000000..7f69f7e
--- /dev/null
+++ b/test/simple/test-string-decoder.js
@@ -0,0 +1,163 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+var StringDecoder = require('../../').StringDecoder;
+var decoder = new StringDecoder('utf8');
+
+
+
+var buffer = new Buffer('$');
+assert.deepEqual('$', decoder.write(buffer));
+
+buffer = new Buffer('¢');
+assert.deepEqual('', decoder.write(buffer.slice(0, 1)));
+assert.deepEqual('¢', decoder.write(buffer.slice(1, 2)));
+
+buffer = new Buffer('€');
+assert.deepEqual('', decoder.write(buffer.slice(0, 1)));
+assert.deepEqual('', decoder.write(buffer.slice(1, 2)));
+assert.deepEqual('€', decoder.write(buffer.slice(2, 3)));
+
+buffer = new Buffer([0xF0, 0xA4, 0xAD, 0xA2]);
+var s = '';
+s += decoder.write(buffer.slice(0, 1));
+s += decoder.write(buffer.slice(1, 2));
+s += decoder.write(buffer.slice(2, 3));
+s += decoder.write(buffer.slice(3, 4));
+assert.ok(s.length > 0);
+
+// CESU-8
+buffer = new Buffer('EDA0BDEDB18D', 'hex'); // THUMBS UP SIGN (in CESU-8)
+var s = '';
+s += decoder.write(buffer.slice(0, 1));
+s += decoder.write(buffer.slice(1, 2));
+s += decoder.write(buffer.slice(2, 3)); // complete lead surrogate
+assert.equal(s, '');
+s += decoder.write(buffer.slice(3, 4));
+s += decoder.write(buffer.slice(4, 5));
+s += decoder.write(buffer.slice(5, 6)); // complete trail surrogate
+assert.equal(s, '\uD83D\uDC4D'); // THUMBS UP SIGN (in UTF-16)
+
+var s = '';
+s += decoder.write(buffer.slice(0, 2));
+s += decoder.write(buffer.slice(2, 4)); // complete lead surrogate
+assert.equal(s, '');
+s += decoder.write(buffer.slice(4, 6)); // complete trail surrogate
+assert.equal(s, '\uD83D\uDC4D'); // THUMBS UP SIGN (in UTF-16)
+
+var s = '';
+s += decoder.write(buffer.slice(0, 3)); // complete lead surrogate
+assert.equal(s, '');
+s += decoder.write(buffer.slice(3, 6)); // complete trail surrogate
+assert.equal(s, '\uD83D\uDC4D'); // THUMBS UP SIGN (in UTF-16)
+
+var s = '';
+s += decoder.write(buffer.slice(0, 4)); // complete lead surrogate
+assert.equal(s, '');
+s += decoder.write(buffer.slice(4, 5));
+s += decoder.write(buffer.slice(5, 6)); // complete trail surrogate
+assert.equal(s, '\uD83D\uDC4D'); // THUMBS UP SIGN (in UTF-16)
+
+var s = '';
+s += decoder.write(buffer.slice(0, 5)); // complete lead surrogate
+assert.equal(s, '');
+s += decoder.write(buffer.slice(5, 6)); // complete trail surrogate
+assert.equal(s, '\uD83D\uDC4D'); // THUMBS UP SIGN (in UTF-16)
+
+var s = '';
+s += decoder.write(buffer.slice(0, 6));
+assert.equal(s, '\uD83D\uDC4D'); // THUMBS UP SIGN (in UTF-16)
+
+
+// UCS-2
+decoder = new StringDecoder('ucs2');
+buffer = new Buffer('ab', 'ucs2');
+assert.equal(decoder.write(buffer), 'ab'); // 2 complete chars
+buffer = new Buffer('abc', 'ucs2');
+assert.equal(decoder.write(buffer.slice(0, 3)), 'a'); // 'a' and first of 'b'
+assert.equal(decoder.write(buffer.slice(3, 6)), 'bc'); // second of 'b' and 'c'
+
+
+// UTF-16LE
+buffer = new Buffer('3DD84DDC', 'hex'); // THUMBS UP SIGN (in CESU-8)
+var s = '';
+s += decoder.write(buffer.slice(0, 1));
+s += decoder.write(buffer.slice(1, 2)); // complete lead surrogate
+assert.equal(s, '');
+s += decoder.write(buffer.slice(2, 3));
+s += decoder.write(buffer.slice(3, 4)); // complete trail surrogate
+assert.equal(s, '\uD83D\uDC4D'); // THUMBS UP SIGN (in UTF-16)
+
+var s = '';
+s += decoder.write(buffer.slice(0, 2)); // complete lead surrogate
+assert.equal(s, '');
+s += decoder.write(buffer.slice(2, 4)); // complete trail surrogate
+assert.equal(s, '\uD83D\uDC4D'); // THUMBS UP SIGN (in UTF-16)
+
+var s = '';
+s += decoder.write(buffer.slice(0, 3)); // complete lead surrogate
+assert.equal(s, '');
+s += decoder.write(buffer.slice(3, 4)); // complete trail surrogate
+assert.equal(s, '\uD83D\uDC4D'); // THUMBS UP SIGN (in UTF-16)
+
+var s = '';
+s += decoder.write(buffer.slice(0, 4));
+assert.equal(s, '\uD83D\uDC4D'); // THUMBS UP SIGN (in UTF-16)
+
+
+// A mixed ascii and non-ascii string
+// Test stolen from deps/v8/test/cctest/test-strings.cc
+// U+02E4 -> CB A4
+// U+0064 -> 64
+// U+12E4 -> E1 8B A4
+// U+0030 -> 30
+// U+3045 -> E3 81 85
+var expected = '\u02e4\u0064\u12e4\u0030\u3045';
+var buffer = new Buffer([0xCB, 0xA4, 0x64, 0xE1, 0x8B, 0xA4,
+                         0x30, 0xE3, 0x81, 0x85]);
+var charLengths = [0, 0, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5];
+
+// Split the buffer into 3 segments
+//  |----|------|-------|
+//  0    i      j       buffer.length
+// Scan through every possible 3 segment combination
+// and make sure that the string is always parsed.
+common.print('scanning ');
+for (var j = 2; j < buffer.length; j++) {
+  for (var i = 1; i < j; i++) {
+    var decoder = new StringDecoder('utf8');
+
+    var sum = decoder.write(buffer.slice(0, i));
+
+    // just check that we've received the right amount
+    // after the first write
+    assert.equal(charLengths[i], sum.length);
+
+    sum += decoder.write(buffer.slice(i, j));
+    sum += decoder.write(buffer.slice(j, buffer.length));
+    assert.equal(expected, sum);
+    common.print('.');
+  }
+}
+console.log(' crayon!');
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-string-decoder.git



More information about the Pkg-javascript-commits mailing list