diff --git a/__tests__/unit/node/markdown/plugins/snippet.test.ts b/__tests__/unit/node/markdown/plugins/snippet.test.ts index 0781b373..aa940784 100644 --- a/__tests__/unit/node/markdown/plugins/snippet.test.ts +++ b/__tests__/unit/node/markdown/plugins/snippet.test.ts @@ -99,145 +99,228 @@ describe('node/markdown/plugins/snippet', () => { }) }) - test('rawPathToToken', () => { - rawPathTokenMap.forEach(([rawPath, token]) => { + describe('rawPathToToken', () => { + test.each(rawPathTokenMap)('%s', (rawPath, token) => { expect(removeEmptyKeys(rawPathToToken(rawPath))).toEqual(token) }) }) describe('findRegion', () => { - test('when c# region with matching tag', () => { - const lines = `Console.WriteLine("Before region"); -#region hello -Console.WriteLine("Hello, World!"); -#endregion hello -Console.WriteLine("After region");`.split('\n') - const result = findRegion(lines, 'hello') + it('returns null when no region markers are present', () => { + const lines = ['function foo() {', ' console.log("hello");', '}'] + expect(findRegion(lines, 'foo')).toBeNull() + }) - expect(lines.slice(result?.start, result?.end).join('\n')).toBe( - 'Console.WriteLine("Hello, World!");' - ) + it('ignores non-matching region names', () => { + const lines = [ + '// #region regionA', + 'some code here', + '// #endregion regionA' + ] + expect(findRegion(lines, 'regionC')).toBeNull() }) - test('when c# region is not indented with spaces and no matching tag', () => { - const lines = `Console.WriteLine("Before region"); -#region hello -Console.WriteLine("Hello, World!"); -#endregion -Console.WriteLine("After region");`.split('\n') - const result = findRegion(lines, 'hello') - expect(lines.slice(result?.start, result?.end).join('\n')).toBe( - 'Console.WriteLine("Hello, World!");' - ) + it('returns null if a region start marker exists without a matching end marker', () => { + const lines = [ + '// #region missingEnd', + 'console.log("inside region");', + 'console.log("still inside");' + ] + expect(findRegion(lines, 'missingEnd')).toBeNull() }) - test('when c# region is indented with spaces and no matching tag', () => { - const lines = ` Console.WriteLine("Before region"); - #region hello - Console.WriteLine("Hello, World!"); - #endregion hello - Console.WriteLine("After region");`.split('\n') - const result = findRegion(lines, 'hello') - expect(lines.slice(result?.start, result?.end).join('\n')).toBe( - ' Console.WriteLine("Hello, World!");' - ) + it('returns null if an end marker exists without a preceding start marker', () => { + const lines = [ + '// #endregion ghostRegion', + 'console.log("stray end marker");' + ] + expect(findRegion(lines, 'ghostRegion')).toBeNull() }) - test('when c# region with matching tag', () => { - const lines = `Console.WriteLine("Before region"); -#region hello -Console.WriteLine("Hello, World!"); -#endregion hello -Console.WriteLine("After region");`.split('\n') - const result = findRegion(lines, 'hello') - expect(lines.slice(result?.start, result?.end).join('\n')).toBe( - 'Console.WriteLine("Hello, World!");' - ) + it('detects C#/JavaScript style region markers with matching tags', () => { + const lines = [ + 'Console.WriteLine("Before region");', + '#region hello', + 'Console.WriteLine("Hello, World!");', + '#endregion hello', + 'Console.WriteLine("After region");' + ] + const result = findRegion(lines, 'hello') + expect(result).not.toBeNull() + if (result) { + expect(lines.slice(result.start, result.end).join('\n')).toBe( + 'Console.WriteLine("Hello, World!");' + ) + } }) - test('when c# region is not indented with spaces and no matching tag', () => { - const lines = `Console.WriteLine("Before region"); -#region hello -Console.WriteLine("Hello, World!"); -#endregion -Console.WriteLine("After region");`.split('\n') + + it('detects region markers even when the end marker omits the region name', () => { + const lines = [ + 'Console.WriteLine("Before region");', + '#region hello', + 'Console.WriteLine("Hello, World!");', + '#endregion', + 'Console.WriteLine("After region");' + ] const result = findRegion(lines, 'hello') + expect(result).not.toBeNull() + if (result) { + expect(lines.slice(result.start, result.end).join('\n')).toBe( + 'Console.WriteLine("Hello, World!");' + ) + } + }) - expect(lines.slice(result?.start, result?.end).join('\n')).toBe( - 'Console.WriteLine("Hello, World!");' - ) + it('handles indented region markers correctly', () => { + const lines = [ + ' Console.WriteLine("Before region");', + ' #region hello', + ' Console.WriteLine("Hello, World!");', + ' #endregion hello', + ' Console.WriteLine("After region");' + ] + const result = findRegion(lines, 'hello') + expect(result).not.toBeNull() + if (result) { + expect(lines.slice(result.start, result.end).join('\n')).toBe( + ' Console.WriteLine("Hello, World!");' + ) + } }) - test('when typescript region has matching tag', () => { - const lines = `let regexp: RegExp[] = [] -// #region foo -let start = -1 -// #endregion foo`.split('\n') + it('detects TypeScript style region markers', () => { + const lines = [ + 'let regexp: RegExp[] = [];', + '// #region foo', + 'let start = -1;', + '// #endregion foo' + ] const result = findRegion(lines, 'foo') + expect(result).not.toBeNull() + if (result) { + expect(lines.slice(result.start, result.end).join('\n')).toBe( + 'let start = -1;' + ) + } + }) - expect(lines.slice(result?.start, result?.end).join('\n')).toBe( - 'let start = -1' - ) + it('detects CSS style region markers', () => { + const lines = [ + '.body-content {', + '/* #region foo */', + ' padding-left: 15px;', + '/* #endregion foo */', + ' padding-right: 15px;', + '}' + ] + const result = findRegion(lines, 'foo') + expect(result).not.toBeNull() + if (result) { + expect(lines.slice(result.start, result.end).join('\n')).toBe( + ' padding-left: 15px;' + ) + } }) - test('when typescript region is indented with spaces and no matching tag', () => { - const lines = ` let regexp: RegExp[] = [] - // #region foo - let start = -1 - // #endregion`.split('\n') + + it('detects HTML style region markers', () => { + const lines = [ + '
more text
`.split('\n') + it('returns the first complete region when multiple regions exist', () => { + const lines = [ + '// #region foo', + 'first region content', + '// #endregion foo', + '// #region foo', + 'second region content', + '// #endregion foo' + ] const result = findRegion(lines, 'foo') - - expect(lines.slice(result?.start, result?.end).join('\n')).toBe( - 'more text
`.split('\n') - const result = findRegion(lines, 'foo') - expect(lines.slice(result?.start, result?.end).join('\n')).toBe( - '