Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions samples/OptionAnalyzer.Test/OptionAnalyzer.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="TestHelpers.fs" />
<Compile Include="UnitTests.fs" />
</ItemGroup>

Expand Down
105 changes: 105 additions & 0 deletions samples/OptionAnalyzer.Test/TestHelpers.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
module OptionAnalyzer.TestHelpers

open NUnit.Framework
open FSharp.Analyzers.SDK
open FSharp.Analyzers.SDK.Testing

let runtimeTfm =
let v = System.Environment.Version

"net"
+ string v.Major
+ "."
+ string v.Minor

let testPackages: Package list =
[
{
Name = "Newtonsoft.Json"
Version = "13.0.3"
}
{
Name = "Fantomas.FCS"
Version = "6.2.0"
}
]

let mkTestProjectOptions () =
mkOptionsFromProject runtimeTfm testPackages

let mkTestProjectSnapshot () =
mkSnapshotFromProject runtimeTfm testPackages

/// Source snippets shared by RunAnalyzersSafely and RunAnalyzers tests.
module ClientTestSources =

let optionValue =
"""
module M

let notUsed() =
let option : Option<int> = None
option.Value
"""

let ignoreNextLine =
"""
module M

let notUsed() =
let option : Option<int> = None
// fsharpanalyzer: ignore-line-next OV001
option.Value
"""

let ignoreCurrentLine =
"""
module M

let notUsed() =
let option : Option<int> = None
option.Value // fsharpanalyzer: ignore-line OV001
"""

let ignoreFile =
"""
// fsharpanalyzer: ignore-file OV001
module M

let notUsed() =
let option : Option<int> = None
option.Value
"""

let ignoreRange =
"""
// fsharpanalyzer: ignore-region-start OV001
module M

let notUsed() =
let option : Option<int> = None
option.Value
// fsharpanalyzer: ignore-region-end
"""

/// Create a Client, load analyzers from the current directory,
/// and return the client and load stats.
let loadAnalyzers () =
let client = Client<CliAnalyzerAttribute, CliContext>()
let path = System.IO.Path.GetFullPath(".")
let stats = client.LoadAnalyzers(path)
client, stats

/// Assert that the first AnalysisResult from RunAnalyzersSafely
/// contains messages (expectEmpty = false) or is empty (expectEmpty = true).
let assertSafeResult expectEmpty (results: AnalysisResult list) =
match List.tryHead results with
| Some result ->
match result.Output with
| Ok msgs ->
if expectEmpty then
Assert.That(msgs, Is.Empty)
else
Assert.That(msgs, Is.Not.Empty)
| Error ex -> Assert.Fail(sprintf "Expected messages but got exception: %A" ex)
| None -> Assert.Fail("Expected at least one analyzer result")
Loading
Loading