How to use callsites method in ava

Best JavaScript code snippet using ava

templates.js

Source:templates.js Github

copy

Full Screen

1// Copyright 2014 the V8 project authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4var num = 5;5var str = "str";6function fn() { return "result"; }7var obj = {8 num: num,9 str: str,10 fn: function() { return "result"; }11};12(function testBasicExpressions() {13 assertEquals("foo 5 bar", `foo ${num} bar`);14 assertEquals("foo str bar", `foo ${str} bar`);15 assertEquals("foo [object Object] bar", `foo ${obj} bar`);16 assertEquals("foo result bar", `foo ${fn()} bar`);17 assertEquals("foo 5 bar", `foo ${obj.num} bar`);18 assertEquals("foo str bar", `foo ${obj.str} bar`);19 assertEquals("foo result bar", `foo ${obj.fn()} bar`);20})();21(function testExpressionsContainingTemplates() {22 assertEquals("foo bar 5", `foo ${`bar ${num}`}`);23})();24(function testMultilineTemplates() {25 assertEquals("foo\n bar\n baz", `foo26 bar27 baz`);28 assertEquals("foo\n bar\n baz", eval("`foo\r\n bar\r baz`"));29})();30(function testLineContinuation() {31 assertEquals("\n", `\32`);33})();34(function testTaggedTemplates() {35 var calls = 0;36 (function(s) {37 calls++;38 })`test`;39 assertEquals(1, calls);40 calls = 0;41 // assert tag is invoked in right context42 obj = {43 fn: function() {44 calls++;45 assertEquals(obj, this);46 }47 };48 obj.fn`test`;49 assertEquals(1, calls);50 calls = 0;51 // Simple templates only have a callSiteObj52 (function(s) {53 calls++;54 assertEquals(1, arguments.length);55 })`test`;56 assertEquals(1, calls);57 // Templates containing expressions have the values of evaluated expressions58 calls = 0;59 (function(site, n, s, o, f, r) {60 calls++;61 assertEquals(6, arguments.length);62 assertEquals("number", typeof n);63 assertEquals("string", typeof s);64 assertEquals("object", typeof o);65 assertEquals("function", typeof f);66 assertEquals("result", r);67 })`${num}${str}${obj}${fn}${fn()}`;68 assertEquals(1, calls);69 // The TV and TRV of NoSubstitutionTemplate :: `` is the empty code unit70 // sequence.71 calls = 0;72 (function(s) {73 calls++;74 assertEquals(1, s.length);75 assertEquals(1, s.raw.length);76 assertEquals("", s[0]);77 // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest">78 assertEquals("", s.raw[0]);79 })``;80 assertEquals(1, calls);81 // The TV and TRV of TemplateHead :: `${ is the empty code unit sequence.82 calls = 0;83 (function(s) {84 calls++;85 assertEquals(2, s.length);86 assertEquals(2, s.raw.length);87 assertEquals("", s[0]);88 assertEquals("", s.raw[0]);89 })`${1}`;90 assertEquals(1, calls);91 // The TV and TRV of TemplateMiddle :: }${ is the empty code unit sequence.92 calls = 0;93 (function(s) {94 calls++;95 assertEquals(3, s.length);96 assertEquals(3, s.raw.length);97 assertEquals("", s[1]);98 assertEquals("", s.raw[1]);99 })`${1}${2}`;100 assertEquals(1, calls);101 // The TV and TRV of TemplateTail :: }` is the empty code unit sequence.102 calls = 0;103 (function(s) {104 calls++;105 assertEquals(2, s.length);106 assertEquals(2, s.raw.length);107 assertEquals("", s[1]);108 assertEquals("", s.raw[1]);109 })`${1}`;110 assertEquals(1, calls);111 // The TV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TV of112 // TemplateCharacters.113 calls = 0;114 (function(s) { calls++; assertEquals("foo", s[0]); })`foo`;115 assertEquals(1, calls);116 // The TV of TemplateHead :: ` TemplateCharacters ${ is the TV of117 // TemplateCharacters.118 calls = 0;119 (function(s) { calls++; assertEquals("foo", s[0]); })`foo${1}`;120 assertEquals(1, calls);121 // The TV of TemplateMiddle :: } TemplateCharacters ${ is the TV of122 // TemplateCharacters.123 calls = 0;124 (function(s) { calls++; assertEquals("foo", s[1]); })`${1}foo${2}`;125 assertEquals(1, calls);126 // The TV of TemplateTail :: } TemplateCharacters ` is the TV of127 // TemplateCharacters.128 calls = 0;129 (function(s) { calls++; assertEquals("foo", s[1]); })`${1}foo`;130 assertEquals(1, calls);131 // The TV of TemplateCharacters :: TemplateCharacter is the TV of132 // TemplateCharacter.133 calls = 0;134 (function(s) { calls++; assertEquals("f", s[0]); })`f`;135 assertEquals(1, calls);136 // The TV of TemplateCharacter :: $ is the code unit value 0x0024.137 calls = 0;138 (function(s) { calls++; assertEquals("$", s[0]); })`$`;139 assertEquals(1, calls);140 // The TV of TemplateCharacter :: \ EscapeSequence is the CV of141 // EscapeSequence.142 calls = 0;143 (function(s) { calls++; assertEquals("안녕", s[0]); })`\uc548\uB155`;144 (function(s) { calls++; assertEquals("\xff", s[0]); })`\xff`;145 (function(s) { calls++; assertEquals("\n", s[0]); })`\n`;146 assertEquals(3, calls);147 // The TV of TemplateCharacter :: LineContinuation is the TV of148 // LineContinuation. The TV of LineContinuation :: \ LineTerminatorSequence is149 // the empty code unit sequence.150 calls = 0;151 (function(s) { calls++; assertEquals("", s[0]); })`\152`;153 assertEquals(1, calls);154 // The TRV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TRV of155 // TemplateCharacters.156 calls = 0;157 (function(s) { calls++; assertEquals("test", s.raw[0]); })`test`;158 assertEquals(1, calls);159 // The TRV of TemplateHead :: ` TemplateCharacters ${ is the TRV of160 // TemplateCharacters.161 calls = 0;162 (function(s) { calls++; assertEquals("test", s.raw[0]); })`test${1}`;163 assertEquals(1, calls);164 // The TRV of TemplateMiddle :: } TemplateCharacters ${ is the TRV of165 // TemplateCharacters.166 calls = 0;167 (function(s) { calls++; assertEquals("test", s.raw[1]); })`${1}test${2}`;168 assertEquals(1, calls);169 // The TRV of TemplateTail :: } TemplateCharacters ` is the TRV of170 // TemplateCharacters.171 calls = 0;172 (function(s) { calls++; assertEquals("test", s.raw[1]); })`${1}test`;173 assertEquals(1, calls);174 // The TRV of TemplateCharacters :: TemplateCharacter is the TRV of175 // TemplateCharacter.176 calls = 0;177 (function(s) { calls++; assertEquals("f", s.raw[0]); })`f`;178 assertEquals(1, calls);179 // The TRV of TemplateCharacter :: $ is the code unit value 0x0024.180 calls = 0;181 (function(s) { calls++; assertEquals("\u0024", s.raw[0]); })`$`;182 assertEquals(1, calls);183 // The TRV of EscapeSequence :: 0 is the code unit value 0x0030.184 calls = 0;185 (function(s) { calls++; assertEquals("\u005C\u0030", s.raw[0]); })`\0`;186 assertEquals(1, calls);187 // The TRV of TemplateCharacter :: \ EscapeSequence is the sequence consisting188 // of the code unit value 0x005C followed by the code units of TRV of189 // EscapeSequence.190 // The TRV of EscapeSequence :: HexEscapeSequence is the TRV of the191 // HexEscapeSequence.192 calls = 0;193 (function(s) { calls++; assertEquals("\u005Cxff", s.raw[0]); })`\xff`;194 assertEquals(1, calls);195 // The TRV of EscapeSequence :: UnicodeEscapeSequence is the TRV of the196 // UnicodeEscapeSequence.197 calls = 0;198 (function(s) { calls++; assertEquals("\u005Cuc548", s.raw[0]); })`\uc548`;199 assertEquals(1, calls);200 // The TRV of CharacterEscapeSequence :: SingleEscapeCharacter is the TRV of201 // the SingleEscapeCharacter.202 calls = 0;203 (function(s) { calls++; assertEquals("\u005C\u0027", s.raw[0]); })`\'`;204 (function(s) { calls++; assertEquals("\u005C\u0022", s.raw[0]); })`\"`;205 (function(s) { calls++; assertEquals("\u005C\u005C", s.raw[0]); })`\\`;206 (function(s) { calls++; assertEquals("\u005Cb", s.raw[0]); })`\b`;207 (function(s) { calls++; assertEquals("\u005Cf", s.raw[0]); })`\f`;208 (function(s) { calls++; assertEquals("\u005Cn", s.raw[0]); })`\n`;209 (function(s) { calls++; assertEquals("\u005Cr", s.raw[0]); })`\r`;210 (function(s) { calls++; assertEquals("\u005Ct", s.raw[0]); })`\t`;211 (function(s) { calls++; assertEquals("\u005Cv", s.raw[0]); })`\v`;212 (function(s) { calls++; assertEquals("\u005C`", s.raw[0]); })`\``;213 assertEquals(10, calls);214 // The TRV of CharacterEscapeSequence :: NonEscapeCharacter is the CV of the215 // NonEscapeCharacter.216 calls = 0;217 (function(s) { calls++; assertEquals("\u005Cz", s.raw[0]); })`\z`;218 assertEquals(1, calls);219 // The TRV of LineTerminatorSequence :: <LF> is the code unit value 0x000A.220 // The TRV of LineTerminatorSequence :: <CR> is the code unit value 0x000A.221 // The TRV of LineTerminatorSequence :: <CR><LF> is the sequence consisting of222 // the code unit value 0x000A.223 calls = 0;224 function testRawLineNormalization(cs) {225 calls++;226 assertEquals(cs.raw[0], "\n\n\n");227 assertEquals(cs.raw[1], "\n\n\n");228 }229 eval("testRawLineNormalization`\r\n\n\r${1}\r\n\n\r`");230 assertEquals(1, calls);231 // The TRV of LineContinuation :: \ LineTerminatorSequence is the sequence232 // consisting of the code unit value 0x005C followed by the code units of TRV233 // of LineTerminatorSequence.234 calls = 0;235 function testRawLineContinuation(cs) {236 calls++;237 assertEquals(cs.raw[0], "\u005C\n\u005C\n\u005C\n");238 assertEquals(cs.raw[1], "\u005C\n\u005C\n\u005C\n");239 }240 eval("testRawLineContinuation`\\\r\n\\\n\\\r${1}\\\r\n\\\n\\\r`");241 assertEquals(1, calls);242})();243(function testCallSiteObj() {244 var calls = 0;245 function tag(cs) {246 calls++;247 assertTrue(cs.hasOwnProperty("raw"));248 assertTrue(Object.isFrozen(cs));249 assertTrue(Object.isFrozen(cs.raw));250 var raw = Object.getOwnPropertyDescriptor(cs, "raw");251 assertFalse(raw.writable);252 assertFalse(raw.configurable);253 assertFalse(raw.enumerable);254 assertEquals(Array.prototype, Object.getPrototypeOf(cs.raw));255 assertTrue(Array.isArray(cs.raw));256 assertEquals(Array.prototype, Object.getPrototypeOf(cs));257 assertTrue(Array.isArray(cs));258 var cooked0 = Object.getOwnPropertyDescriptor(cs, "0");259 assertFalse(cooked0.writable);260 assertFalse(cooked0.configurable);261 assertTrue(cooked0.enumerable);262 var raw0 = Object.getOwnPropertyDescriptor(cs.raw, "0");263 assertFalse(raw0.writable);264 assertFalse(raw0.configurable);265 assertTrue(raw0.enumerable);266 var length = Object.getOwnPropertyDescriptor(cs, "length");267 assertFalse(length.writable);268 assertFalse(length.configurable);269 assertFalse(length.enumerable);270 length = Object.getOwnPropertyDescriptor(cs.raw, "length");271 assertFalse(length.writable);272 assertFalse(length.configurable);273 assertFalse(length.enumerable);274 }275 tag`${1}`;276 assertEquals(1, calls);277})();278(function testUTF16ByteOrderMark() {279 assertEquals("\uFEFFtest", `\uFEFFtest`);280 assertEquals("\uFEFFtest", eval("`\uFEFFtest`"));281})();282(function testStringRawAsTagFn() {283 assertEquals("\\u0065\\`\\r\\r\\n\\ntestcheck",284 String.raw`\u0065\`\r\r\n\n${"test"}check`);285 assertEquals("\\\n\\\n\\\n", eval("String.raw`\\\r\\\r\n\\\n`"));286 assertEquals("", String.raw``);287})();288(function testCallSiteCaching() {289 var callSites = [];290 function tag(cs) { callSites.push(cs); }291 var a = 1;292 var b = 2;293 tag`head${a}tail`;294 tag`head${b}tail`;295 assertEquals(2, callSites.length);296 assertSame(callSites[0], callSites[1]);297 eval("tag`head${a}tail`");298 assertEquals(3, callSites.length);299 assertSame(callSites[1], callSites[2]);300 eval("tag`head${b}tail`");301 assertEquals(4, callSites.length);302 assertSame(callSites[2], callSites[3]);303 (new Function("tag", "a", "b", "return tag`head${a}tail`;"))(tag, 1, 2);304 assertEquals(5, callSites.length);305 assertSame(callSites[3], callSites[4]);306 (new Function("tag", "a", "b", "return tag`head${b}tail`;"))(tag, 1, 2);307 assertEquals(6, callSites.length);308 assertSame(callSites[4], callSites[5]);309 callSites = [];310 tag`foo${a}bar`;311 tag`foo\${.}bar`;312 assertEquals(2, callSites.length);313 assertEquals(2, callSites[0].length);314 assertEquals(1, callSites[1].length);315 callSites = [];316 eval("tag`\\\r\n\\\n\\\r`");317 eval("tag`\\\r\n\\\n\\\r`");318 assertEquals(2, callSites.length);319 assertSame(callSites[0], callSites[1]);320 assertEquals("", callSites[0][0]);321 assertEquals("\\\n\\\n\\\n", callSites[0].raw[0]);322 callSites = [];323 tag`\uc548\ub155`;324 tag`\uc548\ub155`;325 assertEquals(2, callSites.length);326 assertSame(callSites[0], callSites[1]);327 assertEquals("안녕", callSites[0][0]);328 assertEquals("\\uc548\\ub155", callSites[0].raw[0]);329 callSites = [];330 tag`\uc548\ub155`;331 tag`안녕`;332 assertEquals(2, callSites.length);333 assertTrue(callSites[0] !== callSites[1]);334 assertEquals("안녕", callSites[0][0]);335 assertEquals("\\uc548\\ub155", callSites[0].raw[0]);336 assertEquals("안녕", callSites[1][0]);337 assertEquals("안녕", callSites[1].raw[0]);338 // Extra-thorough UTF8 decoding test.339 callSites = [];340 tag`Iñtërnâtiônàlizætiøn\u2603\uD83D\uDCA9`;341 tag`Iñtërnâtiônàlizætiøn☃💩`;342 assertEquals(2, callSites.length);343 assertTrue(callSites[0] !== callSites[1]);344 assertEquals("Iñtërnâtiônàlizætiøn☃💩", callSites[0][0]);345 assertEquals(346 "Iñtërnâtiônàlizætiøn\\u2603\\uD83D\\uDCA9", callSites[0].raw[0]);347 assertEquals("Iñtërnâtiônàlizætiøn☃💩", callSites[1][0]);348 assertEquals("Iñtërnâtiônàlizætiøn☃💩", callSites[1].raw[0]);349})();350(function testExtendedArrayPrototype() {351 Object.defineProperty(Array.prototype, 0, {352 set: function() {353 assertUnreachable();354 },355 configurable: true356 });357 function tag(){}358 tag`a${1}b`;359 delete Array.prototype[0];360})();361(function testRawLineNormalization() {362 function raw0(callSiteObj) {363 return callSiteObj.raw[0];364 }365 assertEquals(eval("raw0`\r`"), "\n");366 assertEquals(eval("raw0`\r\n`"), "\n");367 assertEquals(eval("raw0`\r\r\n`"), "\n\n");368 assertEquals(eval("raw0`\r\n\r\n`"), "\n\n");369 assertEquals(eval("raw0`\r\r\r\n`"), "\n\n\n");370})();371(function testHarmonyUnicode() {372 function raw0(callSiteObj) {373 return callSiteObj.raw[0];374 }375 assertEquals(raw0`a\u{62}c`, "a\\u{62}c");376 assertEquals(raw0`a\u{000062}c`, "a\\u{000062}c");377 assertEquals(raw0`a\u{0}c`, "a\\u{0}c");378 assertEquals(`a\u{62}c`, "abc");379 assertEquals(`a\u{000062}c`, "abc");380})();381(function testLiteralAfterRightBrace() {382 // Regression test for https://code.google.com/p/v8/issues/detail?id=3734383 function f() {}384 `abc`;385 function g() {}`def`;386 {387 // block388 }389 `ghi`;390 {391 // block392 }`jkl`;393})();394(function testLegacyOctal() {395 assertEquals('\u0000', `\0`);396 assertEquals('\u0000a', `\0a`);397 for (var i = 0; i < 8; i++) {398 var code = "`\\0" + i + "`";399 assertThrows(code, SyntaxError);400 code = "(function(){})" + code;401 assertThrows(code, SyntaxError);402 }403 assertEquals('\\0', String.raw`\0`);404})();405(function testSyntaxErrorsNonEscapeCharacter() {406 assertThrows("`\\x`", SyntaxError);407 assertThrows("`\\u`", SyntaxError);408 for (var i = 1; i < 8; i++) {409 var code = "`\\" + i + "`";410 assertThrows(code, SyntaxError);411 code = "(function(){})" + code;412 assertThrows(code, SyntaxError);413 }414})();415(function testValidNumericEscapes() {416 assertEquals("8", `\8`);417 assertEquals("9", `\9`);418 assertEquals("\u00008", `\08`);419 assertEquals("\u00009", `\09`);420})();421(function testLegacyOctalEscapesInExpressions() {422 // Allowed in sloppy expression423 assertEquals("\x07", `${"\07"}`);424 // Disallowed in template tail425 assertThrows("`${\"\\07\"}\\07`", SyntaxError);426 // Disallowed in strict expression427 assertThrows("`${(function() { \"use strict\"; return \"\\07\"; })()}`",428 SyntaxError);429})();430var global = this;431(function testCallNew() {432 "use strict";433 var called = false;434 var calledWith;435 global.log = function(x) { called = true; calledWith = x; }436 assertInstanceof(new Function`log("test")`, Object);437 assertTrue(called);438 assertSame("test", calledWith);439 delete global.log;440})();441(function testCallNew2() {442 "use strict";443 var log = [];444 function tag(x) {445 log.push(x);446 if (!(this instanceof tag)) {447 return tag;448 }449 this.x = x === void 0 ? null : x;450 return this;451 }452 // No arguments passed to constructor453 var instance = new tag`x``y``z`;454 assertInstanceof(instance, tag);455 assertSame(tag.prototype, Object.getPrototypeOf(instance));456 assertEquals({ x: null }, instance);457 assertEquals([["x"], ["y"], ["z"], undefined], log);458 // Arguments passed to constructor459 log.length = 0;460 instance = new tag`x2` `y2` `z2` (`test`);461 assertInstanceof(instance, tag);462 assertSame(tag.prototype, Object.getPrototypeOf(instance));463 assertEquals({ x: "test" }, instance);464 assertEquals([["x2"], ["y2"], ["z2"], "test"], log);465})();466(function testCallResultOfTagFn() {467 "use strict";468 var i = 0;469 var raw = [];470 function tag(cs) {471 var args = Array.prototype.slice.call(arguments);472 var text = String.raw.apply(null, args);473 if (i++ < 2) {474 raw.push("tag;" + text);475 return tag;476 }477 raw.push("raw;" + text);478 return text;479 }480 assertEquals("test3", tag`test1``test2``test3`);481 assertEquals([482 "tag;test1",483 "tag;test2",484 "raw;test3"485 ], raw);486})();487(function testReturnValueAsTagFn() {488 "use strict";489 var i = 0;490 function makeTag() {491 return function tag(cs) {492 var args = Array.prototype.slice.call(arguments, 1);493 var rcs = [];494 rcs.raw = cs.map(function(s) {495 return '!' + s + '!';496 });497 args.unshift(rcs);498 return String.raw.apply(null, args);499 }500 }501 assertEquals('!hi!', makeTag()`hi`);502 assertEquals('!test!0!test!', makeTag()`test${0}test`);503 assertEquals('!!', makeTag()``);504});505(function testToStringSubstitutions() {506 var a = {507 toString: function() { return "a"; },508 valueOf: function() { return "-a-"; }509 };510 var b = {511 toString: function() { return "b"; },512 valueOf: function() { return "-b-"; }513 };514 assertEquals("a", `${a}`);515 assertEquals("ab", `${a}${b}`);516 assertEquals("-a--b-", `${a + b}`);517 assertEquals("-a-", `${a + ""}`);518 assertEquals("1a", `1${a}`);519 assertEquals("1a2", `1${a}2`);520 assertEquals("1a2b", `1${a}2${b}`);521 assertEquals("1a2b3", `1${a}2${b}3`);522})();523(function testToStringSubstitutionsOrder() {524 var subs = [];525 var log = [];526 function getter(name, value) {527 return {528 get: function() {529 log.push("get" + name);530 return value;531 },532 set: function(v) {533 log.push("set" + name);534 }535 };536 }537 Object.defineProperties(subs, {538 0: getter(0, "a"),539 1: getter(1, "b"),540 2: getter(2, "c")541 });542 assertEquals("-a-b-c-", `-${subs[0]}-${subs[1]}-${subs[2]}-`);543 assertArrayEquals(["get0", "get1", "get2"], log);544})();545(function testTaggedToStringSubstitutionsOrder() {546 var subs = [];547 var log = [];548 var tagged = [];549 function getter(name, value) {550 return {551 get: function() {552 log.push("get" + name);553 return value;554 },555 set: function(v) {556 log.push("set" + name);557 }558 };559 }560 Object.defineProperties(subs, {561 0: getter(0, 1),562 1: getter(1, 2),563 2: getter(2, 3)564 });565 function tag(cs) {566 var n_substitutions = arguments.length - 1;567 var n_cooked = cs.length;568 var e = cs[0];569 var i = 0;570 assertEquals(n_cooked, n_substitutions + 1);571 while (i < n_substitutions) {572 var sub = arguments[i++ + 1];573 var tail = cs[i];574 tagged.push(sub);575 e = e.concat(sub, tail);576 }577 return e;578 }579 assertEquals("-1-2-3-", tag`-${subs[0]}-${subs[1]}-${subs[2]}-`);580 assertArrayEquals(["get0", "get1", "get2"], log);581 assertArrayEquals([1, 2, 3], tagged);582 tagged.length = 0;583 log.length = 0;584 assertEquals("-1-", tag`-${subs[0]}-`);585 assertArrayEquals(["get0"], log);586 assertArrayEquals([1], tagged);587})();588// Since the first argument to the tag function is always an array,589// eval calls will always just return that array.590(function testEvalTagStrict() {591 "use strict";592 var f = (x) => eval`a${x}b`;593 var result = f();594 assertEquals(["a", "b"], result);595 assertSame(result, f());596})();597(function testEvalTagSloppy() {598 var f = (x) => eval`a${x}b`;599 var result = f();600 assertEquals(["a", "b"], result);601 assertSame(result, f());...

Full Screen

Full Screen

strict.js

Source:strict.js Github

copy

Full Screen

...3var path = require('path')4var semver = require('semver')5var test = require('tape')6var stackman = require('../')()7test('stackman.callsites()', function (t) {8 var err = new Error('foo')9 stackman.callsites(err, function (err, callsites) {10 t.error(err)11 t.ok(Array.isArray(callsites))12 t.ok(callsites.length > 0, 'should have at least one element')13 callsites.forEach(function (callsite, index) {14 t.equal(typeof callsite.getThis, 'function', 'getThis should be a function')15 t.equal(typeof callsite.getTypeName, 'function', 'getTypeName should be a function')16 t.equal(typeof callsite.getTypeNameSafely, 'function', 'getTypeNameSafely should be a function')17 t.equal(typeof callsite.getFunction, 'function', 'getFunction should be a function')18 t.equal(typeof callsite.getFunctionName, 'function', 'getFunctionName should be a function')19 t.equal(typeof callsite.getFunctionNameSanitized, 'function', 'getFunctionNameSanitized should be a function')20 t.equal(typeof callsite.getMethodName, 'function', 'getMethodName should be a function')21 t.equal(typeof callsite.getFileName, 'function', 'getFileName should be a function')22 t.equal(typeof callsite.getRelativeFileName, 'function', 'getRelativeFileName should be a function')23 t.equal(typeof callsite.getLineNumber, 'function', 'getLineNumber should be a function')24 t.equal(typeof callsite.getColumnNumber, 'function', 'getColumnNumber should be a function')25 t.equal(typeof callsite.getEvalOrigin, 'function', 'getEvalOrigin should be a function')26 t.equal(typeof callsite.getModuleName, 'function', 'getModuleName should be a function')27 t.equal(typeof callsite.isToplevel, 'function', 'isToplevel should be a function')28 t.equal(typeof callsite.isEval, 'function', 'isEval should be a function')29 t.equal(typeof callsite.isNative, 'function', 'isNative should be a function')30 t.equal(typeof callsite.isConstructor, 'function', 'isConstructor should be a function')31 t.equal(typeof callsite.isApp, 'function', 'isApp should be a function')32 t.equal(typeof callsite.isModule, 'function', 'isModule should be a function')33 t.equal(typeof callsite.isNode, 'function', 'isNode should be a function')34 t.equal(typeof callsite.sourceContext, 'function', 'sourceContext should be a function')35 })36 t.end()37 })38})39test('callsite.getThis()', function (t) {40 var err = new Error('foo')41 var self = this42 stackman.callsites(err, function (err, callsites) {43 t.error(err)44 var callsite = callsites[0]45 if (semver.gte(process.version, '0.12.0')) {46 t.equal(callsite.getThis(), undefined)47 } else {48 t.equal(callsite.getThis(), self)49 }50 t.end()51 })52})53test('callsite.getTypeName()', function (t) {54 var err = new Error('foo')55 stackman.callsites(err, function (err, callsites) {56 t.error(err)57 t.equal(callsites[0].getTypeName(), 'Test')58 t.end()59 })60})61test('callsite.getTypeNameSafely()', function (t) {62 // TODO: It would be nice if we could get the non-safe version to63 // throw in a test64 var err = new Error('foo')65 stackman.callsites(err, function (err, callsites) {66 t.error(err)67 t.equal(callsites[0].getTypeNameSafely(), 'Test')68 t.end()69 })70})71test('callsite.getFunction()', function fn (t) {72 var err = new Error('foo')73 stackman.callsites(err, function (err, callsites) {74 t.error(err)75 var callsite = callsites[0]76 if (semver.gte(process.version, '0.12.0')) {77 t.equal(callsite.getFunction(), undefined)78 } else {79 t.equal(callsite.getFunction(), fn)80 }81 t.end()82 })83})84test('callsite.getFunctionName() - anonymous', function (t) {85 var err = new Error('foo')86 stackman.callsites(err, function (err, callsites) {87 t.error(err)88 t.equal(callsites[0].getFunctionName(), null)89 t.end()90 })91})92test('callsite.getFunctionName() - named', function named (t) {93 var err = new Error('foo')94 stackman.callsites(err, function (err, callsites) {95 t.error(err)96 t.equal(callsites[0].getFunctionName(), 'named')97 t.end()98 })99})100test('callsite.getFunctionNameSanitized() - anonymous', function (t) {101 var err = new Error('foo')102 stackman.callsites(err, function (err, callsites) {103 t.error(err)104 t.equal(callsites[0].getFunctionNameSanitized(), 'Test.<anonymous>')105 t.end()106 })107})108test('callsite.getFunctionNameSanitized() - named', function named (t) {109 var err = new Error('foo')110 stackman.callsites(err, function (err, callsites) {111 t.error(err)112 t.equal(callsites[0].getFunctionNameSanitized(), 'named')113 t.end()114 })115})116test('callsite.getMethodName()', function (t) {117 var err = new Error('foo')118 stackman.callsites(err, function (err, callsites) {119 t.error(err)120 t.equal(callsites[0].getMethodName(), null)121 t.end()122 })123})124test('callsite.getFileName()', function (t) {125 var err = new Error('foo')126 stackman.callsites(err, function (err, callsites) {127 t.error(err)128 t.equal(callsites[0].getFileName(), __filename)129 t.end()130 })131})132test('callsite.getRelativeFileName()', function (t) {133 var err = new Error('foo')134 stackman.callsites(err, function (err, callsites) {135 t.error(err)136 t.equal(callsites[0].getRelativeFileName(), 'test/strict.js')137 t.end()138 })139})140test('callsite.getLineNumber()', function (t) {141 var err = new Error('foo')142 stackman.callsites(err, function (err, callsites) {143 t.error(err)144 t.ok(callsites[0].getLineNumber() > 1)145 t.end()146 })147})148test('callsite.getColumnNumber()', function (t) {149 var err = new Error('foo')150 stackman.callsites(err, function (err, callsites) {151 t.error(err)152 t.equal(callsites[0].getColumnNumber(), 13)153 t.end()154 })155})156test('callsite.getEvalOrigin()', function (t) {157 var err = new Error('foo')158 stackman.callsites(err, function (err, callsites) {159 t.error(err)160 t.equal(callsites[0].getEvalOrigin(), semver.gte(process.version, '12.11.0') ? undefined : __filename)161 t.end()162 })163})164test('callsite.getEvalOrigin()', function (t) {165 var err = eval('new Error(\'foo\')') // eslint-disable-line no-eval166 stackman.callsites(err, function (err, callsites) {167 t.error(err)168 var actual = callsites[0].getEvalOrigin()169 var expected = new RegExp(`^eval at <anonymous> \\(${__filename}:\\d+:\\d+\\)$`)170 t.ok(expected.test(actual), 'should match regex', { actual, expected })171 t.end()172 })173})174test('callsite.getModuleName()', function (t) {175 var err = new Error('foo')176 stackman.callsites(err, function (err, callsites) {177 t.error(err)178 var callsite = callsites[0]179 if (__filename.indexOf(path.sep + 'node_modules' + path.sep) === -1) {180 t.equal(callsite.getModuleName(), null)181 } else {182 t.equal(callsite.getModuleName(), 'stackman')183 }184 t.end()185 })186})187test('callsite.isToplevel()', function (t) {188 var err = new Error('foo')189 stackman.callsites(err, function (err, callsites) {190 t.error(err)191 t.equal(callsites[0].isToplevel(), false)192 t.end()193 })194})195test('callsite.isEval()', function (t) {196 var err = new Error('foo')197 stackman.callsites(err, function (err, callsites) {198 t.error(err)199 t.equal(callsites[0].isEval(), false)200 t.end()201 })202})203test('callsite.isNative()', function (t) {204 var err = new Error('foo')205 stackman.callsites(err, function (err, callsites) {206 t.error(err)207 t.equal(callsites[0].isNative(), false)208 t.end()209 })210})211test('callsite.isConstructor()', function (t) {212 var err = new Error('foo')213 stackman.callsites(err, function (err, callsites) {214 t.error(err)215 t.equal(callsites[0].isConstructor(), false)216 t.end()217 })218})219test('callsite.isApp()', function (t) {220 var err = new Error('foo')221 stackman.callsites(err, function (err, callsites) {222 t.error(err)223 var callsite = callsites[0]224 if (__filename.indexOf(path.sep + 'node_modules' + path.sep) === -1) {225 t.equal(callsite.isApp(), true)226 } else {227 t.equal(callsite.isApp(), false)228 }229 t.end()230 })231})232test('callsite.isModule()', function (t) {233 var err = new Error('foo')234 stackman.callsites(err, function (err, callsites) {235 t.error(err)236 var callsite = callsites[0]237 if (__filename.indexOf(path.sep + 'node_modules' + path.sep) === -1) {238 t.equal(callsite.isModule(), false)239 } else {240 t.equal(callsite.isModule(), true)241 }242 t.end()243 })244})245test('callsite.isNode()', function (t) {246 var err = new Error('foo')247 stackman.callsites(err, function (err, callsites) {248 t.error(err)249 t.equal(callsites[0].isNode(), false)250 t.end()251 })252})253test('callsite.sourceContext()', function (t) {254 var err = new Error()255 stackman.callsites(err, function (err, callsites) {256 t.error(err)257 callsites[0].sourceContext(function (err, context) {258 t.error(err)259 t.equal(typeof context, 'object')260 t.equal(typeof context.line, 'string')261 t.ok(Array.isArray(context.pre), 'should be an array')262 t.ok(Array.isArray(context.post), 'should be an array')263 t.equal(context.pre.length, 2)264 t.equal(context.post.length, 2)265 t.equal(context.line.trim(), 'var err = new Error()')266 t.end()267 })268 })269})270test('callsite.sourceContext() - node core', function (t) {271 var err = new Error()272 stackman.callsites(err, function (err, callsites) {273 t.error(err)274 var callsite = callsites[0]275 Object.defineProperty(callsite, 'isNode', {276 writable: true,277 value: function () { return true }278 })279 callsites[0].sourceContext(function (err, context) {280 t.equal(err.message, 'Can\'t get source context of a Node core callsite')281 t.end()282 })283 })284})285test('callsite.sourceContext(0)', function (t) {286 var err = new Error()287 stackman.callsites(err, function (err, callsites) {288 t.error(err)289 callsites[0].sourceContext(0, function (err, context) {290 t.equal(err.message, 'Cannot collect less than one line of source context')291 t.notOk(context)292 t.end()293 })294 })295})296test('callsite.sourceContext(1)', function (t) {297 var err = new Error()298 stackman.callsites(err, function (err, callsites) {299 t.error(err)300 callsites[0].sourceContext(1, function (err, context) {301 t.error(err)302 t.equal(context.pre.length, 0)303 t.equal(context.line.trim(), 'var err = new Error()')304 t.equal(context.post.length, 0)305 t.end()306 })307 })308})309test('callsite.sourceContext(2)', function (t) {310 // line before311 var err = new Error()312 stackman.callsites(err, function (err, callsites) {313 t.error(err)314 callsites[0].sourceContext(2, function (err, context) {315 t.error(err)316 t.equal(context.pre.length, 1)317 t.equal(context.pre[0].trim(), '// line before')318 t.equal(context.line.trim(), 'var err = new Error()')319 t.equal(context.post.length, 0)320 t.end()321 })322 })323})324test('callsite.sourceContext(3)', function (t) {325 // line before326 var err = new Error()327 // line after328 stackman.callsites(err, function (err, callsites) {329 t.error(err)330 callsites[0].sourceContext(3, function (err, context) {331 t.error(err)332 t.equal(context.pre.length, 1)333 t.equal(context.pre[0].trim(), '// line before')334 t.equal(context.line.trim(), 'var err = new Error()')335 t.equal(context.post.length, 1)336 t.equal(context.post[0].trim(), '// line after')337 t.end()338 })339 })340})341test('callsite.sourceContext(4)', function (t) {342 // line before 2343 // line before 1344 var err = new Error()345 // line after346 stackman.callsites(err, function (err, callsites) {347 t.error(err)348 callsites[0].sourceContext(4, function (err, context) {349 t.error(err)350 t.equal(context.pre.length, 2)351 t.equal(context.pre[0].trim(), '// line before 2')352 t.equal(context.pre[1].trim(), '// line before 1')353 t.equal(context.line.trim(), 'var err = new Error()')354 t.equal(context.post.length, 1)355 t.equal(context.post[0].trim(), '// line after')356 t.end()357 })358 })359})360test('stackman.properties()', function (t) {361 fs.readFile('./no_such_file', function (err) {362 err.foo = 'bar'363 var props = stackman.properties(err)364 t.equal(props.errno, err.errno)365 t.equal(props.code, 'ENOENT')366 t.equal(props.path, './no_such_file')367 t.equal(props.foo, 'bar')368 t.end()369 })370})371test('stackman.sourceContexts(callsites)', function (t) {372 var err = new Error()373 stackman.callsites(err, function (err, callsites) {374 t.error(err)375 stackman.sourceContexts(callsites, function (err, contexts) {376 t.error(err)377 contexts.forEach(function (context, index) {378 var callsite = callsites[index]379 if (callsite.isNode()) {380 t.equal(context, null)381 } else {382 t.equal(typeof context, 'object')383 t.equal(typeof context.line, 'string')384 t.ok(Array.isArray(context.pre), 'should be an array')385 t.ok(Array.isArray(context.post), 'should be an array')386 t.equal(context.pre.length, 2)387 t.equal(context.post.length, 2)388 }389 })390 t.end()391 })392 })393})394test('stackman.sourceContexts(callsites, {lines: 7})', function (t) {395 var err = new Error()396 stackman.callsites(err, function (err, callsites) {397 t.error(err)398 stackman.sourceContexts(callsites, { lines: 7 }, function (err, contexts) {399 t.error(err)400 contexts.forEach(function (context, index) {401 var callsite = callsites[index]402 if (callsite.isNode()) {403 t.equal(context, null)404 } else {405 t.equal(typeof context, 'object')406 t.equal(typeof context.line, 'string')407 t.ok(Array.isArray(context.pre), 'should be an array')408 t.ok(Array.isArray(context.post), 'should be an array')409 t.equal(context.pre.length, 3)410 t.equal(context.post.length, 3)411 }412 })413 t.end()414 })415 })416})417test('stackman.sourceContexts(callsites, {inAppLines: 7, libraryLines: 3})', function (t) {418 var err = new Error()419 stackman.callsites(err, function (err, callsites) {420 t.error(err)421 stackman.sourceContexts(callsites, { inAppLines: 7, libraryLines: 3 }, function (err, contexts) {422 t.error(err)423 contexts.forEach(function (context, index) {424 var callsite = callsites[index]425 if (callsite.isNode()) {426 t.equal(context, null)427 } else {428 t.equal(typeof context, 'object')429 t.equal(typeof context.line, 'string')430 t.ok(Array.isArray(context.pre), 'should be an array')431 t.ok(Array.isArray(context.post), 'should be an array')432 if (callsite.isApp()) {433 t.equal(context.pre.length, 3)434 t.equal(context.post.length, 3)435 } else {436 t.equal(context.pre.length, 1)437 t.equal(context.post.length, 1)438 }439 }440 })441 t.end()442 })443 })444})445test('stackman.sourceContexts(callsites, {libraryLines: 0})', function (t) {446 var err = new Error()447 stackman.callsites(err, function (err, callsites) {448 t.error(err)449 stackman.sourceContexts(callsites, { libraryLines: 0 }, function (err, contexts) {450 t.error(err)451 contexts.forEach(function (context, index) {452 var callsite = callsites[index]453 if (callsite.isApp()) {454 t.equal(typeof context, 'object')455 t.equal(typeof context.line, 'string')456 t.ok(Array.isArray(context.pre), 'should be an array')457 t.ok(Array.isArray(context.post), 'should be an array')458 t.equal(context.pre.length, 2)459 t.equal(context.post.length, 2)460 } else {461 t.equal(context, null)...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var callsites = require('callsites');2var stack = callsites();3console.log(stack[1].getFileName());4console.log(stack[1].getFunctionName());5console.log(stack[1].getMethodName());6console.log(stack[1].getLineNumber());7console.log(stack[1].getColumnNumber());8console.log(stack[1].getThis());9console.log(stack[1].getTypeName());10console.log(stack[1].isConstructor());11console.log(stack[1].isToplevel());12console.log(stack[1].isEval());13console.log(stack[1].isNative());14console.log(stack[1].isAsync());15console.log(stack[1].isPromiseAll());16console.log(stack[1].isPromiseThen());17console.log(stack[1].getEvalOrigin());18console.log(stack[1].getFileName());19console.log(stack[1].getFunction());20console.log(stack[1].getFunctionDeclaration());21console.log(stack[1].getEvalOrigin());22console.log(stack[1].getThis());23console.log(stack[1].getTypeName());24console.log(stack[1].isConstructor());25console.log(stack[1].isToplevel());26console.log(stack[1].isEval());27console.log(stack[1].isNative());28console.log(stack[1].isAsync());29console.log(stack[1].isPromiseAll());30console.log(stack[1].isPromiseThen());31console.log(stack[1].getEvalOrigin());32console.log(stack[1].toString());33console.log(stack[1].getColumnNumber());34console.log(stack[1].getLineNumber());35console.log(stack[1].getMethodName());36console.log(stack[1].getFunctionName());37console.log(stack[1].getFileName());38var callsites = require('callsites');39var stack = callsites();40console.log(stack[1].getFileName());41console.log(stack[1].getFunctionName());42console.log(stack[1].getMethodName());43console.log(stack[1].getLineNumber());44console.log(stack[1].getColumnNumber());45console.log(stack[1].getThis());46console.log(stack[1].getTypeName());47console.log(stack[1].isConstructor());48console.log(stack[1].isToplevel());49console.log(stack[1].isEval());50console.log(stack[1].isNative());51console.log(stack[1].isAsync());52console.log(stack[1].isPromiseAll());

Full Screen

Using AI Code Generation

copy

Full Screen

1var callsites = require('callsites');2var stack = callsites();3console.log(stack[0].getFileName());4console.log(stack[0].getLineNumber());5console.log(stack[0].getColumnNumber());6var callsites = require('callsites');7var stack = callsites();8console.log(stack[0].getFileName());9console.log(stack[0].getLineNumber());10console.log(stack[0].getColumnNumb

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('ava');2const path = require('path');3const callsites = require('callsites');4test('my passing test', t => {5 const stack = callsites();6 const callerPath = stack[1].getFileName();7 const callerDir = path.dirname(callerPath);8 t.is(callerDir, '/Users/username/ava-test');9});

Full Screen

Using AI Code Generation

copy

Full Screen

1import test from 'ava'2import callsites from 'callsites'3import path from 'path'4test('callsites', t => {5 const stack = callsites()6 stack.forEach((site, i) => {7 console.log(i, site.getFileName(), site.getFunctionName())8 })9 t.pass()10})11import test from 'ava'12import path from 'path'13test('callsites', t => {14 t.pass()15})

Full Screen

Using AI Code Generation

copy

Full Screen

1var stackTrace = require('stack-trace');2var trace = stackTrace.get();3var trace = require('trace');4var callsites = trace.getCallsites();5var stackTrace = require('stack-trace');6var trace = stackTrace.get();7var trace = require('trace');8var callsites = trace.getCallsites();9var stackTrace = require('stack-trace');10var trace = stackTrace.get();11var trace = require('trace');12var callsites = trace.getCallsites();13var stackTrace = require('stack-trace');14var trace = stackTrace.get();15var trace = require('trace');16var callsites = trace.getCallsites();17var stackTrace = require('stack-trace');18var trace = stackTrace.get();19var trace = require('trace');20var callsites = trace.getCallsites();21var stackTrace = require('stack-trace');22var trace = stackTrace.get();23var trace = require('trace');24var callsites = trace.getCallsites();

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('ava');2const {callsites} = require('callsites');3test('test', t => {4 t.is(callsites()[0].getFileName(), __filename);5});6const test = require('ava');7const {callsites} = require('callsites');8test('test', t => {9 t.is(callsites()[0].getFileName(), __filename);10});11const test = require('ava');12const {callsites} = require('callsites');13test('test', t => {14 t.is(callsites()[0].getFileName(), __filename);15});16const test = require('ava');17const {callsites} = require('callsites');18test('test', t => {19 t.is(callsites()[0].getFileName(), __filename);20});21const test = require('ava');22const {callsites} = require('callsites');23test('test', t => {24 t.is(callsites()[0].getFileName(), __filename);25});26const test = require('ava');27const {callsites} = require('callsites');28test('test', t => {29 t.is(callsites()[0].getFileName(), __filename);30});31const test = require('ava');32const {callsites} = require('callsites');33test('test', t => {34 t.is(callsites()[0].getFileName(), __filename);35});36const test = require('ava');37const {callsites} = require('callsites');38test('test', t => {39 t.is(callsites()[0].getFileName(), __filename);40});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ava automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful