Debian Codegraph @

spec/api/plugin-api.spec.js

raw symbols SCIP
1 /* global peg */
2
3 "use strict";
4
5 describe("plugin API", function() {
6 beforeEach(function() {
7 this.addMatchers({
8 toBeObject: function() {
9 this.message = function() {
10 return "Expected " + jasmine.pp(this.actual) + " "
11 + (this.isNot ? "not " : "")
12 + "to be an object.";
13 };
14
15 return this.actual !== null && typeof this.actual === "object";
16 },
17
18 toBeArray: function() {
19 this.message = function() {
20 return "Expected " + jasmine.pp(this.actual) + " "
21 + (this.isNot ? "not " : "")
22 + "to be an array.";
23 };
24
25 return Object.prototype.toString.apply(this.actual) === "[object Array]";
26 },
27
28 toBeFunction: function() {
29 this.message = function() {
30 return "Expected " + jasmine.pp(this.actual) + " "
31 + (this.isNot ? "not " : "")
32 + "to be a function.";
33 };
34
35 return typeof this.actual === "function";
36 }
37 });
38 });
39
40 describe("use", function() {
41 var grammar = 'start = "a"';
42
43 it("is called for each plugin", function() {
44 var pluginsUsed = [false, false, false],
45 plugins = [
46 { use: function() { pluginsUsed[0] = true; } },
47 { use: function() { pluginsUsed[1] = true; } },
48 { use: function() { pluginsUsed[2] = true; } }
49 ];
50
51 peg.generate(grammar, { plugins: plugins });
52
53 expect(pluginsUsed).toEqual([true, true, true]);
54 });
55
56 it("receives configuration", function() {
57 var plugin = {
58 use: function(config) {
59 var i;
60
61 expect(config).toBeObject();
62
63 expect(config.parser).toBeObject();
64 expect(config.parser.parse('start = "a"')).toBeObject();
65
66 expect(config.passes).toBeObject();
67
68 expect(config.passes.check).toBeArray();
69 for (i = 0; i < config.passes.check.length; i++) {
70 expect(config.passes.check[i]).toBeFunction();
71 }
72
73 expect(config.passes.transform).toBeArray();
74 for (i = 0; i < config.passes.transform.length; i++) {
75 expect(config.passes.transform[i]).toBeFunction();
76 }
77
78 expect(config.passes.generate).toBeArray();
79 for (i = 0; i < config.passes.generate.length; i++) {
80 expect(config.passes.generate[i]).toBeFunction();
81 }
82 }
83 };
84
85 peg.generate(grammar, { plugins: [plugin] });
86 });
87
88 it("receives options", function() {
89 var plugin = {
90 use: function(config, options) {
91 expect(options).toEqual(generateOptions);
92 }
93 },
94 generateOptions = { plugins: [plugin], foo: 42 };
95
96 peg.generate(grammar, generateOptions);
97 });
98
99 it("can replace parser", function() {
100 var plugin = {
101 use: function(config) {
102 var parser = peg.generate([
103 'start = .* {',
104 ' return {',
105 ' type: "grammar",',
106 ' rules: [',
107 ' {',
108 ' type: "rule",',
109 ' name: "start",',
110 ' expression: { type: "literal", value: text(), ignoreCase: false }',
111 ' }',
112 ' ]',
113 ' };',
114 '}'
115 ].join("\n"));
116
117 config.parser = parser;
118 }
119 },
120 parser = peg.generate('a', { plugins: [plugin] });
121
122 expect(parser.parse("a")).toBe("a");
123 });
124
125 it("can change compiler passes", function() {
126 var plugin = {
127 use: function(config) {
128 var pass = function(ast) {
129 ast.code = '({ parse: function() { return 42; } })';
130 };
131
132 config.passes.generate = [pass];
133 }
134 },
135 parser = peg.generate(grammar, { plugins: [plugin] });
136
137 expect(parser.parse("a")).toBe(42);
138 });
139
140 it("can change options", function() {
141 var grammar = [
142 'a = "x"',
143 'b = "x"',
144 'c = "x"'
145 ].join("\n"),
146 plugin = {
147 use: function(config, options) {
148 options.allowedStartRules = ["b", "c"];
149 }
150 },
151 parser = peg.generate(grammar, {
152 allowedStartRules: ["a"],
153 plugins: [plugin]
154 });
155
156 expect(function() { parser.parse("x", { startRule: "a" }); }).toThrow();
157 expect(parser.parse("x", { startRule: "b" })).toBe("x");
158 expect(parser.parse("x", { startRule: "c" })).toBe("x");
159 });
160 });
161 });