Skip to content

Commit 2f4ed8f

Browse files
AlexGalichenkoOleksandr_Halichenko
andauthored
added math expression step and 'some' validation (#5)
* added math expression step added some validation * added types to vars Co-authored-by: Oleksandr_Halichenko <[email protected]>
1 parent 3e53df5 commit 2f4ed8f

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ Steps to work with memory module
44
```javascript
55
module.exports = {
66
default: {
7-
require: [
8-
'@qavajs/steps-config-loader',
9-
'@qavajs/steps-memory'
10-
],
11-
7+
require: ['@qavajs/steps-memory']
128
}
139
}
1410
```
15-
## Steps
16-
[Steps](docs/steps.md)

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qavajs/steps-memory",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "steps to perform memory validation",
55
"main": "index.js",
66
"scripts": {

src/index.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,41 @@ import { getValue } from './transformers';
1414
Then(
1515
'I expect {string} {memoryValidation} {string}',
1616
async function (value1: string, validationType: string, value2: string) {
17-
const val1 = await getValue(value1);
18-
const val2 = await getValue(value2);
17+
const val1: any = await getValue(value1);
18+
const val2: any = await getValue(value2);
1919
const validation: Function = getValidation(validationType);
2020
validation(val1, val2);
2121
});
2222

23+
/**
24+
* Verify that at least x elements in array pass validation
25+
* @param {string} arr - arr
26+
* @param {string} validation - validation
27+
* @param {string} expectedValue - expected value
28+
* @example I expect at least 1 element(s) in '$arr' array to be above '$expectedValue'
29+
* @example I expect at least 2 element(s) in '$arr' array to be above '50'
30+
*/
31+
Then(
32+
'I expect at least {int} elements in {string} array {memoryValidation} {string}',
33+
async function (expectedNumber: number, arr: string, validationType: string, expectedValue: string) {
34+
const array: Array<any> = await getValue(arr);
35+
const val: any = await getValue(expectedValue);
36+
const validation: Function = getValidation(validationType);
37+
const failCounter = { fail: 0, pass: 0 };
38+
for (const value of array) {
39+
try {
40+
validation(await value, val);
41+
failCounter.pass++;
42+
} catch (err) {
43+
failCounter.fail++;
44+
}
45+
}
46+
if (failCounter.pass < expectedNumber) {
47+
throw new Error(`Less than ${expectedNumber} pass ${validationType} verification`);
48+
}
49+
}
50+
);
51+
2352
/**
2453
* Verify that every element in array satisfies validation against other value
2554
* @param {string} arr - arr
@@ -31,21 +60,37 @@ Then(
3160
Then(
3261
'I expect every element in {string} array {memoryValidation} {string}',
3362
async function (arr: string, validationType: string, expectedValue: string) {
34-
const array = await getValue(arr);
35-
const val = await getValue(expectedValue);
63+
const array: Array<any> = await getValue(arr);
64+
const val: any = await getValue(expectedValue);
3665
const validation: Function = getValidation(validationType);
3766
for (const value of array) {
3867
validation(await value, val);
3968
}
4069
}
4170
);
4271

72+
/**
73+
* Save result of math expression and save result to memory
74+
* @param expression - string expression
75+
* @param key - key to store value
76+
* @example When I save result of math expression '{$variable} + 42' as 'result'
77+
* @example When I save result of math expression '{$random()} * 100' as 'result'
78+
*/
79+
When(
80+
'I save result of math expression {string} as {string}',
81+
async function (expression: string, key: string) {
82+
const resolvedExpression: string = await getValue(expression);
83+
const exprFn = new Function('return ' + resolvedExpression);
84+
memory.setValue(key, exprFn);
85+
}
86+
);
87+
4388
/**
4489
* Save value to memory
4590
* @param {string} value
4691
* @param {string} key
4792
* @example I save 'value' to memory as 'key'
4893
*/
49-
When('I save {string} to memory as {string}', function (value, key) {
94+
When('I save {string} to memory as {string}', function (value: string, key: string) {
5095
memory.setValue(key, value);
5196
});

0 commit comments

Comments
 (0)