1 | 'use strict';
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
11 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
12 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
13 | function verb(n) { return function (v) { return step([n, v]); }; }
|
14 | function step(op) {
|
15 | if (f) throw new TypeError("Generator is already executing.");
|
16 | while (_) try {
|
17 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
18 | if (y = 0, t) op = [op[0] & 2, t.value];
|
19 | switch (op[0]) {
|
20 | case 0: case 1: t = op; break;
|
21 | case 4: _.label++; return { value: op[1], done: false };
|
22 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
23 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
24 | default:
|
25 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
26 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
27 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
28 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
29 | if (t[2]) _.ops.pop();
|
30 | _.trys.pop(); continue;
|
31 | }
|
32 | op = body.call(thisArg, _);
|
33 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
34 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
35 | }
|
36 | };
|
37 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
38 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
39 | };
|
40 | var _this = this;
|
41 | Object.defineProperty(exports, "__esModule", { value: true });
|
42 | var assert_1 = __importDefault(require("assert"));
|
43 | var assert_throws_1 = __importDefault(require("./assert_throws"));
|
44 | var CucumberExpression_1 = __importDefault(require("../src/CucumberExpression"));
|
45 | var RegularExpression_1 = __importDefault(require("../src/RegularExpression"));
|
46 | var ParameterTypeRegistry_1 = __importDefault(require("../src/ParameterTypeRegistry"));
|
47 | var ParameterType_1 = __importDefault(require("../src/ParameterType"));
|
48 | var Color = (function () {
|
49 |
|
50 | function Color(name) {
|
51 | this.name = name;
|
52 | }
|
53 | return Color;
|
54 | }());
|
55 | var CssColor = (function () {
|
56 | function CssColor(name) {
|
57 | this.name = name;
|
58 | }
|
59 | return CssColor;
|
60 | }());
|
61 | describe('Custom parameter type', function () {
|
62 | var parameterTypeRegistry;
|
63 | beforeEach(function () {
|
64 | parameterTypeRegistry = new ParameterTypeRegistry_1.default();
|
65 |
|
66 |
|
67 | parameterTypeRegistry.defineParameterType(new ParameterType_1.default('color',
|
68 | /red|blue|yellow/,
|
69 | Color,
|
70 | function (// type
|
71 | s) { return new Color(s); },
|
72 | false,
|
73 | true
|
74 | ));
|
75 |
|
76 |
|
77 | });
|
78 | describe('CucumberExpression', function () {
|
79 | it('throws exception for illegal character in parameter name', function () {
|
80 | assert_throws_1.default(function () { return new ParameterType_1.default('[string]', /.*/, String, function (s) { return s; }, false, true); }, "Illegal character '[' in parameter name {[string]}");
|
81 | });
|
82 | it('matches parameters with custom parameter type', function () {
|
83 | var expression = new CucumberExpression_1.default('I have a {color} ball', parameterTypeRegistry);
|
84 | var value = expression.match('I have a red ball')[0].getValue(null);
|
85 | assert_1.default.strictEqual(value.name, 'red');
|
86 | });
|
87 | it('matches parameters with multiple capture groups', function () {
|
88 | var Coordinate = (function () {
|
89 | function Coordinate(x, y, z) {
|
90 | this.x = x;
|
91 | this.y = y;
|
92 | this.z = z;
|
93 | }
|
94 | return Coordinate;
|
95 | }());
|
96 | parameterTypeRegistry.defineParameterType(new ParameterType_1.default('coordinate', /(\d+),\s*(\d+),\s*(\d+)/, Coordinate, function (x, y, z) {
|
97 | return new Coordinate(Number(x), Number(y), Number(z));
|
98 | }, true, true));
|
99 | var expression = new CucumberExpression_1.default('A {int} thick line from {coordinate} to {coordinate}', parameterTypeRegistry);
|
100 | var args = expression.match('A 5 thick line from 10,20,30 to 40,50,60');
|
101 | var thick = args[0].getValue(null);
|
102 | assert_1.default.strictEqual(thick, 5);
|
103 | var from = args[1].getValue(null);
|
104 | assert_1.default.strictEqual(from.x, 10);
|
105 | assert_1.default.strictEqual(from.y, 20);
|
106 | assert_1.default.strictEqual(from.z, 30);
|
107 | var to = args[2].getValue(null);
|
108 | assert_1.default.strictEqual(to.x, 40);
|
109 | assert_1.default.strictEqual(to.y, 50);
|
110 | assert_1.default.strictEqual(to.z, 60);
|
111 | });
|
112 | it('matches parameters with custom parameter type using optional capture group', function () {
|
113 | parameterTypeRegistry = new ParameterTypeRegistry_1.default();
|
114 | parameterTypeRegistry.defineParameterType(new ParameterType_1.default('color', [/red|blue|yellow/, /(?:dark|light) (?:red|blue|yellow)/], Color, function (s) { return new Color(s); }, false, true));
|
115 | var expression = new CucumberExpression_1.default('I have a {color} ball', parameterTypeRegistry);
|
116 | var value = expression.match('I have a dark red ball')[0].getValue(null);
|
117 | assert_1.default.strictEqual(value.name, 'dark red');
|
118 | });
|
119 | it('defers transformation until queried from argument', function () {
|
120 | parameterTypeRegistry.defineParameterType(new ParameterType_1.default('throwing', /bad/, null, function (s) {
|
121 | throw new Error("Can't transform [" + s + "]");
|
122 | }, false, true));
|
123 | var expression = new CucumberExpression_1.default('I have a {throwing} parameter', parameterTypeRegistry);
|
124 | var args = expression.match('I have a bad parameter');
|
125 | assert_throws_1.default(function () { return args[0].getValue(null); }, "Can't transform [bad]");
|
126 | });
|
127 | describe('conflicting parameter type', function () {
|
128 | it('is detected for type name', function () {
|
129 | assert_throws_1.default(function () {
|
130 | return parameterTypeRegistry.defineParameterType(new ParameterType_1.default('color', /.*/, CssColor, function (s) { return new CssColor(s); }, false, true));
|
131 | }, 'There is already a parameter type with name color');
|
132 | });
|
133 | it('is not detected for type', function () {
|
134 | parameterTypeRegistry.defineParameterType(new ParameterType_1.default('whatever', /.*/, Color, function (s) { return new Color(s); }, false, false));
|
135 | });
|
136 | it('is not detected for regexp', function () {
|
137 | parameterTypeRegistry.defineParameterType(new ParameterType_1.default('css-color', /red|blue|yellow/, CssColor, function (s) { return new CssColor(s); }, true, false));
|
138 | assert_1.default.strictEqual(new CucumberExpression_1.default('I have a {css-color} ball', parameterTypeRegistry)
|
139 | .match('I have a blue ball')[0]
|
140 | .getValue(null).constructor, CssColor);
|
141 | assert_1.default.strictEqual(new CucumberExpression_1.default('I have a {css-color} ball', parameterTypeRegistry)
|
142 | .match('I have a blue ball')[0]
|
143 | .getValue(null).name, 'blue');
|
144 | assert_1.default.strictEqual(new CucumberExpression_1.default('I have a {color} ball', parameterTypeRegistry)
|
145 | .match('I have a blue ball')[0]
|
146 | .getValue(null).constructor, Color);
|
147 | assert_1.default.strictEqual(new CucumberExpression_1.default('I have a {color} ball', parameterTypeRegistry)
|
148 | .match('I have a blue ball')[0]
|
149 | .getValue(null).name, 'blue');
|
150 | });
|
151 | });
|
152 |
|
153 | it('creates arguments using async transform', function () { return __awaiter(_this, void 0, void 0, function () {
|
154 | var expression, args, value;
|
155 | var _this = this;
|
156 | return __generator(this, function (_a) {
|
157 | switch (_a.label) {
|
158 | case 0:
|
159 | parameterTypeRegistry = new ParameterTypeRegistry_1.default();
|
160 |
|
161 | parameterTypeRegistry.defineParameterType(new ParameterType_1.default('asyncColor', /red|blue|yellow/, Color, function (s) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
162 | return [2 , new Color(s)];
|
163 | }); }); }, false, true));
|
164 | expression = new CucumberExpression_1.default('I have a {asyncColor} ball', parameterTypeRegistry);
|
165 | return [4 , expression.match('I have a red ball')];
|
166 | case 1:
|
167 | args = _a.sent();
|
168 | return [4 , args[0].getValue(null)];
|
169 | case 2:
|
170 | value = _a.sent();
|
171 | assert_1.default.strictEqual(value.name, 'red');
|
172 | return [2 ];
|
173 | }
|
174 | });
|
175 | }); });
|
176 | });
|
177 | describe('RegularExpression', function () {
|
178 | it('matches arguments with custom parameter type', function () {
|
179 | var expression = new RegularExpression_1.default(/I have a (red|blue|yellow) ball/, parameterTypeRegistry);
|
180 | var value = expression.match('I have a red ball')[0].getValue(null);
|
181 | assert_1.default.strictEqual(value.constructor, Color);
|
182 | assert_1.default.strictEqual(value.name, 'red');
|
183 | });
|
184 | });
|
185 | });
|
186 |
|
\ | No newline at end of file |