Best JavaScript code snippet using sinon
slim.js
Source:slim.js
1// CodeMirror, copyright (c) by Marijn Haverbeke and others2// Distributed under an MIT license: http://codemirror.net/LICENSE3// Slim Highlighting for CodeMirror copyright (c) HicknHack Software Gmbh4(function(mod) {5 if (typeof exports == "object" && typeof module == "object") // CommonJS6 mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"), require("../ruby/ruby"));7 else if (typeof define == "function" && define.amd) // AMD8 define(["../../lib/codemirror", "../htmlmixed/htmlmixed", "../ruby/ruby"], mod);9 else // Plain browser env10 mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13 CodeMirror.defineMode("slim", function(config) {14 var htmlMode = CodeMirror.getMode(config, {name: "htmlmixed"});15 var rubyMode = CodeMirror.getMode(config, "ruby");16 var modes = { html: htmlMode, ruby: rubyMode };17 var embedded = {18 ruby: "ruby",19 javascript: "javascript",20 css: "text/css",21 sass: "text/x-sass",22 scss: "text/x-scss",23 less: "text/x-less",24 styl: "text/x-styl", // no highlighting so far25 coffee: "coffeescript",26 asciidoc: "text/x-asciidoc",27 markdown: "text/x-markdown",28 textile: "text/x-textile", // no highlighting so far29 creole: "text/x-creole", // no highlighting so far30 wiki: "text/x-wiki", // no highlighting so far31 mediawiki: "text/x-mediawiki", // no highlighting so far32 rdoc: "text/x-rdoc", // no highlighting so far33 builder: "text/x-builder", // no highlighting so far34 nokogiri: "text/x-nokogiri", // no highlighting so far35 erb: "application/x-erb"36 };37 var embeddedRegexp = function(map){38 var arr = [];39 for(var key in map) arr.push(key);40 return new RegExp("^("+arr.join('|')+"):");41 }(embedded);42 var styleMap = {43 "commentLine": "comment",44 "slimSwitch": "operator special",45 "slimTag": "tag",46 "slimId": "attribute def",47 "slimClass": "attribute qualifier",48 "slimAttribute": "attribute",49 "slimSubmode": "keyword special",50 "closeAttributeTag": null,51 "slimDoctype": null,52 "lineContinuation": null53 };54 var closing = {55 "{": "}",56 "[": "]",57 "(": ")"58 };59 var nameStartChar = "_a-zA-Z\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD";60 var nameChar = nameStartChar + "\\-0-9\xB7\u0300-\u036F\u203F-\u2040";61 var nameRegexp = new RegExp("^[:"+nameStartChar+"](?::["+nameChar+"]|["+nameChar+"]*)");62 var attributeNameRegexp = new RegExp("^[:"+nameStartChar+"][:\\."+nameChar+"]*(?=\\s*=)");63 var wrappedAttributeNameRegexp = new RegExp("^[:"+nameStartChar+"][:\\."+nameChar+"]*");64 var classNameRegexp = /^\.-?[_a-zA-Z]+[\w\-]*/;65 var classIdRegexp = /^#[_a-zA-Z]+[\w\-]*/;66 function backup(pos, tokenize, style) {67 var restore = function(stream, state) {68 state.tokenize = tokenize;69 if (stream.pos < pos) {70 stream.pos = pos;71 return style;72 }73 return state.tokenize(stream, state);74 };75 return function(stream, state) {76 state.tokenize = restore;77 return tokenize(stream, state);78 };79 }80 function maybeBackup(stream, state, pat, offset, style) {81 var cur = stream.current();82 var idx = cur.search(pat);83 if (idx > -1) {84 state.tokenize = backup(stream.pos, state.tokenize, style);85 stream.backUp(cur.length - idx - offset);86 }87 return style;88 }89 function continueLine(state, column) {90 state.stack = {91 parent: state.stack,92 style: "continuation",93 indented: column,94 tokenize: state.line95 };96 state.line = state.tokenize;97 }98 function finishContinue(state) {99 if (state.line == state.tokenize) {100 state.line = state.stack.tokenize;101 state.stack = state.stack.parent;102 }103 }104 function lineContinuable(column, tokenize) {105 return function(stream, state) {106 finishContinue(state);107 if (stream.match(/^\\$/)) {108 continueLine(state, column);109 return "lineContinuation";110 }111 var style = tokenize(stream, state);112 if (stream.eol() && stream.current().match(/(?:^|[^\\])(?:\\\\)*\\$/)) {113 stream.backUp(1);114 }115 return style;116 };117 }118 function commaContinuable(column, tokenize) {119 return function(stream, state) {120 finishContinue(state);121 var style = tokenize(stream, state);122 if (stream.eol() && stream.current().match(/,$/)) {123 continueLine(state, column);124 }125 return style;126 };127 }128 function rubyInQuote(endQuote, tokenize) {129 // TODO: add multi line support130 return function(stream, state) {131 var ch = stream.peek();132 if (ch == endQuote && state.rubyState.tokenize.length == 1) {133 // step out of ruby context as it seems to complete processing all the braces134 stream.next();135 state.tokenize = tokenize;136 return "closeAttributeTag";137 } else {138 return ruby(stream, state);139 }140 };141 }142 function startRubySplat(tokenize) {143 var rubyState;144 var runSplat = function(stream, state) {145 if (state.rubyState.tokenize.length == 1 && !state.rubyState.context.prev) {146 stream.backUp(1);147 if (stream.eatSpace()) {148 state.rubyState = rubyState;149 state.tokenize = tokenize;150 return tokenize(stream, state);151 }152 stream.next();153 }154 return ruby(stream, state);155 };156 return function(stream, state) {157 rubyState = state.rubyState;158 state.rubyState = rubyMode.startState();159 state.tokenize = runSplat;160 return ruby(stream, state);161 };162 }163 function ruby(stream, state) {164 return rubyMode.token(stream, state.rubyState);165 }166 function htmlLine(stream, state) {167 if (stream.match(/^\\$/)) {168 return "lineContinuation";169 }170 return html(stream, state);171 }172 function html(stream, state) {173 if (stream.match(/^#\{/)) {174 state.tokenize = rubyInQuote("}", state.tokenize);175 return null;176 }177 return maybeBackup(stream, state, /[^\\]#\{/, 1, htmlMode.token(stream, state.htmlState));178 }179 function startHtmlLine(lastTokenize) {180 return function(stream, state) {181 var style = htmlLine(stream, state);182 if (stream.eol()) state.tokenize = lastTokenize;183 return style;184 };185 }186 function startHtmlMode(stream, state, offset) {187 state.stack = {188 parent: state.stack,189 style: "html",190 indented: stream.column() + offset, // pipe + space191 tokenize: state.line192 };193 state.line = state.tokenize = html;194 return null;195 }196 function comment(stream, state) {197 stream.skipToEnd();198 return state.stack.style;199 }200 function commentMode(stream, state) {201 state.stack = {202 parent: state.stack,203 style: "comment",204 indented: state.indented + 1,205 tokenize: state.line206 };207 state.line = comment;208 return comment(stream, state);209 }210 function attributeWrapper(stream, state) {211 if (stream.eat(state.stack.endQuote)) {212 state.line = state.stack.line;213 state.tokenize = state.stack.tokenize;214 state.stack = state.stack.parent;215 return null;216 }217 if (stream.match(wrappedAttributeNameRegexp)) {218 state.tokenize = attributeWrapperAssign;219 return "slimAttribute";220 }221 stream.next();222 return null;223 }224 function attributeWrapperAssign(stream, state) {225 if (stream.match(/^==?/)) {226 state.tokenize = attributeWrapperValue;227 return null;228 }229 return attributeWrapper(stream, state);230 }231 function attributeWrapperValue(stream, state) {232 var ch = stream.peek();233 if (ch == '"' || ch == "\'") {234 state.tokenize = readQuoted(ch, "string", true, false, attributeWrapper);235 stream.next();236 return state.tokenize(stream, state);237 }238 if (ch == '[') {239 return startRubySplat(attributeWrapper)(stream, state);240 }241 if (stream.match(/^(true|false|nil)\b/)) {242 state.tokenize = attributeWrapper;243 return "keyword";244 }245 return startRubySplat(attributeWrapper)(stream, state);246 }247 function startAttributeWrapperMode(state, endQuote, tokenize) {248 state.stack = {249 parent: state.stack,250 style: "wrapper",251 indented: state.indented + 1,252 tokenize: tokenize,253 line: state.line,254 endQuote: endQuote255 };256 state.line = state.tokenize = attributeWrapper;257 return null;258 }259 function sub(stream, state) {260 if (stream.match(/^#\{/)) {261 state.tokenize = rubyInQuote("}", state.tokenize);262 return null;263 }264 var subStream = new CodeMirror.StringStream(stream.string.slice(state.stack.indented), stream.tabSize);265 subStream.pos = stream.pos - state.stack.indented;266 subStream.start = stream.start - state.stack.indented;267 subStream.lastColumnPos = stream.lastColumnPos - state.stack.indented;268 subStream.lastColumnValue = stream.lastColumnValue - state.stack.indented;269 var style = state.subMode.token(subStream, state.subState);270 stream.pos = subStream.pos + state.stack.indented;271 return style;272 }273 function firstSub(stream, state) {274 state.stack.indented = stream.column();275 state.line = state.tokenize = sub;276 return state.tokenize(stream, state);277 }278 function createMode(mode) {279 var query = embedded[mode];280 var spec = CodeMirror.mimeModes[query];281 if (spec) {282 return CodeMirror.getMode(config, spec);283 }284 var factory = CodeMirror.modes[query];285 if (factory) {286 return factory(config, {name: query});287 }288 return CodeMirror.getMode(config, "null");289 }290 function getMode(mode) {291 if (!modes.hasOwnProperty(mode)) {292 return modes[mode] = createMode(mode);293 }294 return modes[mode];295 }296 function startSubMode(mode, state) {297 var subMode = getMode(mode);298 var subState = subMode.startState && subMode.startState();299 state.subMode = subMode;300 state.subState = subState;301 state.stack = {302 parent: state.stack,303 style: "sub",304 indented: state.indented + 1,305 tokenize: state.line306 };307 state.line = state.tokenize = firstSub;308 return "slimSubmode";309 }310 function doctypeLine(stream, _state) {311 stream.skipToEnd();312 return "slimDoctype";313 }314 function startLine(stream, state) {315 var ch = stream.peek();316 if (ch == '<') {317 return (state.tokenize = startHtmlLine(state.tokenize))(stream, state);318 }319 if (stream.match(/^[|']/)) {320 return startHtmlMode(stream, state, 1);321 }322 if (stream.match(/^\/(!|\[\w+])?/)) {323 return commentMode(stream, state);324 }325 if (stream.match(/^(-|==?[<>]?)/)) {326 state.tokenize = lineContinuable(stream.column(), commaContinuable(stream.column(), ruby));327 return "slimSwitch";328 }329 if (stream.match(/^doctype\b/)) {330 state.tokenize = doctypeLine;331 return "keyword";332 }333 var m = stream.match(embeddedRegexp);334 if (m) {335 return startSubMode(m[1], state);336 }337 return slimTag(stream, state);338 }339 function slim(stream, state) {340 if (state.startOfLine) {341 return startLine(stream, state);342 }343 return slimTag(stream, state);344 }345 function slimTag(stream, state) {346 if (stream.eat('*')) {347 state.tokenize = startRubySplat(slimTagExtras);348 return null;349 }350 if (stream.match(nameRegexp)) {351 state.tokenize = slimTagExtras;352 return "slimTag";353 }354 return slimClass(stream, state);355 }356 function slimTagExtras(stream, state) {357 if (stream.match(/^(<>?|><?)/)) {358 state.tokenize = slimClass;359 return null;360 }361 return slimClass(stream, state);362 }363 function slimClass(stream, state) {364 if (stream.match(classIdRegexp)) {365 state.tokenize = slimClass;366 return "slimId";367 }368 if (stream.match(classNameRegexp)) {369 state.tokenize = slimClass;370 return "slimClass";371 }372 return slimAttribute(stream, state);373 }374 function slimAttribute(stream, state) {375 if (stream.match(/^([\[\{\(])/)) {376 return startAttributeWrapperMode(state, closing[RegExp.$1], slimAttribute);377 }378 if (stream.match(attributeNameRegexp)) {379 state.tokenize = slimAttributeAssign;380 return "slimAttribute";381 }382 if (stream.peek() == '*') {383 stream.next();384 state.tokenize = startRubySplat(slimContent);385 return null;386 }387 return slimContent(stream, state);388 }389 function slimAttributeAssign(stream, state) {390 if (stream.match(/^==?/)) {391 state.tokenize = slimAttributeValue;392 return null;393 }394 // should never happen, because of forward lookup395 return slimAttribute(stream, state);396 }397 function slimAttributeValue(stream, state) {398 var ch = stream.peek();399 if (ch == '"' || ch == "\'") {400 state.tokenize = readQuoted(ch, "string", true, false, slimAttribute);401 stream.next();402 return state.tokenize(stream, state);403 }404 if (ch == '[') {405 return startRubySplat(slimAttribute)(stream, state);406 }407 if (ch == ':') {408 return startRubySplat(slimAttributeSymbols)(stream, state);409 }410 if (stream.match(/^(true|false|nil)\b/)) {411 state.tokenize = slimAttribute;412 return "keyword";413 }414 return startRubySplat(slimAttribute)(stream, state);415 }416 function slimAttributeSymbols(stream, state) {417 stream.backUp(1);418 if (stream.match(/^[^\s],(?=:)/)) {419 state.tokenize = startRubySplat(slimAttributeSymbols);420 return null;421 }422 stream.next();423 return slimAttribute(stream, state);424 }425 function readQuoted(quote, style, embed, unescaped, nextTokenize) {426 return function(stream, state) {427 finishContinue(state);428 var fresh = stream.current().length == 0;429 if (stream.match(/^\\$/, fresh)) {430 if (!fresh) return style;431 continueLine(state, state.indented);432 return "lineContinuation";433 }434 if (stream.match(/^#\{/, fresh)) {435 if (!fresh) return style;436 state.tokenize = rubyInQuote("}", state.tokenize);437 return null;438 }439 var escaped = false, ch;440 while ((ch = stream.next()) != null) {441 if (ch == quote && (unescaped || !escaped)) {442 state.tokenize = nextTokenize;443 break;444 }445 if (embed && ch == "#" && !escaped) {446 if (stream.eat("{")) {447 stream.backUp(2);448 break;449 }450 }451 escaped = !escaped && ch == "\\";452 }453 if (stream.eol() && escaped) {454 stream.backUp(1);455 }456 return style;457 };458 }459 function slimContent(stream, state) {460 if (stream.match(/^==?/)) {461 state.tokenize = ruby;462 return "slimSwitch";463 }464 if (stream.match(/^\/$/)) { // tag close hint465 state.tokenize = slim;466 return null;467 }468 if (stream.match(/^:/)) { // inline tag469 state.tokenize = slimTag;470 return "slimSwitch";471 }472 startHtmlMode(stream, state, 0);473 return state.tokenize(stream, state);474 }475 var mode = {476 // default to html mode477 startState: function() {478 var htmlState = htmlMode.startState();479 var rubyState = rubyMode.startState();480 return {481 htmlState: htmlState,482 rubyState: rubyState,483 stack: null,484 last: null,485 tokenize: slim,486 line: slim,487 indented: 0488 };489 },490 copyState: function(state) {491 return {492 htmlState : CodeMirror.copyState(htmlMode, state.htmlState),493 rubyState: CodeMirror.copyState(rubyMode, state.rubyState),494 subMode: state.subMode,495 subState: state.subMode && CodeMirror.copyState(state.subMode, state.subState),496 stack: state.stack,497 last: state.last,498 tokenize: state.tokenize,499 line: state.line500 };501 },502 token: function(stream, state) {503 if (stream.sol()) {504 state.indented = stream.indentation();505 state.startOfLine = true;506 state.tokenize = state.line;507 while (state.stack && state.stack.indented > state.indented && state.last != "slimSubmode") {508 state.line = state.tokenize = state.stack.tokenize;509 state.stack = state.stack.parent;510 state.subMode = null;511 state.subState = null;512 }513 }514 if (stream.eatSpace()) return null;515 var style = state.tokenize(stream, state);516 state.startOfLine = false;517 if (style) state.last = style;518 return styleMap.hasOwnProperty(style) ? styleMap[style] : style;519 },520 blankLine: function(state) {521 if (state.subMode && state.subMode.blankLine) {522 return state.subMode.blankLine(state.subState);523 }524 },525 innerMode: function(state) {526 if (state.subMode) return {state: state.subState, mode: state.subMode};527 return {state: state, mode: mode};528 }529 //indent: function(state) {530 // return state.indented;531 //}532 };533 return mode;534 }, "htmlmixed", "ruby");535 CodeMirror.defineMIME("text/x-slim", "slim");536 CodeMirror.defineMIME("application/x-slim", "slim");...
Tokenizer.test.js
Source:Tokenizer.test.js
1import {Tokenizer, TokenType} from '../Tokenizer';23describe("Tokenizer",()=>{4 let tokenizer;56 beforeEach(()=>{7 tokenizer=new Tokenizer();8 })910 it("intentionally fail with invalid character",()=>{11 expect(() => {12 tokenizer.tokenize('\\');13 }).toThrow();14 })1516 it("intentionally fail with open ended string",()=>{17 expect(() => {18 tokenizer.tokenize('"');19 }).toThrow();2021 expect(() => {22 tokenizer.tokenize("'");23 }).toThrow();24 })2526 it("intentionally fail with incomplete OR",()=>{27 expect(() => {28 tokenizer.tokenize('|');29 }).toThrow();30 })3132 it("intentionally fail with incomplete AND",()=>{33 expect(() => {34 tokenizer.tokenize('&');35 }).toThrow();36 })37 38 it("whitespace program",()=>{39 expect(tokenizer.tokenize(' ')).toEqual([{line: 1, type: TokenType.NewLine, value: ""}]);40 })4142 it("blank program",()=>{43 expect(tokenizer.tokenize('')).toEqual([{line: 1, type: TokenType.NewLine, value: null}]);44 })4546 it("comment only program has just a newline token with the comment",()=>{47 const commentCode = '//adsasdas';48 let tokenList=tokenizer.tokenize(commentCode);49 expect(tokenList).toEqual([{line: 1, type: TokenType.NewLine, value: commentCode}]);50 })5152 it("LineDelim",()=>{53 expect(tokenizer.tokenize(';')[0]).toEqual({line: 1, type: TokenType.LineDelim, value: null});54 })5556 it("NewLine",()=>{57 expect(tokenizer.tokenize('\n')[0]).toEqual({line: 2, type: TokenType.NewLine, value: ""});58 })5960 it("Double",()=>{61 expect(tokenizer.tokenize('double')[0]).toEqual({line: 1, type: TokenType.Double, value: null});62 })6364 it("String",()=>{65 expect(tokenizer.tokenize('string')[0]).toEqual({line: 1, type: TokenType.String, value: null});66 })6768 it("Bool",()=>{69 expect(tokenizer.tokenize('bool')[0]).toEqual({line: 1, type: TokenType.Bool, value: null});70 })7172 it("DoubleLiteral",()=>{73 expect(tokenizer.tokenize('123.45')[0]).toEqual({line: 1, type: TokenType.DoubleLiteral, value: 123.45});74 expect(tokenizer.tokenize('45')[0]).toEqual({line: 1, type: TokenType.DoubleLiteral, value: 45});75 expect(tokenizer.tokenize('.45')[0]).toEqual({line: 1, type: TokenType.DoubleLiteral, value: .45});76 expect(tokenizer.tokenize('45.')[0]).toEqual({line: 1, type: TokenType.DoubleLiteral, value: 45.});77 })7879 it("StringLiteral",()=>{80 expect(tokenizer.tokenize('""')[0]).toEqual({line: 1, type: TokenType.StringLiteral, value: ""});81 expect(tokenizer.tokenize('"1"')[0]).toEqual({line: 1, type: TokenType.StringLiteral, value: "1"});82 const allCharDQ='`1234567890-=qwertyuiop[]\\asdfghjkl;\'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?';83 expect(tokenizer.tokenize('"'+allCharDQ+'"')[0]).toEqual({line: 1, type: TokenType.StringLiteral, value: allCharDQ});8485 expect(tokenizer.tokenize("''")[0]).toEqual({line: 1, type: TokenType.StringLiteral, value: ""});86 expect(tokenizer.tokenize("'1'")[0]).toEqual({line: 1, type: TokenType.StringLiteral, value: "1"});87 const allCharSQ="`1234567890-=qwertyuiop[]\\asdfghjkl;\"zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?";88 expect(tokenizer.tokenize("'"+allCharSQ+"'")[0]).toEqual({line: 1, type: TokenType.StringLiteral, value: allCharSQ});89 })9091 it("Ident",()=>{92 const expectGoodIdent = (name) => {93 expect(tokenizer.tokenize(name)[0]).toEqual({line: 1, type: TokenType.Ident, value: name});94 }95 const expectBadIdent = (name) => {96 expect(tokenizer.tokenize(name)[0]).not.toEqual({line: 1, type: TokenType.Ident, value: name});97 }9899 expectGoodIdent("a");100 expectGoodIdent("_");101 expectGoodIdent("True");102 expectGoodIdent("False");103 expectGoodIdent("Null");104 expectGoodIdent("_qwertyuiopasdfghjklzxcvbnm_QWERTYUIOPASDFGHJKLZXCVBNM1234567890_");105 expectBadIdent("0NAME");106 expectBadIdent("A.B");107 })108109 it("True",()=>{110 expect(tokenizer.tokenize('true')[0]).toEqual({line: 1, type: TokenType.True, value: null});111 })112113 it("False",()=>{114 expect(tokenizer.tokenize('false')[0]).toEqual({line: 1, type: TokenType.False, value: null});115 })116117 it("Null",()=>{118 expect(tokenizer.tokenize('null')[0]).toEqual({line: 1, type: TokenType.Null, value: null});119 })120121 it("LeftParen",()=>{122 expect(tokenizer.tokenize('(')[0]).toEqual({line: 1, type: TokenType.LeftParen, value: null});123 })124125 it("RightParen",()=>{126 expect(tokenizer.tokenize(')')[0]).toEqual({line: 1, type: TokenType.RightParen, value: null});127 })128129 it("LeftSqaure",()=>{130 expect(tokenizer.tokenize('[')[0]).toEqual({line: 1, type: TokenType.LeftSqaure, value: null});131 })132133 it("RightSqaure",()=>{134 expect(tokenizer.tokenize(']')[0]).toEqual({line: 1, type: TokenType.RightSqaure, value: null});135 })136137 it("Comma",()=>{138 expect(tokenizer.tokenize(',')[0]).toEqual({line: 1, type: TokenType.Comma, value: null});139 })140141 it("Dot",()=>{142 expect(tokenizer.tokenize('.')[0]).toEqual({line: 1, type: TokenType.Dot, value: null});143 })144145 it("Not",()=>{146 expect(tokenizer.tokenize('!')[0]).toEqual({line: 1, type: TokenType.Not, value: null});147 })148149 it("And",()=>{150 expect(tokenizer.tokenize('&&')[0]).toEqual({line: 1, type: TokenType.And, value: null});151 })152153 it("Or",()=>{154 expect(tokenizer.tokenize('||')[0]).toEqual({line: 1, type: TokenType.Or, value: null});155 })156157 it("Plus",()=>{158 expect(tokenizer.tokenize('+')[0]).toEqual({line: 1, type: TokenType.Plus, value: null});159 })160161 it("Minus",()=>{162 expect(tokenizer.tokenize('-')[0]).toEqual({line: 1, type: TokenType.Minus, value: null});163 })164165 it("Divide",()=>{166 expect(tokenizer.tokenize('/')[0]).toEqual({line: 1, type: TokenType.Divide, value: null});167 })168169 it("Multiply",()=>{170 expect(tokenizer.tokenize('*')[0]).toEqual({line: 1, type: TokenType.Multiply, value: null});171 })172173 it("Mod",()=>{174 expect(tokenizer.tokenize('%')[0]).toEqual({line: 1, type: TokenType.Mod, value: null});175 })176177 it("Exponent",()=>{178 expect(tokenizer.tokenize('^')[0]).toEqual({line: 1, type: TokenType.Exponent, value: null});179 })180181 it("Question",()=>{182 expect(tokenizer.tokenize('?')[0]).toEqual({line: 1, type: TokenType.Question, value: null});183 })184185 it("Colon",()=>{186 expect(tokenizer.tokenize(':')[0]).toEqual({line: 1, type: TokenType.Colon, value: null});187 })188189 it("Assignment",()=>{190 expect(tokenizer.tokenize('=')[0]).toEqual({line: 1, type: TokenType.Assignment, value: null});191 })192193 it("Equals",()=>{194 expect(tokenizer.tokenize('==')[0]).toEqual({line: 1, type: TokenType.Equals, value: null});195 })196197 it("NotEquals",()=>{198 expect(tokenizer.tokenize('!=')[0]).toEqual({line: 1, type: TokenType.NotEquals, value: null});199 })200201 it("Lesser",()=>{202 expect(tokenizer.tokenize('<')[0]).toEqual({line: 1, type: TokenType.Lesser, value: null});203 })204205 it("LesserEquals",()=>{206 expect(tokenizer.tokenize('<=')[0]).toEqual({line: 1, type: TokenType.LesserEquals, value: null});207 })208209 it("Greater",()=>{210 expect(tokenizer.tokenize('>')[0]).toEqual({line: 1, type: TokenType.Greater, value: null});211 })212213 it("GreaterEquals",()=>{214 expect(tokenizer.tokenize('>=')[0]).toEqual({line: 1, type: TokenType.GreaterEquals, value: null});215 })216217 it("Min",()=>{218 expect(tokenizer.tokenize('min')[0]).toEqual({line: 1, type: TokenType.Min, value: null});219 })220221 it("Max",()=>{222 expect(tokenizer.tokenize('max')[0]).toEqual({line: 1, type: TokenType.Max, value: null});223 })224225 it("Abs",()=>{226 expect(tokenizer.tokenize('abs')[0]).toEqual({line: 1, type: TokenType.Abs, value: null});227 })228229 it("Clamp",()=>{230 expect(tokenizer.tokenize('clamp')[0]).toEqual({line: 1, type: TokenType.Clamp, value: null});231 })232233 it("Floor",()=>{234 expect(tokenizer.tokenize('floor')[0]).toEqual({line: 1, type: TokenType.Floor, value: null});235 })236237 it("Ceil",()=>{238 expect(tokenizer.tokenize('ceil')[0]).toEqual({line: 1, type: TokenType.Ceil, value: null});239 })240241 it("LCase",()=>{242 expect(tokenizer.tokenize('lcase')[0]).toEqual({line: 1, type: TokenType.LCase, value: null});243 })244245 it("UCase",()=>{246 expect(tokenizer.tokenize('ucase')[0]).toEqual({line: 1, type: TokenType.UCase, value: null});247 })248249 it("Trim",()=>{250 expect(tokenizer.tokenize('trim')[0]).toEqual({line: 1, type: TokenType.Trim, value: null});251 })252253 it("Len",()=>{254 expect(tokenizer.tokenize('len')[0]).toEqual({line: 1, type: TokenType.Len, value: null});255 })256257 it("SubStr",()=>{258 expect(tokenizer.tokenize('substr')[0]).toEqual({line: 1, type: TokenType.SubStr, value: null});259 })260261 it("While",()=>{262 expect(tokenizer.tokenize('while')[0]).toEqual({line: 1, type: TokenType.While, value: null});263 })264265 it("For",()=>{266 expect(tokenizer.tokenize('for')[0]).toEqual({line: 1, type: TokenType.For, value: null});267 })268269 it("Loop",()=>{270 expect(tokenizer.tokenize('loop')[0]).toEqual({line: 1, type: TokenType.Loop, value: null});271 })272273 it("If",()=>{274 expect(tokenizer.tokenize('if')[0]).toEqual({line: 1, type: TokenType.If, value: null});275 })276277 it("Else",()=>{278 expect(tokenizer.tokenize('else')[0]).toEqual({line: 1, type: TokenType.Else, value: null});279 })280281 it("Break",()=>{282 expect(tokenizer.tokenize('break')[0]).toEqual({line: 1, type: TokenType.Break, value: null});283 })284285 it("LeftCurly",()=>{286 expect(tokenizer.tokenize('{')[0]).toEqual({line: 1, type: TokenType.LeftCurly, value: null});287 })288289 it("RightCurly",()=>{290 expect(tokenizer.tokenize('}')[0]).toEqual({line: 1, type: TokenType.RightCurly, value: null});291 })292293 it("Return",()=>{294 expect(tokenizer.tokenize('return')[0]).toEqual({line: 1, type: TokenType.Return, value: null});295 })296297 it("Exit",()=>{298 expect(tokenizer.tokenize('exit')[0]).toEqual({line: 1, type: TokenType.Exit, value: null});299 })
...
Using AI Code Generation
1var chai = require('chai');2var sinon = require('sinon');3var sinonChai = require('sinon-chai');4chai.use(sinonChai);5var expect = chai.expect;6describe('Test', function () {7 it('should have been called once', function () {8 var callback = sinon.spy();9 callback();10 expect(callback).to.have.been.calledOnce;11 });12});13chai.Assertion.addMethod('calledOnce', function () {14 this.assert(15 'expected #{this} to have been called once but it was called #{act} times',16 'expected #{this} to not have been called once but it was',17 );18});19chai.Assertion.addMethod('calledOnce', function () {20 this.assert(21 'expected #{this} to have been called once but it was never called',22 'expected #{this} to not have been called once but it was',23 );24});
Using AI Code Generation
1var sinon = require('sinon');2var n = require('sinon');3var assert = require('assert');4var obj = {5 method: function() {6 return true;7 }8};9var spy = sinon.spy(obj, "method");10obj.method(42);11obj.method(1, 2, 3);12assert(spy.calledWith(42));13assert(spy.calledWith(1, 2, 3));14assert(spy.calledWithMatch(1, 2));15assert(spy.neverCalledWith(5));16assert(spy.alwaysCalledWithMatch(1));17assert(spy.calledOn(obj));18assert(spy.calledOnce);19assert(spy.calledTwice);20assert(spy.calledThrice);21assert(spy.callCount === 2);22assert(spy.firstCall.calledWith(42));23assert(spy.secondCall.calledWith(1, 2, 3));24assert(spy.thirdCall === null);25assert(spy.lastCall.calledWith(1, 2, 3));26spy.restore();27assert(!spy.called);28var sinon = require('sinon');29var n = require('sinon');30var assert = require('assert');31var obj = {32 method: function() {33 return true;34 }35};36var spy = sinon.spy(obj, "method");37obj.method(42);38obj.method(1, 2, 3);39assert(spy.calledWith(42));40assert(spy.calledWith(1, 2, 3));41assert(spy.calledWithMatch(1, 2));42assert(spy.neverCalledWith(5));43assert(spy.alwaysCalledWithMatch(1));44assert(spy.calledOn(obj));45assert(spy.calledOnce);46assert(spy.calledTwice);47assert(spy.calledThrice);48assert(spy.callCount === 2);49assert(spy.firstCall.calledWith(42));50assert(spy.secondCall.calledWith(1, 2, 3));51assert(spy.thirdCall === null);52assert(spy.lastCall.calledWith(1, 2, 3));53spy.restore();54assert(!spy.called);55var sinon = require('sinon');56var n = require('sinon');57var assert = require('assert');58var obj = {59 method: function() {60 return true;61 }62};63var spy = sinon.spy(obj,
Using AI Code Generation
1import chai from 'chai';2import sinonChai from 'sinon-chai';3chai.use(sinonChai);4import { shallowMount } from '@vue/test-utils';5import { expect } from 'chai';6import sinon from 'sinon';7import MessageItem from '@/components/MessageItem.vue';8describe('MessageItem.vue', () => {9 it('renders props.msg when passed', () => {10 const msg = 'new message';11 const wrapper = shallowMount(MessageItem, {12 propsData: { msg },13 });14 expect(wrapper.text()).to.include(msg);15 });16 it('calls deleteMessage when delete button is clicked', () => {17 const spy = sinon.spy();18 const wrapper = shallowMount(MessageItem, {19 propsData: {20 },21 });22 wrapper.find('button').trigger('click');23 expect(spy).to.have.been.calledOnce;24 });25});
Using AI Code Generation
1var sinon = require('sinon');2var n = require('sinon');3var assert = require('assert');4var myObj = {5 myMethod: function () {6 return true;7 }8};9var spy = sinon.spy(myObj, "myMethod");10myObj.myMethod();11assert(spy.calledOnce);12spy.restore();13var stub = sinon.stub(myObj, "myMethod");14stub.returns(false);15assert(!myObj.myMethod());16stub.restore();17var mock = sinon.mock(myObj);18mock.expects("myMethod").once();19myObj.myMethod();20mock.verify();21var sandbox = sinon.sandbox.create();22sandbox.stub(myObj, "myMethod");23sandbox.mock(myObj).expects("myMethod").once();24myObj.myMethod();25sandbox.verifyAndRestore();26var spy = sinon.spy();27spy(42);28sinon.assert.calledWith(spy, 42);29var test = sinon.test(function () {30 var stub = this.stub(myObj, "myMethod");31 myObj.myMethod();32 sinon.assert.called(stub);33});34test();35var testCase = sinon.testCase({36 setUp: function () {37 this.stub = sinon.stub(myObj, "myMethod");38 },39 tearDown: function () {40 this.stub.restore();41 },42 "test example": function () {43 myObj.myMethod();44 sinon.assert.called(this.stub);45 }46});47var sandbox = sinon.createSandbox();48sandbox.stub(myObj, "myMethod");49sandbox.mock(myObj).expects("myMethod").once();50myObj.myMethod();51sandbox.verifyAndRestore();52var fake = sinon.fake();53fake(42);54sinon.assert.calledWith(fake, 42);55var fake = sinon.fake.returns(42);56fake();57sinon.assert.calledWith(fake, 42);58var fake = sinon.fake.yields(42);59fake(function (value) {60 value;61});
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var n = require('../app');4var stub = sinon.stub(n, 'tokenize');5describe('tokenize', function() {6 it('should tokenize a string', function() {7 stub.withArgs('hello world').returns(['hello', 'world']);8 assert.deepEqual(n.tokenize('hello world'), ['hello', 'world']);9 });10});
Using AI Code Generation
1var n = require('sinon');2var m = require('sinon-chai');3var sinon = require('sinon');4var expect = require('chai').expect;5function testFunction(a,b){6 return a+b;7}8describe('testFunction', function(){9 it('should be a function', function(){10 expect(testFunction).to.be.a('function');11 });12 it('should return the sum of the two arguments', function(){13 expect(testFunction(2,3)).to.equal(5);14 });15 it('should return the sum of the two arguments', function(){16 expect(testFunction(2,3)).to.equal(5);17 });18});19function testFunction2(a,b){20 return a+b;21}22describe('testFunction2', function(){23 it('should be a function', function(){24 expect(testFunction2).to.be.a('function');25 });26 it('should return the sum of the two arguments', function(){27 expect(testFunction2(2,3)).to.equal(5);28 });29 it('should return the sum of the two arguments', function(){30 expect(testFunction2(2,3)).to.equal(5);31 });32});33function testFunction3(a,b){34 return a+b;35}36describe('testFunction3', function(){37 it('should be a function', function(){38 expect(testFunction3).to.be.a('function');39 });40 it('should return the sum of the two arguments', function(){41 expect(testFunction3(2,3)).to.equal(5);42 });43 it('should return the sum of the two arguments', function(){44 expect(testFunction3(2,3)).to.equal(5);45 });46});47function testFunction4(a,b){48 return a+b;49}50describe('testFunction4', function(){51 it('should be a function', function(){52 expect(testFunction4).to.be.a('function');53 });54 it('should return the sum of the two arguments', function(){55 expect(testFunction4(2,3)).to.equal(5);56 });57 it('should return the sum of the two arguments', function(){58 expect(testFunction4(2,3)).to.equal(5);59 });60});61function testFunction5(a,b){62 return a+b;63}64describe('testFunction
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!