|
| 1 | +import { compact_file } from './compact-file' |
| 2 | +import * as fs from 'fs' |
| 3 | +import * as path from 'path' |
| 4 | + |
| 5 | +describe('compact_file', () => { |
| 6 | + const load_test_case = ( |
| 7 | + test_case: string, |
| 8 | + directory = 'c-style' |
| 9 | + ): { input: string; expected: string } => { |
| 10 | + const case_path = path.join(__dirname, 'test-cases', directory, test_case) |
| 11 | + const input = fs.readFileSync(path.join(case_path, 'input.txt'), 'utf-8') |
| 12 | + const expected = fs.readFileSync( |
| 13 | + path.join(case_path, 'output.txt'), |
| 14 | + 'utf-8' |
| 15 | + ) |
| 16 | + return { input, expected } |
| 17 | + } |
| 18 | + |
| 19 | + it('should remove single line comments', () => { |
| 20 | + const { input, expected } = load_test_case('remove-single-line-comments') |
| 21 | + expect(compact_file(input, '.ts')).toBe(expected) |
| 22 | + }) |
| 23 | + |
| 24 | + it('should remove block comments on a single line', () => { |
| 25 | + const { input, expected } = load_test_case( |
| 26 | + 'remove-block-comments-single-line' |
| 27 | + ) |
| 28 | + expect(compact_file(input, '.ts')).toBe(expected) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should remove multi-line block comments', () => { |
| 32 | + const { input, expected } = load_test_case( |
| 33 | + 'remove-multi-line-block-comments' |
| 34 | + ) |
| 35 | + expect(compact_file(input, '.ts')).toBe(expected) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should preserve comments inside double quotes', () => { |
| 39 | + const { input, expected } = load_test_case('preserve-double-quotes') |
| 40 | + expect(compact_file(input, '.ts')).toBe(expected) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should preserve comments inside single quotes', () => { |
| 44 | + const { input, expected } = load_test_case('preserve-single-quotes') |
| 45 | + expect(compact_file(input, '.ts')).toBe(expected) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should preserve comments inside template literals', () => { |
| 49 | + const { input, expected } = load_test_case('preserve-template-literals') |
| 50 | + expect(compact_file(input, '.ts')).toBe(expected) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should preserve block comments inside strings', () => { |
| 54 | + const { input, expected } = load_test_case( |
| 55 | + 'preserve-block-comments-in-strings' |
| 56 | + ) |
| 57 | + expect(compact_file(input, '.ts')).toBe(expected) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should handle escaped quotes inside strings', () => { |
| 61 | + const { input, expected } = load_test_case('handle-escaped-quotes') |
| 62 | + expect(compact_file(input, '.ts')).toBe(expected) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should handle complex mixed content', () => { |
| 66 | + const { input, expected } = load_test_case('complex-mixed-content') |
| 67 | + expect(compact_file(input, '.ts')).toBe(expected) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should handle input with only comments', () => { |
| 71 | + const { input, expected } = load_test_case('only-comments') |
| 72 | + expect(compact_file(input, '.ts')).toBe(expected) |
| 73 | + }) |
| 74 | + |
| 75 | + it('should strip function bodies when enabled', () => { |
| 76 | + const { input, expected } = load_test_case('javascript-bodies') |
| 77 | + expect(compact_file(input, '.ts')).toBe(expected) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should strip python function bodies', () => { |
| 81 | + const { input, expected } = load_test_case('python', '.') |
| 82 | + expect(compact_file(input, '.py')).toBe(expected) |
| 83 | + }) |
| 84 | +}) |
0 commit comments