VerifyCodeFix verifies that applying a code fix produces the expected file content.
(t *testing.T, options VerifyCodeFixOptions)
| 1585 | |
| 1586 | // VerifyCodeFix verifies that applying a code fix produces the expected file content. |
| 1587 | func (f *FourslashTest) VerifyCodeFix(t *testing.T, options VerifyCodeFixOptions) { |
| 1588 | t.Helper() |
| 1589 | |
| 1590 | if options.UserPreferences != nil { |
| 1591 | reset := f.ConfigureWithReset(t, *options.UserPreferences) |
| 1592 | defer reset() |
| 1593 | } |
| 1594 | |
| 1595 | actions := f.getCodeFixActions(t) |
| 1596 | |
| 1597 | if len(actions) == 0 { |
| 1598 | t.Fatalf("No code fixes returned.") |
| 1599 | } |
| 1600 | if options.Index >= len(actions) { |
| 1601 | t.Fatalf("Code fix index %d out of range (got %d fixes)", options.Index, len(actions)) |
| 1602 | } |
| 1603 | |
| 1604 | matchingAction := actions[options.Index] |
| 1605 | if matchingAction.Title != options.Description { |
| 1606 | found := false |
| 1607 | for _, action := range actions { |
| 1608 | if action.Title == options.Description { |
| 1609 | matchingAction = action |
| 1610 | found = true |
| 1611 | break |
| 1612 | } |
| 1613 | } |
| 1614 | if !found { |
| 1615 | var titles []string |
| 1616 | for _, a := range actions { |
| 1617 | titles = append(titles, a.Title) |
| 1618 | } |
| 1619 | t.Fatalf("No code fix with description %q at index %d found. Available fixes: %v", options.Description, options.Index, titles) |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | originalContent := f.getScriptInfo(f.activeFilename).content |
| 1624 | expectedContent := options.NewFileContent |
| 1625 | if options.NewRangeContent != "" { |
| 1626 | selection := f.getSelection() |
| 1627 | if selection.Pos() == selection.End() { |
| 1628 | ranges := f.getRangesInFile(f.activeFilename) |
| 1629 | if len(ranges) == 0 { |
| 1630 | t.Fatal("Expected a selected range or fourslash range for NewRangeContent verification.") |
| 1631 | } |
| 1632 | selection = ranges[0].Range |
| 1633 | } |
| 1634 | expectedContent = originalContent[:selection.Pos()] + options.NewRangeContent + originalContent[selection.End():] |
| 1635 | } |
| 1636 | |
| 1637 | if options.ApplyChanges { |
| 1638 | if matchingAction.Edit != nil && matchingAction.Edit.Changes != nil { |
| 1639 | expectedURI := lsconv.FileNameToDocumentURI(f.activeFilename) |
| 1640 | for uri, edits := range *matchingAction.Edit.Changes { |
| 1641 | if uri != expectedURI { |
| 1642 | t.Fatalf("Code fix returned edits for unexpected URI %q (expected %q)", uri, expectedURI) |
| 1643 | } |
| 1644 | f.applyTextEdits(t, edits) |