forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-internal-validators-validateoneof.js
69 lines (62 loc) · 2.01 KB
/
test-internal-validators-validateoneof.js
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
// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const { validateOneOf } = require('internal/validators');
{
// validateOneOf number incorrect.
const allowed = [2, 3];
assert.throws(() => validateOneOf(1, 'name', allowed), {
code: 'ERR_INVALID_ARG_VALUE',
// eslint-disable-next-line quotes
message: `The argument 'name' must be one of: 2, 3. Received 1`
});
assert.throws(() => validateOneOf(1, 'name', allowed, true), {
code: 'ERR_INVALID_OPT_VALUE',
message: 'The value "1" is invalid for option "name". ' +
'Must be one of: 2, 3'
});
}
{
// validateOneOf number correct.
validateOneOf(2, 'name', [1, 2]);
}
{
// validateOneOf string incorrect.
const allowed = ['b', 'c'];
assert.throws(() => validateOneOf('a', 'name', allowed), {
code: 'ERR_INVALID_ARG_VALUE',
// eslint-disable-next-line quotes
message: `The argument 'name' must be one of: 'b', 'c'. Received 'a'`
});
assert.throws(() => validateOneOf('a', 'name', allowed, true), {
code: 'ERR_INVALID_OPT_VALUE',
// eslint-disable-next-line quotes
message: `The value "a" is invalid for option "name". ` +
"Must be one of: 'b', 'c'",
});
}
{
// validateOneOf string correct.
validateOneOf('two', 'name', ['one', 'two']);
}
{
// validateOneOf Symbol incorrect.
const allowed = [Symbol.for('b'), Symbol.for('c')];
assert.throws(() => validateOneOf(Symbol.for('a'), 'name', allowed), {
code: 'ERR_INVALID_ARG_VALUE',
// eslint-disable-next-line quotes
message: `The argument 'name' must be one of: Symbol(b), Symbol(c). ` +
'Received Symbol(a)'
});
assert.throws(() => validateOneOf(Symbol.for('a'), 'name', allowed, true), {
code: 'ERR_INVALID_OPT_VALUE',
message: 'The value "Symbol(a)" is invalid for option "name". ' +
'Must be one of: Symbol(b), Symbol(c)',
});
}
{
// validateOneOf Symbol correct.
const allowed = [Symbol.for('b'), Symbol.for('c')];
validateOneOf(Symbol.for('b'), 'name', allowed);
}