Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Check replacement text in rename refactor #361

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 57 additions & 29 deletions Tests/RefactorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open Ionide.LanguageServerProtocol.Types
open Xunit
open Misc

let editRanges =
let editsByFile =
function
| Refactor.Edit wsEdit ->
match wsEdit.DocumentChanges with
Expand All @@ -18,15 +18,15 @@ let editRanges =
| _ -> failwith $"Refactoring should always produce TextDocumentEdits"

let doc = Path.GetFileName(docEdit.TextDocument.Uri)
let ranges = docEdit.Edits |> Array.map (fun x -> x.Range)
let ranges = docEdit.Edits |> Array.map (fun x -> x.Range, x.NewText)
doc, ranges)
|> Map.ofArray
| _ ->
match wsEdit.Changes with
| Some docEditMap ->
Map.toSeq docEditMap
|> Seq.map (fun (doc, edits) ->
let ranges = edits |> Array.map (fun x -> x.Range)
let ranges = edits |> Array.map (fun x -> x.Range, x.NewText)
(Path.GetFileName doc), ranges)
|> Map.ofSeq
| _ -> Map.empty
Expand Down Expand Up @@ -65,15 +65,18 @@ module RenameTests =
Map.ofSeq [
"doc1.md",
[|
Range.Mk(5, 1, 5, 5)
Range.Mk(2, 6, 2, 10)
Range.Mk(1, 7, 1, 11)
Range.Mk(5, 1, 5, 5), "newLbl"
Range.Mk(2, 6, 2, 10), "newLbl"
Range.Mk(1, 7, 1, 11), "newLbl"
|]
]

let actualRanges = editRanges res
let actualRanges = editsByFile res

Assert.Equal<Range>(Map.find "doc1.md" expectedRanges, Map.find "doc1.md" actualRanges)
Assert.Equal<Range * string>(
Map.find "doc1.md" expectedRanges,
Map.find "doc1.md" actualRanges
)

[<Fact>]
let onDefLabel () =
Expand All @@ -85,22 +88,25 @@ module RenameTests =
Map.ofSeq [
"doc1.md",
[|
Range.Mk(5, 1, 5, 5)
Range.Mk(2, 6, 2, 10)
Range.Mk(1, 7, 1, 11)
Range.Mk(5, 1, 5, 5), "newLbl"
Range.Mk(2, 6, 2, 10), "newLbl"
Range.Mk(1, 7, 1, 11), "newLbl"
|]
]

let actualRanges = editRanges res
let actualRanges = editsByFile res

Assert.Equal<Range>(Map.find "doc1.md" expectedRanges, Map.find "doc1.md" actualRanges)
Assert.Equal<Range * string>(
Map.find "doc1.md" expectedRanges,
Map.find "doc1.md" actualRanges
)

module HeadingLinks =
let mkWorkspace () =
let doc1 =
Helpers.FakeDoc.Mk(
// 0 1 2
// 0123456789012345678901234567890
// 0 1 2
// 0123456789012345678901234567890
[|
"# Doc 1"
"## Doc 1.2"
Expand All @@ -113,8 +119,8 @@ module RenameTests =

let doc2 =
Helpers.FakeDoc.Mk(
// 0 1 2
// 0123456789012345678901234567890
// 0 1 2
// 0123456789012345678901234567890
[|
"# Doc 2"
"[[doc-1]]"
Expand All @@ -138,14 +144,25 @@ module RenameTests =

let expectedRanges =
Map.ofSeq [
"doc1.md", [| Range.Mk(0, 2, 0, 7) |]
"doc2.md", [| Range.Mk(2, 2, 2, 7); Range.Mk(1, 2, 1, 7) |]
"doc1.md", [| Range.Mk(0, 2, 0, 7), "New Title" |]
"doc2.md",
[|
Range.Mk(2, 2, 2, 7), "New Title"
Range.Mk(1, 2, 1, 7), "New Title"
|]
]

let actualRanges = editRanges res
let actualRanges = editsByFile res

Assert.Equal<Range * string>(
Map.find "doc1.md" expectedRanges,
Map.find "doc1.md" actualRanges
)

Assert.Equal<Range>(Map.find "doc1.md" expectedRanges, Map.find "doc1.md" actualRanges)
Assert.Equal<Range>(Map.find "doc2.md" expectedRanges, Map.find "doc2.md" actualRanges)
Assert.Equal<Range * string>(
Map.find "doc2.md" expectedRanges,
Map.find "doc2.md" actualRanges
)

[<Fact>]
let onSubtitle () =
Expand All @@ -157,14 +174,25 @@ module RenameTests =
Map.ofSeq [
"doc1.md",
[|
Range.Mk(4, 9, 4, 15)
Range.Mk(3, 7, 3, 13)
Range.Mk(1, 3, 1, 10)
Range.Mk(4, 9, 4, 15), "new-title"
Range.Mk(3, 7, 3, 13), "New Title"
Range.Mk(1, 3, 1, 10), "New Title"
|]
"doc2.md",
[|
Range.Mk(3, 12, 3, 18), "new-title"
Range.Mk(2, 8, 2, 14), "New Title"
|]
"doc2.md", [| Range.Mk(3, 12, 3, 18); Range.Mk(2, 8, 2, 14) |]
]

let actualRanges = editRanges res
let actualRanges = editsByFile res

Assert.Equal<Range * string>(
Map.find "doc1.md" expectedRanges,
Map.find "doc1.md" actualRanges
)

Assert.Equal<Range>(Map.find "doc1.md" expectedRanges, Map.find "doc1.md" actualRanges)
Assert.Equal<Range>(Map.find "doc2.md" expectedRanges, Map.find "doc2.md" actualRanges)
Assert.Equal<Range * string>(
Map.find "doc2.md" expectedRanges,
Map.find "doc2.md" actualRanges
)
Loading