How to use createLiteral method in ts-auto-mock

Best JavaScript code snippet using ts-auto-mock

ast_value_spec.ts

Source:ast_value_spec.ts Github

copy

Full Screen

...21}22const host: AstHost<ts.Expression> = new TypeScriptAstHost();23const factory = new TypeScriptAstFactory(/* annotateForClosureCompiler */ false);24const nestedObj = factory.createObjectLiteral([25 {propertyName: 'x', quoted: false, value: factory.createLiteral(42)},26 {propertyName: 'y', quoted: false, value: factory.createLiteral('X')},27]);28const nestedArray =29 factory.createArrayLiteral([factory.createLiteral(1), factory.createLiteral(2)]);30const obj = AstObject.parse<TestObject, ts.Expression>(31 factory.createObjectLiteral([32 {propertyName: 'a', quoted: false, value: factory.createLiteral(42)},33 {propertyName: 'b', quoted: false, value: factory.createLiteral('X')},34 {propertyName: 'c', quoted: false, value: factory.createLiteral(true)},35 {propertyName: 'd', quoted: false, value: nestedObj},36 {propertyName: 'e', quoted: false, value: nestedArray},37 ]),38 host);39describe('AstObject', () => {40 describe('has()', () => {41 it('should return true if the property exists on the object', () => {42 expect(obj.has('a')).toBe(true);43 expect(obj.has('b')).toBe(true);44 expect(obj.has('missing')).toBe(false);45 // @ts-expect-error46 expect(obj.has('x')).toBe(false);47 });48 });49 describe('getNumber()', () => {50 it('should return the number value of the property', () => {51 expect(obj.getNumber('a')).toEqual(42);52 });53 it('should throw an error if the property is not a number', () => {54 // @ts-expect-error55 expect(() => obj.getNumber('b'))56 .toThrowError('Unsupported syntax, expected a numeric literal.');57 });58 });59 describe('getString()', () => {60 it('should return the string value of the property', () => {61 expect(obj.getString('b')).toEqual('X');62 });63 it('should throw an error if the property is not a string', () => {64 // @ts-expect-error65 expect(() => obj.getString('a'))66 .toThrowError('Unsupported syntax, expected a string literal.');67 });68 });69 describe('getBoolean()', () => {70 it('should return the boolean value of the property', () => {71 expect(obj.getBoolean('c')).toEqual(true);72 });73 it('should throw an error if the property is not a boolean', () => {74 // @ts-expect-error75 expect(() => obj.getBoolean('b'))76 .toThrowError('Unsupported syntax, expected a boolean literal.');77 });78 });79 describe('getObject()', () => {80 it('should return an AstObject instance parsed from the value of the property', () => {81 expect(obj.getObject('d')).toEqual(AstObject.parse(nestedObj, host));82 });83 it('should throw an error if the property is not an object expression', () => {84 // @ts-expect-error85 expect(() => obj.getObject('b'))86 .toThrowError('Unsupported syntax, expected an object literal.');87 });88 });89 describe('getArray()', () => {90 it('should return an array of AstValue instances of parsed from the value of the property',91 () => {92 expect(obj.getArray('e')).toEqual([93 new AstValue(factory.createLiteral(1), host),94 new AstValue(factory.createLiteral(2), host)95 ]);96 });97 it('should throw an error if the property is not an array of expressions', () => {98 // @ts-expect-error99 expect(() => obj.getArray('b'))100 .toThrowError('Unsupported syntax, expected an array literal.');101 });102 });103 describe('getOpaque()', () => {104 it('should return the expression value of the property wrapped in a `WrappedNodeExpr`', () => {105 expect(obj.getOpaque('d')).toEqual(jasmine.any(WrappedNodeExpr));106 expect(obj.getOpaque('d').node).toEqual(obj.getNode('d'));107 });108 it('should throw an error if the property does not exist', () => {109 expect(() => obj.getOpaque('missing'))110 .toThrowError(`Expected property 'missing' to be present.`);111 // @ts-expect-error112 expect(() => obj.getOpaque('x')).toThrowError(`Expected property 'x' to be present.`);113 });114 });115 describe('getNode()', () => {116 it('should return the original expression value of the property', () => {117 expect(obj.getNode('a')).toEqual(factory.createLiteral(42));118 });119 it('should throw an error if the property does not exist', () => {120 expect(() => obj.getNode('missing'))121 .toThrowError(`Expected property 'missing' to be present.`);122 // @ts-expect-error123 expect(() => obj.getNode('x')).toThrowError(`Expected property 'x' to be present.`);124 });125 });126 describe('getValue()', () => {127 it('should return the expression value of the property wrapped in an `AstValue`', () => {128 expect(obj.getValue('a')).toEqual(jasmine.any(AstValue));129 expect(obj.getValue('a').getNumber()).toEqual(42);130 });131 it('should throw an error if the property does not exist', () => {132 expect(() => obj.getValue('missing'))133 .toThrowError(`Expected property 'missing' to be present.`);134 // @ts-expect-error135 expect(() => obj.getValue('x')).toThrowError(`Expected property 'x' to be present.`);136 });137 });138 describe('toLiteral()', () => {139 it('should convert the AstObject to a raw object with each property mapped', () => {140 expect(obj.toLiteral(value => value.getOpaque())).toEqual({141 a: obj.getOpaque('a'),142 b: obj.getOpaque('b'),143 c: obj.getOpaque('c'),144 d: obj.getOpaque('d'),145 e: obj.getOpaque('e'),146 });147 });148 });149 describe('toMap()', () => {150 it('should convert the AstObject to a Map with each property mapped', () => {151 expect(obj.toMap(value => value.getOpaque())).toEqual(new Map([152 ['a', obj.getOpaque('a')],153 ['b', obj.getOpaque('b')],154 ['c', obj.getOpaque('c')],155 ['d', obj.getOpaque('d')],156 ['e', obj.getOpaque('e')],157 ]));158 });159 });160});161describe('AstValue', () => {162 function createAstValue<T>(node: ts.Expression): AstValue<T, ts.Expression> {163 return new AstValue<T, ts.Expression>(node, host);164 }165 describe('getSymbolName', () => {166 it('should return the name of an identifier', () => {167 expect(createAstValue(factory.createIdentifier('Foo')).getSymbolName()).toEqual('Foo');168 });169 it('should return the name of a property access', () => {170 const propertyAccess = factory.createPropertyAccess(171 factory.createIdentifier('Foo'), factory.createIdentifier('Bar'));172 expect(createAstValue(propertyAccess).getSymbolName()).toEqual('Bar');173 });174 it('should return null if no symbol name is available', () => {175 expect(createAstValue(factory.createLiteral('a')).getSymbolName()).toBeNull();176 });177 });178 describe('isNumber', () => {179 it('should return true if the value is a number', () => {180 expect(createAstValue(factory.createLiteral(42)).isNumber()).toEqual(true);181 });182 it('should return false if the value is not a number', () => {183 expect(createAstValue(factory.createLiteral('a')).isNumber()).toEqual(false);184 });185 });186 describe('getNumber', () => {187 it('should return the number value of the AstValue', () => {188 expect(createAstValue<number>(factory.createLiteral(42)).getNumber()).toEqual(42);189 });190 it('should throw an error if the property is not a number', () => {191 // @ts-expect-error192 expect(() => createAstValue<string>(factory.createLiteral('a')).getNumber())193 .toThrowError('Unsupported syntax, expected a numeric literal.');194 });195 });196 describe('isString', () => {197 it('should return true if the value is a string', () => {198 expect(createAstValue(factory.createLiteral('a')).isString()).toEqual(true);199 });200 it('should return false if the value is not a string', () => {201 expect(createAstValue(factory.createLiteral(42)).isString()).toEqual(false);202 });203 });204 describe('getString', () => {205 it('should return the string value of the AstValue', () => {206 expect(createAstValue<string>(factory.createLiteral('X')).getString()).toEqual('X');207 });208 it('should throw an error if the property is not a string', () => {209 // @ts-expect-error210 expect(() => createAstValue<number>(factory.createLiteral(42)).getString())211 .toThrowError('Unsupported syntax, expected a string literal.');212 });213 });214 describe('isBoolean', () => {215 it('should return true if the value is a boolean', () => {216 expect(createAstValue(factory.createLiteral(true)).isBoolean()).toEqual(true);217 });218 it('should return false if the value is not a boolean', () => {219 expect(createAstValue(factory.createLiteral(42)).isBoolean()).toEqual(false);220 });221 });222 describe('getBoolean', () => {223 it('should return the boolean value of the AstValue', () => {224 expect(createAstValue<boolean>(factory.createLiteral(true)).getBoolean()).toEqual(true);225 });226 it('should throw an error if the property is not a boolean', () => {227 // @ts-expect-error228 expect(() => createAstValue<number>(factory.createLiteral(42)).getBoolean())229 .toThrowError('Unsupported syntax, expected a boolean literal.');230 });231 });232 describe('isObject', () => {233 it('should return true if the value is an object literal', () => {234 expect(createAstValue(nestedObj).isObject()).toEqual(true);235 });236 it('should return false if the value is not an object literal', () => {237 expect(createAstValue(factory.createLiteral(42)).isObject()).toEqual(false);238 });239 });240 describe('getObject', () => {241 it('should return the AstObject value of the AstValue', () => {242 expect(createAstValue<object>(nestedObj).getObject())243 .toEqual(AstObject.parse(nestedObj, host));244 });245 it('should throw an error if the property is not an object literal', () => {246 // @ts-expect-error247 expect(() => createAstValue<number>(factory.createLiteral(42)).getObject())248 .toThrowError('Unsupported syntax, expected an object literal.');249 });250 });251 describe('isArray', () => {252 it('should return true if the value is an array literal', () => {253 expect(createAstValue(nestedArray).isArray()).toEqual(true);254 });255 it('should return false if the value is not an object literal', () => {256 expect(createAstValue(factory.createLiteral(42)).isArray()).toEqual(false);257 });258 });259 describe('getArray', () => {260 it('should return an array of AstValue objects from the AstValue', () => {261 expect(createAstValue<number[]>(nestedArray).getArray()).toEqual([262 createAstValue(factory.createLiteral(1)),263 createAstValue(factory.createLiteral(2)),264 ]);265 });266 it('should throw an error if the property is not an array', () => {267 // @ts-expect-error268 expect(() => createAstValue<number>(factory.createLiteral(42)).getArray())269 .toThrowError('Unsupported syntax, expected an array literal.');270 });271 });272 describe('isFunction', () => {273 it('should return true if the value is a function expression', () => {274 const funcExpr = factory.createFunctionExpression(275 'foo', [],276 factory.createBlock([factory.createReturnStatement(factory.createLiteral(42))]));277 expect(createAstValue(funcExpr).isFunction()).toEqual(true);278 });279 it('should return false if the value is not a function expression', () => {280 expect(createAstValue(factory.createLiteral(42)).isFunction()).toEqual(false);281 });282 });283 describe('getFunctionReturnValue', () => {284 it('should return the "return value" of the function expression', () => {285 const funcExpr = factory.createFunctionExpression(286 'foo', [],287 factory.createBlock([factory.createReturnStatement(factory.createLiteral(42))]));288 expect(createAstValue<Function>(funcExpr).getFunctionReturnValue())289 .toEqual(createAstValue(factory.createLiteral(42)));290 });291 it('should throw an error if the property is not a function expression', () => {292 // @ts-expect-error293 expect(() => createAstValue<number>(factory.createLiteral(42)).getFunctionReturnValue())294 .toThrowError('Unsupported syntax, expected a function.');295 });296 it('should throw an error if the property is a function expression with no return value',297 () => {298 const funcExpr = factory.createFunctionExpression(299 'foo', [], factory.createBlock([factory.createExpressionStatement(300 factory.createLiteral('do nothing'))]));301 expect(() => createAstValue<Function>(funcExpr).getFunctionReturnValue())302 .toThrowError(303 'Unsupported syntax, expected a function body with a single return statement.');304 });305 });306 describe('isCallExpression', () => {307 it('should return true if the value represents a call expression', () => {308 const callExpr = factory.createCallExpression(factory.createIdentifier('foo'), [], false);309 expect(createAstValue<Function>(callExpr).isCallExpression()).toBe(true);310 });311 it('should return false if the value does not represent a call expression', () => {312 const fooExpr = factory.createIdentifier('foo');313 expect(createAstValue<Function>(fooExpr).isCallExpression()).toBe(false);314 });315 });316 describe('getCallee', () => {317 it('should return the callee expression as a value', () => {318 const callExpr = factory.createCallExpression(factory.createIdentifier('foo'), [], false);319 expect(createAstValue<Function>(callExpr).getCallee())320 .toEqual(createAstValue(factory.createIdentifier('foo')));321 });322 it('should throw an error if the value is not a call expression', () => {323 expect(() => createAstValue<number>(factory.createLiteral(42)).getCallee())324 .toThrowError('Unsupported syntax, expected a call expression.');325 });326 });327 describe('getArguments', () => {328 it('should return the arguments as an array of values', () => {329 const callExpr = factory.createCallExpression(330 factory.createIdentifier('foo'),331 [332 factory.createLiteral(1),333 factory.createLiteral(2),334 ],335 false);336 expect(createAstValue<Function>(callExpr).getArguments()).toEqual([337 createAstValue(factory.createLiteral(1)),338 createAstValue(factory.createLiteral(2)),339 ]);340 });341 it('should throw an error if the value is not a call expression', () => {342 expect(() => createAstValue<number>(factory.createLiteral(42)).getArguments())343 .toThrowError('Unsupported syntax, expected a call expression.');344 });345 });346 describe('getOpaque()', () => {347 it('should return the value wrapped in a `WrappedNodeExpr`', () => {348 expect(createAstValue(factory.createLiteral(42)).getOpaque())349 .toEqual(jasmine.any(WrappedNodeExpr));350 expect(createAstValue(factory.createLiteral(42)).getOpaque().node)351 .toEqual(factory.createLiteral(42));352 });353 });354 describe('getRange()', () => {355 it('should return the source range of the AST node', () => {356 const file = ts.createSourceFile(357 'test.ts', '// preamble\nx = \'moo\';', ts.ScriptTarget.ES2015,358 /* setParentNodes */ true);359 // Grab the `'moo'` string literal from the generated AST360 const stmt = file.statements[0] as ts.ExpressionStatement;361 const mooString =362 (stmt.expression as ts.AssignmentExpression<ts.Token<ts.SyntaxKind.EqualsToken>>).right;363 // Check that this string literal has the expected range.364 expect(createAstValue(mooString).getRange())365 .toEqual({startLine: 1, startCol: 4, startPos: 16, endPos: 21});...

Full Screen

Full Screen

EVMStack.test.ts

Source:EVMStack.test.ts Github

copy

Full Screen

...4 beforeEach(() => {5 stack = new EVMStack()6 })7 test('push', () => {8 stack.push(Word.createLiteral('80'))9 stack.push(Word.createLiteral('81'))10 expect(stack.length()).toEqual(2)11 })12 test('pop', () => {13 stack.push(Word.createLiteral('80'))14 stack.push(Word.createLiteral('81'))15 const pop = stack.pop()16 expect(pop).toEqual(Word.createLiteral('81'))17 expect(stack.length()).toEqual(1)18 })19 test('peek', () => {20 stack.push(Word.createLiteral('80'))21 stack.push(Word.createLiteral('81'))22 const pop = stack.peek()23 expect(pop).toEqual(Word.createLiteral('81'))24 expect(stack.length()).toEqual(2)25 })26 test('get', () => {27 stack.push(Word.createLiteral('80'))28 stack.push(Word.createLiteral('81'))29 stack.push(Word.createLiteral('82'))30 const get0 = stack.get(0)31 const get1 = stack.get(1)32 const get2 = stack.get(2)33 expect(get0).toEqual(Word.createLiteral('82'))34 expect(get1).toEqual(Word.createLiteral('81'))35 expect(get2).toEqual(Word.createLiteral('80'))36 expect(stack.length()).toEqual(3)37 })38 test('put', () => {39 stack.push(Word.createLiteral('01'))40 stack.push(Word.createLiteral('02'))41 stack.push(Word.createLiteral('03'))42 stack.put(2, Word.createLiteral('99'))43 stack.put(0, Word.createLiteral('ff'))44 const top0 = stack.pop()45 const top1 = stack.pop()46 const top2 = stack.pop()47 expect(top0).toEqual(Word.createLiteral('ff'))48 expect(top1).toEqual(Word.createLiteral('02'))49 expect(top2).toEqual(Word.createLiteral('99'))50 })...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createLiteral } from 'ts-auto-mock';2const literal = createLiteral(1);3import { createLiteral } from 'ts-auto-mock';4const literal = createLiteral('1');5export const createLiteral = (value: number) => value;6import { createLiteral } from './test1';7const literal = createLiteral('1');8export const createLiteral = (value: number) => value;9import { createLiteral } from './test1';10const literal = createLiteral('1');11export const createLiteral = (value: number) => value;12import { createLiteral } from './test1';13const literal = createLiteral('1');14export const createLiteral = (value: number) => value;

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createLiteral } from 'ts-auto-mock';2const literal = createLiteral('string');3console.log(literal);4import { createLiteral } from 'ts-auto-mock';5const literal = createLiteral(1);6console.log(literal);7import { createLiteral } from 'ts-auto-mock';8const literal = createLiteral(true);9console.log(literal);10import { createLiteral } from 'ts-auto-mock';11const literal = createLiteral(null);12console.log(literal);13import { createLiteral } from 'ts-auto-mock';14const literal = createLiteral(undefined);15console.log(literal);16import { createLiteral } from 'ts-auto-mock';17const literal = createLiteral(Symbol('test'));18console.log(literal);19import { createLiteral } from 'ts-auto-mock';20const literal = createLiteral([1, 2, 3]);21console.log(literal);22import { createLiteral } from 'ts-auto-mock';23const literal = createLiteral({ a: 1, b: 2, c: 3 });24console.log(literal);25import { createLiteral } from 'ts-auto-mock';26const literal = createLiteral(new Date());27console.log(literal);28import { createLiteral } from 'ts-auto-mock';29const literal = createLiteral(/test/);30console.log(literal);31import { createLiteral } from 'ts-auto-mock';32const literal = createLiteral(new Error());33console.log(literal);

Full Screen

Using AI Code Generation

copy

Full Screen

1const createLiteral = require('ts-auto-mock').createLiteral;2const literal = createLiteral('test');3console.log(literal);4const createLiteral = require('ts-auto-mock').createLiteral;5const literal = createLiteral(1);6console.log(literal);7const createLiteral = require('ts-auto-mock').createLiteral;8const literal = createLiteral(true);9console.log(literal);10const createLiteral = require('ts-auto-mock').createLiteral;11const literal = createLiteral({});12console.log(literal);13const createLiteral = require('ts-auto-mock').createLiteral;14const literal = createLiteral([]);15console.log(literal);16const createLiteral = require('ts-auto-mock').createLiteral;17const literal = createLiteral(null);18console.log(literal);19const createLiteral = require('ts-auto-mock').createLiteral;20const literal = createLiteral(undefined);21console.log(literal);22const createLiteral = require('ts-auto-mock').createLiteral;23const literal = createLiteral(NaN);24console.log(literal);25const createLiteral = require('ts-auto-mock').createLiteral;26const literal = createLiteral(Infinity);27console.log(literal);28const createLiteral = require('ts-auto-mock').createLiteral;29const literal = createLiteral(-Infinity);30console.log(literal);31const createLiteral = require('ts-auto-mock').createLiteral;32const literal = createLiteral(0);33console.log(literal);

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createLiteral } from 'ts-auto-mock';2const literal = createLiteral('test');3const literal2 = createLiteral(1);4const literal3 = createLiteral(true);5const literal4 = createLiteral(undefined);6const literal5 = createLiteral(null);7const literal6 = createLiteral({});8const literal7 = createLiteral([]);9const literal8 = createLiteral(() => {});10const literal9 = createLiteral(Symbol());11const literal10 = createLiteral(new Date());12const literal11 = createLiteral({ test: 'test' });13const literal12 = createLiteral([1, 2, 3]);14const literal13 = createLiteral(new Set());15const literal14 = createLiteral(new Map());16const literal15 = createLiteral(new WeakMap());17const literal16 = createLiteral(new WeakSet());18const literal17 = createLiteral(new Error());19const literal18 = createLiteral(new RegExp('test'));20const literal19 = createLiteral(new Promise(() => {}));21const literal20 = createLiteral(new Int8Array());22const literal21 = createLiteral(new Int16Array());

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createLiteral } from 'ts-auto-mock/extension';2const result = createLiteral('test');3console.log(result);4import { createLiteral } from 'ts-auto-mock/extension';5const result = createLiteral('test');6console.log(result);

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createLiteral } from 'ts-auto-mock';2const literal = createLiteral('string');3const literal = createLiteral(123);4const literal = createLiteral(true);5const literal = createLiteral(null);6const literal = createLiteral(undefined);7const literal = createLiteral(Symbol());8const literal = createLiteral(NaN);9const literal = createLiteral(Infinity);10const literal = createLiteral(-Infinity);11const literal = createLiteral({});12const literal = createLiteral([]);13const literal = createLiteral([1, 2, 3]);14const literal = createLiteral({ key: 'value' });15const literal = createLiteral(new Date());16const literal = createLiteral(/test/);17const literal = createLiteral(new Set());18const literal = createLiteral(new Map());19const literal = createLiteral(new WeakSet());20const literal = createLiteral(new WeakMap());21const literal = createLiteral(new ArrayBuffer(8));22const literal = createLiteral(new SharedArrayBuffer(8));23const literal = createLiteral(new DataView(new ArrayBuffer(8)));

Full Screen

Using AI Code Generation

copy

Full Screen

1const { createLiteral } = require("ts-auto-mock");2const test = createLiteral("test");3const { createLiteral } = require("ts-auto-mock");4const test = createLiteral(10);5const { createLiteral } = require("ts-auto-mock");6const test = createLiteral(true);7const { createLiteral } = require("ts-auto-mock");8const test = createLiteral(null);9const { createLiteral } = require("ts-auto-mock");10const test = createLiteral(undefined);11const { createLiteral } = require("ts-auto-mock");12const test = createLiteral(Symbol("test"));13const { createLiteral } = require("ts-auto-mock");14const test = createLiteral({ test: "test" });15const { createLiteral } = require("ts-auto-mock");16const test = createLiteral([1, 2, 3]);17const { createLiteral } = require("ts-auto-mock");18const test = createLiteral(new Date());19const { createLiteral } = require("ts

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createLiteral } from 'ts-auto-mock';2const example = createLiteral('test');3console.log(example);4import { createLiteral } from 'ts-auto-mock';5const example = createLiteral('test');6console.log(example);

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 ts-auto-mock 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