Skip to content

Commit c355459

Browse files
committed
current version + tests
1 parent f26b334 commit c355459

File tree

5 files changed

+440
-4
lines changed

5 files changed

+440
-4
lines changed

index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
var Buffer = require('buffer').Buffer;
23-
2422
function assertEncoding(encoding) {
2523
if (encoding && !Buffer.isEncoding(encoding)) {
2624
throw new Error('Unknown encoding: ' + encoding);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"main": "index.js",
66
"dependencies": {},
77
"devDependencies": {
8-
"tape": "~1.0.4"
8+
"tap": "~0.4.8"
99
},
1010
"scripts": {
11-
"test": "tape test/*.js"
11+
"test": "tap test/simple/*.js"
1212
},
1313
"repository": {
1414
"type": "git",

test/common.js

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var path = require('path');
23+
var assert = require('assert');
24+
25+
exports.testDir = path.dirname(__filename);
26+
exports.fixturesDir = path.join(exports.testDir, 'fixtures');
27+
exports.libDir = path.join(exports.testDir, '../lib');
28+
exports.tmpDir = path.join(exports.testDir, 'tmp');
29+
exports.PORT = +process.env.NODE_COMMON_PORT || 12346;
30+
31+
if (process.platform === 'win32') {
32+
exports.PIPE = '\\\\.\\pipe\\libuv-test';
33+
} else {
34+
exports.PIPE = exports.tmpDir + '/test.sock';
35+
}
36+
37+
var util = require('util');
38+
for (var i in util) exports[i] = util[i];
39+
//for (var i in exports) global[i] = exports[i];
40+
41+
function protoCtrChain(o) {
42+
var result = [];
43+
for (; o; o = o.__proto__) { result.push(o.constructor); }
44+
return result.join();
45+
}
46+
47+
exports.indirectInstanceOf = function(obj, cls) {
48+
if (obj instanceof cls) { return true; }
49+
var clsChain = protoCtrChain(cls.prototype);
50+
var objChain = protoCtrChain(obj);
51+
return objChain.slice(-clsChain.length) === clsChain;
52+
};
53+
54+
55+
exports.ddCommand = function(filename, kilobytes) {
56+
if (process.platform === 'win32') {
57+
var p = path.resolve(exports.fixturesDir, 'create-file.js');
58+
return '"' + process.argv[0] + '" "' + p + '" "' +
59+
filename + '" ' + (kilobytes * 1024);
60+
} else {
61+
return 'dd if=/dev/zero of="' + filename + '" bs=1024 count=' + kilobytes;
62+
}
63+
};
64+
65+
66+
exports.spawnCat = function(options) {
67+
var spawn = require('child_process').spawn;
68+
69+
if (process.platform === 'win32') {
70+
return spawn('more', [], options);
71+
} else {
72+
return spawn('cat', [], options);
73+
}
74+
};
75+
76+
77+
exports.spawnPwd = function(options) {
78+
var spawn = require('child_process').spawn;
79+
80+
if (process.platform === 'win32') {
81+
return spawn('cmd.exe', ['/c', 'cd'], options);
82+
} else {
83+
return spawn('pwd', [], options);
84+
}
85+
};
86+
87+
88+
// Turn this off if the test should not check for global leaks.
89+
exports.globalCheck = true;
90+
91+
process.on('exit', function() {
92+
if (!exports.globalCheck) return;
93+
var knownGlobals = [setTimeout,
94+
setInterval,
95+
typeof setImmediate == 'undefined' ? null : setImmediate,
96+
clearTimeout,
97+
clearInterval,
98+
typeof clearImmediate == 'undefined' ? null : clearImmediate,
99+
console,
100+
Buffer,
101+
process,
102+
global].filter(Boolean);
103+
104+
if (global.gc) {
105+
knownGlobals.push(gc);
106+
}
107+
108+
if (global.DTRACE_HTTP_SERVER_RESPONSE) {
109+
knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE);
110+
knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST);
111+
knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE);
112+
knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST);
113+
knownGlobals.push(DTRACE_NET_STREAM_END);
114+
knownGlobals.push(DTRACE_NET_SERVER_CONNECTION);
115+
knownGlobals.push(DTRACE_NET_SOCKET_READ);
116+
knownGlobals.push(DTRACE_NET_SOCKET_WRITE);
117+
}
118+
if (global.COUNTER_NET_SERVER_CONNECTION) {
119+
knownGlobals.push(COUNTER_NET_SERVER_CONNECTION);
120+
knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE);
121+
knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST);
122+
knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE);
123+
knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST);
124+
knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE);
125+
}
126+
127+
if (global.ArrayBuffer) {
128+
knownGlobals.push(ArrayBuffer);
129+
knownGlobals.push(Int8Array);
130+
knownGlobals.push(Uint8Array);
131+
knownGlobals.push(Uint8ClampedArray);
132+
knownGlobals.push(Int16Array);
133+
knownGlobals.push(Uint16Array);
134+
knownGlobals.push(Int32Array);
135+
knownGlobals.push(Uint32Array);
136+
knownGlobals.push(Float32Array);
137+
knownGlobals.push(Float64Array);
138+
knownGlobals.push(DataView);
139+
}
140+
141+
for (var x in global) {
142+
var found = false;
143+
144+
for (var y in knownGlobals) {
145+
if (global[x] === knownGlobals[y]) {
146+
found = true;
147+
break;
148+
}
149+
}
150+
151+
if (!found) {
152+
console.error('Unknown global: %s', x);
153+
assert.ok(false, 'Unknown global found');
154+
}
155+
}
156+
});
157+
158+
159+
var mustCallChecks = [];
160+
161+
162+
function runCallChecks(exitCode) {
163+
if (exitCode !== 0) return;
164+
165+
var failed = mustCallChecks.filter(function(context) {
166+
return context.actual !== context.expected;
167+
});
168+
169+
failed.forEach(function(context) {
170+
console.log('Mismatched %s function calls. Expected %d, actual %d.',
171+
context.name,
172+
context.expected,
173+
context.actual);
174+
console.log(context.stack.split('\n').slice(2).join('\n'));
175+
});
176+
177+
if (failed.length) process.exit(1);
178+
}
179+
180+
181+
exports.mustCall = function(fn, expected) {
182+
if (typeof expected !== 'number') expected = 1;
183+
184+
var context = {
185+
expected: expected,
186+
actual: 0,
187+
stack: (new Error).stack,
188+
name: fn.name || '<anonymous>'
189+
};
190+
191+
// add the exit listener only once to avoid listener leak warnings
192+
if (mustCallChecks.length === 0) process.on('exit', runCallChecks);
193+
194+
mustCallChecks.push(context);
195+
196+
return function() {
197+
context.actual++;
198+
return fn.apply(this, arguments);
199+
};
200+
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
// verify that the string decoder works getting 1 byte at a time,
23+
// the whole buffer at once, and that both match the .toString(enc)
24+
// result of the entire buffer.
25+
26+
var assert = require('assert');
27+
var SD = require('../../').StringDecoder;
28+
var encodings = ['base64', 'hex', 'utf8', 'utf16le', 'ucs2'];
29+
30+
var bufs = [ '☃💩', 'asdf' ].map(function(b) {
31+
return new Buffer(b);
32+
});
33+
34+
// also test just arbitrary bytes from 0-15.
35+
for (var i = 1; i <= 16; i++) {
36+
var bytes = new Array(i).join('.').split('.').map(function(_, j) {
37+
return j + 0x78;
38+
});
39+
bufs.push(new Buffer(bytes));
40+
}
41+
42+
encodings.forEach(testEncoding);
43+
44+
console.log('ok');
45+
46+
function testEncoding(encoding) {
47+
bufs.forEach(function(buf) {
48+
testBuf(encoding, buf);
49+
});
50+
}
51+
52+
function testBuf(encoding, buf) {
53+
console.error('# %s', encoding, buf);
54+
55+
// write one byte at a time.
56+
var s = new SD(encoding);
57+
var res1 = '';
58+
for (var i = 0; i < buf.length; i++) {
59+
res1 += s.write(buf.slice(i, i + 1));
60+
}
61+
res1 += s.end();
62+
63+
// write the whole buffer at once.
64+
var res2 = '';
65+
var s = new SD(encoding);
66+
res2 += s.write(buf);
67+
res2 += s.end();
68+
69+
// .toString() on the buffer
70+
var res3 = buf.toString(encoding);
71+
72+
console.log('expect=%j', res3);
73+
assert.equal(res1, res3, 'one byte at a time should match toString');
74+
assert.equal(res2, res3, 'all bytes at once should match toString');
75+
}

0 commit comments

Comments
 (0)