Skip to content

Commit

Permalink
[test] Use tape instead of mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Apr 11, 2016
1 parent a87a6f5 commit 394f8b1
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 113 deletions.
15 changes: 6 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@
"index.js"
],
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/lpinca/forwarded-parse.git"
},
"repository": "lpinca/forwarded-parse",
"scripts": {
"test-travis": "istanbul cover _mocha --report lcovonly",
"coverage": "istanbul cover _mocha",
"test": "mocha"
"test-travis": "istanbul cover tape --report lcovonly -- test.js",
"coverage": "istanbul cover tape -- test.js",
"test": "tape test.js"
},
"devDependencies": {
"istanbul": "0.4.x",
"mocha": "2.3.x",
"pre-commit": "1.1.x"
"pre-commit": "1.1.x",
"tape": "4.5.x"
}
}
228 changes: 124 additions & 104 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,131 +1,151 @@
/* istanbul ignore next */
describe('forwarded-parse', function () {
'use strict';
'use strict';

var assert = require('assert')
, parse = require('./');
var test = require('tape')
, parse = require('./');

it('is exported as a function', function () {
assert.strictEqual(typeof parse, 'function');
});
test('is exported as a function', function (t) {
t.equal(typeof parse, 'function');
t.end();
});

it('parses the pairs as expected', function () {
assert.deepEqual(parse('foo=a,foo=b;bar=c;baz=d;qux=e'), {
foo: [ 'a', 'b' ],
bar: [ 'c' ],
baz: [ 'd' ],
qux: [ 'e' ]
});
test('parses the pairs as expected', function (t) {
t.deepEqual(parse('foo=a,foo=b;bar=c;baz=d;qux=e'), {
foo: [ 'a', 'b' ],
bar: [ 'c' ],
baz: [ 'd' ],
qux: [ 'e' ]
});

it('handles double quotes and escaped characters', function () {
assert.deepEqual(parse([
'foo="bar"',
'foo="ba\\r"',
'foo=",;"',
'foo=""',
'foo=" "',
'foo="\t"',
'foo="\\""',
'foo="\\\\"',
'foo="¥"',
'foo="\\§"'
].join(',')), {
foo: [ 'bar', 'bar', ',;', '', ' ', '\t', '"', '\\', '¥', '§' ]
});
});
t.end();
});

it('ignores the optional white spaces', function () {
assert.deepEqual(parse('foo=a,foo=b, foo="c" ,foo=d , foo=e'), {
foo: [ 'a', 'b', 'c', 'd', 'e' ]
});

assert.deepEqual(parse('foo=a;bar=b; baz=c ;qux="d" ; norf=e'), {
foo: [ 'a' ],
bar: [ 'b' ],
baz: [ 'c' ],
qux: [ 'd' ],
norf: [ 'e' ]
});
test('handles double quotes and escaped characters', function (t) {
t.deepEqual(parse([
'foo="bar"',
'foo="ba\\r"',
'foo=",;"',
'foo=""',
'foo=" "',
'foo="\t"',
'foo="\\""',
'foo="\\\\"',
'foo="¥"',
'foo="\\§"'
].join(',')), {
foo: [ 'bar', 'bar', ',;', '', ' ', '\t', '"', '\\', '¥', '§' ]
});

it('ignores the case of the parameter names', function () {
assert.deepEqual(parse('foo=a,Foo=b'), {
foo: [ 'a', 'b' ]
});
});
t.end();
});

it('does not allow empty parameters', function () {
assert.throws(function () {
parse('foo=bar,=baz');
}, /Unexpected character '=' at index 8/);
test('ignores the optional white spaces', function (t) {
t.deepEqual(parse('foo=a,foo=b, foo="c" ,foo=d , foo=e'), {
foo: [ 'a', 'b', 'c', 'd', 'e' ]
});

it('throws an error if a parameter is not made of 1*tchar', function () {
assert.throws(function () {
parse('f@r=192.0.2.43');
}, /Unexpected character '@' at index 1/);
t.deepEqual(parse('foo=a;bar=b; baz=c ;qux="d" ; norf=e'), {
foo: [ 'a' ],
bar: [ 'b' ],
baz: [ 'c' ],
qux: [ 'd' ],
norf: [ 'e' ]
});

it('throws an error if it detects a misplaced whitespace', function () {
assert.throws(function () {
parse('for =192.0.2.43');
}, /Unexpected character ' ' at index 3/);
t.end();
});

assert.throws(function () {
parse('for= 192.0.2.43');
}, /Unexpected character ' ' at index 4/);
test('ignores the case of the parameter names', function (t) {
t.deepEqual(parse('foo=a,Foo=b'), {
foo: [ 'a', 'b' ]
});

it('throws an error if an identifier is not a token / quoted-string', function () {
assert.throws(function () {
parse('foo=b"ar"');
}, /Unexpected character '"' at index 5/);
t.end();
});

assert.throws(function () {
parse('foo="ba"r, foo=baz');
}, /Unexpected character 'r' at index 8/);
test('does not allow empty parameters', function (t) {
t.throws(function () {
parse('foo=bar,=baz');
}, /Unexpected character '=' at index 8/);

assert.throws(function () {
parse('foo=ba r, foo=baz');
}, /Unexpected character 'r' at index 7/);
t.end();
});

assert.throws(function () {
parse('foo=, foo=baz');
}, /Unexpected character ',' at index 4/);
});
test('throws an error if a parameter is not made of 1*tchar', function (t) {
t.throws(function () {
parse('f@r=192.0.2.43');
}, /Unexpected character '@' at index 1/);

it('throws an error when escaping a character not in quotes', function () {
assert.throws(function () {
parse('foo=b\\ar');
}, /Unexpected character '\\' at index 5/);
});
t.end();
});

it('throws an error if an identifier contains an invalid character', function () {
assert.throws(function () {
parse('foo=Ω, foo=baz');
}, /Unexpected character 'Ω' at index 4/);
test('throws an error if it detects a misplaced whitespace', function (t) {
t.throws(function () {
parse('for =192.0.2.43');
}, /Unexpected character ' ' at index 3/);

assert.throws(function () {
parse('foo="Ω", foo=baz');
}, /Unexpected character 'Ω' at index 5/);
});
t.throws(function () {
parse('for= 192.0.2.43');
}, /Unexpected character ' ' at index 4/);

it('throws an error if it detects a premature end of input', function () {
assert.throws(function () {
parse('foo=');
}, /Unexpected end of input/);
t.end();
});

assert.throws(function () {
parse('foo');
}, /Unexpected end of input/);
test('throws an error if an identifier is not a token / quoted-string', function (t) {
t.throws(function () {
parse('foo=b"ar"');
}, /Unexpected character '"' at index 5/);

assert.throws(function () {
parse('foo="bar');
}, /Unexpected end of input/);
t.throws(function () {
parse('foo="ba"r, foo=baz');
}, /Unexpected character 'r' at index 8/);

assert.throws(function () {
parse('foo=bar ');
}, /Unexpected end of input/);
});
t.throws(function () {
parse('foo=ba r, foo=baz');
}, /Unexpected character 'r' at index 7/);

t.throws(function () {
parse('foo=, foo=baz');
}, /Unexpected character ',' at index 4/);

t.end();
});

test('throws an error when escaping a character not in quotes', function (t) {
t.throws(function () {
parse('foo=b\\ar');
}, /Unexpected character '\\' at index 5/);

t.end();
});

test('throws an error if an identifier contains an invalid character', function (t) {
t.throws(function () {
parse('foo=Ω, foo=baz');
}, /Unexpected character 'Ω' at index 4/);

t.throws(function () {
parse('foo="Ω", foo=baz');
}, /Unexpected character 'Ω' at index 5/);

t.end();
});

test('throws an error if it detects a premature end of input', function (t) {
t.throws(function () {
parse('foo=');
}, /Unexpected end of input/);

t.throws(function () {
parse('foo');
}, /Unexpected end of input/);

t.throws(function () {
parse('foo="bar');
}, /Unexpected end of input/);

t.throws(function () {
parse('foo=bar ');
}, /Unexpected end of input/);

t.end();
});

0 comments on commit 394f8b1

Please sign in to comment.