1import { findMatchingBracketPosition } from '../matchBrackets'; 2 3describe(findMatchingBracketPosition, () => { 4 it('should handle one line search', () => { 5 expect(findMatchingBracketPosition('foo()', '(')).toBe(4); 6 expect(findMatchingBracketPosition('withParameter("a", 1, null)', '(')).toBe(26); 7 }); 8 9 it('should handle backward search', () => { 10 expect(findMatchingBracketPosition('foo()', ')')).toBe(3); 11 expect(findMatchingBracketPosition('withParameter("a", 1, null)', ')')).toBe(13); 12 }); 13 14 it('should handle nested brackets call', () => { 15 expect(findMatchingBracketPosition('foo(boo(), 0)', '(')).toBe(12); 16 }); 17 18 it('should handle nested brackets multi-lines call', () => { 19 const content = ` 20void foo() { 21 doSomething(); 22 if (doSomeCheck(123)) { 23 return -1; 24 } 25 return 0; 26}`; 27 const lastBrace = content.length - 1; 28 expect(findMatchingBracketPosition(content, '{')).toBe(lastBrace); 29 30 // `findMatchingBracketPosition` will search first `bracket` in forward direction first 31 // and search matching bracket either forward or backward. 32 // In this case, search `'}'` will match the ended bracket of `if (doSomeCheck(123)) {` block. 33 const firstBrace = content.indexOf('{'); 34 const secondBrace = content.indexOf('{', firstBrace + 1); 35 expect(findMatchingBracketPosition(content, '}')).toBe(secondBrace); 36 }); 37 38 it('should return -1 for not found cases', () => { 39 expect(findMatchingBracketPosition('', '(')).toBe(-1); 40 expect(findMatchingBracketPosition('foo', '(')).toBe(-1); 41 expect(findMatchingBracketPosition('foo(', '(')).toBe(-1); 42 expect(findMatchingBracketPosition('foo()', '{')).toBe(-1); 43 expect(findMatchingBracketPosition('foo()', '}')).toBe(-1); 44 expect(findMatchingBracketPosition('foo(bar()', '(')).toBe(-1); 45 }); 46}); 47