Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/engine/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

const uid = require('../util/uid');
const xmlEscape = require('../util/xml-escape');

class Variable {
/**
Expand Down Expand Up @@ -36,7 +37,7 @@ class Variable {
toXML (isLocal) {
isLocal = (isLocal === true);
return `<variable type="${this.type}" id="${this.id}" islocal="${isLocal
}" iscloud="${this.isCloud}">${this.name}</variable>`;
}" iscloud="${this.isCloud}">${xmlEscape(this.name)}</variable>`;
}

/**
Expand Down
110 changes: 110 additions & 0 deletions test/unit/engine_variable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const test = require('tap').test;
const Variable = require('../../src/engine/variable');
const htmlparser = require('htmlparser2');

test('spec', t => {
t.type(typeof Variable.SCALAR_TYPE, typeof Variable.LIST_TYPE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary to fix now, but this test (and the one below) would probably be better written as the following:

Suggested change
t.type(typeof Variable.SCALAR_TYPE, typeof Variable.LIST_TYPE);
t.type(typeof Variable.SCALAR_TYPE, 'string');

We usually use the 'spec' test to verify the existence and types of properties on the class we are testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this. It must be string not only same type.
I overlooked that the return value is used by scratch-blocks as it is.

t.type(typeof Variable.SCALAR_TYPE, typeof Variable.BROADCAST_MESSAGE_TYPE);

const varId = 'varId';
const varName = 'varName';
const varIsCloud = false;
let v = new Variable(
varId,
varName,
Variable.SCALAR_TYPE,
varIsCloud
);

t.type(Variable, 'function');
t.type(v, 'object');
t.ok(v instanceof Variable);

t.equal(v.id, varId);
t.equal(v.name, varName);
t.equal(v.type, Variable.SCALAR_TYPE);
t.type(v.value, 'number');
t.equal(v.isCloud, varIsCloud);

t.type(v.toXML, 'function');

v = new Variable(
varId,
varName,
Variable.LIST_TYPE,
varIsCloud
);
t.ok(Array.isArray(v.value));

v = new Variable(
varId,
varName,
Variable.BROADCAST_MESSAGE_TYPE,
varIsCloud
);
t.equal(v.value, 'varName');

t.end();
});

test('toXML', t => {
const varId = 'varId';
const varName = 'varName';
const varIsCloud = false;
const varIsLocal = false;
const v = new Variable(
varId,
varName,
Variable.SCALAR_TYPE,
varIsCloud
);

const parser = new htmlparser.Parser({
onopentag: function (name, attribs){
if (name === 'variable'){
t.equal(attribs.type, Variable.SCALAR_TYPE);
t.equal(attribs.id, varId);
t.equal(attribs.iscloud, varIsCloud.toString());
t.equal(attribs.islocal, varIsLocal.toString());
}
},
ontext: function (text){
t.equal(text, varName);
}
}, {decodeEntities: false});
parser.write(v.toXML(false));
parser.end();

t.end();
});

test('escape variable name for XML', t => {
const varId = 'varId';
const varName = '<>&\'"';
const varIsCloud = false;
const varIsLocal = false;
const v = new Variable(
varId,
varName,
Variable.SCALAR_TYPE,
varIsCloud
);

const parser = new htmlparser.Parser({
onopentag: function (name, attribs){
if (name === 'variable'){
t.equal(attribs.type, Variable.SCALAR_TYPE);
t.equal(attribs.id, varId);
t.equal(attribs.iscloud, varIsCloud.toString());
t.equal(attribs.islocal, varIsLocal.toString());
}
},
ontext: function (text){
t.equal(text, '&lt;&gt;&amp;&apos;&quot;');
}
}, {decodeEntities: false});
parser.write(v.toXML(false));
parser.end();

t.end();
});