|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { cmakeify, CMakeValue } from '@cmt/cmakeValue'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Tests for the cmakeify function that converts values to CMake cache variable format. |
| 6 | + * |
| 7 | + * This tests the REAL implementation from @cmt/cmakeValue (not a mirrored copy). |
| 8 | + * The cmakeValue module is pure TypeScript with no vscode dependencies, making it |
| 9 | + * safe to import in backend tests. |
| 10 | + * |
| 11 | + * Key behavior tested: |
| 12 | + * - String values have semicolons ESCAPED (`;` → `\;`) to prevent CMake list interpretation |
| 13 | + * - Array values are joined with semicolons WITHOUT escaping to form CMake lists |
| 14 | + */ |
| 15 | + |
| 16 | +suite('[cmakeify]', () => { |
| 17 | + |
| 18 | + test('Boolean true converts to BOOL TRUE', () => { |
| 19 | + const result = cmakeify(true); |
| 20 | + expect(result.type).to.equal('BOOL'); |
| 21 | + expect(result.value).to.equal('TRUE'); |
| 22 | + }); |
| 23 | + |
| 24 | + test('Boolean false converts to BOOL FALSE', () => { |
| 25 | + const result = cmakeify(false); |
| 26 | + expect(result.type).to.equal('BOOL'); |
| 27 | + expect(result.value).to.equal('FALSE'); |
| 28 | + }); |
| 29 | + |
| 30 | + test('Simple string converts to STRING', () => { |
| 31 | + const result = cmakeify('hello'); |
| 32 | + expect(result.type).to.equal('STRING'); |
| 33 | + expect(result.value).to.equal('hello'); |
| 34 | + }); |
| 35 | + |
| 36 | + test('String with semicolon is escaped', () => { |
| 37 | + const result = cmakeify('clang;lld'); |
| 38 | + expect(result.type).to.equal('STRING'); |
| 39 | + expect(result.value).to.equal('clang\\;lld'); |
| 40 | + }); |
| 41 | + |
| 42 | + test('Number converts to STRING', () => { |
| 43 | + const result = cmakeify(42); |
| 44 | + expect(result.type).to.equal('STRING'); |
| 45 | + expect(result.value).to.equal('42'); |
| 46 | + }); |
| 47 | + |
| 48 | + test('String array joins with semicolons without escaping', () => { |
| 49 | + const result = cmakeify(['clang', 'lld']); |
| 50 | + expect(result.type).to.equal('STRING'); |
| 51 | + expect(result.value).to.equal('clang;lld'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('String array with multiple elements creates CMake list', () => { |
| 55 | + const result = cmakeify(['clang', 'lld', 'compiler-rt', 'libcxx']); |
| 56 | + expect(result.type).to.equal('STRING'); |
| 57 | + expect(result.value).to.equal('clang;lld;compiler-rt;libcxx'); |
| 58 | + }); |
| 59 | + |
| 60 | + test('Empty array produces empty string', () => { |
| 61 | + const result = cmakeify([]); |
| 62 | + expect(result.type).to.equal('STRING'); |
| 63 | + expect(result.value).to.equal(''); |
| 64 | + }); |
| 65 | + |
| 66 | + test('Single element array produces single value', () => { |
| 67 | + const result = cmakeify(['clang']); |
| 68 | + expect(result.type).to.equal('STRING'); |
| 69 | + expect(result.value).to.equal('clang'); |
| 70 | + }); |
| 71 | + |
| 72 | + test('CMakeValue passthrough preserves type and value', () => { |
| 73 | + const input: CMakeValue = { type: 'FILEPATH', value: '/path/to/file' }; |
| 74 | + const result = cmakeify(input); |
| 75 | + expect(result.type).to.equal('FILEPATH'); |
| 76 | + expect(result.value).to.equal('/path/to/file'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('LLVM_ENABLE_PROJECTS use case - array notation avoids escaping', () => { |
| 80 | + // This is the main use case from issue #4503 |
| 81 | + const result = cmakeify(['clang', 'lld']); |
| 82 | + expect(result.type).to.equal('STRING'); |
| 83 | + // Array notation should NOT escape semicolons - they are joined directly |
| 84 | + expect(result.value).to.equal('clang;lld'); |
| 85 | + // This would produce: -DLLVM_ENABLE_PROJECTS:STRING=clang;lld |
| 86 | + }); |
| 87 | + |
| 88 | + test('LLVM_ENABLE_PROJECTS use case - string notation escapes semicolons', () => { |
| 89 | + // When using string notation, semicolons are escaped |
| 90 | + const result = cmakeify('clang;lld'); |
| 91 | + expect(result.type).to.equal('STRING'); |
| 92 | + // String notation DOES escape semicolons |
| 93 | + expect(result.value).to.equal('clang\\;lld'); |
| 94 | + // This would produce: -DLLVM_ENABLE_PROJECTS:STRING=clang\;lld |
| 95 | + }); |
| 96 | +}); |
0 commit comments