Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import * as fc from 'fast-check';
2
3import { regexEscape, runEncode } from './encoding.js';
4
5describe('Encoding Utilities', () => {
6 describe('runEncode', () => {
7 test('empty string', () => {
8 expect(runEncode('')).toMatchObject([]);
9 });
10 test('single character', () => {
11 expect(runEncode('a')).toMatchObject([['a', 1]]);
12 });
13 test('duplicate multiple characters', () => {
14 expect(runEncode('aaaabbaaabbbb')).toMatchObject([
15 ['a', 4],
16 ['b', 2],
17 ['a', 3],
18 ['b', 4],
19 ]);
20 });
21 });
22
23 describe('regexEscape', () => {
24 test('should always produce a valid regex that matches the literal characters of the input', () => {
25 fc.assert(
26 fc.property(fc.string({ size: '+2' }), (str) => {
27 // eslint-disable-next-line security/detect-non-literal-regexp
28 const regex = new RegExp(`^${regexEscape(str)}$`);
29 return regex.test(str);
30 }),
31 );
32 });
33 });
34});