Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for number parsing #241

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/fixtures/serde-test-cases/serialization/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

module.exports = [].concat(
require('./number'),
require('./string'),
require('./array'),
require('./object')
);
19 changes: 19 additions & 0 deletions test/fixtures/serde-test-cases/serialization/number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

module.exports = [
{
name: 'NaN',
value: NaN,
serialized: 'NaN'
},
{
name: 'Infinity',
value: Infinity,
serialized: 'Infinity'
},
{
name: '-Infinity',
value: -Infinity,
serialized: '-Infinity'
}
];
5 changes: 5 additions & 0 deletions test/fixtures/serde-test-cases/serialization/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ module.exports = [
name: 'object with non-identifier keys',
value: { '*': 42 },
serialized: '{\'*\':42}'
},
{
name: 'object with keyword as key',
value: { while: true },
serialized: '{while:true}'
}
];
19 changes: 19 additions & 0 deletions test/fixtures/serde-test-cases/serialization/string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

module.exports = [
{
name: 'string containing inline comment',
value: '// inline comment',
serialized: '\'// inline comment\''
},
{
name: 'string containing block comment',
value: '/* block comment */',
serialized: '\'/* block comment */\''
},
{
name: 'string with number inside',
value: '0.5',
serialized: '\'0.5\''
}
];
5 changes: 5 additions & 0 deletions test/fixtures/todo/serde/deserialization/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = [].concat(
require('./number')
);
34 changes: 34 additions & 0 deletions test/fixtures/todo/serde/deserialization/number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

module.exports = [
{
name: 'binary number starting with 0b',
value: 42,
serialized: '0b101010'
},
{
name: 'binary number starting with 0B',
value: 42,
serialized: '0B101010'
},
{
name: 'octal number starting with 0o',
value: 42,
serialized: '0o52'
},
{
name: 'octal number starting with 0O',
value: 42,
serialized: '0O52'
},
{
name: 'hex number starting with 0x',
value: 42,
serialized: '0x2a'
},
{
name: 'hex number starting with 0X',
value: 42,
serialized: '0X2A'
}
];
6 changes: 6 additions & 0 deletions test/fixtures/todo/serde/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
serialization: require('./serialization'),
deserialization: require('./deserialization'),
};
5 changes: 5 additions & 0 deletions test/fixtures/todo/serde/serialization/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = [].concat(
require('./object')
);
20 changes: 20 additions & 0 deletions test/fixtures/todo/serde/serialization/object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

module.exports = [
{
name: 'object with numeric literal as key',
value: { 42: true },
serialized: '{42:true}'
},
{
name: 'object with identifier names as keys',
value: { $: 'dollar', _$_: 'multiple symbols' },
serialized: '{$:\'dollar\',_$_:\'multiple symbols\'}'
},
{
name: 'object with identifier name, ' +
'containing non-latin Unicode literals, as a key',
value: { ümlåût: 'that\'s not really an ümlaüt, but this is' },
serialized: '{ümlåût: \'that\\\'s not really an ümlaüt, but this is\'}'
}
];
20 changes: 20 additions & 0 deletions test/todo/serde.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const jstp = require('../..');

const test = require('tap').test;
const testCases = require('../fixtures/todo/serde');

testCases.deserialization.forEach((testCase) => {
test(`must deserialize ${testCase.name}`, (test) => {
test.strictSame(jstp.parse(testCase.serialized), testCase.value);
test.end();
});
});

testCases.serialization.forEach((testCase) => {
test(`must serialize ${testCase.name}`, (test) => {
test.strictSame(jstp.stringify(testCase.value), testCase.serialized);
test.end();
});
});