|
1 |
| -import { isMatchingPattern, truncate } from '../src/string'; |
| 1 | +import { isMatchingPattern, stringMatchesSomePattern, truncate } from '../src/string'; |
2 | 2 |
|
3 | 3 | describe('truncate()', () => {
|
4 | 4 | test('it works as expected', () => {
|
@@ -63,3 +63,48 @@ describe('isMatchingPattern()', () => {
|
63 | 63 | expect(isMatchingPattern([] as any, 'foo')).toEqual(false);
|
64 | 64 | });
|
65 | 65 | });
|
| 66 | + |
| 67 | +describe('stringMatchesSomePattern()', () => { |
| 68 | + test('match using string substring if `requireExactStringMatch` not given', () => { |
| 69 | + expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'])).toEqual(true); |
| 70 | + expect(stringMatchesSomePattern('foobar', ['foo', 'nope'])).toEqual(true); |
| 71 | + expect(stringMatchesSomePattern('foobar', ['baz', 'nope'])).toEqual(false); |
| 72 | + }); |
| 73 | + |
| 74 | + test('match using string substring if `requireExactStringMatch` is `false`', () => { |
| 75 | + expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'], false)).toEqual(true); |
| 76 | + expect(stringMatchesSomePattern('foobar', ['foo', 'nope'], false)).toEqual(true); |
| 77 | + expect(stringMatchesSomePattern('foobar', ['baz', 'nope'], false)).toEqual(false); |
| 78 | + }); |
| 79 | + |
| 80 | + test('match using exact string match if `requireExactStringMatch` is `true`', () => { |
| 81 | + expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'], true)).toEqual(true); |
| 82 | + expect(stringMatchesSomePattern('foobar', ['foo', 'nope'], true)).toEqual(false); |
| 83 | + expect(stringMatchesSomePattern('foobar', ['baz', 'nope'], true)).toEqual(false); |
| 84 | + }); |
| 85 | + |
| 86 | + test('matches when `testString` constains a pattern but not vice-versa', () => { |
| 87 | + expect(stringMatchesSomePattern('foobar', ['foo', 'nope'])).toEqual(true); |
| 88 | + expect(stringMatchesSomePattern('foobar', ['foobarbaz', 'nope'])).toEqual(false); |
| 89 | + }); |
| 90 | + |
| 91 | + test('match using regexp test', () => { |
| 92 | + expect(stringMatchesSomePattern('foobar', [/^foo/, 'nope'])).toEqual(true); |
| 93 | + expect(stringMatchesSomePattern('foobar', [/foo/, 'nope'])).toEqual(true); |
| 94 | + expect(stringMatchesSomePattern('foobar', [/b.{1}r/, 'nope'])).toEqual(true); |
| 95 | + expect(stringMatchesSomePattern('foobar', [/^foo$/, 'nope'])).toEqual(false); |
| 96 | + }); |
| 97 | + |
| 98 | + test('should match empty pattern as true', () => { |
| 99 | + expect(stringMatchesSomePattern('foo', ['', 'nope'])).toEqual(true); |
| 100 | + expect(stringMatchesSomePattern('bar', ['', 'nope'])).toEqual(true); |
| 101 | + expect(stringMatchesSomePattern('', ['', 'nope'])).toEqual(true); |
| 102 | + }); |
| 103 | + |
| 104 | + test('should bail out with false when given non-string value', () => { |
| 105 | + expect(stringMatchesSomePattern(null as any, ['foo', 'nope'])).toEqual(false); |
| 106 | + expect(stringMatchesSomePattern(undefined as any, ['foo', 'nope'])).toEqual(false); |
| 107 | + expect(stringMatchesSomePattern({} as any, ['foo', 'nope'])).toEqual(false); |
| 108 | + expect(stringMatchesSomePattern([] as any, ['foo', 'nope'])).toEqual(false); |
| 109 | + }); |
| 110 | +}); |
0 commit comments