How to use variableValue method in apickli

Best JavaScript code snippet using apickli

parseTypeScriptFile.js

Source:parseTypeScriptFile.js Github

copy

Full Screen

1const types = require("ast-types");2const fs = require('fs')3const source = fs.readFileSync(process.argv[2]).toString()4// const source = `5//6// const globalVariable = 'test'7//8// const tLiteralVariable = \`9// This is a test.10// This is also a test.11// \`12//13// export const boomtownURL = envUrls[0].boomtown.a[0].lot.of.properties14//15// function testFunction(test: string) {16// const yellow = getYellow()17// const green = 'green'18// }19//20// const [body, email] = generateUser(baseName, i)21//22// const automationListingUrl = \`\${boomtownURL}/api/automations/?sAction=listing\${anotherTest}\`23//24// const thisIsATest = test + test2 + test325//26// `27const recast = require('recast')28const tsParser = require("recast/parsers/typescript")29const ast = recast.parse(source, {30 parser: tsParser31});32let objectJSON = {33 "file": {34 "imports": [],35 "functions": [],36 "variables": []37 }38}39let finalObject = ''40let args = ''41function findAllProperties(mainObject) {42 if (mainObject) {43 if (mainObject.name) {44 finalObject = mainObject.name + '.' + finalObject45 findAllProperties(mainObject.object)46 }47 if (mainObject.property) {48 if (mainObject.property.type === 'NumericLiteral') {49 finalObject = '[' + mainObject.property.value + ']' + '.' + finalObject50 findAllProperties(mainObject.object)51 }52 if (mainObject.property.name) {53 finalObject = mainObject.property.name + '.' + finalObject54 findAllProperties(mainObject.object)55 }56 }57 }58}59// types.visit(ast,{60// visitMemberExpression(path) {61// const finalProperty = path.node.property.name62// findAllProperties(path.node.object)63// finalObject = finalObject + finalProperty64// return false65// },66// })67function parseNonStringTemplateLiteral(declaration, j, variableValue) {68 if (declaration.init.expressions[j]) {69 if (declaration.init.expressions[j].type === 'CallExpression') {70 if (declaration.init.expressions[j].arguments) {71 for (const argument of declaration.init.expressions[j].arguments) {72 args = args + ',' + argument.name73 variableValue = variableValue +74 '${' +75 declaration.init.expressions[j].callee.name +76 `(${args})` +77 '}'78 args = ''79 }80 } else {81 variableValue = variableValue + '${' + declaration.init.expressions[j].callee.name82 }83 } else {84 variableValue = variableValue + '${' + declaration.init.expressions[j].name + '}'85 }86 }87 return variableValue;88}89function parseTemplateLiteral(declaration, variableValue) {90 let j = 091 for (let quasi of declaration.init.quasis) {92 if (quasi.value.cooked === '') {93 variableValue = parseNonStringTemplateLiteral(declaration, j, variableValue);94 j++95 } else if (quasi.value.cooked !== 'undefined') {96 variableValue = variableValue + quasi.value.cooked97 }98 }99 return variableValue;100}101let binaryExpressionValue = ''102function parseBinaryExpression(declaration) {103 const left = declaration.init.left104 const right = declaration.init.right105 for (const l of left) {106 if (l) {107 if (l.type !== 'BinaryExpression') {108 if (l.name) {109 binaryExpressionValue = binaryExpressionValue + l.name110 }111 if (l.value) {112 binaryExpressionValue = binaryExpressionValue + l.value113 }114 if (l.quasis) {115 binaryExpressionValue = binaryExpressionValue + parseTemplateLiteral(l)116 }117 }118 if (l.type === 'BinaryExpression') {119 binaryExpressionValue = binaryExpressionValue + l.operator120 parseBinaryExpression([l.left, l.right], right)121 }122 }123 }124 for (const r of right) {125 if (r) {126 if (r.type !== 'BinaryExpression') {127 if (r.name) {128 binaryExpressionValue = binaryExpressionValue + r.name129 }130 if (r.value) {131 binaryExpressionValue = binaryExpressionValue + r.value132 }133 if (r.quasis) {134 binaryExpressionValue = binaryExpressionValue + parseTemplateLiteral(r)135 }136 }137 if (right.type === 'BinaryExpression') {138 binaryExpressionValue = binaryExpressionValue + r.operator139 parseBinaryExpression(left, [r.left, r.right])140 }141 }142 }143 return binaryExpressionValue144}145function parseDeclaration(declaration, variableType, variableValue) {146 if (declaration.init) {147 variableType = declaration.init.type148 if (declaration.init.value) {149 variableValue = declaration.init.value150 }151 if (declaration.init.type === 'TemplateLiteral') {152 variableValue = parseTemplateLiteral(declaration, variableValue);153 }154 if (declaration.init.type === 'MemberExpression') {155 const finalProperty = declaration.init.property.name156 findAllProperties(declaration.init.object)157 finalObject = finalObject + finalProperty158 variableValue = finalObject159 }160 if (declaration.init.type === 'BinaryExpression') {161 variableValue = parseBinaryExpression(declaration)162 binaryExpressionValue = ''163 }164 if (declaration.init.callee) {165 variableValue = declaration.init.callee.name166 }167 }168 return {variableType, variableValue};169}170function provisionVariable(path, variableName, variableType, variableValue) {171 for (const declaration of path.node.declarations) {172 if (declaration.id.name) {173 variableName = declaration.id.name174 }175 if (declaration.id.elements) {176 for (const element of declaration.id.elements) {177 variableName = variableName + ',' + element.name178 }179 }180 const parsedDeclaration = parseDeclaration(declaration, variableType, variableValue);181 variableType = parsedDeclaration.variableType;182 variableValue = parsedDeclaration.variableValue;183 }184 return {variableName, variableType, variableValue};185}186types.visit(ast, {187 visitVariableDeclaration(path) {188 let variableJSON = {}189 let variableName = ''190 let variableType = ''191 let variableValue = ''192 let variableUsedBy = []193 const variable = provisionVariable(path, variableName, variableType, variableValue);194 variableName = variable.variableName;195 variableType = variable.variableType;196 variableValue = variable.variableValue;197 variableJSON = {198 'name': variableName,199 'type': variableType,200 'value': variableValue201 }202 finalObject = ''203 objectJSON['file']['variables'].push(variableJSON)204 return false205 }206})207types.visit(ast, {208 visitFunction(path) {209 if (path.node.type === 'FunctionDeclaration') {210 let funcJSON = {}211 let funcParams = {}212 let funcReturns = "Nothing"213 let funcCalls = []214 let funcName = 'anonymous'215 if (path.node.id) {216 funcName = path.node.id.name217 }218 for (const param of path.node.params) {219 funcParams[param.name] = param.typeAnnotation.typeAnnotation.type220 }221 for (const block of path.node.body.body) {222 if (block.type === 'ReturnStatement') {223 funcReturns = block.argument.name224 }225 if (block.type === 'ExpressionStatement') {226 if (block.expression.callee) {227 funcCalls.push(block.expression.callee.name)228 }229 }230 if (block.type === 'VariableDeclaration') {231 if (block.declarations) {232 for (const declaration of block.declarations) {233 if (declaration.init.callee) {234 funcCalls.push(declaration.init.callee.name)235 }236 }237 }238 }239 }240 funcJSON = {241 'name': funcName,242 'parameters': funcParams,243 'returns': funcReturns,244 'calls': [...funcCalls]245 }246 objectJSON['file']['functions'].push(funcJSON)247 }248 return false249 }250})251console.log(JSON.stringify(objectJSON))...

Full Screen

Full Screen

config.test.js

Source:config.test.js Github

copy

Full Screen

1import { mergeConfig } from "../config";2import Context from "../context";3jest.mock("../context");4describe("Config", () => {5 describe("mergeConfig()", () => {6 it("should create getters that call context.variable()", (done) => {7 const context = new Context();8 const variableKeys = {9 button: "exp_test_abc",10 "banner.border": "exp_test_ab",11 "banner.size": "exp_test_ab",12 "home.arrow.direction": "exp_test_arrow",13 };14 const expectedValues = {15 button: true,16 "banner.border": 10,17 "banner.size": 812,18 "home.arrow.direction": "up",19 };20 context.variableKeys.mockReturnValue(variableKeys);21 context.variableValue.mockImplementation((key) => expectedValues[key]);22 const previousConfig = {23 button: false,24 banner: {25 size: 420,26 border: 0,27 },28 home: {29 arrow: {30 direction: "down",31 },32 },33 other: "unused",34 };35 const expectedConfig = {36 button: true,37 banner: {38 size: 812,39 border: 10,40 },41 home: {42 arrow: {43 direction: "up",44 },45 },46 other: "unused",47 };48 const actual = mergeConfig(context, previousConfig);49 expect(actual).not.toBe(previousConfig); // should be a clone and new properties are not values, but have accessors50 expect(actual).toMatchObject(expectedConfig);51 expect(context.variableValue).toHaveBeenCalledTimes(4); // called during equality check above52 context.variableValue.mockClear();53 expect(actual.button).toEqual(expectedConfig.button);54 expect(context.variableValue).toHaveBeenCalledTimes(1);55 expect(context.variableValue).toHaveBeenCalledWith("button", previousConfig.button);56 context.variableValue.mockClear();57 expect(actual.banner.border).toEqual(expectedConfig.banner.border);58 expect(context.variableValue).toHaveBeenCalledTimes(1);59 expect(context.variableValue).toHaveBeenCalledWith("banner.border", previousConfig.banner.border);60 context.variableValue.mockClear();61 expect(actual.banner.size).toEqual(expectedConfig.banner.size);62 expect(context.variableValue).toHaveBeenCalledTimes(1);63 expect(context.variableValue).toHaveBeenCalledWith("banner.size", previousConfig.banner.size);64 context.variableValue.mockClear();65 expect(actual.home.arrow.direction).toEqual(expectedConfig.home.arrow.direction);66 expect(context.variableValue).toHaveBeenCalledTimes(1);67 expect(context.variableValue).toHaveBeenCalledWith(68 "home.arrow.direction",69 previousConfig.home.arrow.direction70 );71 context.variableValue.mockClear();72 expect(actual.other).toEqual(expectedConfig.other);73 expect(context.variableValue).not.toHaveBeenCalled();74 done();75 });76 it("should warn about mismatching object merges", (done) => {77 jest.spyOn(console, "warn").mockImplementation(() => {});78 const context = new Context();79 const variableKeys = {80 "button.active": "exp_test_abc",81 "banner.border": "exp_test_ab",82 "banner.size": "exp_test_ab",83 "home.arrow.direction": "exp_test_arrow",84 };85 const expectedValues = {86 "button.active": true,87 "banner.border": 10,88 "banner.size": 812,89 "home.arrow.direction": "up",90 };91 context.variableKeys.mockReturnValue(variableKeys);92 context.variableValue.mockImplementation((key) => expectedValues[key]);93 const previousConfig = {94 button: true,95 banner: {96 size: 420,97 border: 0,98 },99 home: {100 arrow: "down",101 },102 other: "unused",103 };104 const expectedConfig = {105 button: {106 active: true,107 },108 banner: {109 size: 812,110 border: 10,111 },112 home: {113 arrow: {114 direction: "up",115 },116 },117 other: "unused",118 };119 const actual = mergeConfig(context, previousConfig);120 expect(actual).not.toBe(previousConfig); // should be a clone and new properties are not values, but have accessors121 expect(actual).toMatchObject(expectedConfig);122 expect(context.variableValue).toHaveBeenCalledTimes(4); // called during equality check above123 context.variableValue.mockClear();124 expect(console.warn).toHaveBeenCalledTimes(2);125 expect(console.warn).toHaveBeenCalledWith(126 "Config key 'button.active' for experiment 'exp_test_abc' is overriding non-object value at 'button' with an object."127 );128 expect(console.warn).toHaveBeenCalledWith(129 "Config key 'home.arrow.direction' for experiment 'exp_test_arrow' is overriding non-object value at 'home.arrow' with an object."130 );131 expect(actual.button.active).toEqual(expectedConfig.button.active);132 expect(context.variableValue).toHaveBeenCalledTimes(1);133 expect(context.variableValue).toHaveBeenCalledWith("button.active", undefined);134 context.variableValue.mockClear();135 expect(actual.home.arrow.direction).toEqual(expectedConfig.home.arrow.direction);136 expect(context.variableValue).toHaveBeenCalledTimes(1);137 expect(context.variableValue).toHaveBeenCalledWith("home.arrow.direction", undefined);138 context.variableValue.mockClear();139 done();140 });141 it("should error with multiple experiments overriding key", (done) => {142 jest.spyOn(console, "error").mockImplementation(() => {});143 jest.spyOn(console, "warn").mockImplementation(() => {});144 const context = new Context();145 const variableKeys = {146 "button.active": true,147 "banner.border": "exp_test_ab",148 "banner.size": "exp_test_abc",149 "home.arrow": "exp_test_arrow",150 "home.arrow.direction": "exp_test_arrow_direction",151 };152 const expectedValues = {153 "button.active": true,154 "banner.border": 10,155 "banner.size": 812,156 "home.arrow": "up",157 "home.arrow.direction": "up",158 };159 context.variableKeys.mockReturnValue(variableKeys);160 context.variableValue.mockImplementation((key) => expectedValues[key]);161 const previousConfig = {162 button: {163 active: false,164 },165 banner: {166 size: 420,167 border: 0,168 },169 home: {170 arrow: "down",171 },172 other: "unused",173 };174 const expectedConfig = {175 button: {176 active: true,177 },178 banner: {179 size: 812,180 border: 10,181 },182 home: {183 arrow: "up",184 },185 other: "unused",186 };187 const actual = mergeConfig(context, previousConfig);188 expect(actual).not.toBe(previousConfig); // should be a clone and new properties are not values, but have accessors189 expect(actual).toMatchObject(expectedConfig);190 expect(context.variableValue).toHaveBeenCalledTimes(4); // called during equality check above191 context.variableValue.mockClear();192 expect(console.error).toHaveBeenCalledTimes(1);193 expect(console.error).toHaveBeenCalledWith(194 "Config key 'home.arrow' already set by experiment 'exp_test_arrow'."195 );196 done();197 });198 });...

Full Screen

Full Screen

PatternLayoutBase.ts

Source:PatternLayoutBase.ts Github

copy

Full Screen

1import {Layout} from "../model/Layout";2import {ILayout} from "./ILayout";3import {LogMessage} from "../model/LogMessage";4import {DateUtil} from "../util/DateUtil";5import {StringUtil} from "../util/StringUtil";6/**7 *8 * 功能描述: 对日志的匹配和替换9 *10 * @className PatternLayoutBase11 * @projectName type-slf412 * @author yanshaowen13 * @date 2019/3/4 10:4714 */15export class PatternLayoutBase implements ILayout {16 private readonly pattern: string;17 // 匹配key和参数18 private static variableRe = /%[a-zA-Z_]\w*({.*})*/g;19 private static keyRe = /%([a-zA-Z_]\w*)/;20 constructor(layout: Layout) {21 this.pattern = layout.pattern;22 }23 public toString(logMessage: LogMessage): string {24 try {25 return PatternLayoutBase.variableReplace(this.pattern, {26 date: logMessage.startTime,27 level: logMessage.level.name,28 class: logMessage.stackType.className,29 method: logMessage.stackType.methodName,30 file: logMessage.stackType.file,31 line: logMessage.stackType.line,32 row: logMessage.stackType.row,33 msg: logMessage.data,34 n: "\n",35 stack: logMessage.error,36 });37 } catch (e) {38 return e.message + e.stack;39 }40 }41 public static variableReplace(str: string, variables: object) {42 const matchList = StringUtil.match(str, PatternLayoutBase.variableRe);43 if (!Array.isArray(matchList) || matchList.length === 0) {44 return str;45 }46 const matchSet = new Set(matchList);47 let msgResult = "";48 let currentIndex = 0;49 let ofI = 0;50 // 上一次循环是否有引号51 let perHasMark = true;52 for (const v of matchSet) {53 ofI ++;54 const variableKey = v.value.match(PatternLayoutBase.keyRe)[1];55 let variableValue = variables[variableKey];56 if (variableKey === "date") {57 const params = v.value.slice(5, v.value.length);58 if (variableValue instanceof Date) {59 let format = "yyyy-mm-dd HH:mm:ss.S";60 if (params.length > 0) {61 const params1 = params.split("}")[0];62 format = params1.slice(1, params1.length);63 }64 variableValue = DateUtil.format(variableValue, format);65 }66 }67 if (variableValue instanceof Error) {68 const values = [];69 Object.keys(variableValue).map((k) => {70 if (k !== "name") {71 if (typeof variableValue[k] === "object") {72 values.push(`${k}:${JSON.stringify(variableValue[k])}`);73 } else {74 values.push(`${k}:${variableValue[k]}`);75 }76 }77 });78 variableValue = ` ${variableValue.stack}\n${values.toString()} `;79 } else if (typeof variableValue === "object" && "toString" in variableValue) {80 variableValue = variableValue.toString();81 }82 if (typeof variableValue === "string" && variableKey !== "n") {83 variableValue = variableValue.replace(/(\r\n)|(\n)/g, "\\n");84 }85 // msgResult += str.substring(currentIndex, v.index);86 if (!perHasMark) {87 currentIndex++;88 }89 if (variableValue === null || variableValue === undefined) {90 perHasMark = false;91 msgResult += str.substring(currentIndex, v.index - 1);92 msgResult += "null";93 } else if (typeof variableValue === "number" || typeof variableValue === "boolean") {94 perHasMark = false;95 msgResult += str.substring(currentIndex, v.index - 1);96 msgResult += variableValue;97 } else {98 perHasMark = true;99 msgResult += str.substring(currentIndex, v.index);100 try {101 msgResult += variableValue.toString().replace(/[\\]?"/g, "\\\"");102 } catch (e) {103 msgResult += variableValue;104 }105 }106 currentIndex = v.index + v.value.length;107 }108 if (currentIndex < str.length) {109 if (!perHasMark) {110 currentIndex++;111 }112 msgResult += str.substring(currentIndex, str.length);113 }114 return msgResult;115 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var apickli = require('apickli');2var { defineSupportCode } = require('cucumber');3defineSupportCode(function({ Before }) {4 Before(function () {5 this.apickli = new apickli.Apickli('https', 'httpbin.org');6 });7});8var { defineSupportCode } = require('cucumber');9defineSupportCode(function({ Given }) {10 Given('I set header {string} to {string}', function (header, value) {11 this.apickli.addRequestHeader(header, this.apickli.variableValue(value));12 });13});

Full Screen

Using AI Code Generation

copy

Full Screen

1var apickli = require('apickli');2var {defineSupportCode} = require('cucumber');3var apickliApickli = new apickli.Apickli('http', 'localhost:8080');4defineSupportCode(function({Given, When, Then}) {5 Given('I have set a variable named {string} to {string}', function (variableName, variableValue, callback) {6 this.apickli.setGlobalVariable(variableName, variableValue);7 callback();8 });9 When('I set header {string} to the value of the global variable {string}', function (header, variableName, callback) {10 this.apickli.addRequestHeader(header, this.apickli.getGlobalVariable(variableName));11 callback();12 });13 Then('the header {string} should have the value {string}', function (header, value, callback) {14 this.apickli.assertRequestHeaderValue(header, value);15 callback();16 });17});

Full Screen

Using AI Code Generation

copy

Full Screen

1var apickli = require('apickli');2var apickliJson = new apickli.Apickli('http', 'localhost:3000');3var apickliXml = new apickli.Apickli('http', 'localhost:3000');4apickliJson.addRequestHeader('Accept', 'application/json');5apickliXml.addRequestHeader('Accept', 'application/xml');6apickliJson.get('/api/users', function (error, response) {7 if (error) {8 console.log(error);9 } else {10 console.log(apickliJson.getResponseObject().length);11 }12});13apickliXml.get('/api/users', function (error, response) {14 if (error) {15 console.log(error);16 } else {17 console.log(apickliXml.getResponseObject().length);18 }19});20var apickli = require('apickli');21var apickliJson = new apickli.Apickli('http', 'localhost:3000');22var apickliXml = new apickli.Apickli('http', 'localhost:3000');23apickliJson.addRequestHeader('Accept', 'application/json');24apickliXml.addRequestHeader('Accept', 'application/xml');25apickliJson.get('/api/users', function (error, response) {26 if (error) {27 console.log(error);28 } else {29 console.log(apickliJson.variableValue('response.length'));30 }31});32apickliXml.get('/api/users', function (error, response) {33 if (error) {34 console.log(error);35 } else {36 console.log(apickliXml.variableValue('response.length'));37 }38});39var apickli = require('apickli

Full Screen

Using AI Code Generation

copy

Full Screen

1var apickli = require('apickli');2var {defineSupportCode} = require('cucumber');3defineSupportCode(function({Given, When, Then}) {4 Given('I set variable {string} to value {string}', function (variableName, variableValue, callback) {5 this.apickli.storeValueInScenarioScope(variableName, variableValue);6 callback();7 });8});9Given('I set variable {string} to value {string}', function (variableName, variableValue, callback) {10 this.apickli.storeValueInScenarioScope(variableName, variableValue);11 callback();12});

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 apickli 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