Skip to content

Commit 0aeabd7

Browse files
committed
feat(random): add support for custom generators
1 parent 893c134 commit 0aeabd7

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

src/random.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict'
33
import { runFolder } from '@nordicsemiconductor/bdd-markdown'
44
import path from 'node:path'
55
import { fileURLToPath } from 'node:url'
6-
import { steps } from './random.js'
6+
import { steps, UUIDv4, email, IMEI } from './random.js'
77
const __dirname = fileURLToPath(new URL('.', import.meta.url))
88

99
void describe('Random', () => {
@@ -12,7 +12,14 @@ void describe('Random', () => {
1212
folder: path.join(__dirname, 'test-data', 'random'),
1313
name: 'Random',
1414
})
15-
runner.addStepRunners(...steps)
15+
runner.addStepRunners(
16+
...steps({
17+
UUIDv4,
18+
email,
19+
IMEI,
20+
color: () => 'red',
21+
}),
22+
)
1623
const ctx = {}
1724
const result = await runner.run(ctx)
1825
assert.equal(result.ok, true)
@@ -30,5 +37,12 @@ void describe('Random', () => {
3037
),
3138
true,
3239
)
40+
assert.equal(
41+
'imei' in ctx &&
42+
typeof ctx.imei === 'string' &&
43+
parseInt(ctx.imei, 10) >= 350006660000000,
44+
true,
45+
)
46+
assert.equal('color' in ctx && ctx.color, 'red')
3347
})
3448
})

src/random.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,36 @@ import {
55
import { Type } from '@sinclair/typebox'
66
import { generateCode } from '@hello.nrfcloud.com/proto/fingerprint'
77

8-
export const email = regExpMatchedStep(
9-
{
10-
regExp: /^I have a random email in `(?<storeName>[^`]+)`$/,
11-
schema: Type.Object({
12-
storeName: Type.String({ minLength: 1 }),
13-
}),
14-
},
15-
async ({ match: { storeName }, log: { progress }, context }) => {
16-
const randomEmail = `${generateCode()}@example.com`
17-
progress(randomEmail)
8+
const random = (
9+
id: string,
10+
generator: () => string,
11+
): StepRunner<Record<string, any>> =>
12+
regExpMatchedStep(
13+
{
14+
regExp: new RegExp(`^I have a random ${id} in \`(?<storeName>[^\`]+)\`$`),
15+
schema: Type.Object({
16+
storeName: Type.String({ minLength: 1 }),
17+
}),
18+
},
19+
async ({ match: { storeName }, log: { progress }, context }) => {
20+
const randomString = generator()
21+
progress(randomString)
1822

19-
context[storeName] = randomEmail
20-
},
21-
)
23+
context[storeName] = randomString
24+
},
25+
)
2226

23-
export const uuidv4 = regExpMatchedStep(
24-
{
25-
regExp: /^I have a random UUIDv4 in `(?<storeName>[^`]+)`$/,
26-
schema: Type.Object({
27-
storeName: Type.String({ minLength: 1 }),
28-
}),
29-
},
30-
async ({ match: { storeName }, log: { progress }, context }) => {
31-
const randomUUIDv4 = crypto.randomUUID()
32-
progress(randomUUIDv4)
27+
export const UUIDv4 = (): string => crypto.randomUUID()
28+
export const email = (): string => `${generateCode()}@example.com`
29+
export const IMEI = (): string =>
30+
(350006660000000 + Math.floor(Math.random() * 10000000)).toString()
3331

34-
context[storeName] = randomUUIDv4
32+
export const steps: (
33+
generators?: Record<string, () => string>,
34+
) => StepRunner<Record<string, any>>[] = (
35+
generators = {
36+
email,
37+
UUIDv4,
38+
IMEI,
3539
},
36-
)
37-
38-
export const steps: StepRunner<Record<string, any>>[] = [email, uuidv4]
40+
) => Object.entries(generators).map(([id, generator]) => random(id, generator))

src/test-data/random/Random.feature.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ Given I have a random email in `email`
77
## Store an id
88

99
Given I have a random UUIDv4 in `uuid`
10+
11+
## Store an IMEI
12+
13+
Given I have a random IMEI in `imei`
14+
15+
## Custom example
16+
17+
Given I have a random color in `color`

0 commit comments

Comments
 (0)