renovate/lib/util/regex.spec.ts

41 lines
983 B
TypeScript
Raw Normal View History

2019-08-25 12:29:51 +00:00
import RE2 from 're2';
2020-03-05 20:57:24 +00:00
import { CONFIG_VALIDATION } from '../constants/error-messages';
2020-05-01 16:03:48 +00:00
import { regEx } from './regex';
2019-08-25 12:29:51 +00:00
describe('util/regex', () => {
2019-08-25 12:29:51 +00:00
beforeEach(() => {
jest.resetModules();
});
it('uses RE2', () => {
expect(regEx('foo')).toBeInstanceOf(RE2);
});
it('throws unsafe 2', () => {
expect(() => regEx(`x++`)).toThrow(CONFIG_VALIDATION);
2019-08-25 12:29:51 +00: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);
});
2019-08-25 12:29:51 +00:00
it('Falls back to RegExp', () => {
jest.doMock('re2', () => {
throw new Error();
});
2020-03-05 20:57:24 +00:00
const regex = require('./regex');
expect(regex.regEx('foo')).toBeInstanceOf(RegExp);
2019-08-25 12:29:51 +00:00
});
});