Current Dev State

This commit is contained in:
Tim Lorsbach
2025-06-23 20:13:54 +02:00
parent b4f9bb277d
commit ded50edaa2
22617 changed files with 4345095 additions and 174 deletions

View File

@ -0,0 +1,356 @@
var VarStream = require('../src/VarStream')
, fs = require('fs')
, assert = require('assert')
, StringDecoder = require('string_decoder').StringDecoder;
// Tests
describe('Writing varstreams', function() {
describe("should work when", function() {
it("outputting simple values", function() {
var data = '';
new VarStream.Writer(function(str) {
data += str;
},
VarStream.Reader.STRICT_MODE).write({
'var1': 'machin',
'var2': 'bidule',
'var3': 'truc'
});
assert.equal(data,
'var1=machin\nvar2=bidule\nvar3=truc\n');
});
it("outputting variable trees", function() {
var data = '';
new VarStream.Writer(function(str) {
data += str;
}).write({
'var1': {
'var11': {
'var111':'machin'
},
'var12': 'bidule',
'var13': 'truc'
}
});
assert.equal(data,
'var1.var11.var111=machin\nvar1.var12=bidule\nvar1.var13=truc\n');
});
it("outputting variable trees and optimizing it", function() {
var data = '';
new VarStream.Writer(function(str) {
data += str;
},
VarStream.Writer.MORPH_CONTEXTS
).write({
'var1': {
'var11': {
'var111':'machin'
},
'var12': 'bidule',
'var13': 'truc'
}
});
assert.equal(data,
'var1.var11.var111=machin\nvar1.var12=bidule\n^.var13=truc\n');
});
it("outputting simple arrays", function() {
var data = '';
new VarStream.Writer(function(str) {
data += str;
}).write({
'var1': {
'var11': {
'var111': ['machin', 'bidule', 'truc']
},
'var12': ['machin', 'bidule', 'truc'],
'var13': ['machin', 'bidule', 'truc']
}
});
assert.equal(data,
'var1.var11.var111.0=machin\n' +
'var1.var11.var111.1=bidule\n' +
'var1.var11.var111.2=truc\n' +
'var1.var12.0=machin\n' +
'var1.var12.1=bidule\n' +
'var1.var12.2=truc\n' +
'var1.var13.0=machin\n' +
'var1.var13.1=bidule\n' +
'var1.var13.2=truc\n'
);
});
it("outputting simple arrays in merging mode", function() {
var data = '';
new VarStream.Writer(function(str) {
data += str;
},
VarStream.Writer.MERGE_ARRAYS
).write({
'var1': {
'var11': {
'var111': ['machin', 'bidule', 'truc']
},
'var12': ['machin', 'bidule', 'truc'],
'var13': ['machin', 'bidule', 'truc']
}
});
assert.equal(data,
'var1.var11.var111.+=machin\n' +
'var1.var11.var111.+=bidule\n' +
'var1.var11.var111.+=truc\n' +
'var1.var12.+=machin\n' +
'var1.var12.+=bidule\n' +
'var1.var12.+=truc\n' +
'var1.var13.+=machin\n' +
'var1.var13.+=bidule\n' +
'var1.var13.+=truc\n'
);
});
it("outputting simple arrays and optimizing", function() {
var data = '';
new VarStream.Writer(function(str) {
data += str;
},
VarStream.Writer.MORPH_CONTEXTS
).write({
'var1': {
'var11': {
'var111': ['machin', 'bidule', 'truc']
},
'var12': ['machin', 'bidule', 'truc'],
'var13': ['machin', 'bidule', 'truc']
}
});
assert.equal(data,
'var1.var11.var111.0=machin\n' +
'^.1=bidule\n' +
'^.2=truc\n' +
'var1.var12.0=machin\n' +
'^.1=bidule\n' +
'^.2=truc\n' +
'var1.var13.0=machin\n' +
'^.1=bidule\n' +
'^.2=truc\n'
);
});
it("outputting object collections", function() {
var data = '';
new VarStream.Writer(function(str) {
data += str;
}).write({
'var1': {
'var11': {
'var111': [{
name: 'machin',
type: 'obj'
}, {
name: 'bidule',
type: 'obj'
}, {
name: 'truc',
type: 'obj'
}]
},
'var12': [{
name: 'machin',
type: 'obj'
}, {
name: 'bidule',
type: 'obj'
}, {
name: 'truc',
type: 'obj'
}],
'var13': [{
name: 'machin',
type: 'obj'
}, {
name: 'bidule',
type: 'obj'
}, {
name: 'truc',
type: 'obj'
}]
}
});
assert.equal(data,
'var1.var11.var111.0.name=machin\n' +
'var1.var11.var111.0.type=obj\n' +
'var1.var11.var111.1.name=bidule\n' +
'var1.var11.var111.1.type=obj\n' +
'var1.var11.var111.2.name=truc\n' +
'var1.var11.var111.2.type=obj\n' +
'var1.var12.0.name=machin\n' +
'var1.var12.0.type=obj\n' +
'var1.var12.1.name=bidule\n' +
'var1.var12.1.type=obj\n' +
'var1.var12.2.name=truc\n' +
'var1.var12.2.type=obj\n' +
'var1.var13.0.name=machin\n' +
'var1.var13.0.type=obj\n' +
'var1.var13.1.name=bidule\n' +
'var1.var13.1.type=obj\n' +
'var1.var13.2.name=truc\n' +
'var1.var13.2.type=obj\n'
);
});
it("outputting object collections in merging mode", function() {
var data = '';
new VarStream.Writer(function(str) {
data += str;
},
VarStream.Writer.MERGE_ARRAYS
).write({
'var1': {
'var11': {
'var111': [{
name: 'machin',
type: 'obj'
}, {
name: 'bidule',
type: 'obj'
}, {
name: 'truc',
type: 'obj'
}]
},
'var12': [{
name: 'machin',
type: 'obj'
}, {
name: 'bidule',
type: 'obj'
}, {
name: 'truc',
type: 'obj'
}],
'var13': [{
name: 'machin',
type: 'obj'
}, {
name: 'bidule',
type: 'obj'
}, {
name: 'truc',
type: 'obj'
}]
}
});
assert.equal(data,
'var1.var11.var111.+.name=machin\n' +
'var1.var11.var111.*.type=obj\n' +
'var1.var11.var111.+.name=bidule\n' +
'var1.var11.var111.*.type=obj\n' +
'var1.var11.var111.+.name=truc\n' +
'var1.var11.var111.*.type=obj\n' +
'var1.var12.+.name=machin\n' +
'var1.var12.*.type=obj\n' +
'var1.var12.+.name=bidule\n' +
'var1.var12.*.type=obj\n' +
'var1.var12.+.name=truc\n' +
'var1.var12.*.type=obj\n' +
'var1.var13.+.name=machin\n' +
'var1.var13.*.type=obj\n' +
'var1.var13.+.name=bidule\n' +
'var1.var13.*.type=obj\n' +
'var1.var13.+.name=truc\n' +
'var1.var13.*.type=obj\n'
);
});
it("outputting object collections and optimizing", function() {
var data = '';
new VarStream.Writer(function(str) {
data += str;
},
VarStream.Writer.MORPH_CONTEXTS
).write({
'var1': {
'var11': {
'var111': [{
name: 'machin',
type: 'obj'
}, {
name: 'bidule',
type: 'obj'
}, {
name: 'truc',
type: 'obj'
}]
},
'var12': [{
name: 'machin',
type: 'obj'
}, {
name: 'bidule',
type: 'obj'
}, {
name: 'truc',
type: 'obj'
}],
'var13': [{
name: 'machin',
type: 'obj'
}, {
name: 'bidule',
type: 'obj'
}, {
name: 'truc',
type: 'obj'
}]
}
});
assert.equal(data,
'var1.var11.var111.0.name=machin\n' +
'^.type=obj\n' +
'var1.var11.var111.1.name=bidule\n' +
'^.type=obj\n' +
'var1.var11.var111.2.name=truc\n' +
'^.type=obj\n' +
'var1.var12.0.name=machin\n' +
'^.type=obj\n' +
'var1.var12.1.name=bidule\n' +
'^.type=obj\n' +
'var1.var12.2.name=truc\n' +
'^.type=obj\n' +
'var1.var13.0.name=machin\n' +
'^.type=obj\n' +
'var1.var13.1.name=bidule\n' +
'^.type=obj\n' +
'var1.var13.2.name=truc\n' +
'^.type=obj\n'
);
});
});
});
describe('Writing bad varstreams', function() {
describe("should raise exceptions when in strict mode and", function() {
it("the given root object is not an object or an array", function() {
assert.throws(
function() {
new VarStream.Writer(function() {}).write('');
},
function(err) {
if(err instanceof Error
&&err.message==='The root scope must be an Object or an Array.') {
return true;
}
}
);
});
});
});

View File

@ -0,0 +1,32 @@
aSimpleIntValue=7
aSimpleIntNegativeValue=4988
aSimpleFloatValue=401.99999999999994
aSimpleFloatNegativeValue=476400
aSimpleBoolValueTrue=true
aSimpleBoolValueFalse=false
aSimpleNullValue=null
aSimpleStringValue=I'm the king of the world! Yep!
aSimpleStringMultilineValue=I'm the king of the world!\
You know!\
It's true.\
Yep.
treeRoot.branch1.aSimpleIntValue=2000
treeRoot.branch1.aSimpleIntNegativeValue=-2000
treeRoot.branch3.aSimpleBoolValueFalse=false
treeRoot.branch3.branch1.aSimpleNullValue=null
treeRoot.branch3.branch1.aSimpleStringValue=I'm not the king of the world!
treeRoot.branch3.branch2.aSimpleStringMultilineValue=I'm not the king of the world!\
You know!\
It's true.
treeRoot.branch3.branch2.aSimpleWellDeclaredStringValue="I'm not the king of the world!"
treeRoot.branch3.branch2.branch1.aSimpleWellDeclaredStringMultilineValue="I'm not the king of the world!
treeRoot.branch3.aSimpleBoolValueTrue=true
treeRoot.branch3.branch4&=treeRoot.branch3.branch2
treeRoot.branch2.aSimpleFloatValue=2.0001
treeRoot.branch2.aSimpleFloatNegativeValue=-1000.0002
aSimpleWellDeclaredStringValue=undefined" Yep!"
aSimpleWellDeclaredStringMultilineValue=undefined"
aSimpleArray.0=10
aSimpleArray.1=10
aSimpleArray2.0.value=9
aSimpleArray2.1.value=9

View File

@ -0,0 +1,12 @@
# Simple values
aSimpleIntValue=1898
aSimpleIntNegativeValue=-1669
aSimpleFloatValue=1.0025
aSimpleFloatNegativeValue=-1191.0025
aSimpleBoolValueTrue=true
aSimpleBoolValueFalse=false
aSimpleNullValue=null
aSimpleStringValue=I'm the king of the world!
aSimpleStringMultilineValue=I'm the king of the world!\
You know!\
It's true.

View File

@ -0,0 +1,16 @@
# Simple values in a tree
treeRoot.branch1.aSimpleIntValue=1898
treeRoot.branch1.aSimpleIntNegativeValue=-1669
treeRoot.branch2.aSimpleFloatValue=1.0025
treeRoot.branch2.aSimpleFloatNegativeValue=-1191.0025
treeRoot.branch3.aSimpleBoolValueTrue=true
treeRoot.branch3.aSimpleBoolValueFalse=false
treeRoot.branch3.branch1.aSimpleNullValue=null
treeRoot.branch3.branch1.aSimpleStringValue=I'm the king of the world!
treeRoot.branch3.branch2.aSimpleStringMultilineValue=I'm the king of the world!\
You know!\
It's true.
treeRoot.branch3.branch2.aSimpleWellDeclaredStringValue="I'm the king of the world!"
treeRoot.branch3.branch2.branch1.aSimpleWellDeclaredStringMultilineValue="I'm the king of the world!
You \"know\"!
It's true."

View File

@ -0,0 +1,5 @@
treeRoot.branch1.aSimpleIntNegativeValue=
treeRoot.branch2=
treeRoot.branch3.aSimpleBoolValueTrue=
treeRoot.branch3.branch1.aSimpleStringValue=
# above lines should delete indexes

View File

@ -0,0 +1,16 @@
# Simple values in a tree with backward references
treeRoot.branch1.aSimpleIntValue=2000
^.aSimpleIntNegativeValue=-2000
treeRoot.branch2.aSimpleFloatValue=2.0001
^.aSimpleFloatNegativeValue=-1000.0002
treeRoot.branch3.aSimpleBoolValueTrue=true
^1.branch3.aSimpleBoolValueFalse=false
^2.branch1.aSimpleNullValue=null
^.aSimpleStringValue=I'm not the king of the world!
^2.branch2.aSimpleStringMultilineValue=I'm not the king of the world!\
You know!\
It's true.
^.aSimpleWellDeclaredStringValue="I'm not the king of the world!"
^.branch1.aSimpleWellDeclaredStringMultilineValue="I'm not the king of the world!
You \"know\"!
It's true."

View File

@ -0,0 +1,22 @@
# Simple values in a tree with operators
# Numbers
aSimpleIntValue+=5
aSimpleIntValue*=2
aSimpleIntValue-=15
aSimpleIntValue%=8
aSimpleIntNegativeValue+=6
aSimpleIntNegativeValue*=-3
aSimpleIntNegativeValue-=1
aSimpleFloatValue+=0.0025
aSimpleFloatValue/=0.0025
aSimpleFloatNegativeValue-=-0.0025
aSimpleFloatNegativeValue/=-0.0025
# Strings
aSimpleStringValue+= Yep!
aSimpleStringMultilineValue+=\
Yep.
aSimpleWellDeclaredStringValue+=" Yep!"
aSimpleWellDeclaredStringMultilineValue+="
Yep."
# Objects
treeRoot.branch3.branch4&=treeRoot.branch3.branch2

View File

@ -0,0 +1,20 @@
# Playing with arrays
aSimpleArray.!=0
aSimpleArray.+=1
aSimpleArray.+=2
aSimpleArray.+=3
^.+=4
^.+=5
^.+=6
^.+=7
^.*=8
^.*=9
aSimpleArray.+=9
aSimpleArray.*=8
aSimpleArray2.!.value=0
aSimpleArray2.+.value=1
aSimpleArray2.!.value=2
aSimpleArray2.+.value=3
aSimpleArray2.*.value=4
aSimpleArray2.0.value=5
aSimpleArray2.9.value=5

View File

@ -0,0 +1,5 @@
# More complicated backward references
aSimpleArray.+.test.test=Final pop !
"-1.test.test=Final pop modified !
aSimpleArray.+.test.test=New final pop !
"-2.+.test.test=New final pop modified !

View File

@ -0,0 +1,5 @@
# More complicated backward references
aSimpleArray.+.test.test=Final pop !
"-1.test.test=Final pop modified !
aSimpleArray.+.test.test=New final pop !
"-2.+.test.test=New final pop modified !

View File

@ -0,0 +1,5 @@
aSimpleArray.!=10
aSimpleArray.+&=aSimpleArray.*
aSimpleArray2.!.value=9
aSimpleArray2.+.value&=aSimpleArray2.*.value

View File

@ -0,0 +1,16 @@
# Simple values
aSimpleIntValue=1898
aSimpleIntNegativeValue=-1669
aSimpleFloatValue=1.0025
aSimpleFloatNegativeValue=-1191.0025
aSimpleBoolValueTrue=true
aSimpleBoolValueFalse=false
aSimpleNullValue=null
aSimpleStringValue=I'm the king of the world!
aSimpleStringMultilineValue=I'm the king of the world!\
You know!\
It's true.
aSimpleWellDeclaredStringValue="I'm the king of the world!"
aSimpleWellDeclaredStringMultilineValue="I'm the king of the world!
You \"know\"!
It's true."

View File

@ -0,0 +1,18 @@
aComplexerArray.+.value=plop
aComplexerArray.*.value2=plip
aComplexerArray.*.subarray.+.value=plop
aComplexerArray.*.subarray.*.value=plip
aComplexerArray.*.subarray.+.value=plop
aComplexerArray.*.subarray.*.value=plip
aComplexerArray.*.subarray.+.value=plop
aComplexerArray.*.subarray.*.value=plip
aComplexerArray.*.subarray.*.subarray.+.value=plop
aComplexerArray.*.subarray.*.subarray.*.value=plip
aComplexerArray.+.value=plop
aComplexerArray.*.value2=plip
aComplexerArray.*.+=plop
aComplexerArray.*.+=plip
aComplexerArray.*.*.+=plop
aComplexerArray.*.*.+=plip
aComplexerArray.+.value=plop
aComplexerArray.*.value2=plip

View File

@ -0,0 +1,6 @@
childs.+.childs.+.title=test-file1
childs.*.childs.*.parent&=childs.*
childs.*.childs.+.title=test-file2
childs.*.childs.*.parent&=childs.*
childs.*.index.title=test-index
# voir avec root scope

9
static/js/ketcher2/node_modules/varstream/tests/index.html generated vendored Executable file
View File

@ -0,0 +1,9 @@
<html>
<head>
<title>Test page</title>
<script type="text/javascript" src="./../src/VarStreamReader.js"></script>
</head>
<body>
Helper for debug / tests
</body>
</html>

View File

@ -0,0 +1,993 @@
var VarStream = require('../src/VarStream')
, fs = require('fs')
, assert = require('assert')
, StringDecoder = require('string_decoder').StringDecoder;
// Tests
describe('Parsing VarStream', function() {
var scope = {};
var myVarStream=new VarStream(scope,'vars');
it("should work for simple datas", function(done) {
var readStream = fs.createReadStream(__dirname+'/fixtures/1-simple.dat');
readStream.pipe(myVarStream, { end: false });
readStream.on('end', function () {
// Numbers
assert.equal(typeof scope.vars.aSimpleIntValue, 'number');
assert.equal(scope.vars.aSimpleIntValue,1898);
assert.equal(typeof scope.vars.aSimpleIntNegativeValue, 'number');
assert.equal(scope.vars.aSimpleIntNegativeValue,-1669);
assert.equal(typeof scope.vars.aSimpleFloatValue, 'number');
assert.equal(scope.vars.aSimpleFloatValue,1.0025);
assert.equal(typeof scope.vars.aSimpleFloatNegativeValue, 'number');
assert.equal(scope.vars.aSimpleFloatNegativeValue,-1191.0025);
// Booleans
assert.equal(typeof scope.vars.aSimpleBoolValueTrue, 'boolean');
assert.equal(scope.vars.aSimpleBoolValueTrue,true);
assert.equal(typeof scope.vars.aSimpleBoolValueFalse, 'boolean');
assert.equal(scope.vars.aSimpleBoolValueFalse,false);
// Strings
assert.equal(typeof scope.vars.aSimpleStringValue, 'string');
assert.equal(scope.vars.aSimpleStringValue,"I'm the king of the world!");
assert.equal(typeof scope.vars.aSimpleStringMultilineValue, 'string');
assert.equal(scope.vars.aSimpleStringMultilineValue,
"I'm the king of the world!\nYou know!\nIt's true.");
done();
});
});
/*
it("should work for other text", function(done) {
fs.createReadStream(__dirname+'/fixtures/9-othertext.dat').pipe(myVarStream)
.once('end', function () {
// Strings
assert.equal(typeof scope.vars.aSimpleWellDeclaredStringValue, 'string');
assert.equal(scope.vars.aSimpleWellDeclaredStringValue,
"I'm the king of the world!");
assert.equal(typeof scope.vars.aSimpleWellDeclaredStringMultilineValue,
'string');
assert.equal(scope.vars.aSimpleWellDeclaredStringMultilineValue,
"I'm the king of the world!\nYou \"know\"!\nIt's true.");
done();
});
});*/
it("should work for data trees", function(done) {
var readStream = fs.createReadStream(__dirname+'/fixtures/2-trees.dat');
readStream.pipe(myVarStream, { end: false });
readStream.on('end', function () {
assert.equal(typeof scope.vars.treeRoot.branch1,'object');
assert.equal(scope.vars.treeRoot.branch1.aSimpleIntValue,1898);
assert.equal(scope.vars.treeRoot.branch1.aSimpleIntNegativeValue,-1669);
assert.equal(typeof scope.vars.treeRoot.branch2,'object');
assert.equal(scope.vars.treeRoot.branch2.aSimpleFloatValue,1.0025);
assert.equal(scope.vars.treeRoot.branch2.aSimpleFloatNegativeValue,
-1191.0025);
assert.equal(typeof scope.vars.treeRoot.branch3,'object');
assert.equal(scope.vars.treeRoot.branch3.aSimpleBoolValueTrue,true);
assert.equal(scope.vars.treeRoot.branch3.aSimpleBoolValueFalse,false);
assert.equal(typeof scope.vars.treeRoot.branch3.branch1,'object');
assert.equal(scope.vars.treeRoot.branch3.branch1.aSimpleNullValue,null);
assert.equal(scope.vars.treeRoot.branch3.branch1.aSimpleStringValue,
"I'm the king of the world!");
assert.equal(typeof scope.vars.treeRoot.branch3.branch2,'object');
assert.equal(scope.vars.treeRoot.branch3.branch2.aSimpleStringMultilineValue,
"I'm the king of the world!\r\nYou know!\r\nIt's true.");
//assert.equal(scope.vars.treeRoot.branch3.branch2.aSimpleWellDeclaredStringValue,
// "I'm the king of the world!");
//assert.equal(typeof scope.vars.treeRoot.branch3.branch2.branch1,'object');
//assert.equal(
// scope.vars.treeRoot.branch3.branch2.branch1.aSimpleWellDeclaredStringMultilineValue,
// "I'm the king of the world!\nYou \"know\"!\nIt's true.");
done();
});
});
it("should deleted empty datas", function(done) {
var readStream = fs.createReadStream(__dirname+'/fixtures/3-delete.dat');
readStream.pipe(myVarStream, { end: false });
readStream.on('end', function () {
assert.equal(typeof scope.vars.treeRoot.branch1.aSimpleIntNegativeValue,'undefined');
assert.equal(typeof scope.vars.treeRoot.branch2,'undefined');
assert.equal(typeof scope.vars.treeRoot.branch3.aSimpleBoolValueTrue,'undefined');
assert.equal(typeof scope.vars.treeRoot.branch3.branch1.aSimpleStringValue,'string');
assert.equal(scope.vars.treeRoot.branch3.branch1.aSimpleStringValue,'');
done();
});
});
it("should take backward references in count", function(done) {
var readStream = fs.createReadStream(__dirname+'/fixtures/4-backward.dat');
readStream.pipe(myVarStream, { end: false });
readStream.on('end', function () {
assert.equal(typeof scope.vars.treeRoot.branch1,'object');
assert.strictEqual(scope.vars.treeRoot.branch1.aSimpleIntValue,2000);
assert.strictEqual(scope.vars.treeRoot.branch1.aSimpleIntNegativeValue,-2000);
assert.equal(typeof scope.vars.treeRoot.branch2,'object');
assert.strictEqual(scope.vars.treeRoot.branch2.aSimpleFloatValue,2.0001);
assert.strictEqual(scope.vars.treeRoot.branch2.aSimpleFloatNegativeValue,-1000.0002);
assert.equal(typeof scope.vars.treeRoot.branch3,'object');
assert.strictEqual(scope.vars.treeRoot.branch3.aSimpleBoolValueTrue,true);
assert.strictEqual(scope.vars.treeRoot.branch3.aSimpleBoolValueFalse,false);
assert.equal(typeof scope.vars.treeRoot.branch3.branch1,'object');
assert.strictEqual(scope.vars.treeRoot.branch3.branch1.aSimpleNullValue,null);
assert.strictEqual(scope.vars.treeRoot.branch3.branch1.aSimpleStringValue,
"I'm not the king of the world!");
//assert.equal(typeof scope.vars.treeRoot.branch3.branch2,'object');
//assert.equal(scope.vars.treeRoot.branch3.branch2.aSimpleStringMultilineValue,
// "I'm not the king of the world!\nYou know!\nIt's true.");
//assert.equal(scope.vars.treeRoot.branch3.branch2.aSimpleWellDeclaredStringValue,
// "I'm not the king of the world!");
//assert.equal(typeof scope.vars.treeRoot.branch3.branch2.branch1,'object');
//assert.equal(
// scope.vars.treeRoot.branch3.branch2.branch1.aSimpleWellDeclaredStringMultilineValue,
// "I'm not the king of the world!\nYou \"know\"!\nIt's true.");
done();
});
});
it("should work with operators", function(done) {
var readStream = fs.createReadStream(__dirname+'/fixtures/5-operators.dat');
readStream.pipe(myVarStream, { end: false });
readStream.on('end', function () {
// Numbers
assert.equal(typeof scope.vars.aSimpleIntValue, 'number');
assert.equal(scope.vars.aSimpleIntValue,(((1898+5)*2)-15)%8);
assert.equal(typeof scope.vars.aSimpleIntNegativeValue, 'number');
assert.equal(scope.vars.aSimpleIntNegativeValue,(((-1669)+6)*-3)-1);
assert.equal(typeof scope.vars.aSimpleFloatValue, 'number');
assert.equal(scope.vars.aSimpleFloatValue,(1.0025+0.0025)/0.0025);
assert.equal(typeof scope.vars.aSimpleFloatNegativeValue, 'number');
assert.equal(scope.vars.aSimpleFloatNegativeValue,((-1191.0025)-(-0.0025))/-0.0025);
// Booleans
assert.equal(typeof scope.vars.aSimpleBoolValueTrue, 'boolean');
assert.equal(scope.vars.aSimpleBoolValueTrue,true);
assert.equal(typeof scope.vars.aSimpleBoolValueFalse, 'boolean');
assert.equal(scope.vars.aSimpleBoolValueFalse,false);
// Strings
assert.equal(typeof scope.vars.aSimpleStringValue, 'string');
assert.equal(scope.vars.aSimpleStringValue,"I'm the king of the world! Yep!");
assert.equal(typeof scope.vars.aSimpleStringMultilineValue, 'string');
assert.equal(scope.vars.aSimpleStringMultilineValue,
"I'm the king of the world!\nYou know!\nIt's true.\r\nYep.");
/*assert.equal(typeof scope.vars.aSimpleWellDeclaredStringValue, 'string');
assert.equal(scope.vars.aSimpleWellDeclaredStringValue,
"I'm the king of the world! Yep!");
assert.equal(typeof scope.vars.aSimpleWellDeclaredStringMultilineValue,
'string');
assert.equal(scope.vars.aSimpleWellDeclaredStringMultilineValue,
"I'm the king of the world!\nYou \"know\"!\nIt's true.\nYep.");
assert.equal(scope.vars.treeRoot.branch3.branch4,
scope.vars.treeRoot.branch3.branch2);*/
done();
});
});
it("should work with arrays", function(done) {
var readStream = fs.createReadStream(__dirname+'/fixtures/6-arrays.dat');
readStream.pipe(myVarStream, { end: false });
readStream.on('end', function () {
// First array
assert.equal(typeof scope.vars.aSimpleArray,'object');
assert.equal(scope.vars.aSimpleArray instanceof Array,true);
assert.equal(scope.vars.aSimpleArray.length,9);
assert.equal(scope.vars.aSimpleArray[0],0);
assert.equal(scope.vars.aSimpleArray[1],1);
assert.equal(scope.vars.aSimpleArray[2],2);
assert.equal(scope.vars.aSimpleArray[3],3);
assert.equal(scope.vars.aSimpleArray[4],4);
assert.equal(scope.vars.aSimpleArray[5],5);
assert.equal(scope.vars.aSimpleArray[6],6);
assert.equal(scope.vars.aSimpleArray[7],9);
assert.equal(scope.vars.aSimpleArray[8],8);
// Second array
assert.equal(typeof scope.vars.aSimpleArray2,'object');
assert.equal(scope.vars.aSimpleArray2 instanceof Array,true);
assert.equal(scope.vars.aSimpleArray2.length,10);
assert.equal(scope.vars.aSimpleArray2[0].value,5);
assert.equal(scope.vars.aSimpleArray2[1].value,4);
assert.equal(typeof scope.vars.aSimpleArray2[2],'undefined');
assert.equal(typeof scope.vars.aSimpleArray2[3],'undefined');
assert.equal(typeof scope.vars.aSimpleArray2[4],'undefined');
assert.equal(typeof scope.vars.aSimpleArray2[5],'undefined');
assert.equal(typeof scope.vars.aSimpleArray2[6],'undefined');
assert.equal(typeof scope.vars.aSimpleArray2[7],'undefined');
assert.equal(typeof scope.vars.aSimpleArray2[8],'undefined');
assert.equal(scope.vars.aSimpleArray2[9].value,5);
done();
});
});
it("Should work with truncated content beetween chunks", function(done) {
var readStream = fs.createReadStream(__dirname+'/fixtures/7-truncated-part1.dat');
readStream.pipe(myVarStream, { end: false })
readStream.on('end', function () {
done();
});
});
it("Should work with truncated content beetween chunks", function(done) {
var readStream = fs.createReadStream(__dirname+'/fixtures/7-truncated-part2.dat');
readStream.pipe(myVarStream, { end: false });
readStream.on('end', function () {
done();
});
});
it("Should interpret rightvals first", function(done) {
var readStream = fs.createReadStream(__dirname+'/fixtures/8-rightval.dat');
readStream.pipe(myVarStream, { end: false });
readStream.on('end', function () {
assert.equal(scope.vars.aSimpleArray[0],10);
assert.equal(scope.vars.aSimpleArray[1],10);
assert.equal(scope.vars.aSimpleArray2[0].value,9);
assert.equal(scope.vars.aSimpleArray2[1].value,9);
done();
});
});
it("Should output good contents", function(done) {
var content = ''
, decoder = new StringDecoder('utf8');
myVarStream.on('data', function(chunk) {
content += decoder.write(chunk);
});
myVarStream.on('end', function() {
assert.equal(content,
fs.readFileSync(__dirname+'/fixtures/0-result.dat', 'utf8'));
done();
});
});
});
describe('Reading chunked varstreams', function() {
it("Should work at the lval level", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
myVarStream.read('aVar1=ok\naVa');
assert.equal(scope.vars.aVar1,'ok');
assert.equal(typeof scope.vars.aVar2,'undefined');
myVarStream.read('r2=1000\naVar3=2000\n');
assert.equal(scope.vars.aVar2,1000);
assert.equal(scope.vars.aVar3,2000);
});
it("Should work at the operator level", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
myVarStream.read('aVar1=ok\naVar2');
assert.equal(scope.vars.aVar1,'ok');
assert.equal(typeof scope.vars.aVar2,'undefined');
myVarStream.read('=1000\naVar3=2000\n');
assert.equal(scope.vars.aVar2,1000);
assert.equal(scope.vars.aVar3,2000);
});
it("Should work at the next operator level", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
myVarStream.read('aVar1=ok\naVar2=');
assert.equal(scope.vars.aVar1,'ok');
assert.equal(typeof scope.vars.aVar2,'undefined');
myVarStream.read('1000\naVar3=2000\n');
assert.equal(scope.vars.aVar2,1000);
assert.equal(scope.vars.aVar3,2000);
});
it("Should work at the rval level", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
myVarStream.read('aVar1=ok\naVar2=1');
assert.equal(scope.vars.aVar1,'ok');
assert.equal(typeof scope.vars.aVar2,'undefined');
myVarStream.read('000\naVar3=2000\n');
assert.equal(scope.vars.aVar2,1000);
assert.equal(scope.vars.aVar3,2000);
});
it("Should work at the multiline level", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
myVarStream.read('aVar1=ok\naVar2=Y');
assert.equal(scope.vars.aVar1,'ok');
assert.equal(typeof scope.vars.aVar2,'undefined');
myVarStream.read('ep!\\\n');
assert.equal(scope.vars.aVar2,'Yep!\n');
myVarStream.read(' That\'');
assert.equal(scope.vars.aVar2,'Yep!\n That\'');
myVarStream.read('s me!\n');
assert.equal(scope.vars.aVar2,'Yep!\n That\'s me!');
});
});
describe('Reading varstreams', function() {
it("should work well with props containing underscores", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
myVarStream.read('a_obj.a_text=This is a text \\\\\n');
assert.strictEqual(scope.vars.a_obj.a_text,'This is a text \\');
myVarStream.read('a_obj.a_text=This is \\a text.\n');
assert.strictEqual(scope.vars.a_obj.a_text,'This is \\a text.');
myVarStream.read('a_obj.a_text=This is \\\\a text.\n');
assert.strictEqual(scope.vars.a_obj.a_text,'This is \\a text.');
});
it("should work well when chars are escaped", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
myVarStream.read('aObj.aText=This is a text \\\\\n');
assert.strictEqual(scope.vars.aObj.aText,'This is a text \\');
myVarStream.read('aObj.aText=This is \\a text.\n');
assert.strictEqual(scope.vars.aObj.aText,'This is \\a text.');
myVarStream.read('aObj.aText=This is \\\\a text.\n');
assert.strictEqual(scope.vars.aObj.aText,'This is \\a text.');
});
it("should work when the stream refers to its root scope with ^", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
myVarStream.read('aObj.parent&=^\n');
assert.equal(scope.vars,scope.vars.aObj.parent);
});
it("should work when the stream refers to its root scope with ^0", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
myVarStream.read('aObj.parent&=^\n');
assert.equal(scope.vars,scope.vars.aObj.parent);
});
it("should work well when chars are escaped in multiline strings", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
myVarStream.read('aObj.aText=This is a \\\n');
assert.strictEqual(scope.vars.aObj.aText,'This is a \n');
myVarStream.read('multiline \\ text.\\\n');
assert.strictEqual(scope.vars.aObj.aText,'This is a \nmultiline \\ text.\n');
myVarStream.read('With two \\\\ lines.\\\\\n');
assert.strictEqual(scope.vars.aObj.aText,'This is a \nmultiline \\ text.\nWith two \\ lines.\\');
});
});
describe('Reading bad varstreams', function() {
describe("should silently fail when", function() {
it("a line ends with no value after =", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});
it("a line has an empty lvalue", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\n=truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});
it("a line has an empty lvalue with a multiline rightvalue", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\n=truc\\n1\\n2\\nmachin\n\nvalidVar2=bidule\n'); // Something wrong here
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});
it("a line ends with no value after &=", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar&=\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});
it("a line ends with no value after +=", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar+=\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});
it("a line ends with no value after -=", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar-=\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});
it("a line ends with no value after /=", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar/=\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});
it("when there are an empty node at start", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\n.ASimpleVar=truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});
it("when there are an empty node somewhere", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar.aNode..anotherNode.and=truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
ASimpleVar: {},
validVar2: 'bidule'
});
});
it("when there are an empty node at end", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar.anode.=truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
ASimpleVar: {},
validVar2: 'bidule'
});
});
it("when there malformed nodes at start", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\n$%*.ASimpleVar.anode.$=truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});
it("when there malformed nodes somewhere", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar.anode.$.another=truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
ASimpleVar: {},
validVar2: 'bidule'
});
});
it("when there malformed nodes at end", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar.anode.$=truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
ASimpleVar: {},
validVar2: 'bidule'
});
});
it("there are malformed backward reference in a leftval", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\n^3g.SimpleVar.anode=truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});
it("there are malformed backward reference in a leftval 2", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\n^g3.SimpleVar.anode=truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});
it("there are malformed backward reference in a righval", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar.anode&=^3g.truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
ASimpleVar: {anode: null},
validVar2: 'bidule'
});
});
it("there are malformed backward reference in a righval 2", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar.anode&=^g3.truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
ASimpleVar: {anode: null},
validVar2: 'bidule'
});
});
it("there are out of range backward reference in a leftval", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\n^12.ASimpleVar.anode=truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
validVar2: 'bidule'
});
});/*
it("there are out of range backward reference in a rightval", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar.anode+=^12.truc.truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
ASimpleVar: {anode: null},
validVar2: 'bidule'
});
});*/
it("a legal char is escaped", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar.anode=truc\\truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
ASimpleVar: {anode: 'truc\\truc'},
validVar2: 'bidule'
});
});
it("a legal char is escaped in a multiline value", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars');
assert.doesNotThrow(function() {
myVarStream.read('validVar1=machin\nASimpleVar.anode=truc\\\ntruc\\truc\nvalidVar2=bidule\n');
});
assert.deepEqual(scope.vars, {
validVar1: 'machin',
ASimpleVar: {anode: 'truc\ntruc\\truc'},
validVar2: 'bidule'
});
});
});
describe("in strict mode, should raise exceptions when", function() {
it("a line ends with no =", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Unexpected new line found while parsing '
+' a leftValue.') {
return true;
}
}
);
});
it("a line has no lvalue", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('=ASimpleVar\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Found an empty leftValue.') {
return true;
}
}
);
});
it("a line ends with no value after &=", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar&=\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Found an empty rightValue.') {
return true;
}
}
);
});
it("a line ends with no value after +=", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar+=\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Found an empty rightValue.') {
return true;
}
}
);
});
it("a line ends with no value after -=", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar-=\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Found an empty rightValue.') {
return true;
}
}
);
});
it("a line ends with no value after *=", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar*=\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Found an empty rightValue.') {
return true;
}
}
);
});
it("a line ends with no value after /=", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar/=\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Found an empty rightValue.') {
return true;
}
}
);
});
it("there are a empty node at start", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('.ASimpleVar=true\n');
},
function(err) {
if(err instanceof Error
&&err.message==='The leftValue can\'t have empty nodes'
+' (.ASimpleVar).') {
return true;
}
}
);
});
it("there are a empty node somewhere", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar.prop..prop.prop=true\n');
},
function(err) {
if(err instanceof Error
&&err.message==='The leftValue can\'t have empty nodes'
+' (ASimpleVar.prop..prop.prop).') {
return true;
}
}
);
});
it("there are a empty node at end", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar.=false\n');
},
function(err) {
if(err instanceof Error
&&err.message==='The leftValue can\'t have empty nodes (ASimpleVar.).') {
return true;
}
}
);
});
it("there are malformed nodes", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimp-+leVar.ds+d=false\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Unexpected char after the "-" operator. Expected "="'
+' found "+".') {
return true;
}
}
);
});
it("there are malformed nodes", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar.ds$d=false\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Illegal chars found in a the node "ds$d".') {
return true;
}
}
);
});
it("there are out of range backward reference", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar.prop1.prop2=false\n^4.test=true\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Backward reference index is greater than the'
+' previous node max index.') {
return true;
}
}
);
});
it("there are malformed backward reference in a righval", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar.prop1.prop2=false\n^4b.test=true\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Malformed backward reference.') {
return true;
}
}
);
});
it("there are malformed backward reference in a righval 2", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('ASimpleVar.prop1.prop2=false\n^b5.test=true\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Malformed backward reference.') {
return true;
}
}
);
});
it("there are malformed backward reference in a leftval", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('^b5.test=true\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Malformed backward reference.') {
return true;
}
}
);
});
it("there are malformed backward reference in a leftval 2", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('^5b.test=true\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Malformed backward reference.') {
return true;
}
}
);
});
it("there are bad range backward reference in a leftval", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('^8.test=true\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Backward reference index is greater than the previous node max index.') {
return true;
}
}
);
});
it("a legal char is escaped", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('test=t\\rue\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Found an escape char but there was nothing to escape.') {
return true;
}
}
);
});
it("a legal char is escaped in a multiline value", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('test=truc\\\ntruc\\truc\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Found an escape char but there was nothing to escape.') {
return true;
}
}
);
});
it("an operator is not folled by =", function() {
var scope = {};
var myVarStream=new VarStream.Reader(scope,'vars',
VarStream.Reader.STRICT_MODE);
assert.throws(
function() {
myVarStream.read('test&\n');
},
function(err) {
if(err instanceof Error
&&err.message==='Unexpected char after the "&" operator. Expected "=" found "\n".') {
return true;
}
}
);
});
});
});

View File

@ -0,0 +1,172 @@
var VarStream = require('../src/VarStream')
, fs = require('fs')
, assert = require('assert')
, StringDecoder = require('string_decoder').StringDecoder;
describe('VarStream constructor', function() {
it('should work when new is omitted', function() {
assert.doesNotThrow(function() {
VarStream({}, 'prop');
});
});
it('should accept options', function() {
assert.doesNotThrow(function() {
new VarStream({}, 'prop', VarStream.VarStreamReader.OPTIONS);
});
});
it('should fail when no root object is given', function() {
assert.throws(function() {
new VarStream();
}, function(err) {
if(err instanceof Error
&&err.message==='No root object provided.') {
return true;
}
});
});
it('should fail when no property is given', function() {
assert.throws(function() {
new VarStream({});
}, function(err) {
if(err instanceof Error
&&err.message==='No root property name given.') {
return true;
}
});
});
it('should fail when an empty property is given', function() {
assert.throws(function() {
new VarStream({}, '');
}, function(err) {
if(err instanceof Error
&&err.message==='No root property name given.') {
return true;
}
});
});
});
describe('VarStream duplex stream', function() {
it('should work as expected', function(done) {
var root = {};
var stream = new VarStream(root, 'plop');
stream.on('finish', function() {
assert.equal(root.plop.plap, 'plip');
assert.equal(root.plop.plop, 'plup');
done();
});
stream.write('plap=plip\n');
stream.write('plop=plup\n');
stream.end();
});
});
describe('VarStream.stringify()', function() {
it('should fail with no input', function() {
assert.throws(function() {
VarStream.stringify();
}, function(err) {
if(err instanceof Error
&&err.message==='The stringified object must be an instance of Object.') {
return true;
}
});
});
it('should fail with input non-object input', function() {
assert.throws(function() {
VarStream.stringify('aiie caramba');
}, function(err) {
if(err instanceof Error
&&err.message==='The stringified object must be an instance of Object.') {
return true;
}
});
});
});
describe('VarStream.parse()', function() {
it('should work with an empty string', function() {
var obj = VarStream.parse('');
assert.deepEqual(obj, {});
});
});
describe('Helpers decoding/rencoding', function() {
var dir = __dirname+'/fixtures'
, files = fs.readdirSync(dir)
;
it('should work with some null values', function() {
var cnt = VarStream.stringify({
test: undefined,
test2: null
});
assert.deepEqual(
VarStream.stringify(VarStream.parse(VarStream.stringify(VarStream.parse(cnt)))),
VarStream.stringify(VarStream.parse(cnt))
);
});
it('should work with values referring to the root scope', function() {
var obj = {
test2: {}
};
obj.test = obj;
obj.test2.test = obj;
var cnt = VarStream.stringify(obj);
assert.deepEqual(
VarStream.stringify(VarStream.parse(VarStream.stringify(VarStream.parse(cnt)))),
VarStream.stringify(VarStream.parse(cnt))
);
});
it('should work with complexer arrays', function() {
var obj = VarStream.parse(fs.readFileSync(__dirname+'/fixtures/y-complexarray.dat', {encoding: 'utf-8'}));
assert.deepEqual(
VarStream.stringify(VarStream.parse(VarStream.stringify(obj))),
VarStream.stringify(obj)
);
});
it('should work with circular references', function() {
var obj = VarStream.parse(fs.readFileSync(__dirname+'/fixtures/z-circular.dat', {encoding: 'utf-8'}));
assert.deepEqual(
VarStream.stringify(VarStream.parse(VarStream.stringify(obj))),
VarStream.stringify(obj)
);
});
it('should work with some null values in varstream format', function() {
var obj = VarStream.parse('test2=null\ntest3=\n');
assert.deepEqual(
VarStream.stringify(VarStream.parse(VarStream.stringify(obj))),
VarStream.stringify(obj)
);
});
files.forEach(function(file) {
if('3-delete.dat' === file) return;
it('should work with "'+file+'"', function() {
var cnt = VarStream.stringify(VarStream.parse(fs.readFileSync(dir + '/' +file, {encoding: 'utf-8'})));
assert.deepEqual(
VarStream.stringify(VarStream.parse(VarStream.stringify(VarStream.parse(cnt)))),
VarStream.stringify(VarStream.parse(cnt))
);
});
})
});