|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { createSafeDsServices } from '../../../src/language/safe-ds-module.js'; |
| 3 | +import { EmptyFileSystem } from 'langium'; |
| 4 | +import { getNodeOfType } from '../../helpers/nodeFinder.js'; |
| 5 | +import { |
| 6 | + isSdsClass, |
| 7 | + isSdsModule, |
| 8 | + isSdsTemplateStringEnd, |
| 9 | + isSdsTemplateStringInner, |
| 10 | + isSdsTemplateStringStart, |
| 11 | +} from '../../../src/language/generated/ast.js'; |
| 12 | + |
| 13 | +const services = createSafeDsServices(EmptyFileSystem).SafeDs; |
| 14 | + |
| 15 | +describe('runConverter', () => { |
| 16 | + it('should remove backticks from IDs (package)', async () => { |
| 17 | + const code = ` |
| 18 | + package \`foo\`.bar |
| 19 | + `; |
| 20 | + |
| 21 | + const module = await getNodeOfType(services, code, isSdsModule); |
| 22 | + expect(module.name).toBe('foo.bar'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should remove backticks from IDs (declaration)', async () => { |
| 26 | + const code = ` |
| 27 | + class \`MyClass\` |
| 28 | + `; |
| 29 | + |
| 30 | + const firstClass = await getNodeOfType(services, code, isSdsClass); |
| 31 | + expect(firstClass.name).toBe('MyClass'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should remove delimiters from TEMPLATE_STRING_STARTs', async () => { |
| 35 | + const code = ` |
| 36 | + pipeline myPipeline { |
| 37 | + "start{{ 1 }}inner{{ 2 }}end"; |
| 38 | + } |
| 39 | + `; |
| 40 | + |
| 41 | + const firstTemplateStringStart = await getNodeOfType(services, code, isSdsTemplateStringStart); |
| 42 | + expect(firstTemplateStringStart.value).toBe('start'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should handle escape sequences in TEMPLATE_STRING_STARTs', async () => { |
| 46 | + const code = ` |
| 47 | + pipeline myPipeline { |
| 48 | + "\\tstart{{ 1 }}inner{{ 2 }}end"; |
| 49 | + } |
| 50 | + `; |
| 51 | + |
| 52 | + const firstTemplateStringStart = await getNodeOfType(services, code, isSdsTemplateStringStart); |
| 53 | + expect(firstTemplateStringStart.value).toBe('\tstart'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should remove delimiters from TEMPLATE_STRING_INNERs', async () => { |
| 57 | + const code = ` |
| 58 | + pipeline myPipeline { |
| 59 | + "start{{ 1 }}inner{{ 2 }}end"; |
| 60 | + } |
| 61 | + `; |
| 62 | + |
| 63 | + const firstTemplateStringInner = await getNodeOfType(services, code, isSdsTemplateStringInner); |
| 64 | + expect(firstTemplateStringInner.value).toBe('inner'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle escape sequences in TEMPLATE_STRING_INNERs', async () => { |
| 68 | + const code = ` |
| 69 | + pipeline myPipeline { |
| 70 | + "start{{ 1 }}\\tinner{{ 2 }}end"; |
| 71 | + } |
| 72 | + `; |
| 73 | + |
| 74 | + const firstTemplateStringInner = await getNodeOfType(services, code, isSdsTemplateStringInner); |
| 75 | + expect(firstTemplateStringInner.value).toBe('\tinner'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should remove delimiters from TEMPLATE_STRING_ENDs', async () => { |
| 79 | + const code = ` |
| 80 | + pipeline myPipeline { |
| 81 | + "start{{ 1 }}inner{{ 2 }}end"; |
| 82 | + } |
| 83 | + `; |
| 84 | + |
| 85 | + const firstTemplateStringEnd = await getNodeOfType(services, code, isSdsTemplateStringEnd); |
| 86 | + expect(firstTemplateStringEnd.value).toBe('end'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should handle escape sequences in TEMPLATE_STRING_ENDs', async () => { |
| 90 | + const code = ` |
| 91 | + pipeline myPipeline { |
| 92 | + "start{{ 1 }}inner{{ 2 }}\\tend"; |
| 93 | + } |
| 94 | + `; |
| 95 | + |
| 96 | + const firstTemplateStringEnd = await getNodeOfType(services, code, isSdsTemplateStringEnd); |
| 97 | + expect(firstTemplateStringEnd.value).toBe('\tend'); |
| 98 | + }); |
| 99 | +}); |
0 commit comments