forked from marko-js/marko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen-test.js
58 lines (44 loc) · 1.79 KB
/
codegen-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
require('./util/patch-module');
var chai = require('chai');
chai.config.includeStack = true;
var expect = require('chai').expect;
var path = require('path');
var compiler = require('../compiler');
var builder = compiler.createBuilder();
var autotest = require('./autotest');
var CompileContext = require('../compiler/CompileContext');
var CodeGenerator = require('../compiler/CodeGenerator');
var CodeWriter = require('../compiler/CodeWriter');
function createCodeGenerator(context) {
return new CodeGenerator(context);
}
function createCodeWriter(context) {
return new CodeWriter(context, builder);
}
describe('compiler/codegen', function() {
var autoTestDir = path.join(__dirname, 'autotests/codegen');
autotest.scanDir(autoTestDir, function run(dir, helpers, done) {
var main = require(path.join(dir, 'index.js'));
var generateCodeFunc = main;
var context = new CompileContext('dummy', path.join(dir, 'dummy.marko'), builder);
var codegen = createCodeGenerator(context);
var codeWriter = createCodeWriter(context);
var ast = generateCodeFunc(builder, codegen);
var finalAST = codegen.generateCode(ast);
codeWriter.write(finalAST);
helpers.compare(codeWriter.getCode(), '.js');
done();
});
it('should not allow a return outside a function', function() {
let builder = compiler.createBuilder();
var context = new CompileContext('dummy', path.join(autoTestDir, 'dummy.marko'), builder);
var codegen = createCodeGenerator(context);
var rootNode = builder.program([
builder.returnStatement('foo')
]);
expect(function() {
codegen.generateCode(rootNode);
}).to.throw('"return" not allowed outside a function body');
});
});