Skip to content

Commit

Permalink
Correctly run other string tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Oct 4, 2023
1 parent b52ed91 commit 6646fa7
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 23 deletions.
42 changes: 31 additions & 11 deletions tests/FSharp.Analyzers.Tests/StringAnalyzerTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,56 @@ type TestCases() =

interface IEnumerable with
member _.GetEnumerator () : IEnumerator =
constructTestCaseEnumerator [| "string" ; "endswith" |]
[| "endswith" ; "indexof" ; "startswith" |]
|> Seq.collect (fun subFolder ->
let folder = Path.Combine (dataFolder, "string", subFolder)
Directory.EnumerateFiles (folder, "*.fs")
)
|> constructTestCaseEnumeratorAux

let findStringAnalyzerFor (fileName : string) =
fileName.Split Path.DirectorySeparatorChar
|> Array.skip 1
|> Array.head
|> function
| "endswith" -> StringAnalyzer.endsWithAnalyzer
| "startswith" -> StringAnalyzer.startsWithAnalyzer
| "indexof" -> StringAnalyzer.indexOfAnalyzer
| unknown -> failwithf $"Unknown subfolder \"%s{unknown}\", please configure analyzer"

[<TestCaseSource(typeof<TestCases>)>]
let EndsWithTests (fileName : string) =
let StringTests (fileName : string) =
task {
let fileName = Path.Combine (dataFolder, fileName)
let fullPath = Path.Combine (dataFolder, fileName)

let! messages =
File.ReadAllText fileName
File.ReadAllText fullPath
|> getContext projectOptions
|> StringAnalyzer.endsWithAnalyzer
|> findStringAnalyzerFor fileName

do! assertExpected fileName messages
do! assertExpected fullPath messages
}

type NegativeTestCases() =

interface IEnumerable with
member _.GetEnumerator () : IEnumerator =
constructTestCaseEnumerator [| "string" ; "endswith" ; "negative" |]
[| "endswith" ; "indexof" ; "startswith" |]
|> Seq.collect (fun subFolder ->
let folder = Path.Combine (dataFolder, "string", subFolder, "negative")
Directory.EnumerateFiles (folder, "*.fs")
)
|> constructTestCaseEnumeratorAux

[<TestCaseSource(typeof<NegativeTestCases>)>]
let NegativeEndsWithTests (fileName : string) =
let NegativeStringTests (fileName : string) =
task {
let fileName = Path.Combine (dataFolder, fileName)
let fullPath = Path.Combine (dataFolder, fileName)

let! messages =
File.ReadAllText fileName
File.ReadAllText fullPath
|> getContext projectOptions
|> StringAnalyzer.endsWithAnalyzer
|> findStringAnalyzerFor fileName

Assert.IsEmpty messages
}
13 changes: 9 additions & 4 deletions tests/FSharp.Analyzers.Tests/Testing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Testing =

open System
open System.IO
open System.Collections
open System.Threading.Tasks
open NUnit.Framework
open FSharp.Analyzers.SDK
Expand Down Expand Up @@ -48,12 +49,16 @@ module Testing =

let dataFolder = Path.Combine (__SOURCE_DIRECTORY__, "..", "data")

let constructTestCaseEnumerator (subDataPath : string array) =
let testsDirectory = Path.Combine (dataFolder, Path.Combine subDataPath)

Directory.EnumerateFiles (testsDirectory, "*.fs")
let constructTestCaseEnumeratorAux (files : string seq) : IEnumerator =
files
|> Seq.map (fun f ->
let fileName = Path.GetRelativePath (dataFolder, f)
[| fileName :> obj |]
)
|> fun s -> s.GetEnumerator ()

let constructTestCaseEnumerator (subDataPath : string array) =
let testsDirectory = Path.Combine (dataFolder, Path.Combine subDataPath)

Directory.EnumerateFiles (testsDirectory, "*.fs")
|> constructTestCaseEnumeratorAux
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GRA-003 | Warning | (1,6 - 1,13) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
GRA-STRING-003 | Warning | (1,6 - 1,13) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GRA-003 | Warning | (2,4 - 2,11) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
GRA-STRING-003 | Warning | (2,4 - 2,11) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GRA-003 | Warning | (4,4 - 4,11) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
GRA-STRING-003 | Warning | (4,4 - 4,11) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GRA-003 | Warning | (2,2 - 2,9) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
GRA-STRING-003 | Warning | (2,2 - 2,9) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GRA-002 | Warning | (1,6 - 1,16) | The usage of String.StartsWith with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
GRA-STRING-002 | Warning | (1,6 - 1,16) | The usage of String.StartsWith with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GRA-002 | Warning | (2,4 - 2,14) | The usage of String.StartsWith with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
GRA-STRING-002 | Warning | (2,4 - 2,14) | The usage of String.StartsWith with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GRA-002 | Warning | (4,4 - 4,14) | The usage of String.StartsWith with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
GRA-STRING-002 | Warning | (4,4 - 4,14) | The usage of String.StartsWith with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GRA-002 | Warning | (2,2 - 2,12) | The usage of String.StartsWith with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
GRA-STRING-002 | Warning | (2,2 - 2,12) | The usage of String.StartsWith with a single string argument is discouraged. Signal your intention explicitly by calling an overload.

0 comments on commit 6646fa7

Please sign in to comment.