-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathhelpers-eval.spec.ts
More file actions
170 lines (150 loc) · 5.77 KB
/
helpers-eval.spec.ts
File metadata and controls
170 lines (150 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { describe, expect, it, vi } from 'vitest';
import { FakerError, faker } from '../../src';
import { fakeEval } from '../../src/modules/helpers/eval';
describe('fakeEval()', () => {
it('does not allow empty string input', () => {
expect(() => fakeEval('', faker)).toThrow(
new FakerError('Eval expression cannot be empty.')
);
});
it('does not allow empty entrypoints', () => {
expect(() => fakeEval('foobar', faker, [])).toThrow(
new FakerError('Eval entrypoints cannot be empty.')
);
});
it('supports single pattern part invocations', () => {
const actual = fakeEval('string', faker);
expect(actual).toBeTypeOf('object');
expect(actual).toBe(faker.string);
});
it('supports simple method calls', () => {
const spy = vi.spyOn(faker.string, 'numeric');
const actual = fakeEval('string.numeric', faker);
expect(spy).toHaveBeenCalledWith();
expect(actual).toBeTypeOf('string');
expect(actual).toMatch(/^\d$/);
});
it('supports method calls without arguments', () => {
const spy = vi.spyOn(faker.string, 'numeric');
const actual = fakeEval('string.numeric()', faker);
expect(spy).toHaveBeenCalledWith();
expect(actual).toBeTypeOf('string');
expect(actual).toMatch(/^\d$/);
});
it('supports method calls with simple arguments', () => {
const spy = vi.spyOn(faker.string, 'numeric');
const actual = fakeEval('string.numeric(5)', faker);
expect(spy).toHaveBeenCalledWith(5);
expect(actual).toBeTypeOf('string');
expect(actual).toMatch(/^\d{5}$/);
});
it('supports method calls with complex arguments', () => {
const spy = vi.spyOn(faker.string, 'numeric');
const actual = fakeEval(
'string.numeric({ "length": 5, "allowLeadingZeros": true, "exclude": ["5"] })',
faker
);
expect(spy).toHaveBeenCalledWith({
length: 5,
allowLeadingZeros: true,
exclude: ['5'],
});
expect(actual).toBeTypeOf('string');
expect(actual).toMatch(/^[0-46-9]{5}$/);
});
it('supports method calls with multiple arguments', () => {
const spy = vi.spyOn(faker.helpers, 'mustache');
const actual = fakeEval(
'helpers.mustache("{{foo}}", { "foo": "bar" })',
faker
);
expect(spy).toHaveBeenCalledWith('{{foo}}', { foo: 'bar' });
expect(actual).toBeTypeOf('string');
expect(actual).toBe('bar');
});
it('supports method calls with unquoted string argument', () => {
const spy = vi.spyOn(faker.helpers, 'slugify');
const actual = fakeEval('helpers.slugify(This Works)', faker);
expect(spy).toHaveBeenCalledWith('This Works');
expect(actual).toBeTypeOf('string');
expect(actual).toBe('This-Works');
});
it('supports method calls with wrongly quoted argument', () => {
const spy = vi.spyOn(faker.helpers, 'slugify');
const actual = fakeEval("helpers.slugify('')", faker);
expect(spy).toHaveBeenCalledWith("''");
expect(actual).toBeTypeOf('string');
expect(actual).toBe('');
});
it('should be able to return empty strings', () => {
const actual = fakeEval('string.alphanumeric(0)', faker);
expect(actual).toBeTypeOf('string');
expect(actual).toBe('');
});
it('supports returning complex objects', () => {
const actual = fakeEval('airline.airline', faker);
expect(actual).toBeTypeOf('object');
expect(faker.definitions.airline.airline).toContain(actual);
});
it('supports returning lazy results', () => {
faker.rawDefinitions.custom = {
lazy: () => ({
key: 'lazy result',
}),
};
const actual = fakeEval('custom.lazy.key', faker);
expect(actual).toBeTypeOf('string');
expect(actual).toBe('lazy result');
});
it('supports patterns after a function call', () => {
const actual = fakeEval('airline.airline().name', faker);
expect(actual).toBeTypeOf('string');
expect(faker.definitions.airline.airline.map(({ name }) => name)).toContain(
actual
); // function().name
});
it('supports patterns after a function reference', () => {
const actual = fakeEval('airline.airline.iataCode', faker);
expect(actual).toBeTypeOf('string');
expect(
faker.definitions.airline.airline.map(({ iataCode }) => iataCode)
).toContain(actual);
});
it('requires a dot after a function call', () => {
expect(() => fakeEval('airline.airline()iataCode', faker)).toThrow(
new FakerError(
"Expected dot ('.'), open parenthesis ('('), or nothing after function call but got 'i'"
)
);
});
it('requires a function for parameters', () => {
expect(faker.definitions.person.first_name.generic).toBeDefined();
expect(() => fakeEval('person.first_name().generic', faker)).toThrow(
new FakerError("Cannot resolve expression 'person.first_name().generic'")
);
});
it('requires a valid expression (missing value)', () => {
expect(() => fakeEval('foo.bar', faker)).toThrow(
new FakerError("Cannot resolve expression 'foo.bar'")
);
});
it('requires a valid expression (trailing dot)', () => {
expect(() => fakeEval('airline.airline.', faker)).toThrow(
new FakerError("Found dot without property name in 'airline.'")
);
expect(() => fakeEval('airline.airline.()', faker)).toThrow(
new FakerError("Found dot without property name in 'airline.()'")
);
expect(() => fakeEval('airline.airline.().iataCode', faker)).toThrow(
new FakerError("Found dot without property name in 'airline.().iataCode'")
);
});
it('requires a valid expression (unclosed parenthesis)', () => {
expect(() => fakeEval('airline.airline(', faker)).toThrow(
new FakerError("Missing closing parenthesis in '('")
);
expect(() => fakeEval('airline.airline(.iataCode', faker)).toThrow(
new FakerError("Missing closing parenthesis in '(.iataCode'")
);
});
});