|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import {matchAll} from './match_all_polyfill'; |
| 19 | + |
| 20 | +function format({match, index, input}: {match: string, index: number, |
| 21 | + input: string}) { |
| 22 | + const res: RegExpMatchArray = [match]; |
| 23 | + res.index = index; |
| 24 | + res.input = input; |
| 25 | + return jasmine.objectContaining(res); |
| 26 | +} |
| 27 | + |
| 28 | +describe('matchAll', () => { |
| 29 | + it('finds all matches of a regexp on a string', () => { |
| 30 | + const input = 'asdfasdfasdfasdf'; |
| 31 | + expect([...matchAll(input , /asd/g)]).toEqual([ |
| 32 | + {match: 'asd', index: 0, input}, |
| 33 | + {match: 'asd', index: 4, input}, |
| 34 | + {match: 'asd', index: 8, input}, |
| 35 | + {match: 'asd', index: 12, input}, |
| 36 | + ].map(format)); |
| 37 | + }); |
| 38 | + |
| 39 | + it('supports regexp flags', () => { |
| 40 | + const input = 'asdfASDFasdfASDF'; |
| 41 | + // Case sensitive |
| 42 | + expect([...matchAll(input, /asd/g)]).toEqual([ |
| 43 | + {match: 'asd', index: 0, input}, |
| 44 | + {match: 'asd', index: 8, input}, |
| 45 | + ].map(format)); |
| 46 | + |
| 47 | + // case insensitive |
| 48 | + expect([...matchAll('asdfASDFasdfASDF', /asd/gi)]).toEqual([ |
| 49 | + {match: 'asd', index: 0, input}, |
| 50 | + {match: 'ASD', index: 4, input}, |
| 51 | + {match: 'asd', index: 8, input}, |
| 52 | + {match: 'ASD', index: 12, input}, |
| 53 | + ].map(format)); |
| 54 | + }); |
| 55 | +}); |
0 commit comments