2019-08-25 14:29:51 +02:00
|
|
|
import RE2 from 're2';
|
2020-03-05 21:57:24 +01:00
|
|
|
import { CONFIG_VALIDATION } from '../constants/error-messages';
|
2024-02-06 18:51:41 -03:00
|
|
|
import { regEx } from './regex';
|
2019-08-25 14:29:51 +02:00
|
|
|
|
2021-08-18 08:46:56 +03:00
|
|
|
describe('util/regex', () => {
|
2019-08-25 14:29:51 +02:00
|
|
|
it('uses RE2', () => {
|
2019-09-09 12:21:01 +02:00
|
|
|
expect(regEx('foo')).toBeInstanceOf(RE2);
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2019-09-09 12:21:01 +02:00
|
|
|
it('throws unsafe 2', () => {
|
2020-01-12 13:20:11 +05:30
|
|
|
expect(() => regEx(`x++`)).toThrow(CONFIG_VALIDATION);
|
2019-08-25 14:29:51 +02:00
|
|
|
});
|
|
|
|
|
2022-11-01 12:20:11 +03:00
|
|
|
it('reuses flags from regex', () => {
|
|
|
|
expect(regEx(/foo/i).flags).toBe('iu');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('caches non-stateful regex', () => {
|
|
|
|
expect(regEx('foo')).toBe(regEx('foo'));
|
|
|
|
expect(regEx('foo', 'm')).toBe(regEx('foo', 'm'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not cache stateful regex', () => {
|
|
|
|
expect(regEx('foo', 'g')).not.toBe(regEx('foo', 'g'));
|
|
|
|
expect(regEx(/bar/g)).not.toBe(/bar/g);
|
|
|
|
});
|
|
|
|
|
2023-09-08 13:40:04 +02:00
|
|
|
it('Falls back to RegExp', async () => {
|
2025-02-26 10:35:54 +01:00
|
|
|
vi.resetModules();
|
|
|
|
vi.doMock('../expose.cjs', () => ({
|
|
|
|
re2: () => {
|
|
|
|
throw new Error();
|
|
|
|
},
|
|
|
|
}));
|
2019-08-25 14:29:51 +02:00
|
|
|
|
2025-03-04 08:37:06 +01:00
|
|
|
const regex = await import('./regex.js');
|
2019-09-09 12:21:01 +02:00
|
|
|
expect(regex.regEx('foo')).toBeInstanceOf(RegExp);
|
2019-08-25 14:29:51 +02:00
|
|
|
});
|
|
|
|
});
|