Skip to content

Commit b8307b3

Browse files
committed
test: add tests for safe text transform methods
1 parent ebb9b66 commit b8307b3

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
7+
const {
8+
SafeRegExpPrototypeSymbolMatch,
9+
SafeRegExpPrototypeSymbolReplace,
10+
SafeRegExpPrototypeSymbolSearch,
11+
SafeRegExpPrototypeTest,
12+
SafeRegExpPrototypeSymbolSplit,
13+
SafeStringPrototypeMatch,
14+
SafeStringPrototypeMatchAll,
15+
SafeStringPrototypeSearch,
16+
SafeStringPrototypeReplace,
17+
SafeStringPrototypeReplaceAll,
18+
SafeStringPrototypeSplit,
19+
} = require('internal/test/binding').primordials;
20+
21+
const defineRegExpGetter = (name) =>
22+
Reflect.defineProperty(RegExp.prototype, name, {
23+
get: common.mustNotCall(`get RegExp.prototype.${name}`),
24+
});
25+
26+
RegExp.prototype[Symbol.match] = common.mustNotCall(
27+
'RegExp.prototype[@@match]'
28+
);
29+
RegExp.prototype[Symbol.matchAll] = common.mustNotCall(
30+
'RegExp.prototype[@@matchAll]'
31+
);
32+
RegExp.prototype[Symbol.replace] = common.mustNotCall(
33+
'RegExp.prototype[@@replace]'
34+
);
35+
RegExp.prototype[Symbol.search] = common.mustNotCall(
36+
'RegExp.prototype[@@search]'
37+
);
38+
RegExp.prototype[Symbol.split] = common.mustNotCall(
39+
'RegExp.prototype[@@split]'
40+
);
41+
RegExp.prototype.exec = common.mustNotCall('RegExp.prototype.exec');
42+
[
43+
'flags',
44+
'global',
45+
'ignoreCase',
46+
'multiline',
47+
'dotAll',
48+
'unicode',
49+
'sticky',
50+
].forEach(defineRegExpGetter);
51+
52+
String.prototype[Symbol.match] = common.mustNotCall(
53+
'String.prototype[@@match]'
54+
);
55+
String.prototype[Symbol.matchAll] = common.mustNotCall(
56+
'String.prototype[@@matchAll]'
57+
);
58+
String.prototype[Symbol.replace] = common.mustNotCall(
59+
'String.prototype[@@replace]'
60+
);
61+
String.prototype[Symbol.search] = common.mustNotCall(
62+
'String.prototype[@@search]'
63+
);
64+
String.prototype[Symbol.split] = common.mustNotCall(
65+
'String.prototype[@@split]'
66+
);
67+
68+
{
69+
const expected = ['o'];
70+
expected.groups = undefined;
71+
expected.input = 'foo';
72+
expected.index = 1;
73+
assert.deepStrictEqual(SafeStringPrototypeMatch('foo', 'o'), expected);
74+
}
75+
76+
assert.deepStrictEqual(SafeStringPrototypeMatchAll('foo', 'o'),
77+
['o', 'o']);
78+
79+
assert.strictEqual(SafeStringPrototypeReplace('foo', 'o', 'a'), 'fao');
80+
assert.strictEqual(SafeStringPrototypeReplaceAll('foo', 'o', 'a'), 'faa');
81+
assert.strictEqual(SafeStringPrototypeSearch('foo', 'o'), 1);
82+
assert.deepStrictEqual(SafeStringPrototypeSplit('foo', 'o'), ['f', '', '']);
83+
84+
{
85+
const expected = ['o'];
86+
expected.groups = undefined;
87+
expected.input = 'foo';
88+
expected.index = 1;
89+
assert.deepStrictEqual(SafeRegExpPrototypeSymbolMatch(/o/, 'foo'), expected);
90+
}
91+
92+
assert.strictEqual(SafeRegExpPrototypeSymbolReplace(/o/, 'foo', 'a'), 'fao');
93+
assert.strictEqual(SafeRegExpPrototypeSymbolSearch(/o/, 'foo'), 1);
94+
assert.deepStrictEqual(SafeRegExpPrototypeSymbolSplit(/o/y, 'foo'), ['f', '', '']);
95+
assert.strictEqual(SafeRegExpPrototypeTest(/o/, 'foo'), true);

0 commit comments

Comments
 (0)