1 | var should = require("should");
|
2 |
|
3 | var fs = require("fs");
|
4 | var path = require("path");
|
5 |
|
6 | var runLoader = require("./fakeModuleSystem");
|
7 | var twigLoader = require("../");
|
8 |
|
9 | var fixtures = path.join(__dirname, "fixtures");
|
10 |
|
11 | describe("include", function () {
|
12 | it("should generate correct code", function (done) {
|
13 | var template = path.join(fixtures, "include", "template.html.twig");
|
14 | runLoader(twigLoader, path.join(fixtures, "include"), template, fs.readFileSync(template, "utf-8"), function (err, result) {
|
15 | if (err) throw err;
|
16 |
|
17 | result.should.have.type("string");
|
18 |
|
19 |
|
20 | result.should.match(/require\(\"\.\/a\.html\.twig\"\);/);
|
21 | result.should.match(/require\(\"\.\/b\.html\"\);/);
|
22 | result.should.match(/require\(\"\.\/c\.html\.twig\"\);/);
|
23 | result.should.match(/require\(\"\.\/d\.html\.twig\"\);/);
|
24 | result.should.match(/require\(\"\.\/e\.html\.twig\"\);/);
|
25 | result.should.match(/require\(\"\.\/f\.html\.twig\"\);/);
|
26 | result.should.match(/require\(\"\.\/g\.html\.twig\"\);/);
|
27 |
|
28 | done();
|
29 | });
|
30 | });
|
31 |
|
32 |
|
33 |
|
34 | it("should leave dynamic includes in tact", function (done) {
|
35 | var template = path.join(fixtures, "include", "template.dynamic.html.twig");
|
36 | runLoader(twigLoader, path.join(fixtures, "include"), template, fs.readFileSync(template, "utf-8"), function (err, result) {
|
37 | if (err) throw err;
|
38 |
|
39 | result.should.have.type("string");
|
40 |
|
41 |
|
42 | result.should.not.match(/require\("~"\);/);
|
43 | result.should.not.match(/require\(".\/"\);/);
|
44 | result.should.not.match(/require\("name"\);/);
|
45 | result.should.not.match(/require\("block\.name"\);/);
|
46 |
|
47 |
|
48 |
|
49 | done();
|
50 | });
|
51 | });
|
52 |
|
53 |
|
54 |
|
55 | it("should leave non-existing includes in tact", function (done) {
|
56 | var template = path.join(fixtures, "include", "template.alias.html.twig");
|
57 | runLoader(twigLoader, path.join(fixtures, "include"), template, fs.readFileSync(template, "utf-8"), function (err, result) {
|
58 | if (err) throw err;
|
59 |
|
60 | result.should.have.type("string");
|
61 |
|
62 |
|
63 | result.should.not.match(/require\("foo"\);/);
|
64 |
|
65 |
|
66 |
|
67 | done();
|
68 | });
|
69 | });
|
70 |
|
71 |
|
72 | it("should generate same template id for resource and dependency", function (done) {
|
73 | var template = path.join(fixtures, "include", "template.nested.html.twig");
|
74 | runLoader(twigLoader, path.join(fixtures, "include"), template, fs.readFileSync(template, "utf-8"), function (err, result) {
|
75 | if (err) throw err;
|
76 |
|
77 | result.should.have.type("string");
|
78 |
|
79 | result.should.match(/require\("\.\/nested\.html"\);/);
|
80 |
|
81 |
|
82 | var nestedTemplateId = result.match(/"value":"([^"]+)"/i)[1];
|
83 |
|
84 |
|
85 | var nestedTemplate = path.join(fixtures, "include", "nested.html.twig");
|
86 | runLoader(twigLoader, path.join(fixtures, "include"), nestedTemplate, fs.readFileSync(nestedTemplate, "utf-8"), function (err, result) {
|
87 | if (err) throw err;
|
88 |
|
89 | result.should.have.type("string");
|
90 |
|
91 |
|
92 |
|
93 | var templateId = result.match(/twig\({"id":"([^"]+)"/i)[1];
|
94 |
|
95 | templateId.should.equal(nestedTemplateId);
|
96 |
|
97 | done();
|
98 | });
|
99 | });
|
100 | });
|
101 | });
|