|
| 1 | +import { describe, test, expect } from 'bun:test'; |
| 2 | +import { BindTargetDeclaration } from '../../src/parsers/inputFieldParser/InputFieldDeclaration'; |
| 3 | +import { TestPlugin } from './mocks/TestAPI'; |
| 4 | +import { BindTargetScope } from '../../src/metadata/BindTargetScope'; |
| 5 | + |
| 6 | +const plugin = new TestPlugin(); |
| 7 | +const parser = plugin.api.bindTargetParser; |
| 8 | + |
| 9 | +const validBindTargetFiles = [ |
| 10 | + undefined, |
| 11 | + 'file', |
| 12 | + 'testFile', |
| 13 | + 'test_file', |
| 14 | + 'test-file', |
| 15 | + 'test file', |
| 16 | + 'path/to/file', |
| 17 | + 'something/in/japanese こんにちは', |
| 18 | + 'this is/some-path/to_some_file/in chinese/你叫什么名字', |
| 19 | +]; |
| 20 | + |
| 21 | +const validBindTargetPaths: [string, string[]][] = [ |
| 22 | + ['test', ['test']], |
| 23 | + ['["test"]', ['test']], |
| 24 | + ['this["is"]', ['this', 'is']], |
| 25 | + ['this["is"]["a"]', ['this', 'is', 'a']], |
| 26 | + ['this["is"]["a"].path', ['this', 'is', 'a', 'path']], |
| 27 | + ['["this"].is.a["path"]', ['this', 'is', 'a', 'path']], |
| 28 | + ['[0]', ['0']], |
| 29 | + ['test[0]', ['test', '0']], |
| 30 | + ['test["foo"][1]', ['test', 'foo', '1']], |
| 31 | + ['test["foo"][1].bar', ['test', 'foo', '1', 'bar']], |
| 32 | +]; |
| 33 | + |
| 34 | +const localScopeBindTarget: BindTargetDeclaration = { |
| 35 | + filePath: 'scope_test_file', |
| 36 | + metadataPath: ['scope_test_metadata'], |
| 37 | + boundToLocalScope: false, |
| 38 | + listenToChildren: false, |
| 39 | +}; |
| 40 | + |
| 41 | +const localScope = new BindTargetScope(localScopeBindTarget); |
| 42 | + |
| 43 | +interface BindTargetCombination { |
| 44 | + filePath: string | undefined; |
| 45 | + metadataPath: string[]; |
| 46 | + metadataPathString: string; |
| 47 | +} |
| 48 | + |
| 49 | +function generateValidBindTargets(): BindTargetCombination[] { |
| 50 | + const testCases: BindTargetCombination[] = []; |
| 51 | + for (const validBindTargetFile of validBindTargetFiles) { |
| 52 | + for (const validBindTargetPath of validBindTargetPaths) { |
| 53 | + testCases.push({ |
| 54 | + filePath: validBindTargetFile, |
| 55 | + metadataPathString: validBindTargetPath[0], |
| 56 | + metadataPath: validBindTargetPath[1], |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + return testCases; |
| 61 | +} |
| 62 | + |
| 63 | +interface TestCasePart { |
| 64 | + str: string; |
| 65 | + expected: BindTargetDeclaration; |
| 66 | +} |
| 67 | + |
| 68 | +interface TestCase { |
| 69 | + normal: TestCasePart; |
| 70 | + local: TestCasePart; |
| 71 | +} |
| 72 | + |
| 73 | +function generateTestCase(combination: BindTargetCombination): TestCase { |
| 74 | + if (combination.filePath === undefined) { |
| 75 | + let localStr: string; |
| 76 | + if (combination.metadataPathString.startsWith('[')) { |
| 77 | + localStr = `^${combination.metadataPathString}`; |
| 78 | + } else { |
| 79 | + localStr = `^.${combination.metadataPathString}`; |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + normal: { |
| 84 | + str: combination.metadataPathString, |
| 85 | + expected: { |
| 86 | + filePath: undefined, |
| 87 | + metadataPath: combination.metadataPath, |
| 88 | + listenToChildren: false, |
| 89 | + boundToLocalScope: false, |
| 90 | + }, |
| 91 | + }, |
| 92 | + local: { |
| 93 | + str: localStr, |
| 94 | + expected: { |
| 95 | + filePath: localScopeBindTarget.filePath, |
| 96 | + metadataPath: localScopeBindTarget.metadataPath.concat(combination.metadataPath), |
| 97 | + listenToChildren: false, |
| 98 | + boundToLocalScope: true, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }; |
| 102 | + } else { |
| 103 | + let localStr: string; |
| 104 | + if (combination.metadataPathString.startsWith('[')) { |
| 105 | + localStr = `${combination.filePath}#^${combination.metadataPathString}`; |
| 106 | + } else { |
| 107 | + localStr = `${combination.filePath}#^.${combination.metadataPathString}`; |
| 108 | + } |
| 109 | + |
| 110 | + return { |
| 111 | + normal: { |
| 112 | + str: `${combination.filePath}#${combination.metadataPathString}`, |
| 113 | + expected: { |
| 114 | + filePath: combination.filePath, |
| 115 | + metadataPath: combination.metadataPath, |
| 116 | + listenToChildren: false, |
| 117 | + boundToLocalScope: false, |
| 118 | + }, |
| 119 | + }, |
| 120 | + local: { |
| 121 | + str: localStr, |
| 122 | + expected: { |
| 123 | + filePath: localScopeBindTarget.filePath, |
| 124 | + metadataPath: localScopeBindTarget.metadataPath.concat(combination.metadataPath), |
| 125 | + listenToChildren: false, |
| 126 | + boundToLocalScope: true, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +describe('bind target parser', () => { |
| 134 | + describe('should succeed on valid bind target', () => { |
| 135 | + for (const bindTarget of generateValidBindTargets()) { |
| 136 | + const testCase = generateTestCase(bindTarget); |
| 137 | + |
| 138 | + test(testCase.normal.str, () => { |
| 139 | + expect(parser.parseAndValidateBindTarget(testCase.normal.str)).toEqual(testCase.normal.expected); |
| 140 | + }); |
| 141 | + |
| 142 | + test(testCase.local.str, () => { |
| 143 | + expect(parser.parseAndValidateBindTarget(testCase.local.str, localScope)).toEqual(testCase.local.expected); |
| 144 | + }); |
| 145 | + } |
| 146 | + }); |
| 147 | +}); |
0 commit comments