Best JavaScript code snippet using cucumber-gherkin
angular-message-format.js
Source:angular-message-format.js
...940 $exceptionHandler($interpolateMinErr['interr'](text, err));941 }942 };943 }944 function interpolate(text, mustHaveExpression, trustedContext, allOrNothing) {945 var stringifier = getStringifier(trustedContext, allOrNothing, text);946 var parser = new MessageFormatParser(text, 0, $parse, $locale['pluralCat'], stringifier,947 mustHaveExpression, trustedContext, allOrNothing);948 parser.run(parser.ruleInterpolate);949 return parser.parsedFn;950 }951 return {952 'interpolate': interpolate953 };954}];955var $$interpolateDecorator = ['$$messageFormat', '$delegate', function $$interpolateDecorator($$messageFormat, $interpolate) {956 if ($interpolate['startSymbol']() !== '{{' || $interpolate['endSymbol']() !== '}}') {957 throw $interpolateMinErr('nochgmustache', 'angular-message-format.js currently does not allow you to use custom start and end symbols for interpolation.');958 }959 var interpolate = $$messageFormat['interpolate'];960 interpolate['startSymbol'] = $interpolate['startSymbol'];961 interpolate['endSymbol'] = $interpolate['endSymbol'];962 return interpolate;963}];964var $interpolateMinErr;965var isFunction;966var noop;967var toJson;968var $$stringify;969var ngModule = window['angular']['module']('ngMessageFormat', ['ng']);970ngModule['info']({ 'angularVersion': '1.8.0' });971ngModule['factory']('$$messageFormat', $$MessageFormatFactory);972ngModule['config'](['$provide', function($provide) {973 $interpolateMinErr = window['angular']['$interpolateMinErr'];974 isFunction = window['angular']['isFunction'];975 noop = window['angular']['noop'];976 toJson = window['angular']['toJson'];977 $$stringify = window['angular']['$$stringify'];978 $provide['decorator']('$interpolate', $$interpolateDecorator);979}]);980/* TODO: Add tests for:981 ⢠Whitespace preservation in messages.982 ⢠Whitespace ignored around syntax except for offset:N.983 ⢠Escaping for curlies and the # symbol.984 ⢠# symbol value.985 ⢠# symbol value when gender is nested inside plural.986 ⢠Error with nested # symbol.987 ⢠parser error messages.988 ⢠caching.989 ⢠watched expressions.990 ⢠test parsing AngularJS expressions991 ⢠test the different regexes992 ⢠test the different starting rules993*/994describe('$$ngMessageFormat', function() {995 describe('core', function() {996 var $$messageFormat, $parse, $interpolate, $locale, $rootScope;997 function Person(name, gender) {998 this.name = name;999 this.gender = gender;1000 }1001 var alice = new Person('Alice', 'female'),1002 bob = new Person('Bob', 'male'),1003 charlie = new Person('Charlie', 'male'),1004 harry = new Person('Harry Potter', 'male');1005 function initScope($scope) {1006 $scope.recipients = [alice, bob, charlie];1007 $scope.sender = harry;1008 }1009 beforeEach(module('ngMessageFormat'));1010 beforeEach(function() {1011 inject(['$$messageFormat', '$parse', '$locale', '$interpolate', '$rootScope', function(1012 messageFormat, parse, locale, interpolate, rootScope) {1013 $$messageFormat = messageFormat;1014 $parse = parse;1015 $interpolate = interpolate;1016 $locale = locale;1017 $rootScope = rootScope;1018 initScope(rootScope);1019 }]);1020 });1021 describe('mustache', function() {1022 function assertMustache(text, expected) {1023 var parsedFn = $interpolate(text);1024 expect(parsedFn($rootScope)).toEqual(expected);1025 }1026 it('should suppress falsy objects', function() {1027 assertMustache('{{undefined}}', '');1028 assertMustache('{{null}}', '');1029 assertMustache('{{a.b}}', '');1030 });1031 it('should jsonify objects', function() {1032 assertMustache('{{ {} }}', '{}');1033 assertMustache('{{ true }}', 'true');1034 assertMustache('{{ false }}', 'false');1035 assertMustache('{{ 1 }}', '1');1036 assertMustache('{{ \'1\' }}', '1');1037 assertMustache('{{ sender }}', '{"name":"Harry Potter","gender":"male"}');1038 });1039 it('should return function that can be called with no context', inject(function($interpolate) {1040 expect($interpolate('{{sender.name}}')()).toEqual('');1041 }));1042 describe('watchable', function() {1043 it('ckck', function() {1044 var calls = [];1045 $rootScope.$watch($interpolate('{{::name}}'), function(val) {1046 calls.push(val);1047 });1048 $rootScope.$apply();1049 expect(calls.length).toBe(1);1050 $rootScope.name = 'foo';1051 $rootScope.$apply();1052 expect(calls.length).toBe(2);1053 expect(calls[1]).toBe('foo');1054 $rootScope.name = 'bar';1055 $rootScope.$apply();1056 expect(calls.length).toBe(2);1057 });1058 it('should stop watching strings with no expressions after first execution', function() {1059 var spy = jasmine.createSpy();1060 $rootScope.$watch($$messageFormat.interpolate('foo'), spy);1061 $rootScope.$digest();1062 expect($rootScope.$countWatchers()).toBe(0);1063 expect(spy).toHaveBeenCalledWith('foo', 'foo', $rootScope);1064 expect(spy).toHaveBeenCalledTimes(1);1065 });1066 it('should stop watching strings with only constant expressions after first execution', function() {1067 var spy = jasmine.createSpy();1068 $rootScope.$watch($$messageFormat.interpolate('foo {{42}}'), spy);1069 $rootScope.$digest();1070 expect($rootScope.$countWatchers()).toBe(0);1071 expect(spy).toHaveBeenCalledWith('foo 42', 'foo 42', $rootScope);1072 expect(spy).toHaveBeenCalledTimes(1);1073 });1074 });1075 describe('plural', function() {1076 it('no interpolation', function() {1077 var text = '' +1078 '{{recipients.length, plural,\n' +1079 ' =0 {You gave no gifts}\n' +1080 ' =1 {You gave one person a gift}\n' +1081 // "=1" should override "one" for exact value.1082 ' one {YOU SHOULD NEVER SEE THIS MESSAGE}\n' +1083 ' other {You gave some people gifts}\n' +1084 '}}';1085 var parsedFn = $interpolate(text, /*mustHaveExpression=*/true);1086 expect(parsedFn.expressions.length).toBe(1);1087 expect(parsedFn.expressions[0]).toEqual('recipients.length');1088 $rootScope.recipients.length = 2;1089 expect(parsedFn($rootScope)).toEqual('You gave some people gifts');1090 $rootScope.recipients.length = 1;1091 expect(parsedFn($rootScope)).toEqual('You gave one person a gift');1092 $rootScope.recipients.length = 0;1093 expect(parsedFn($rootScope)).toEqual('You gave no gifts');1094 });1095 it('with interpolation', function() {1096 var text = '' +1097 '{{recipients.length, plural,\n' +1098 ' =0 {{{sender.name}} gave no gifts}\n' +1099 ' =1 {{{sender.name}} gave one gift to {{recipients[0].name}}}\n' +1100 // "=1" should override "one" for exact value.1101 ' one {YOU SHOULD NEVER SEE THIS MESSAGE}\n' +1102 ' other {{{sender.name}} gave them a gift}\n' +1103 '}}';1104 var parsedFn = $interpolate(text, /*mustHaveExpression=*/true);1105 expect(parsedFn.expressions.length).toBe(1);1106 expect(parsedFn.expressions[0]).toEqual('recipients.length');1107 $rootScope.recipients.length = 2;1108 expect(parsedFn($rootScope)).toEqual('Harry Potter gave them a gift');1109 $rootScope.recipients.length = 1;1110 expect(parsedFn($rootScope)).toEqual('Harry Potter gave one gift to Alice');1111 $rootScope.recipients.length = 0;1112 expect(parsedFn($rootScope)).toEqual('Harry Potter gave no gifts');1113 });1114 it('with offset, interpolation, "#" symbol with and without escaping', function() {1115 var text = '' +1116 '{{recipients.length, plural, offset:1\n' +1117 // NOTE: It's nonsensical to use "#" for "=0" with a positive offset.1118 ' =0 {{{sender.name}} gave no gifts (\\#=#)}\n' +1119 ' =1 {{{sender.name}} gave one gift to {{recipients[0].name}} (\\#=#)}\n' +1120 ' one {{{sender.name}} gave {{recipients[0].name}} and one other person a gift (\\#=#)}\n' +1121 ' other {{{sender.name}} gave {{recipients[0].name}} and # other people a gift (\\#=#)}\n' +1122 '}}';1123 var parsedFn = $interpolate(text, /*mustHaveExpression=*/true);1124 expect(parsedFn.expressions.length).toBe(1);1125 expect(parsedFn.expressions[0]).toEqual('recipients.length');1126 $rootScope.recipients.length = 3;1127 // "#" should get replaced with the value of "recipients.length - offset"1128 expect(parsedFn($rootScope)).toEqual('Harry Potter gave Alice and 2 other people a gift (#=2)');1129 $rootScope.recipients.length = 2;1130 expect(parsedFn($rootScope)).toEqual('Harry Potter gave Alice and one other person a gift (#=1)');1131 $rootScope.recipients.length = 1;1132 expect(parsedFn($rootScope)).toEqual('Harry Potter gave one gift to Alice (#=0)');1133 $rootScope.recipients.length = 0;1134 expect(parsedFn($rootScope)).toEqual('Harry Potter gave no gifts (#=-1)');1135 });1136 });1137 it('nested plural and select', function() {1138 var text = '' +1139 '{{recipients.length, plural,\n' +1140 ' =0 {You gave no gifts}\n' +1141 ' =1 {{{recipients[0].gender, select,\n' +1142 ' male {You gave him a gift. -{{sender.name}}}\n' +1143 ' female {You gave her a gift. -{{sender.name}}}\n' +1144 ' other {You gave them a gift. -{{sender.name}}}\n' +1145 ' }}\n' +1146 ' }\n' +1147 ' other {You gave {{recipients.length}} people gifts. -{{sender.name}}}\n' +1148 '}}';1149 var parsedFn = $interpolate(text, /*mustHaveExpression=*/true);1150 expect(parsedFn.expressions.length).toBe(1);1151 expect(parsedFn.expressions[0]).toEqual('recipients.length');1152 var result = parsedFn($rootScope);1153 expect(result).toEqual('You gave 3 people gifts. -Harry Potter');1154 });1155 });1156 describe('interpolate', function() {1157 function assertInterpolation(text, expected) {1158 var parsedFn = $$messageFormat.interpolate(text);1159 expect(parsedFn($rootScope)).toEqual(expected);1160 }1161 it('should interpolate a plain string', function() {1162 assertInterpolation(' Hello, world! ', ' Hello, world! ');1163 });1164 it('should interpolate a simple expression', function() {1165 assertInterpolation('Hello, {{sender.name}}!', 'Hello, Harry Potter!');1166 });1167 });1168 });1169 /* NOTE: This describe block includes a copy of interpolateSpec.js to test that1170 * $$messageFormat.interpolate behaves the same as $interpolate.1171 * ONLY the following changes have been made.1172 * - Add beforeEach(module('ngMessageFormat')) at top level of describe()1173 * - Add extra "}" for it('should not unescape markers within expressions'). Original1174 * $interpolate has a bug/feature where a "}}" inside a string is also treated as a1175 * closing symbol. The new service understands the string context and fixes this.1176 * - All tests for startSymbol/endSymbol have been commented out. The new service does not1177 * allow you to change them as of now.1178 * - Instead, I've added tests to assert that we throw an exception if used with redefined1179 * startSymbol/endSymbol. These tests are listed right in the beginning before the1180 * others. allow you to change them as of now.1181 */1182 describe('$interpolate', function() {1183 beforeEach(module('ngMessageFormat'));1184 describe('startSymbol', function() {1185 it('should expose the startSymbol in run phase', inject(function($interpolate) {1186 expect($interpolate.startSymbol()).toBe('{{');1187 }));1188 describe('redefinition', function() {1189 beforeEach(module(function($interpolateProvider) {1190 expect($interpolateProvider.startSymbol()).toBe('{{');1191 $interpolateProvider.startSymbol('((');1192 }));1193 it('should not work when the startSymbol is redefined', function() {1194 expect(function() {1195 inject(inject(function($interpolate) {}));1196 }).toThrowMinErr('$interpolate', 'nochgmustache');1197 });1198 });1199 });1200 describe('endSymbol', function() {1201 it('should expose the endSymbol in run phase', inject(function($interpolate) {1202 expect($interpolate.endSymbol()).toBe('}}');1203 }));1204 describe('redefinition', function() {1205 beforeEach(module(function($interpolateProvider) {1206 expect($interpolateProvider.endSymbol()).toBe('}}');1207 $interpolateProvider.endSymbol('))');1208 }));1209 it('should not work when the endSymbol is redefined', function() {1210 expect(function() {1211 inject(inject(function($interpolate) {}));1212 }).toThrowMinErr('$interpolate', 'nochgmustache');1213 });1214 });1215 });1216 it('should return the interpolation object when there are no bindings and textOnly is undefined',1217 inject(function($interpolate) {1218 var interpolateFn = $interpolate('some text');1219 expect(interpolateFn.exp).toBe('some text');1220 expect(interpolateFn.expressions).toEqual([]);1221 expect(interpolateFn({})).toBe('some text');1222 }));1223 it('should return undefined when there are no bindings and textOnly is set to true',1224 inject(function($interpolate) {1225 expect($interpolate('some text', true)).toBeUndefined();1226 }));1227 it('should return undefined when there are bindings and strict is set to true',1228 inject(function($interpolate) {1229 expect($interpolate('test {{foo}}', false, null, true)({})).toBeUndefined();1230 }));1231 it('should suppress falsy objects', inject(function($interpolate) {1232 expect($interpolate('{{undefined}}')({})).toEqual('');1233 expect($interpolate('{{null}}')({})).toEqual('');1234 expect($interpolate('{{a.b}}')({})).toEqual('');1235 }));1236 it('should jsonify objects', inject(function($interpolate) {1237 expect($interpolate('{{ {} }}')({})).toEqual('{}');1238 expect($interpolate('{{ true }}')({})).toEqual('true');1239 expect($interpolate('{{ false }}')({})).toEqual('false');1240 }));1241 it('should use custom toString when present', inject(function($interpolate, $rootScope) {1242 var context = {1243 a: {1244 toString: function() {1245 return 'foo';1246 }1247 }1248 };1249 expect($interpolate('{{ a }}')(context)).toEqual('foo');1250 }));1251 it('should NOT use toString on array objects', inject(function($interpolate) {1252 expect($interpolate('{{a}}')({ a: [] })).toEqual('[]');1253 }));1254 it('should NOT use toString on Date objects', inject(function($interpolate) {1255 var date = new Date(2014, 10, 10);1256 expect($interpolate('{{a}}')({ a: date })).toBe(JSON.stringify(date));1257 expect($interpolate('{{a}}')({ a: date })).not.toEqual(date.toString());1258 }));1259 it('should return interpolation function', inject(function($interpolate, $rootScope) {1260 var interpolateFn = $interpolate('Hello {{name}}!');1261 expect(interpolateFn.exp).toBe('Hello {{name}}!');1262 expect(interpolateFn.expressions).toEqual(['name']);1263 var scope = $rootScope.$new();1264 scope.name = 'Bubu';1265 expect(interpolateFn(scope)).toBe('Hello Bubu!');1266 }));1267 it('should ignore undefined model', inject(function($interpolate) {1268 expect($interpolate('Hello {{\'World\'}}{{foo}}')({})).toBe('Hello World');1269 }));1270 it('should interpolate with undefined context', inject(function($interpolate) {1271 expect($interpolate('Hello, world!{{bloop}}')()).toBe('Hello, world!');1272 }));1273 describe('watching', function() {1274 it('should be watchable with any input types', inject(function($interpolate, $rootScope) {1275 var lastVal;1276 $rootScope.$watch($interpolate('{{i}}'), function(val) {1277 lastVal = val;1278 });1279 $rootScope.$apply();1280 expect(lastVal).toBe('');1281 $rootScope.i = null;1282 $rootScope.$apply();1283 expect(lastVal).toBe('');1284 $rootScope.i = '';1285 $rootScope.$apply();1286 expect(lastVal).toBe('');1287 $rootScope.i = 0;1288 $rootScope.$apply();1289 expect(lastVal).toBe('0');1290 $rootScope.i = [0];1291 $rootScope.$apply();1292 expect(lastVal).toBe('[0]');1293 $rootScope.i = {a: 1, b: 2};1294 $rootScope.$apply();1295 expect(lastVal).toBe('{"a":1,"b":2}');1296 }));1297 it('should be watchable with literal values', inject(function($interpolate, $rootScope) {1298 var lastVal;1299 $rootScope.$watch($interpolate('{{1}}{{"2"}}{{true}}{{[false]}}{{ {a: 2} }}'), function(val) {1300 lastVal = val;1301 });1302 $rootScope.$apply();1303 expect(lastVal).toBe('12true[false]{"a":2}');1304 expect($rootScope.$countWatchers()).toBe(0);1305 }));1306 it('should respect one-time bindings for each individual expression', inject(function($interpolate, $rootScope) {1307 var calls = [];1308 $rootScope.$watch($interpolate('{{::a | limitTo:1}} {{::s}} {{::i | number}}'), function(val) {1309 calls.push(val);1310 });1311 $rootScope.$apply();1312 expect(calls.length).toBe(1);1313 $rootScope.a = [1];1314 $rootScope.$apply();1315 expect(calls.length).toBe(2);1316 expect(calls[1]).toBe('[1] ');1317 $rootScope.a = [0];1318 $rootScope.$apply();1319 expect(calls.length).toBe(2);1320 $rootScope.i = $rootScope.a = 123;1321 $rootScope.s = 'str!';1322 $rootScope.$apply();1323 expect(calls.length).toBe(3);1324 expect(calls[2]).toBe('[1] str! 123');1325 expect($rootScope.$countWatchers()).toBe(0);1326 }));1327 it('should stop watching strings with no expressions after first execution',1328 inject(function($interpolate, $rootScope) {1329 var spy = jasmine.createSpy();1330 $rootScope.$watch($interpolate('foo'), spy);1331 $rootScope.$digest();1332 expect($rootScope.$countWatchers()).toBe(0);1333 expect(spy).toHaveBeenCalledWith('foo', 'foo', $rootScope);1334 expect(spy).toHaveBeenCalledTimes(1);1335 })1336 );1337 it('should stop watching strings with only constant expressions after first execution',1338 inject(function($interpolate, $rootScope) {1339 var spy = jasmine.createSpy();1340 $rootScope.$watch($interpolate('foo {{42}}'), spy);1341 $rootScope.$digest();1342 expect($rootScope.$countWatchers()).toBe(0);1343 expect(spy).toHaveBeenCalledWith('foo 42', 'foo 42', $rootScope);1344 expect(spy).toHaveBeenCalledTimes(1);1345 })1346 );1347 });1348 describe('interpolation escaping', function() {1349 var obj;1350 beforeEach(function() {1351 obj = {foo: 'Hello', bar: 'World'};1352 });1353 it('should support escaping interpolation signs', inject(function($interpolate) {1354 expect($interpolate('{{foo}} \\{\\{bar\\}\\}')(obj)).toBe('Hello {{bar}}');1355 expect($interpolate('\\{\\{foo\\}\\} {{bar}}')(obj)).toBe('{{foo}} World');1356 }));1357 it('should unescape multiple expressions', inject(function($interpolate) {1358 expect($interpolate('\\{\\{foo\\}\\}\\{\\{bar\\}\\} {{foo}}')(obj)).toBe('{{foo}}{{bar}} Hello');1359 expect($interpolate('{{foo}}\\{\\{foo\\}\\}\\{\\{bar\\}\\}')(obj)).toBe('Hello{{foo}}{{bar}}');1360 expect($interpolate('\\{\\{foo\\}\\}{{foo}}\\{\\{bar\\}\\}')(obj)).toBe('{{foo}}Hello{{bar}}');1361 expect($interpolate('{{foo}}\\{\\{foo\\}\\}{{bar}}\\{\\{bar\\}\\}{{foo}}')(obj)).toBe('Hello{{foo}}World{{bar}}Hello');1362 }));1363 /*1364 *it('should support escaping custom interpolation start/end symbols', function() {1365 * module(function($interpolateProvider) {1366 * $interpolateProvider.startSymbol('[[');1367 * $interpolateProvider.endSymbol(']]');1368 * });1369 * inject(function($interpolate) {1370 * expect($interpolate('[[foo]] \\[\\[bar\\]\\]')(obj)).toBe('Hello [[bar]]');1371 * });1372 *});1373 */1374 it('should unescape incomplete escaped expressions', inject(function($interpolate) {1375 expect($interpolate('\\{\\{foo{{foo}}')(obj)).toBe('{{fooHello');1376 expect($interpolate('\\}\\}foo{{foo}}')(obj)).toBe('}}fooHello');1377 expect($interpolate('foo{{foo}}\\{\\{')(obj)).toBe('fooHello{{');1378 expect($interpolate('foo{{foo}}\\}\\}')(obj)).toBe('fooHello}}');1379 }));1380 it('should not unescape markers within expressions', inject(function($interpolate) {1381 expect($interpolate('{{"\\\\{\\\\{Hello, world!\\\\}\\\\}"}}')(obj)).toBe('\\{\\{Hello, world!\\}\\}');1382 expect($interpolate('{{"\\{\\{Hello, world!\\}\\}"}}')(obj)).toBe('{{Hello, world!}}');1383 expect(function() {1384 $interpolate('{{\\{\\{foo\\}\\}}}')(obj);1385 }).toThrowMinErr('$parse', 'lexerr',1386 'Lexer Error: Unexpected next character at columns 0-0 [\\] in expression [\\{\\{foo\\}\\}]');1387 }));1388 // This test demonstrates that the web-server is responsible for escaping every single instance1389 // of interpolation start/end markers in an expression which they do not wish to evaluate,1390 // because AngularJS will not protect them from being evaluated (due to the added complexity1391 // and maintenance burden of context-sensitive escaping)1392 it('should evaluate expressions between escaped start/end symbols', inject(function($interpolate) {1393 expect($interpolate('\\{\\{Hello, {{bar}}!\\}\\}')(obj)).toBe('{{Hello, World!}}');1394 }));1395 });1396 describe('interpolating in a trusted context', function() {1397 var sce;1398 beforeEach(function() {1399 function log() {}1400 var fakeLog = {log: log, warn: log, info: log, error: log};1401 module(function($provide, $sceProvider) {1402 $provide.value('$log', fakeLog);1403 $sceProvider.enabled(true);1404 });1405 inject(['$sce', function($sce) { sce = $sce; }]);1406 });1407 it('should NOT interpolate non-trusted expressions', inject(function($interpolate, $rootScope) {1408 var scope = $rootScope.$new();1409 scope.foo = 'foo';1410 expect(function() {1411 $interpolate('{{foo}}', true, sce.CSS)(scope);1412 }).toThrowMinErr('$interpolate', 'interr');1413 }));1414 it('should NOT interpolate mistyped expressions', inject(function($interpolate, $rootScope) {1415 var scope = $rootScope.$new();1416 scope.foo = sce.trustAsCss('foo');1417 expect(function() {1418 $interpolate('{{foo}}', true, sce.HTML)(scope);1419 }).toThrowMinErr('$interpolate', 'interr');1420 }));1421 it('should interpolate trusted expressions in a regular context', inject(function($interpolate) {1422 var foo = sce.trustAsCss('foo');1423 expect($interpolate('{{foo}}', true)({foo: foo})).toBe('foo');1424 }));1425 it('should interpolate trusted expressions in a specific trustedContext', inject(function($interpolate) {1426 var foo = sce.trustAsCss('foo');1427 expect($interpolate('{{foo}}', true, sce.CSS)({foo: foo})).toBe('foo');1428 }));1429 // The concatenation of trusted values does not necessarily result in a trusted value. (For1430 // instance, you can construct evil JS code by putting together pieces of JS strings that are by1431 // themselves safe to execute in isolation.)1432 it('should NOT interpolate trusted expressions with multiple parts', inject(function($interpolate) {1433 var foo = sce.trustAsCss('foo');1434 var bar = sce.trustAsCss('bar');1435 expect(function() {1436 return $interpolate('{{foo}}{{bar}}', true, sce.CSS)({foo: foo, bar: bar});1437 }).toThrowMinErr(1438 '$interpolate', 'noconcat', 'Error while interpolating: {{foo}}{{bar}}\n' +1439 'Strict Contextual Escaping disallows interpolations that concatenate multiple ' +1440 'expressions when a trusted value is required. See ' +1441 'http://docs.angularjs.org/api/ng.$sce');1442 }));1443 });1444/*1445 * describe('provider', function() {1446 * beforeEach(module(function($interpolateProvider) {1447 * $interpolateProvider.startSymbol('--');1448 * $interpolateProvider.endSymbol('--');1449 * }));1450 *1451 * it('should not get confused with same markers', inject(function($interpolate) {1452 * expect($interpolate('---').expressions).toEqual([]);1453 * expect($interpolate('----')({})).toEqual('');1454 * expect($interpolate('--1--')({})).toEqual('1');1455 * }));1456 * });1457 */1458 describe('parseBindings', function() {1459 it('should Parse Text With No Bindings', inject(function($interpolate) {1460 expect($interpolate('a').expressions).toEqual([]);1461 }));1462 it('should Parse Empty Text', inject(function($interpolate) {1463 expect($interpolate('').expressions).toEqual([]);1464 }));1465 it('should Parse Inner Binding', inject(function($interpolate) {1466 var interpolateFn = $interpolate('a{{b}}C'),1467 expressions = interpolateFn.expressions;1468 expect(expressions).toEqual(['b']);1469 expect(interpolateFn({b: 123})).toEqual('a123C');1470 }));1471 it('should Parse Ending Binding', inject(function($interpolate) {1472 var interpolateFn = $interpolate('a{{b}}'),1473 expressions = interpolateFn.expressions;1474 expect(expressions).toEqual(['b']);1475 expect(interpolateFn({b: 123})).toEqual('a123');1476 }));1477 it('should Parse Begging Binding', inject(function($interpolate) {1478 var interpolateFn = $interpolate('{{b}}c'),1479 expressions = interpolateFn.expressions;1480 expect(expressions).toEqual(['b']);1481 expect(interpolateFn({b: 123})).toEqual('123c');1482 }));1483 it('should Parse Loan Binding', inject(function($interpolate) {1484 var interpolateFn = $interpolate('{{b}}'),1485 expressions = interpolateFn.expressions;1486 expect(expressions).toEqual(['b']);1487 expect(interpolateFn({b: 123})).toEqual('123');1488 }));1489 it('should Parse Two Bindings', inject(function($interpolate) {1490 var interpolateFn = $interpolate('{{b}}{{c}}'),1491 expressions = interpolateFn.expressions;1492 expect(expressions).toEqual(['b', 'c']);1493 expect(interpolateFn({b: 111, c: 222})).toEqual('111222');1494 }));1495 it('should Parse Two Bindings With Text In Middle', inject(function($interpolate) {1496 var interpolateFn = $interpolate('{{b}}x{{c}}'),1497 expressions = interpolateFn.expressions;1498 expect(expressions).toEqual(['b', 'c']);1499 expect(interpolateFn({b: 111, c: 222})).toEqual('111x222');1500 }));1501 it('should Parse Multiline', inject(function($interpolate) {1502 var interpolateFn = $interpolate('"X\nY{{A\n+B}}C\nD"'),1503 expressions = interpolateFn.expressions;1504 expect(expressions).toEqual(['A\n+B']);1505 expect(interpolateFn({'A': 'aa', 'B': 'bb'})).toEqual('"X\nYaabbC\nD"');1506 }));1507 });1508 describe('isTrustedContext', function() {1509 it('should NOT interpolate a multi-part expression when isTrustedContext is true', inject(function($interpolate) {1510 var isTrustedContext = true;1511 expect(function() {1512 $interpolate('constant/{{var}}', true, isTrustedContext);1513 }).toThrowMinErr(1514 '$interpolate', 'noconcat', 'Error while interpolating: constant/{{var}}\nStrict ' +1515 'Contextual Escaping disallows interpolations that concatenate multiple expressions ' +1516 'when a trusted value is required. See http://docs.angularjs.org/api/ng.$sce');1517 expect(function() {1518 $interpolate('{{var}}/constant', true, isTrustedContext);1519 }).toThrowMinErr(1520 '$interpolate', 'noconcat', 'Error while interpolating: {{var}}/constant\nStrict ' +1521 'Contextual Escaping disallows interpolations that concatenate multiple expressions ' +1522 'when a trusted value is required. See http://docs.angularjs.org/api/ng.$sce');1523 expect(function() {1524 $interpolate('{{foo}}{{bar}}', true, isTrustedContext);1525 }).toThrowMinErr(1526 '$interpolate', 'noconcat', 'Error while interpolating: {{foo}}{{bar}}\nStrict ' +1527 'Contextual Escaping disallows interpolations that concatenate multiple expressions ' +1528 'when a trusted value is required. See http://docs.angularjs.org/api/ng.$sce');1529 }));1530 it('should interpolate a multi-part expression when isTrustedContext is false', inject(function($interpolate) {1531 expect($interpolate('some/{{id}}')({})).toEqual('some/');1532 expect($interpolate('some/{{id}}')({id: 1})).toEqual('some/1');1533 expect($interpolate('{{foo}}{{bar}}')({foo: 1, bar: 2})).toEqual('12');1534 }));1535 });1536/*1537 * describe('startSymbol', function() {1538 *1539 * beforeEach(module(function($interpolateProvider) {1540 * expect($interpolateProvider.startSymbol()).toBe('{{');1541 * $interpolateProvider.startSymbol('((');1542 * }));1543 *1544 *1545 * it('should expose the startSymbol in config phase', module(function($interpolateProvider) {1546 * expect($interpolateProvider.startSymbol()).toBe('((');1547 * }));1548 *1549 *1550 * it('should expose the startSymbol in run phase', inject(function($interpolate) {1551 * expect($interpolate.startSymbol()).toBe('((');1552 * }));1553 *1554 *1555 * it('should not get confused by matching start and end symbols', function() {1556 * module(function($interpolateProvider) {1557 * $interpolateProvider.startSymbol('--');1558 * $interpolateProvider.endSymbol('--');1559 * });1560 *1561 * inject(function($interpolate) {1562 * expect($interpolate('---').expressions).toEqual([]);1563 * expect($interpolate('----')({})).toEqual('');1564 * expect($interpolate('--1--')({})).toEqual('1');1565 * });1566 * });1567 * });1568 */1569/*1570 * describe('endSymbol', function() {1571 *1572 * beforeEach(module(function($interpolateProvider) {1573 * expect($interpolateProvider.endSymbol()).toBe('}}');1574 * $interpolateProvider.endSymbol('))');1575 * }));1576 *1577 *1578 * it('should expose the endSymbol in config phase', module(function($interpolateProvider) {...
line-test.js
Source:line-test.js
...41 assert.deepEqual(ii, [0, 1], "expected index, got {actual}");42 assert.deepEqual(tt, [t, t], "expected this, got {actual}");43 },44 "interpolate defaults to linear": function(line) {45 assert.equal(line().interpolate(), "linear");46 },47 "interpolate can be defined as a constant": function(line) {48 var l = line().interpolate("step-before");49 assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0V1H1");50 assert.equal(l.interpolate(), "step-before");51 },52 "interpolate can be defined as a function": function(line) {53 var l = line().interpolate(interpolate);54 assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0T1,1");55 assert.equal(l.interpolate(), interpolate);56 function interpolate(points) {57 return points.join("T");58 }59 },60 "invalid interpolates fallback to linear": function(line) {61 assert.equal(line().interpolate("__proto__").interpolate(), "linear");62 },63 "tension defaults to .7": function(line) {64 assert.equal(line().tension(), .7);65 },66 "tension can be specified as a constant": function(line) {67 var l = line().tension(.5);68 assert.equal(l.tension(), .5);69 },70 "returns null if input points array is empty": function(line) {71 assert.isNull(line()([]));72 },73 "interpolate(linear)": {74 "supports linear interpolation": function(line) {75 var l = line().interpolate("linear");76 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0L1,1L2,0L3,1L4,0");77 }78 },79 "interpolate(step)": {80 "supports step-before interpolation": function(line) {81 var l = line().interpolate("step-before");82 assert.pathEqual(l([[0, 0]]), "M0,0");83 assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0V1H1");84 assert.pathEqual(l([[0, 0], [1, 1], [2, 0]]), "M0,0V1H1V0H2");85 },86 "supports step interpolation": function(line) {87 var l = line().interpolate("step");88 assert.pathEqual(l([[0, 0]]), "M0,0");89 assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0H0.5V1H1");90 assert.pathEqual(l([[0, 0], [1, 1], [2, 0]]), "M0,0H0.5V1H1.5V0H2");91 },92 "supports step-after interpolation": function(line) {93 var l = line().interpolate("step-after");94 assert.pathEqual(l([[0, 0]]), "M0,0");95 assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0H1V1");96 assert.pathEqual(l([[0, 0], [1, 1], [2, 0]]), "M0,0H1V1H2V0");97 }98 },99 "interpolate(basis)": {100 "supports basis interpolation": function(line) {101 var l = line().interpolate("basis");102 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0L0.16666666666666666,0.16666666666666666C0.3333333333333333,0.3333333333333333,0.6666666666666666,0.6666666666666666,1,0.6666666666666666C1.3333333333333333,0.6666666666666666,1.6666666666666665,0.3333333333333333,2,0.3333333333333333C2.333333333333333,0.3333333333333333,2.6666666666666665,0.6666666666666666,3,0.6666666666666666C3.333333333333333,0.6666666666666666,3.6666666666666665,0.3333333333333333,3.833333333333333,0.16666666666666666L4,0");103 },104 "supports basis-open interpolation": function(line) {105 var l = line().interpolate("basis-open");106 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M1,0.6666666666666666C1.3333333333333333,0.6666666666666666,1.6666666666666665,0.3333333333333333,2,0.3333333333333333C2.333333333333333,0.3333333333333333,2.6666666666666665,0.6666666666666666,3,0.6666666666666666");107 },108 "supports basis-closed interpolation": function(line) {109 var l = line().interpolate("basis-closed");110 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M2,0.3333333333333333C2.333333333333333,0.3333333333333333,2.6666666666666665,0.6666666666666666,3,0.6666666666666666C3.333333333333333,0.6666666666666666,3.6666666666666665,0.3333333333333333,3.1666666666666665,0.16666666666666666C2.6666666666666665,0,1.3333333333333333,0,0.8333333333333333,0.16666666666666666C0.3333333333333333,0.3333333333333333,0.6666666666666666,0.6666666666666666,1,0.6666666666666666C1.3333333333333333,0.6666666666666666,1.6666666666666665,0.3333333333333333,2,0.3333333333333333");111 },112 "basis interpolation reverts to linear with fewer than three points": function(line) {113 var l = line().interpolate("basis"), d = line();114 assert.pathEqual(l([[0, 0]]), d([[0, 0]]));115 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]]));116 },117 "basis-open interpolation reverts to linear with fewer than four points": function(line) {118 var l = line().interpolate("basis-open"), d = line();119 assert.pathEqual(l([[0, 0]]), d([[0, 0]]));120 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]]));121 assert.pathEqual(l([[0, 0], [1, 1], [2, 0]]), d([[0, 0], [1, 1], [2, 0]]));122 },123 "basis-closed interpolation reverts to linear with fewer than three points": function(line) {124 var l = line().interpolate("basis-open"), d = line();125 assert.pathEqual(l([[0, 0]]), d([[0, 0]]));126 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]]));127 }128 },129 "interpolate(bundle)": {130 "supports bundle interpolation": function(line) {131 var l = line().interpolate("bundle");132 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0L0.16666666666666666,0.11666666666666665C0.3333333333333333,0.2333333333333333,0.6666666666666666,0.4666666666666666,1,0.4666666666666666C1.3333333333333333,0.4666666666666666,1.6666666666666665,0.2333333333333333,2,0.2333333333333333C2.333333333333333,0.2333333333333333,2.6666666666666665,0.4666666666666666,3,0.4666666666666666C3.333333333333333,0.4666666666666666,3.6666666666666665,0.2333333333333333,3.833333333333333,0.11666666666666665L4,0");133 },134 "observes the specified tension": function(line) {135 var l = line().interpolate("bundle").tension(1);136 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), line().interpolate("basis")([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]));137 },138 "supports a single-element array": function(line) {139 var l = line().interpolate("bundle").tension(1);140 assert.pathEqual(l([[0, 0]]), "M0,0");141 }142 },143 "interpolate(cardinal)": {144 "supports cardinal interpolation": function(line) {145 var l = line().interpolate("cardinal");146 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0Q0.8,1,1,1C1.3,1,1.7,0,2,0S2.7,1,3,1Q3.2,1,4,0");147 },148 "supports cardinal-open interpolation": function(line) {149 var l = line().interpolate("cardinal-open");150 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M1,1C1.3,1,1.7,0,2,0S2.7,1,3,1");151 },152 "supports cardinal-closed interpolation": function(line) {153 var l = line().interpolate("cardinal-closed");154 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0C-0.45,0.15,0.7,1,1,1S1.7,0,2,0S2.7,1,3,1S4.45,0.15,4,0S0.45,-0.15,0,0");155 },156 "cardinal interpolation reverts to linear with fewer than three points": function(line) {157 var l = line().interpolate("cardinal"), d = line();158 assert.pathEqual(l([[0, 0]]), d([[0, 0]]));159 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]]));160 },161 "cardinal-open interpolation reverts to linear with fewer than four points": function(line) {162 var l = line().interpolate("cardinal-open"), d = line();163 assert.pathEqual(l([[0, 0]]), d([[0, 0]]));164 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]]));165 assert.pathEqual(l([[0, 0], [1, 1], [2, 0]]), d([[0, 0], [1, 1], [2, 0]]));166 },167 "cardinal-closed interpolation reverts to linear with fewer than three points": function(line) {168 var l = line().interpolate("cardinal-open"), d = line();169 assert.pathEqual(l([[0, 0]]), d([[0, 0]]));170 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]]));171 },172 "observes the specified tension": function(line) {173 var l = line().tension(.5);174 assert.pathEqual(l.interpolate("cardinal")([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0Q0.6666666666666667,1,1,1C1.5,1,1.5,0,2,0S2.5,1,3,1Q3.3333333333333335,1,4,0");175 assert.pathEqual(l.interpolate("cardinal-open")([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M1,1C1.5,1,1.5,0,2,0S2.5,1,3,1");176 assert.pathEqual(l.interpolate("cardinal-closed")([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0C-0.75,0.25,0.5,1,1,1S1.5,0,2,0S2.5,1,3,1S4.75,0.25,4,0S0.75,-0.25,0,0");177 }178 },179 "interpolate(monotone)": {180 "supports monotone interpolation": function(line) {181 var l = line().interpolate("monotone");182 assert.pathEqual(l([[0, 0], [1, 1], [2, 1], [3, 0], [4, 0]]), "M0,0C0.08333333333333333,0.08333333333333333,0.6666666666666667,1,1,1S1.6666666666666667,1,2,1S2.6666666666666665,0,3,0S3.8333333333333335,0,4,0");183 },184 "monotone interpolation reverts to linear with fewer than three points": function(line) {185 var l = line().interpolate("monotone"), d = line();186 assert.pathEqual(l([[0, 0]]), d([[0, 0]]));187 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]]));188 }189 }190 }191});...
interpolate-test.js
Source:interpolate-test.js
...7 topic: function() {8 return d3.interpolate;9 },10 "interpolates numbers": function(interpolate) {11 assert.equal(interpolate(2, 12)(.4), 6);12 assert.equal(interpolate("2px", 12)(.4), 6);13 },14 "interpolates colors": function(interpolate) {15 assert.equal(interpolate("#abcdef", "#fedcba")(.4), "#ccd3da");16 },17 "interpolates strings": function(interpolate) {18 assert.equal(interpolate("width:10px;", "width:50px;")(.2), "width:18px;");19 assert.equal(interpolate(2, "12px")(.4), "6px");20 },21 "interpolates arrays": function(interpolate) {22 assert.deepEqual(interpolate([2, 4], [12, 24])(.4), [6, 12]);23 },24 "interpolates objects": function(interpolate) {25 assert.deepEqual(interpolate({foo: 2}, {foo: 12})(.4), {foo: 6});26 },27 "may or may not interpolate between enumerable and non-enumerable properties": function(interpolate) {28 var a = Object.create({}, {foo: {value: 1, enumerable: true}}),29 b = Object.create({}, {foo: {value: 2, enumerable: false}});30 try {31 assert.deepEqual(interpolate(a, b)(1), {});32 } catch (e) {33 assert.deepEqual(interpolate(a, b)(1), {foo: 2});34 }35 try {36 assert.deepEqual(interpolate(b, a)(1), {});37 } catch (e) {38 assert.deepEqual(interpolate(b, a)(1), {foo: 1});39 }40 },41 "interpolates inherited properties of objects": function(interpolate) {42 var a = Object.create({foo: 0}),43 b = Object.create({foo: 2});44 assert.deepEqual(interpolate(a, b)(.5), {foo: 1});45 },46 "doesn't interpret properties in the default object's prototype chain as RGB": function(interpolate) {47 assert.equal(interpolate("hasOwnProperty", "hasOwnProperty")(0), "hasOwnProperty");48 }49 }50});51suite.addBatch({52 "interpolateNumber": {53 topic: function() {54 return d3.interpolateNumber;55 },56 "interpolates numbers": function(interpolate) {57 assert.equal(interpolate(2, 12)(.4), 6);58 assert.equal(interpolate(2, 12)(.6), 8);59 }60 }61});62suite.addBatch({63 "interpolateRound": {64 topic: function() {65 return d3.interpolateRound;66 },67 "interpolates integers": function(interpolate) {68 assert.equal(interpolate(2, 12)(.456), 7);69 assert.equal(interpolate(2, 12)(.678), 9);70 }71 }72});73suite.addBatch({74 "interpolateString": {75 topic: function() {76 return d3.interpolateString;77 },78 "interpolates matching numbers in both strings": function(interpolate) {79 assert.equal(interpolate(" 10/20 30", "50/10 100 ")(.2), "18/18 44 ");80 assert.equal(interpolate(" 10/20 30", "50/10 100 ")(.4), "26/16 58 ");81 },82 "preserves non-numbers in string b": function(interpolate) {83 assert.equal(interpolate(" 10/20 30", "50/10 foo ")(.2), "18/18 foo ");84 assert.equal(interpolate(" 10/20 30", "50/10 foo ")(.4), "26/16 foo ");85 },86 "preserves non-matching numbers in string b": function(interpolate) {87 assert.equal(interpolate(" 10/20 foo", "50/10 100 ")(.2), "18/18 100 ");88 assert.equal(interpolate(" 10/20 bar", "50/10 100 ")(.4), "26/16 100 ");89 },90 "preserves equal-value numbers in both strings": function(interpolate) {91 assert.equal(interpolate(" 10/20 100 20", "50/10 100, 20 ")(.2), "18/18 100, 20 ");92 assert.equal(interpolate(" 10/20 100 20", "50/10 100, 20 ")(.4), "26/16 100, 20 ");93 },94 "interpolates exponent notation correctly": function(interpolate) {95 assert.equal(interpolate("1e+3", "1e+4")(.5), "5500");96 assert.equal(interpolate("1e-3", "1e-4")(.5), "0.00055");97 }98 }99});100suite.addBatch({101 "interpolateRgb": {102 topic: function() {103 return d3.interpolateRgb;104 },105 "parses string input": function(interpolate) {106 assert.equal(interpolate("steelblue", "#f00")(.2), "#6b6890");107 assert.equal(interpolate("steelblue", "#f00")(.6), "#b53448");108 },109 "parses d3.rgb input": function(interpolate) {110 assert.equal(interpolate(d3.rgb("steelblue"), "#f00")(.2), "#6b6890");111 assert.equal(interpolate("steelblue", d3.rgb(255, 0, 0))(.6), "#b53448");112 },113 "parses d3.hsl input": function(interpolate) {114 assert.equal(interpolate(d3.hsl("steelblue"), "#f00")(.2), "#6b6890");115 assert.equal(interpolate("steelblue", d3.hsl(0, 1, .5))(.6), "#b53448");116 },117 "interpolates in RGB color space": function(interpolate) {118 assert.equal(interpolate("steelblue", "#f00")(.2), "#6b6890");119 },120 "outputs an RGB string": function(interpolate) {121 assert.equal(interpolate("steelblue", "#f00")(.2), "#6b6890");122 }123 }124});125suite.addBatch({126 "interpolateHsl": {127 topic: function() {128 return d3.interpolateHsl;129 },130 "parses string input": function(interpolate) {131 assert.equal(interpolate("steelblue", "#f00")(.2), "#38c3a2");132 assert.equal(interpolate("steelblue", "#f00")(.6), "#96e11c");133 },134 "parses d3.hsl input": function(interpolate) {135 assert.equal(interpolate(d3.hsl("steelblue"), "#f00")(.2), "#38c3a2");136 assert.equal(interpolate("steelblue", d3.hsl(0, 1, .5))(.6), "#96e11c");137 },138 "parses d3.rgb input": function(interpolate) {139 assert.equal(interpolate(d3.rgb("steelblue"), "#f00")(.2), "#38c3a2");140 assert.equal(interpolate("steelblue", d3.rgb(255, 0, 0))(.6), "#96e11c");141 },142 "interpolates in HSL color space": function(interpolate) {143 assert.equal(interpolate("steelblue", "#f00")(.2), "#38c3a2");144 },145 "outputs a hexadecimal string": function(interpolate) {146 assert.equal(interpolate("steelblue", "#f00")(.2), "#38c3a2");147 }148 }149});150suite.addBatch({151 "interpolateArray": {152 topic: function() {153 return d3.interpolateArray;154 },155 "interpolates defined elements": function(interpolate) {156 assert.deepEqual(interpolate([2, 12], [4, 24])(.5), [3, 18]);157 },158 "interpolates nested objects and arrays": function(interpolate) {159 assert.deepEqual(interpolate([[2, 12]], [[4, 24]])(.5), [[3, 18]]);160 assert.deepEqual(interpolate([{foo: [2, 12]}], [{foo: [4, 24]}])(.5), [{foo: [3, 18]}]);161 },162 "merges non-shared elements": function(interpolate) {163 assert.deepEqual(interpolate([2, 12], [4, 24, 12])(.5), [3, 18, 12]);164 assert.deepEqual(interpolate([2, 12, 12], [4, 24])(.5), [3, 18, 12]);165 }166 }167});168suite.addBatch({169 "interpolateObject": {170 topic: function() {171 return d3.interpolateObject;172 },173 "interpolates defined properties": function(interpolate) {174 assert.deepEqual(interpolate({a: 2, b: 12}, {a: 4, b: 24})(.5), {a: 3, b: 18});175 },176 "interpolates inherited properties": function(interpolate) {177 function a(a) { this.a = a; }178 a.prototype.b = 12;179 assert.deepEqual(interpolate(new a(2), new a(4))(.5), {a: 3, b: 12});180 },181 "interpolates color properties as rgb": function(interpolate) {182 assert.deepEqual(interpolate({background: "red"}, {background: "green"})(.5), {background: "#804000"});183 assert.deepEqual(interpolate({fill: "red"}, {fill: "green"})(.5), {fill: "#804000"});184 assert.deepEqual(interpolate({stroke: "red"}, {stroke: "green"})(.5), {stroke: "#804000"});185 assert.deepEqual(interpolate({color: "red"}, {color: "green"})(.5), {color: "#804000"});186 },187 "interpolates nested objects and arrays": function(interpolate) {188 assert.deepEqual(interpolate({foo: [2, 12]}, {foo: [4, 24]})(.5), {foo: [3, 18]});189 assert.deepEqual(interpolate({foo: {bar: [2, 12]}}, {foo: {bar: [4, 24]}})(.5), {foo: {bar: [3, 18]}});190 },191 "merges non-shared properties": function(interpolate) {192 assert.deepEqual(interpolate({foo: 2}, {foo: 4, bar: 12})(.5), {foo: 3, bar: 12});193 assert.deepEqual(interpolate({foo: 2, bar: 12}, {foo: 4})(.5), {foo: 3, bar: 12});194 }195 }196});197suite.addBatch({198 "interpolators": {199 topic: function() {200 return d3.interpolate;201 },202 "can register a custom interpolator": function(interpolate) {203 try {204 d3.interpolators.push(function(a, b) {205 return a == "one" && b == "two" && d3.interpolateNumber(1, 2);206 });207 assert.equal(interpolate("one", "two")(-.5), .5);208 assert.equal(interpolate("one", "two")(0), 1);209 assert.equal(interpolate("one", "two")(.5), 1.5);210 assert.equal(interpolate("one", "two")(1), 2);211 assert.equal(interpolate("one", "two")(1.5), 2.5);212 } finally {213 d3.interpolators.pop();214 }215 }216 }217});...
interpolate.js
Source:interpolate.js
...163 na = a.length,164 nb = b.length,165 n0 = Math.min(a.length, b.length),166 i;167 for (i = 0; i < n0; ++i) x.push(d3.interpolate(a[i], b[i]));168 for (; i < na; ++i) c[i] = a[i];169 for (; i < nb; ++i) c[i] = b[i];170 return function(t) {171 for (i = 0; i < n0; ++i) c[i] = x[i](t);172 return c;173 };174};175d3.interpolateObject = function(a, b) {176 var i = {},177 c = {},178 k;179 for (k in a) {180 if (k in b) {181 i[k] = d3_interpolateByName(k)(a[k], b[k]);...
string-test.js
Source:string-test.js
...5suite.addBatch({6 "interpolateString": {7 topic: load("interpolate/string").expression("d3.interpolateString"),8 "interpolates matching numbers in both strings": function(interpolate) {9 assert.strictEqual(interpolate(" 10/20 30", "50/10 100 ")(.2), "18/18 44 ");10 assert.strictEqual(interpolate(" 10/20 30", "50/10 100 ")(.4), "26/16 58 ");11 },12 "coerces objects to strings": function(interpolate) {13 assert.strictEqual(interpolate({toString: function() { return "2px"; }}, {toString: function() { return "12px"; }})(.25), "4.5px");14 },15 "preserves non-numbers in string b": function(interpolate) {16 assert.strictEqual(interpolate(" 10/20 30", "50/10 foo ")(.2), "18/18 foo ");17 assert.strictEqual(interpolate(" 10/20 30", "50/10 foo ")(.4), "26/16 foo ");18 },19 "preserves non-matching numbers in string b": function(interpolate) {20 assert.strictEqual(interpolate(" 10/20 foo", "50/10 100 ")(.2), "18/18 100 ");21 assert.strictEqual(interpolate(" 10/20 bar", "50/10 100 ")(.4), "26/16 100 ");22 },23 "preserves equal-value numbers in both strings": function(interpolate) {24 assert.strictEqual(interpolate(" 10/20 100 20", "50/10 100, 20 ")(.2), "18/18 100, 20 ");25 assert.strictEqual(interpolate(" 10/20 100 20", "50/10 100, 20 ")(.4), "26/16 100, 20 ");26 },27 "interpolates decimal notation correctly": function(interpolate) {28 assert.strictEqual(interpolate("1.", "2.")(.5), "1.5");29 },30 "interpolates exponent notation correctly": function(interpolate) {31 assert.strictEqual(interpolate("1e+3", "1e+4")(.5), "5500");32 assert.strictEqual(interpolate("1e-3", "1e-4")(.5), "0.00055");33 assert.strictEqual(interpolate("1.e-3", "1.e-4")(.5), "0.00055");34 assert.strictEqual(interpolate("-1.e-3", "-1.e-4")(.5), "-0.00055");35 assert.strictEqual(interpolate("+1.e-3", "+1.e-4")(.5), "0.00055");36 assert.strictEqual(interpolate(".1e-2", ".1e-3")(.5), "0.00055");37 },38 "with no numbers, returns the target string": function(interpolate) {39 assert.strictEqual(interpolate("foo", "bar")(.5), "bar");40 assert.strictEqual(interpolate("foo", "")(.5), "");41 assert.strictEqual(interpolate("", "bar")(.5), "bar");42 assert.strictEqual(interpolate("", "")(.5), "");43 },44 "with two numerically-equivalent numbers, returns the default format": function(interpolate) {45 assert.strictEqual(interpolate("top: 1000px;", "top: 1e3px;")(.5), "top: 1000px;");46 assert.strictEqual(interpolate("top: 1e3px;", "top: 1000px;")(.5), "top: 1000px;");47 }48 }49});...
hsl-test.js
Source:hsl-test.js
1var vows = require("vows"),2 load = require("../load"),3 assert = require("../assert");4var suite = vows.describe("d3.interpolateHsl");5suite.addBatch({6 "interpolateHsl": {7 topic: load("interpolate/hsl"), // beware instanceof d3_Color8 "parses string input": function(d3) {9 assert.strictEqual(d3.interpolateHsl("steelblue", "#f00")(.2), "#383dc3");10 assert.strictEqual(d3.interpolateHsl("steelblue", "#f00")(.6), "#dd1ce1");11 },12 "parses d3.hsl input": function(d3) {13 assert.strictEqual(d3.interpolateHsl(d3.hsl("steelblue"), "#f00")(.2), "#383dc3");14 assert.strictEqual(d3.interpolateHsl("steelblue", d3.hsl(0, 1, .5))(.6), "#dd1ce1");15 },16 "parses d3.rgb input": function(d3) {17 assert.strictEqual(d3.interpolateHsl(d3.rgb("steelblue"), "#f00")(.2), "#383dc3");18 assert.strictEqual(d3.interpolateHsl("steelblue", d3.rgb(255, 0, 0))(.6), "#dd1ce1");19 },20 "interpolates in HSL color space": function(d3) {21 assert.strictEqual(d3.interpolateHsl("steelblue", "#f00")(.2), "#383dc3");22 },23 "uses source hue when destination hue is undefined": function(d3) {24 assert.equal(d3.interpolateHsl("#f60", "#000")(.5), "#803300");25 assert.equal(d3.interpolateHsl("#6f0", "#fff")(.5), "#b3ff80");26 },27 "uses destination hue when source hue is undefined": function(d3) {28 assert.equal(d3.interpolateHsl("#000", "#f60")(.5), "#803300");29 assert.equal(d3.interpolateHsl("#fff", "#6f0")(.5), "#b3ff80");30 },31 "uses source saturation when destination saturation is undefined": function(d3) {32 assert.equal(d3.interpolateHsl("#ccc", "#000")(.5), "#666666");33 assert.equal(d3.interpolateHsl("#f00", "#000")(.5), "#800000");34 },35 "uses destination saturation when source saturation is undefined": function(d3) {36 assert.equal(d3.interpolateHsl("#000", "#ccc")(.5), "#666666");37 assert.equal(d3.interpolateHsl("#000", "#f00")(.5), "#800000");38 },39 "outputs a hexadecimal string": function(d3) {40 assert.strictEqual(d3.interpolateHsl("steelblue", "#f00")(.2), "#383dc3");41 }42 }43});...
hcl-test.js
Source:hcl-test.js
1var vows = require("vows"),2 load = require("../load"),3 assert = require("../assert");4var suite = vows.describe("d3.interpolateHcl");5suite.addBatch({6 "interpolateHcl": {7 topic: load("interpolate/hcl"), // beware instanceof d3_Color8 "parses string input": function(d3) {9 assert.strictEqual(d3.interpolateHcl("steelblue", "#f00")(.2), "#6978c9");10 assert.strictEqual(d3.interpolateHcl("steelblue", "#f00")(.6), "#e034a2");11 },12 "parses d3.hsl input": function(d3) {13 assert.strictEqual(d3.interpolateHcl(d3.hsl("steelblue"), "#f00")(.2), "#6978c9");14 assert.strictEqual(d3.interpolateHcl("steelblue", d3.hsl(0, 1, .5))(.6), "#e034a2");15 },16 "parses d3.rgb input": function(d3) {17 assert.strictEqual(d3.interpolateHcl(d3.rgb("steelblue"), "#f00")(.2), "#6978c9");18 assert.strictEqual(d3.interpolateHcl("steelblue", d3.rgb(255, 0, 0))(.6), "#e034a2");19 },20 "interpolates in HSL color space": function(d3) {21 assert.strictEqual(d3.interpolateHcl("steelblue", "#f00")(.2), "#6978c9");22 },23 "uses source hue when destination hue is undefined": function(d3) {24 assert.equal(d3.interpolateHcl("#f60", "#000")(.5), "#9b0000");25 assert.equal(d3.interpolateHcl("#6f0", "#000")(.5), "#008100");26 },27 "uses destination hue when source hue is undefined": function(d3) {28 assert.equal(d3.interpolateHcl("#000", "#f60")(.5), "#9b0000");29 assert.equal(d3.interpolateHcl("#000", "#6f0")(.5), "#008100");30 },31 "uses source chroma when destination chroma is undefined": function(d3) {32 assert.equal(d3.interpolateHcl("#ccc", "#000")(.5), "#616161");33 assert.equal(d3.interpolateHcl("#f00", "#000")(.5), "#a60000");34 },35 "uses destination chroma when source chroma is undefined": function(d3) {36 assert.equal(d3.interpolateHcl("#000", "#ccc")(.5), "#616161");37 assert.equal(d3.interpolateHcl("#000", "#f00")(.5), "#a60000");38 },39 "outputs a hexadecimal string": function(d3) {40 assert.strictEqual(d3.interpolateHcl("steelblue", "#f00")(.2), "#6978c9");41 }42 }43});...
object-test.js
Source:object-test.js
...5suite.addBatch({6 "interpolateObject": {7 topic: load("interpolate/object").expression("d3.interpolateObject"),8 "interpolates defined properties": function(interpolate) {9 assert.deepEqual(interpolate({a: 2, b: 12}, {a: 4, b: 24})(.5), {a: 3, b: 18});10 },11 "interpolates inherited properties": function(interpolate) {12 function a(a) { this.a = a; }13 a.prototype.b = 12;14 assert.deepEqual(interpolate(new a(2), new a(4))(.5), {a: 3, b: 12});15 },16 "interpolates color properties as rgb": function(interpolate) {17 assert.deepEqual(interpolate({background: "red"}, {background: "green"})(.5), {background: "#804000"});18 assert.deepEqual(interpolate({fill: "red"}, {fill: "green"})(.5), {fill: "#804000"});19 assert.deepEqual(interpolate({stroke: "red"}, {stroke: "green"})(.5), {stroke: "#804000"});20 assert.deepEqual(interpolate({color: "red"}, {color: "green"})(.5), {color: "#804000"});21 },22 "interpolates nested objects and arrays": function(interpolate) {23 assert.deepEqual(interpolate({foo: [2, 12]}, {foo: [4, 24]})(.5), {foo: [3, 18]});24 assert.deepEqual(interpolate({foo: {bar: [2, 12]}}, {foo: {bar: [4, 24]}})(.5), {foo: {bar: [3, 18]}});25 },26 "merges non-shared properties": function(interpolate) {27 assert.deepEqual(interpolate({foo: 2}, {foo: 4, bar: 12})(.5), {foo: 3, bar: 12});28 assert.deepEqual(interpolate({foo: 2, bar: 12}, {foo: 4})(.5), {foo: 3, bar: 12});29 }30 }31});...
Using AI Code Generation
1var interpolate = require('cucumber-gherkin').interpolate;2var fs = require('fs');3var featureFile = fs.readFileSync('test.feature', 'utf8');4var feature = interpolate(featureFile, {5});6fs.writeFileSync('test_new.feature', feature, 'utf8');7var exec = require('child_process').exec;8var cmd = 'cucumber-js test_new.feature';9exec(cmd, function(error, stdout, stderr) {10 console.log(stdout);11});
Using AI Code Generation
1var interpolate = require('cucumber-gherkin').interpolate;2var myString = 'I have {x} apples';3var myObject = {x: 5};4console.log(interpolate(myString, myObject));5var interpolate = require('cucumber-gherkin').interpolate;6var myString = 'I have {x} apples';7var myObject = {x: 5};8console.log(interpolate(myString, myObject));9var interpolate = require('cucumber-gherkin').interpolate;10var myString = 'I have {x} apples';11var myObject = {x: 5};12console.log(interpolate(myString, myObject));13var interpolate = require('cucumber-gherkin').interpolate;14var myString = 'I have {x} apples';15var myObject = {x: 5};16console.log(interpolate(myString, myObject));17var interpolate = require('cucumber-gherkin').interpolate;18var myString = 'I have {x} apples';19var myObject = {x: 5};20console.log(interpolate(myString, myObject));21var interpolate = require('cucumber-gherkin').interpolate;22var myString = 'I have {x} apples';23var myObject = {x: 5};24console.log(interpolate(myString, myObject));25var interpolate = require('cucumber-gherkin').interpolate;26var myString = 'I have {x} apples';27var myObject = {x: 5};28console.log(interpolate(my
Using AI Code Generation
1var interpolate = require('cucumber-gherkin').interpolate;2var string = "Hello {name}";3var name = "World";4var result = interpolate(string, {name: name});5console.log(result);6var interpolate = require('cucumber-gherkin').interpolate;7var string = "Hello {name}";8var json = {name: "World"};9var result = interpolate(string, json);10console.log(result);11var interpolate = require('cucumber-gherkin').interpolate;12var string = "Hello {name}";13var yaml = "name: World";14var result = interpolate(string, yaml);15console.log(result);
Using AI Code Generation
1var interpolate = require('cucumber-gherkin').interpolate;2var str = interpolate('I have a {color} ball', {color: 'red'});3console.log(str);4module.exports = function () {5 this.Given(/^I have a (.*) ball$/, function (color) {6 return 'I have a ' + color + ' ball';7 });8};9var interpolate = require('cucumber-gherkin').interpolate;10var str = interpolate('I have a {color} ball and a {color} car', {color: 'red'});11console.log(str);12module.exports = function () {13 this.Given(/^I have a (.*) ball and a (.*) car$/, function (color) {14 return 'I have a ' + color + ' ball and a ' + color + ' car';15 });16};17var interpolate = require('cucumber-gherkin').interpolate;18var str = interpolate('I have a {color} ball and a {color} car', {color: ['red', 'blue']});19console.log(str);20module.exports = function () {21 this.Given(/^I have a (.*) ball and a (.*) car$/, function (color) {22 return 'I have a ' + color[0] + ' ball and a ' + color[1] + ' car';23 });24};
Using AI Code Generation
1var interpolate = require('cucumber-gherkin').interpolate;2var path = require('path');3var featurePath = path.resolve(interpolate('features/feature1.feature'));4var Cucumber = require('cucumber');5var cucumber = Cucumber(featurePath, function (err, results) {6 console.log(results);7});8cucumber.start();9module.exports = function () {10 this.Given(/^I have a step$/, function (callback) {11 callback();12 });13};14var Cucumber = require('cucumber');15var path = require('path');16var interpolate = require('cucumber-gherkin').interpolate;17var featurePath = path.resolve(interpolate('features/feature1.feature'));18Cucumber.Cli({19 cwd: process.cwd(),20}).run(function (succeeded) {21 process.exit(succeeded ? 0 : 1);22});23{24 "dependencies": {25 },26 "devDependencies": {27 },28 "scripts": {29 },30}31{ failedCount: 0,32 [ { stepResults:33 [ { step: [Object],34 failureException: null } ],35 failureException: null } ],36 [ { step: { keyword: 'Given ', name: 'I have a step', line: 4 },
LambdaTest offers a detailed Cucumber testing tutorial, explaining its features, importance, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed Cucumber testing chapters to help you get started:
Get 100 minutes of automation test minutes FREE!!