Skip to content

Commit c3821bf

Browse files
committed
Added tests for Runner invokeInclude
1 parent 8183066 commit c3821bf

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

src/Runtime.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const async = require('async');
44
const fs = require('fs');
55
const RuntimeError = require('./error/Runtime');
6+
const path = require('path');
67

78
const Parser = require('./Parser');
89
const Lexer = require('./Lexer');
@@ -63,9 +64,13 @@ class Runner {
6364
const tasks = {};
6465

6566
for (const value of sources) {
66-
const path = value.type === PARSER_TYPE_VARIABLE ? this.getValueFromVariable(value.name) : value.value;
67+
const templatePath = path.resolve(
68+
value.type === PARSER_TYPE_VARIABLE
69+
? this.getValueFromVariable(value.name)
70+
: value.value
71+
);
6772

68-
tasks[path] = fs.readFile.bind(fs, path);
73+
tasks[templatePath] = fs.readFile.bind(fs, templatePath);
6974
}
7075

7176
async.parallel(tasks, (err, files) => {
@@ -97,13 +102,11 @@ class Runner {
97102
}
98103

99104
invokeInclude(statement) {
100-
const path = statement.value.type === PARSER_TYPE_VARIABLE
105+
const templatePath = statement.value.type === PARSER_TYPE_VARIABLE
101106
? this.getValueFromVariable(statement.value.name)
102107
: statement.value.value;
103108

104-
if (this.astCache[path]) {
105-
this.invokeStatements(this.astCache[path].statements);
106-
}
109+
this.invokeStatements(this.astCache[templatePath].statements);
107110
}
108111

109112
invokeBranch(statement) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const Runtime = require('../../src/Runtime');
5+
const nb = require('../nodeBuilder');
6+
7+
const includeStatement = nb.root(nb.literal('foo'));
8+
9+
describe('Runtime.invokeInclude', function() {
10+
let runtime;
11+
12+
beforeEach(function() {
13+
runtime = new Runtime();
14+
});
15+
16+
it('should include file from static value', function() {
17+
runtime.astCache = { 'foo.tpl': includeStatement };
18+
runtime.invokeInclude(
19+
nb.include(nb.value('foo.tpl'))
20+
);
21+
expect(runtime.result).to.deep.equal([ 'foo' ]);
22+
});
23+
24+
it('should include file from variable', function() {
25+
runtime.astCache = { 'foo.tpl': includeStatement };
26+
runtime.input = { baz: 'foo.tpl' };
27+
runtime.invokeInclude(
28+
nb.include(nb.variable('baz'))
29+
);
30+
expect(runtime.result).to.deep.equal([ 'foo' ]);
31+
});
32+
});

0 commit comments

Comments
 (0)