mirror of
https://github.com/flynx/serialize.js.git
synced 2026-07-10 19:20:56 +00:00
added testing...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
2764fa011e
commit
a37ba58afb
@ -16,5 +16,8 @@
|
|||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/flynx/serialize.js/issues"
|
"url": "https://github.com/flynx/serialize.js/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/flynx/serialize.js#readme"
|
"homepage": "https://github.com/flynx/serialize.js#readme",
|
||||||
|
"dependencies": {
|
||||||
|
"ig-test": "^1.5.9"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,6 +19,24 @@ var RECURSIVE = '<RECURSIVE%>'
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
|
module.DEBUG = false
|
||||||
|
|
||||||
|
var DEBUG_PREFIX = '---'
|
||||||
|
|
||||||
|
var debug = {
|
||||||
|
context: 16,
|
||||||
|
|
||||||
|
log: function(...args){
|
||||||
|
if(!module.DEBUG){
|
||||||
|
return }
|
||||||
|
return console.log(DEBUG_PREFIX, ...arguments) },
|
||||||
|
lex: function(name, str, i, line){
|
||||||
|
return this.log(name +':', str.slice(i, i+this.context) +'...') },
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -158,6 +176,9 @@ module.eJSON = {
|
|||||||
'{': 'object',
|
'{': 'object',
|
||||||
},
|
},
|
||||||
words: {
|
words: {
|
||||||
|
true: true,
|
||||||
|
false: false,
|
||||||
|
|
||||||
Infinity: 'number',
|
Infinity: 'number',
|
||||||
|
|
||||||
'Set(': 'set',
|
'Set(': 'set',
|
||||||
@ -236,6 +257,7 @@ module.eJSON = {
|
|||||||
// -> [value, i, line]
|
// -> [value, i, line]
|
||||||
//
|
//
|
||||||
number: function(state, path, match, str, i, line){
|
number: function(state, path, match, str, i, line){
|
||||||
|
debug.lex('number', str, i, line)
|
||||||
// special cases..,
|
// special cases..,
|
||||||
if(match == 'Infinity'){
|
if(match == 'Infinity'){
|
||||||
return [Infinity, i, line] }
|
return [Infinity, i, line] }
|
||||||
@ -252,6 +274,7 @@ module.eJSON = {
|
|||||||
return [ str.slice(i, j)*1, j, line ] },
|
return [ str.slice(i, j)*1, j, line ] },
|
||||||
// XXX TEST count \\n
|
// XXX TEST count \\n
|
||||||
string: function(state, path, match, str, i, line){
|
string: function(state, path, match, str, i, line){
|
||||||
|
debug.lex('string', str, i, line)
|
||||||
var j = i+1
|
var j = i+1
|
||||||
while(j < str.length
|
while(j < str.length
|
||||||
&& str[j] != match){
|
&& str[j] != match){
|
||||||
@ -274,6 +297,7 @@ module.eJSON = {
|
|||||||
this.error('Unexpected end of input wile looking fot "'+ match +'".', str, i, line) }
|
this.error('Unexpected end of input wile looking fot "'+ match +'".', str, i, line) }
|
||||||
return [ str.slice(i+1, j), j+1, line ] },
|
return [ str.slice(i+1, j), j+1, line ] },
|
||||||
identifier: function(state, path, match, str, i, line){
|
identifier: function(state, path, match, str, i, line){
|
||||||
|
debug.lex('identifier', str, i, line)
|
||||||
if(!/[a-zA-Z_]/.test(str[i])){
|
if(!/[a-zA-Z_]/.test(str[i])){
|
||||||
this.error('Not an identifier: "'+ str[i] +'"', str, i, line) }
|
this.error('Not an identifier: "'+ str[i] +'"', str, i, line) }
|
||||||
var j = i+1
|
var j = i+1
|
||||||
@ -303,7 +327,7 @@ module.eJSON = {
|
|||||||
initial instanceof Array
|
initial instanceof Array
|
||||||
&& initial.length++
|
&& initial.length++
|
||||||
continue }
|
continue }
|
||||||
if(str.slice(i, EMPTY.length) == EMPTY){
|
if(str.slice(i, i+EMPTY.length) == EMPTY){
|
||||||
i += EMPTY.length
|
i += EMPTY.length
|
||||||
// XXX this feels hackish -- can this be deligated to the handler???
|
// XXX this feels hackish -- can this be deligated to the handler???
|
||||||
initial instanceof Array
|
initial instanceof Array
|
||||||
@ -326,6 +350,7 @@ module.eJSON = {
|
|||||||
this.error('Unexpected end of input wile looking for "'+ end +'".', str, i, line) },
|
this.error('Unexpected end of input wile looking for "'+ end +'".', str, i, line) },
|
||||||
|
|
||||||
array: function(state, path, match, str, i, line){
|
array: function(state, path, match, str, i, line){
|
||||||
|
debug.lex('array', str, i, line)
|
||||||
return this.sequence(
|
return this.sequence(
|
||||||
state, path, str, i+1, line,
|
state, path, str, i+1, line,
|
||||||
']',
|
']',
|
||||||
@ -335,6 +360,7 @@ module.eJSON = {
|
|||||||
res[index] = obj
|
res[index] = obj
|
||||||
return [res, i, line] }) },
|
return [res, i, line] }) },
|
||||||
object: function(state, path, match, str, i, line){
|
object: function(state, path, match, str, i, line){
|
||||||
|
debug.lex('object', str, i, line)
|
||||||
return this.sequence(
|
return this.sequence(
|
||||||
state, path, str, i+1, line,
|
state, path, str, i+1, line,
|
||||||
'}',
|
'}',
|
||||||
@ -359,6 +385,7 @@ module.eJSON = {
|
|||||||
{}) },
|
{}) },
|
||||||
|
|
||||||
set: function(state, path, match, str, i, line){
|
set: function(state, path, match, str, i, line){
|
||||||
|
debug.lex('set', str, i, line)
|
||||||
i += match.length
|
i += match.length
|
||||||
;[res, i, line] = this.sequence(
|
;[res, i, line] = this.sequence(
|
||||||
state, path,
|
state, path,
|
||||||
@ -369,33 +396,35 @@ module.eJSON = {
|
|||||||
;[obj, i, line] = this.value(
|
;[obj, i, line] = this.value(
|
||||||
state,
|
state,
|
||||||
[...path, index],
|
[...path, index],
|
||||||
str, i+match.length, line)
|
str, i, line)
|
||||||
res.add(obj)
|
res.add(obj)
|
||||||
return [res, i, line] },
|
return [res, i, line] },
|
||||||
new Set())
|
new Set())
|
||||||
if(str[i] != ')'){
|
if(str[i] != ')'){
|
||||||
this.error('Expected ")", got "'+ str[i] +'".', str, i, line) }
|
this.error('Expected ")", got "'+ str[i] +'".', str, i, line) }
|
||||||
return [res, i, line] },
|
return [res, i+1, line] },
|
||||||
// XXX need to destinguish between key and value in path...
|
// XXX need to destinguish between key and value in path...
|
||||||
map: function(state, path, match, str, i, line){
|
map: function(state, path, match, str, i, line){
|
||||||
|
debug.lex('map', str, i, line)
|
||||||
i += match.length
|
i += match.length
|
||||||
;[res, i, line] = this.sequence(
|
;[res, i, line] = this.sequence(
|
||||||
state, path,
|
state, path,
|
||||||
str, i+1, line,
|
str, i+1, line,
|
||||||
']',
|
']',
|
||||||
function(res, index, str, i, line){
|
function(res, index, str, i, line){
|
||||||
|
debug.lex(' map-content', str, i, line)
|
||||||
var obj
|
var obj
|
||||||
;[[key, value], i, line] = this.value(
|
;[[key, value], i, line] = this.value(
|
||||||
state,
|
state,
|
||||||
// XXX need a way to index path or value...
|
// XXX need a way to index path or value...
|
||||||
[...path, index],
|
[...path, index],
|
||||||
str, i+match.length, line)
|
str, i, line)
|
||||||
res.set(key, value)
|
res.set(key, value)
|
||||||
return [res, i, line] },
|
return [res, i, line] },
|
||||||
new Set())
|
new Map())
|
||||||
if(str[i] != ')'){
|
if(str[i] != ')'){
|
||||||
this.error('Expected ")", got "'+ str[i] +'".', str, i, line) }
|
this.error('Expected ")", got "'+ str[i] +'".', str, i, line) }
|
||||||
return [res, i, line] },
|
return [res, i+1, line] },
|
||||||
|
|
||||||
// XXX should this be done inline or on a separate stage?
|
// XXX should this be done inline or on a separate stage?
|
||||||
// for inline we need these:
|
// for inline we need these:
|
||||||
@ -413,6 +442,7 @@ module.eJSON = {
|
|||||||
// stage two... (XXX TEST)
|
// stage two... (XXX TEST)
|
||||||
// ...need to use serialized paths as keys...
|
// ...need to use serialized paths as keys...
|
||||||
recursive: function(state, path, match, str, i, line){
|
recursive: function(state, path, match, str, i, line){
|
||||||
|
debug.lex('recursive', str, i, line)
|
||||||
return this.sequence(
|
return this.sequence(
|
||||||
state, path, str, i+match.length, line,
|
state, path, str, i+match.length, line,
|
||||||
'>',
|
'>',
|
||||||
@ -428,6 +458,7 @@ module.eJSON = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
value: function(state, path, str, i=0, line=0){
|
value: function(state, path, str, i=0, line=0){
|
||||||
|
//debug.lex('value', str, i, line)
|
||||||
|
|
||||||
;[i, line] = this.skipWhitespace(str, i, line)
|
;[i, line] = this.skipWhitespace(str, i, line)
|
||||||
|
|
||||||
|
|||||||
85
test.js
Executable file
85
test.js
Executable file
@ -0,0 +1,85 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
/**********************************************************************
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
**********************************************************************/
|
||||||
|
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
|
||||||
|
(function(require){ var module={} // make module AMD/node compatible...
|
||||||
|
/*********************************************************************/
|
||||||
|
|
||||||
|
var test = require('ig-test')
|
||||||
|
|
||||||
|
var eJSON = require('./serialize2')
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
// XXX split into pure and extended JSON...
|
||||||
|
|
||||||
|
var setups = test.Setups({
|
||||||
|
number: function(assert){
|
||||||
|
return '123' },
|
||||||
|
string: function(assert){
|
||||||
|
return '"string"' },
|
||||||
|
'true': function(assert){
|
||||||
|
return 'true' },
|
||||||
|
'false': function(assert){
|
||||||
|
return 'false' },
|
||||||
|
|
||||||
|
'null': function(assert){
|
||||||
|
return 'null' },
|
||||||
|
'undefined': function(assert){
|
||||||
|
return 'undefined' },
|
||||||
|
'NaN': function(assert){
|
||||||
|
return 'NaN' },
|
||||||
|
|
||||||
|
'array-empty': function(assert){
|
||||||
|
return '[]' },
|
||||||
|
// XXX BUG: trailing <empty> gets duplicated...
|
||||||
|
'array-sparse': function(assert){
|
||||||
|
return '[1,<empty>,<empty>,2,<empty>]' },
|
||||||
|
|
||||||
|
'object-empty': function(assert){
|
||||||
|
return '{}' },
|
||||||
|
|
||||||
|
'set-empty': function(assert){
|
||||||
|
return 'Set([])' },
|
||||||
|
|
||||||
|
'map-empty': function(assert){
|
||||||
|
return 'Map([])' },
|
||||||
|
|
||||||
|
// XXX
|
||||||
|
})
|
||||||
|
|
||||||
|
test.Modifiers({
|
||||||
|
'array-stuffed': function(assert, setup){
|
||||||
|
return `[${ setup }]` },
|
||||||
|
'object-stuffed': function(assert, setup){
|
||||||
|
return `{"key":${ setup }}` },
|
||||||
|
'map-stuffed': function(assert, setup){
|
||||||
|
return `Map([["key",${ setup }],[${ setup },"value"]])` },
|
||||||
|
'set-stuffed': function(assert, setup){
|
||||||
|
return `Set([${ setup }])` },
|
||||||
|
})
|
||||||
|
|
||||||
|
test.Tests({
|
||||||
|
'self-apply': function(assert, setup){
|
||||||
|
var res
|
||||||
|
assert(
|
||||||
|
setup == (res = eJSON.serialize(eJSON.deserialize(setup))),
|
||||||
|
'serialize(deserialize( setup )) == setup: expected:', setup, 'got:', res) },
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
typeof(__filename) != 'undefined'
|
||||||
|
&& __filename == (require.main || {}).filename
|
||||||
|
&& test.run()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**********************************************************************
|
||||||
|
* vim:set ts=4 sw=4 nowrap : */ return module })
|
||||||
Loading…
x
Reference in New Issue
Block a user