Skip to content

Commit

Permalink
Tests.Core: apply all suggested fixes
Browse files Browse the repository at this point in the history
Make ApplyQuickFix method apply all fixes and not only first
one. It's necessary when testing rules that can create more
than one suggested fix.

Co-authored-by: Mehrshad <[email protected]>
  • Loading branch information
webwarrior-ws and Mersho committed Dec 27, 2023
1 parent eefb548 commit 2821987
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions tests/FSharpLint.Core.Tests/Rules/TestRuleBase.fs
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,27 @@ type TestRuleBase () =
Assert.IsFalse(this.ErrorsExist, "Expected no errors, but was: " + this.ErrorMsg)

member this.ApplyQuickFix (source:string) =
let firstSuggestedFix =
let suggestedFixes =
suggestions
|> Seq.choose (fun x -> x.Details.SuggestedFix)
|> Seq.tryHead

match firstSuggestedFix |> Option.bind (fun x -> x.Value) with
| Some(fix) ->
let startIndex = ExpressionUtilities.findPos fix.FromRange.Start source
let endIndex = ExpressionUtilities.findPos fix.FromRange.End source

match (startIndex, endIndex) with
| (Some(startIndex), Some(endIndex)) ->
(StringBuilder source)
.Remove(startIndex, endIndex - startIndex)
.Insert(startIndex, fix.ToText)
.ToString()
| _ -> source
| None -> source

Seq.fold
(fun (source: string) (lazyFix: Lazy<Option<SuggestedFix>>) ->
match lazyFix.Value with
| Some(fix) ->
let startIndex = ExpressionUtilities.findPos fix.FromRange.Start source
let endIndex = ExpressionUtilities.findPos fix.FromRange.End source

match (startIndex, endIndex) with
| (Some(startIndex), Some(endIndex)) ->
(StringBuilder source)
.Remove(startIndex, endIndex - startIndex)
.Insert(startIndex, fix.ToText)
.ToString()
| _ -> source
| None -> source )
source
suggestedFixes

[<SetUp>]
member __.SetUp() = suggestions.Clear()

0 comments on commit 2821987

Please sign in to comment.