Skip to content

Commit

Permalink
Merge pull request #72 from jc-white/null-literal
Browse files Browse the repository at this point in the history
Add support for null literal assignments
  • Loading branch information
blurymind authored Apr 14, 2021
2 parents eb50845 + db6a0bf commit 1d7d29d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/parser/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module.exports = {
this.selectable = true;
}
},

LinkNode: class extends Link {
constructor(text, identifier, lineNo) {
super();
Expand Down Expand Up @@ -157,6 +157,14 @@ module.exports = {
}
},

NullLiteralNode: class extends Literal {
constructor(nullLiteral) {
super();
this.type = 'NullLiteralNode';
this.nullLiteral = nullLiteral;
}
},

VariableNode: class extends Literal {
constructor(variableName) {
super();
Expand Down
4 changes: 3 additions & 1 deletion src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class Runner {
* @param {any[]} selections
*/
* handleSelections(selections) {
if (selections.length > 0 || selections[0] instanceof nodeTypes.Shortcut) {
if (selections.length > 0 || selections[0] instanceof nodeTypes.Shortcut) {
// Multiple options to choose from (or just a single shortcut)
// Filter out any conditional dialog options that result to false
const filteredSelections = selections.filter((s) => {
Expand Down Expand Up @@ -304,6 +304,8 @@ class Runner {
return node.stringLiteral;
} else if (node.type === 'BooleanLiteralNode') {
return node.booleanLiteral === 'true';
} else if (node.type === 'NullLiteralNode') {
return null;
} else if (node.type === 'VariableNode') {
return this.variables.get(node.variableName);
} else if (node.type === 'FunctionResultNode') {
Expand Down
34 changes: 34 additions & 0 deletions tests/test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,40 @@ describe('Dialogue', () => {
expect(run.next().done).to.be.true;
});

it('Can evaluate a null assignment', () => {
runner.load(assignmentYarnData);
const run = runner.run('Null');

let value = run.next().value;
expect(value).to.deep.equal(new bondage.TextResult('Test Line', value.data, value.lineNum));

expect(runner.variables.get('testvar')).to.be.undefined;

value = run.next().value;
expect(value).to.deep.equal(new bondage.TextResult('Test Line After', value.data, value.lineNum));

expect(runner.variables.get('testvar')).to.equal(null);

expect(run.next().done).to.be.true;
});

it('Can evaluate a null assignment with expression', () => {
runner.load(assignmentYarnData);
const run = runner.run('NullExpression');

let value = run.next().value;
expect(value).to.deep.equal(new bondage.TextResult('Test Line', value.data, value.lineNum));

expect(runner.variables.get('testvar')).to.be.undefined;

value = run.next().value;
expect(value).to.deep.equal(new bondage.TextResult('Test Line After', value.data, value.lineNum));

expect(runner.variables.get('testvar')).to.equal(null);

expect(run.next().done).to.be.true;
});

it('Can evaluate an assignment from one variable to another', () => {
runner.load(assignmentYarnData);
const run = runner.run('Variable');
Expand Down
20 changes: 20 additions & 0 deletions tests/yarn_files/assignment.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@
},
"colorID": 0
},
{
"title": "Null",
"tags": "Tag",
"body": "Test Line\n<<set $testvar = null>>\nTest Line After",
"position": {
"x": 449,
"y": 252
},
"colorID": 0
},
{
"title": "NullExpression",
"tags": "Tag",
"body": "Test Line\n<<set $testvar = (false || null) && (!true || null)>>\nTest Line After",
"position": {
"x": 449,
"y": 252
},
"colorID": 0
},
{
"title": "Variable",
"tags": "Tag",
Expand Down

0 comments on commit 1d7d29d

Please sign in to comment.