forked from ionide/FSharp.Analyzers.SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit Testing.fsx
70 lines (58 loc) · 2.89 KB
/
Unit Testing.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(**
---
category: end-users
categoryindex: 2
index: 4
---
# Unit testing an analyzer
To help analyzer authors testing their analyzers, there's a dedicated [testing package](https://www.nuget.org/packages/FSharp.Analyzers.SDK.Testing).
It contains easy to use utility functions to create a context for the analyzer and assertion helpers.
[`FSharp.Analyzers.SDK.Testing.mkOptionsFromProject`](../reference/fsharp-analyzers-sdk-testing.html#mkOptionsFromProject) creates the [`FSharpProjectOptions`](https://fsharp.github.io/fsharp-compiler-docs/reference/fsharp-compiler-codeanalysis-fsharpprojectoptions.html) for the given framework (e.g. `net7.0`) and the given list of packages to reference.
[`FSharp.Analyzers.SDK.Testing.getContext`](../reference/fsharp-analyzers-sdk-testing.html#getContext) then takes the `FSharpProjectOptions` and the source code to test and creates a [`CliContext`](../reference/fsharp-analyzers-sdk-clicontext.html) you can pass along to your analyzer function.
The module [`FSharp.Analyzers.SDK.Testing.Assert`](../reference/fsharp-analyzers-sdk-testing-assert.html) offers various functions to help with assertion statements from your favorite unit testing framework.
For a complete example of an unit testing project, take a look at [`OptionAnalyzer.Test`](https://github.com/ionide/FSharp.Analyzers.SDK/tree/7b7ec530c507d765ab18d93ebb7aa45ab59accc2/samples/OptionAnalyzer.Test) in the `samples` folder of the SDK repository.
*)
(*** hide ***)
#r "../../artifacts/bin/FSharp.Analyzers.Cli/release/FSharp.Compiler.Service.dll"
#r "../../artifacts/bin/FSharp.Analyzers.SDK/release/FSharp.Analyzers.SDK.dll"
#r "../../artifacts/bin/FSharp.Analyzers.SDK.Testing/release_net8.0/FSharp.Analyzers.SDK.Testing.dll"
#r "../../artifacts/bin/OptionAnalyzer.Test/release/nunit.framework.dll"
#r "../../artifacts/bin/OptionAnalyzer.Test/release/OptionAnalyzer.dll"
(** *)
open FSharp.Compiler.CodeAnalysis
open FSharp.Analyzers.SDK.Testing
open OptionAnalyzer
open NUnit.Framework
let mutable projectOptions: FSharpProjectOptions = FSharpProjectOptions.zero
[<SetUp>]
let Setup () =
task {
let! opts =
mkOptionsFromProject
"net7.0"
[
// The SDK uses this in a "dotnet add package x --version y" command
// to generate the needed FSharpProjectOptions
{ Name = "Newtonsoft.Json"
Version = "13.0.3" } ]
projectOptions <- opts
}
[<Test>]
let ``warnings are emitted`` () =
async {
let source =
"""
module M
let notUsed() =
let option : Option<int> = None
option.Value
"""
let ctx = getContext projectOptions source
let! msgs = optionValueAnalyzer ctx
Assert.IsNotEmpty msgs
Assert.IsTrue(Assert.messageContains "Option.Value" msgs[0])
}
(**
[Previous]({{fsdocs-previous-page-link}})
[Next]({{fsdocs-next-page-link}})
*)