|
| 1 | +package ls_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/google/go-cmp/cmp" |
| 7 | + "github.com/microsoft/typescript-go/internal/bundled" |
| 8 | + "github.com/microsoft/typescript-go/internal/core" |
| 9 | + "github.com/microsoft/typescript-go/internal/ls" |
| 10 | + "github.com/microsoft/typescript-go/internal/testutil/lstestutil" |
| 11 | + "gotest.tools/v3/assert" |
| 12 | +) |
| 13 | + |
| 14 | +type definitionTestCase struct { |
| 15 | + name string |
| 16 | + files map[string]string |
| 17 | + expected map[string][]ls.Location |
| 18 | +} |
| 19 | + |
| 20 | +func TestDefinition(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + if !bundled.Embedded { |
| 23 | + // Without embedding, we'd need to read all of the lib files out from disk into the MapFS. |
| 24 | + // Just skip this for now. |
| 25 | + t.Skip("bundled files are not embedded") |
| 26 | + } |
| 27 | + |
| 28 | + testCases := []definitionTestCase{ |
| 29 | + { |
| 30 | + name: "localFunction", |
| 31 | + files: map[string]string{ |
| 32 | + mainFileName: ` |
| 33 | +function localFunction() { } |
| 34 | +/*localFunction*/localFunction();`, |
| 35 | + }, |
| 36 | + expected: map[string][]ls.Location{ |
| 37 | + "localFunction": {{ |
| 38 | + FileName: mainFileName, |
| 39 | + Range: core.NewTextRange(9, 22), |
| 40 | + }}, |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, testCase := range testCases { |
| 46 | + t.Run(testCase.name, func(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + runDefinitionTest(t, testCase.files, testCase.expected) |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func runDefinitionTest(t *testing.T, files map[string]string, expected map[string][]ls.Location) { |
| 54 | + parsedFiles := make(map[string]string) |
| 55 | + var markerPositions map[string]*lstestutil.Marker |
| 56 | + for fileName, content := range files { |
| 57 | + if fileName == mainFileName { |
| 58 | + testData := lstestutil.ParseTestData("", content, fileName) |
| 59 | + markerPositions = testData.MarkerPositions |
| 60 | + parsedFiles[fileName] = testData.Files[0].Content // !!! Assumes no usage of @filename |
| 61 | + } else { |
| 62 | + parsedFiles[fileName] = content |
| 63 | + } |
| 64 | + } |
| 65 | + languageService := createLanguageService(mainFileName, parsedFiles) |
| 66 | + for markerName, expectedResult := range expected { |
| 67 | + marker, ok := markerPositions[markerName] |
| 68 | + if !ok { |
| 69 | + t.Fatalf("No marker found for '%s'", markerName) |
| 70 | + } |
| 71 | + locations := languageService.ProvideDefinitions(mainFileName, marker.Position) |
| 72 | + assert.DeepEqual(t, locations, expectedResult, cmp.AllowUnexported(core.TextRange{})) |
| 73 | + } |
| 74 | +} |
0 commit comments