UNPKG

11.7 kBJavaScriptView Raw
1"use strict";
2var __values = (this && this.__values) || function (o) {
3 var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
4 if (m) return m.call(o);
5 return {
6 next: function () {
7 if (o && i >= o.length) o = void 0;
8 return { value: o && o[i++], done: !o };
9 }
10 };
11};
12var __importDefault = (this && this.__importDefault) || function (mod) {
13 return (mod && mod.__esModule) ? mod : { "default": mod };
14};
15Object.defineProperty(exports, "__esModule", { value: true });
16var assert_1 = __importDefault(require("assert"));
17var CucumberExpression_1 = __importDefault(require("../src/CucumberExpression"));
18var ParameterTypeRegistry_1 = __importDefault(require("../src/ParameterTypeRegistry"));
19var ParameterType_1 = __importDefault(require("../src/ParameterType"));
20describe('CucumberExpression', function () {
21 var e_1, _a;
22 it('documents match arguments', function () {
23 var parameterTypeRegistry = new ParameterTypeRegistry_1.default();
24 /// [capture-match-arguments]
25 var expr = 'I have {int} cuke(s)';
26 var expression = new CucumberExpression_1.default(expr, parameterTypeRegistry);
27 var args = expression.match('I have 7 cukes');
28 assert_1.default.strictEqual(7, args[0].getValue(null));
29 /// [capture-match-arguments]
30 });
31 it('matches word', function () {
32 assert_1.default.deepStrictEqual(match('three {word} mice', 'three blind mice'), [
33 'blind',
34 ]);
35 });
36 it('matches double quoted string', function () {
37 assert_1.default.deepStrictEqual(match('three {string} mice', 'three "blind" mice'), [
38 'blind',
39 ]);
40 });
41 it('matches multiple double quoted strings', function () {
42 assert_1.default.deepStrictEqual(match('three {string} and {string} mice', 'three "blind" and "crippled" mice'), ['blind', 'crippled']);
43 });
44 it('matches single quoted string', function () {
45 assert_1.default.deepStrictEqual(match('three {string} mice', "three 'blind' mice"), [
46 'blind',
47 ]);
48 });
49 it('matches multiple single quoted strings', function () {
50 assert_1.default.deepStrictEqual(match('three {string} and {string} mice', "three 'blind' and 'crippled' mice"), ['blind', 'crippled']);
51 });
52 it('does not match misquoted string', function () {
53 assert_1.default.deepStrictEqual(match('three {string} mice', 'three "blind\' mice'), null);
54 });
55 it('matches single quoted string with double quotes', function () {
56 assert_1.default.deepStrictEqual(match('three {string} mice', 'three \'"blind"\' mice'), ['"blind"']);
57 });
58 it('matches double quoted string with single quotes', function () {
59 assert_1.default.deepStrictEqual(match('three {string} mice', 'three "\'blind\'" mice'), ["'blind'"]);
60 });
61 it('matches double quoted string with escaped double quote', function () {
62 assert_1.default.deepStrictEqual(match('three {string} mice', 'three "bl\\"nd" mice'), ['bl"nd']);
63 });
64 it('matches single quoted string with escaped single quote', function () {
65 assert_1.default.deepStrictEqual(match('three {string} mice', "three 'bl\\'nd' mice"), ["bl'nd"]);
66 });
67 it('matches single quoted string with escaped single quote', function () {
68 assert_1.default.deepStrictEqual(match('three {string} mice', "three 'bl\\'nd' mice"), ["bl'nd"]);
69 });
70 it('matches escaped parenthesis', function () {
71 assert_1.default.deepStrictEqual(match('three \\(exceptionally) {string} mice', 'three (exceptionally) "blind" mice'), ['blind']);
72 });
73 it('matches escaped slash', function () {
74 assert_1.default.deepStrictEqual(match('12\\/2020', '12/2020'), []);
75 });
76 it('matches int', function () {
77 assert_1.default.deepStrictEqual(match('{int}', '22'), [22]);
78 });
79 it("doesn't match float as int", function () {
80 assert_1.default.deepStrictEqual(match('{int}', '1.22'), null);
81 });
82 it('matches float', function () {
83 assert_1.default.deepStrictEqual(match('{float}', ""), null);
84 assert_1.default.deepStrictEqual(match('{float}', "."), null);
85 assert_1.default.deepStrictEqual(match('{float}', ","), null);
86 assert_1.default.deepStrictEqual(match('{float}', "-"), null);
87 assert_1.default.deepStrictEqual(match('{float}', "E"), null);
88 assert_1.default.deepStrictEqual(match('{float}', "1,"), null);
89 assert_1.default.deepStrictEqual(match('{float}', ",1"), null);
90 assert_1.default.deepStrictEqual(match('{float}', "1."), null);
91 assert_1.default.deepStrictEqual(match('{float}', "1"), [1]);
92 assert_1.default.deepStrictEqual(match('{float}', "-1"), [-1]);
93 assert_1.default.deepStrictEqual(match('{float}', "1.1"), [1.1]);
94 assert_1.default.deepStrictEqual(match('{float}', "1,000"), null);
95 assert_1.default.deepStrictEqual(match('{float}', "1,000,0"), null);
96 assert_1.default.deepStrictEqual(match('{float}', "1,000.1"), null);
97 assert_1.default.deepStrictEqual(match('{float}', "1,000,10"), null);
98 assert_1.default.deepStrictEqual(match('{float}', "1,0.1"), null);
99 assert_1.default.deepStrictEqual(match('{float}', "1,000,000.1"), null);
100 assert_1.default.deepStrictEqual(match('{float}', "-1.1"), [-1.1]);
101 assert_1.default.deepStrictEqual(match('{float}', ".1"), [0.1]);
102 assert_1.default.deepStrictEqual(match('{float}', "-.1"), [-0.1]);
103 assert_1.default.deepStrictEqual(match('{float}', "-.10000001"), [-0.10000001]);
104 assert_1.default.deepStrictEqual(match('{float}', "1E1"), [1E1]); // precision 1 with scale -1, can not be expressed as a decimal
105 assert_1.default.deepStrictEqual(match('{float}', ".1E1"), [1]);
106 assert_1.default.deepStrictEqual(match('{float}', "E1"), null);
107 assert_1.default.deepStrictEqual(match('{float}', "-.1E-1"), [-0.01]);
108 assert_1.default.deepStrictEqual(match('{float}', "-.1E-2"), [-0.001]);
109 assert_1.default.deepStrictEqual(match('{float}', "-.1E+1"), [-1]);
110 assert_1.default.deepStrictEqual(match('{float}', "-.1E+2"), [-10]);
111 assert_1.default.deepStrictEqual(match('{float}', "-.1E1"), [-1]);
112 assert_1.default.deepStrictEqual(match('{float}', "-.10E2"), [-10]);
113 });
114 it('matches float with zero', function () {
115 assert_1.default.deepEqual(match('{float}', '0'), [0]);
116 });
117 it('matches anonymous', function () {
118 assert_1.default.deepStrictEqual(match('{}', '0.22'), ['0.22']);
119 });
120 it('throws unknown parameter type', function () {
121 try {
122 match('{unknown}', 'something');
123 assert_1.default.fail();
124 }
125 catch (expected) {
126 assert_1.default.strictEqual(expected.message, 'Undefined parameter type {unknown}');
127 }
128 });
129 it('does not allow optional parameter types', function () {
130 try {
131 match('({int})', '3');
132 assert_1.default.fail();
133 }
134 catch (expected) {
135 assert_1.default.strictEqual(expected.message, 'Parameter types cannot be optional: ({int})');
136 }
137 });
138 it('allows escaped optional parameter types', function () {
139 assert_1.default.deepStrictEqual(match('\\({int})', '(3)'), [3]);
140 });
141 it('does not allow text/parameter type alternation', function () {
142 try {
143 match('x/{int}', '3');
144 assert_1.default.fail();
145 }
146 catch (expected) {
147 assert_1.default.strictEqual(expected.message, 'Parameter types cannot be alternative: x/{int}');
148 }
149 });
150 it('does not allow parameter type/text alternation', function () {
151 try {
152 match('{int}/x', '3');
153 assert_1.default.fail();
154 }
155 catch (expected) {
156 assert_1.default.strictEqual(expected.message, 'Parameter types cannot be alternative: {int}/x');
157 }
158 });
159 var _loop_1 = function (c) {
160 it("does not allow parameter type with " + c, function () {
161 try {
162 match("{" + c + "string}", 'something');
163 assert_1.default.fail();
164 }
165 catch (expected) {
166 assert_1.default.strictEqual(expected.message, "Illegal character '" + c + "' in parameter name {" + c + "string}");
167 }
168 });
169 };
170 try {
171 for (var _b = __values('[]()$.|?*+'.split('')), _c = _b.next(); !_c.done; _c = _b.next()) {
172 var c = _c.value;
173 _loop_1(c);
174 }
175 }
176 catch (e_1_1) { e_1 = { error: e_1_1 }; }
177 finally {
178 try {
179 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
180 }
181 finally { if (e_1) throw e_1.error; }
182 }
183 it('exposes source', function () {
184 var expr = 'I have {int} cuke(s)';
185 assert_1.default.strictEqual(new CucumberExpression_1.default(expr, new ParameterTypeRegistry_1.default()).source, expr);
186 });
187 // JavaScript-specific
188 it('delegates transform to custom object', function () {
189 var parameterTypeRegistry = new ParameterTypeRegistry_1.default();
190 parameterTypeRegistry.defineParameterType(new ParameterType_1.default('widget', /\w+/, null, function (s) {
191 return this.createWidget(s);
192 }, false, true));
193 var expression = new CucumberExpression_1.default('I have a {widget}', parameterTypeRegistry);
194 var world = {
195 createWidget: function (s) {
196 return "widget:" + s;
197 },
198 };
199 var args = expression.match("I have a bolt");
200 assert_1.default.strictEqual(args[0].getValue(world), 'widget:bolt');
201 });
202 describe('escapes special characters', function () {
203 ;
204 ['\\', '[', ']', '^', '$', '.', '|', '?', '*', '+'].forEach(function (character) {
205 it("escapes " + character, function () {
206 var expr = "I have {int} cuke(s) and " + character;
207 var expression = new CucumberExpression_1.default(expr, new ParameterTypeRegistry_1.default());
208 var arg1 = expression.match("I have 800 cukes and " + character)[0];
209 assert_1.default.strictEqual(arg1.getValue(null), 800);
210 });
211 });
212 it("escapes .", function () {
213 var expr = "I have {int} cuke(s) and .";
214 var expression = new CucumberExpression_1.default(expr, new ParameterTypeRegistry_1.default());
215 assert_1.default.strictEqual(expression.match("I have 800 cukes and 3"), null);
216 var arg1 = expression.match("I have 800 cukes and .")[0];
217 assert_1.default.strictEqual(arg1.getValue(null), 800);
218 });
219 it("escapes |", function () {
220 var expr = "I have {int} cuke(s) and a|b";
221 var expression = new CucumberExpression_1.default(expr, new ParameterTypeRegistry_1.default());
222 assert_1.default.strictEqual(expression.match("I have 800 cukes and a"), null);
223 assert_1.default.strictEqual(expression.match("I have 800 cukes and b"), null);
224 var arg1 = expression.match("I have 800 cukes and a|b")[0];
225 assert_1.default.strictEqual(arg1.getValue(null), 800);
226 });
227 });
228});
229var match = function (expression, text) {
230 var cucumberExpression = new CucumberExpression_1.default(expression, new ParameterTypeRegistry_1.default());
231 var args = cucumberExpression.match(text);
232 if (!args) {
233 return null;
234 }
235 return args.map(function (arg) { return arg.getValue(null); });
236};
237//# sourceMappingURL=CucumberExpressionTest.js.map
\No newline at end of file