Skip to content

Commit 802f8fa

Browse files
xperiandriclaude
andcommitted
test: assertions that unwrap options, value options and results (#44)
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 0618d6f commit 802f8fa

4 files changed

Lines changed: 298 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/// <summary>
2+
/// Assertions on F# <see cref="T:Microsoft.FSharp.Core.FSharpOption`1"/>,
3+
/// <see cref="T:Microsoft.FSharp.Core.FSharpValueOption`1"/> and <see cref="T:Microsoft.FSharp.Core.FSharpResult`2"/>
4+
/// values as extensions of <see cref="T:Microsoft.VisualStudio.TestTools.UnitTesting.Assert"/>.
5+
/// <para>
6+
/// The assertions whose names start with <c>Want</c> return the unwrapped value or fail the test; their <c>Is</c>
7+
/// twins discard the value.
8+
/// </para>
9+
/// </summary>
10+
[<AutoOpen>]
11+
module FSharp.Collections.Immutable.Tests.AssertExtensions
12+
13+
open System.Runtime.InteropServices
14+
open Microsoft.VisualStudio.TestTools.UnitTesting
15+
16+
// MSTest takes the message of a failure as a text that is not null, while an optional message that the caller
17+
// omitted is null.
18+
let private messageOrEmpty (message : string | null) : string =
19+
match message with
20+
| null -> ""
21+
| message -> message
22+
23+
type Assert with
24+
25+
/// <summary>
26+
/// Returns the value of <see cref="T:Microsoft.FSharp.Core.FSharpOption`1.Some"/>; fails the test with
27+
/// <paramref name="message"/> on <see cref="T:Microsoft.FSharp.Core.FSharpOption`1.None"/>.
28+
/// </summary>
29+
static member WantSome (value, [<Optional>] message : string | null) =
30+
match value with
31+
| Some some -> some
32+
| None ->
33+
Assert.Fail (messageOrEmpty message)
34+
Unchecked.defaultof<_>
35+
36+
/// <summary>
37+
/// Fails the test with <paramref name="message"/> unless <paramref name="value"/> is
38+
/// <see cref="T:Microsoft.FSharp.Core.FSharpOption`1.Some"/>.
39+
/// </summary>
40+
static member IsSome (value, [<Optional>] message : string | null) = Assert.WantSome (value, message) |> ignore
41+
42+
/// <summary>
43+
/// Fails the test with <paramref name="message"/> unless <paramref name="value"/> is
44+
/// <see cref="T:Microsoft.FSharp.Core.FSharpOption`1.None"/>.
45+
/// </summary>
46+
static member IsNone (value, [<Optional>] message : string | null) =
47+
match value with
48+
| Some _ -> Assert.Fail (messageOrEmpty message)
49+
| None -> ()
50+
51+
/// <summary>
52+
/// Returns the value of <see cref="T:Microsoft.FSharp.Core.FSharpValueOption`1.ValueSome"/>; fails the test
53+
/// with <paramref name="message"/> on <see cref="T:Microsoft.FSharp.Core.FSharpValueOption`1.ValueNone"/>.
54+
/// </summary>
55+
static member WantValueSome (value, [<Optional>] message : string | null) =
56+
match value with
57+
| ValueSome some -> some
58+
| ValueNone ->
59+
Assert.Fail (messageOrEmpty message)
60+
Unchecked.defaultof<_>
61+
62+
/// <summary>
63+
/// Fails the test with <paramref name="message"/> unless <paramref name="value"/> is
64+
/// <see cref="T:Microsoft.FSharp.Core.FSharpValueOption`1.ValueSome"/>.
65+
/// </summary>
66+
static member IsValueSome (value, [<Optional>] message : string | null) = Assert.WantValueSome (value, message) |> ignore
67+
68+
/// <summary>
69+
/// Fails the test with <paramref name="message"/> unless <paramref name="value"/> is
70+
/// <see cref="T:Microsoft.FSharp.Core.FSharpValueOption`1.ValueNone"/>.
71+
/// </summary>
72+
static member IsValueNone (value, [<Optional>] message : string | null) =
73+
match value with
74+
| ValueSome _ -> Assert.Fail (messageOrEmpty message)
75+
| ValueNone -> ()
76+
77+
/// <summary>
78+
/// Returns the value of <see cref="T:Microsoft.FSharp.Core.FSharpResult`2.Ok"/>; fails the test with
79+
/// <paramref name="message"/> and the error on <see cref="T:Microsoft.FSharp.Core.FSharpResult`2.Error"/>.
80+
/// </summary>
81+
static member WantOk (value, [<Optional>] message : string | null) =
82+
match value with
83+
| Ok ok -> ok
84+
| Error error ->
85+
match message with
86+
| null -> Assert.Fail (string error)
87+
| message -> Assert.Fail ($"'{message}': {error}")
88+
Unchecked.defaultof<_>
89+
90+
/// <summary>
91+
/// Fails the test with <paramref name="message"/> unless <paramref name="value"/> is
92+
/// <see cref="T:Microsoft.FSharp.Core.FSharpResult`2.Ok"/>.
93+
/// </summary>
94+
static member IsOk (value, [<Optional>] message : string | null) = Assert.WantOk (value, message) |> ignore
95+
96+
/// <summary>
97+
/// Returns the error of <see cref="T:Microsoft.FSharp.Core.FSharpResult`2.Error"/>; fails the test with
98+
/// <paramref name="message"/> and the value on <see cref="T:Microsoft.FSharp.Core.FSharpResult`2.Ok"/>.
99+
/// </summary>
100+
static member WantError (value, [<Optional>] message : string | null) =
101+
match value with
102+
| Error error -> error
103+
| Ok value ->
104+
match message with
105+
| null -> Assert.Fail (string value)
106+
| message -> Assert.Fail ($"'{message}': {value}")
107+
Unchecked.defaultof<_>
108+
109+
/// <summary>
110+
/// Fails the test with <paramref name="message"/> unless <paramref name="value"/> is
111+
/// <see cref="T:Microsoft.FSharp.Core.FSharpResult`2.Error"/>.
112+
/// </summary>
113+
static member IsError (value, [<Optional>] message : string | null) = Assert.WantError (value, message) |> ignore
114+
115+
/// <summary>
116+
/// Fails the test with <paramref name="message"/> unless <paramref name="value"/> is the default value of its
117+
/// type.
118+
/// </summary>
119+
static member inline IsDefaultOf< ^T> (value : ^T, [<Optional>] message : string) =
120+
Assert.AreEqual (box value, box Unchecked.defaultof< ^T>, message)
121+
122+
/// <summary>
123+
/// Fails the test with <paramref name="message"/> unless <paramref name="actual"/> is
124+
/// <see cref="T:Microsoft.FSharp.Core.FSharpResult`2.Ok"/> holding <paramref name="expected"/>.
125+
/// </summary>
126+
static member inline OkEquals< ^R, 'E> (expected : ^R, actual : Result< ^R, 'E >, [<Optional>] message : string | null) =
127+
Assert.AreEqual (box expected, box (Assert.WantOk (actual, message)), message)
128+
129+
/// <summary>
130+
/// Fails the test with <paramref name="message"/> unless <paramref name="actual"/> is
131+
/// <see cref="T:Microsoft.FSharp.Core.FSharpResult`2.Error"/> holding <paramref name="expected"/>.
132+
/// </summary>
133+
static member inline ErrorEquals<'R, ^E> (expected : ^E, actual : Result<'R, ^E>, [<Optional>] message : string | null) =
134+
Assert.AreEqual (box expected, box (Assert.WantError (actual, message)), message)
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
namespace FSharp.Collections.Immutable.Tests
2+
3+
open System
4+
open Microsoft.VisualStudio.TestTools.UnitTesting
5+
6+
/// <summary>
7+
/// The assertions on options, value options and results that extend
8+
/// <see cref="T:Microsoft.VisualStudio.TestTools.UnitTesting.Assert"/>.
9+
/// </summary>
10+
[<TestClass; AssertExtensionsTestCategory>]
11+
type AssertExtensionsTests () =
12+
13+
/// The message of the failure that an assertion raises.
14+
static let failureOf (assertion : unit -> unit) : string =
15+
let failure = Assert.Throws<AssertFailedException>(Action assertion, "The assertion must fail")
16+
failure.Message
17+
18+
static let assertContains (failure : string) (expected : string) (message : string) =
19+
StringAssert.Contains (failure, expected, StringComparison.Ordinal, message)
20+
21+
// The empty option and value option of a known type: a type annotation cannot sit in a tuple of arguments.
22+
static let noInt : int option = None
23+
static let noValueInt : int voption = ValueNone
24+
25+
// ---- option ----
26+
27+
[<TestMethod>]
28+
member _.``WantSome returns the value of Some`` () =
29+
Assert.AreEqual<int>(5, Assert.WantSome (Some 5, "message"), "WantSome must return the value of Some")
30+
31+
[<TestMethod>]
32+
member _.``WantSome fails with the message on None`` () =
33+
let failure = failureOf (fun () -> Assert.WantSome (noInt, "the value is missing") |> ignore)
34+
assertContains failure "the value is missing" "WantSome must fail with its message on None"
35+
36+
[<TestMethod>]
37+
member _.``IsSome passes for Some and fails for None`` () =
38+
Assert.IsSome (Some 5, "message")
39+
let failure = failureOf (fun () -> Assert.IsSome (noInt, "the value is missing"))
40+
assertContains failure "the value is missing" "IsSome must fail with its message on None"
41+
42+
[<TestMethod>]
43+
member _.``IsNone passes for None and fails for Some`` () =
44+
Assert.IsNone (noInt, "message")
45+
let failure = failureOf (fun () -> Assert.IsNone (Some 5, "the value is there"))
46+
assertContains failure "the value is there" "IsNone must fail with its message on Some"
47+
48+
// ---- voption ----
49+
50+
[<TestMethod>]
51+
member _.``WantValueSome returns the value of ValueSome`` () =
52+
Assert.AreEqual<int>(5, Assert.WantValueSome (ValueSome 5, "message"), "WantValueSome must return the value")
53+
54+
[<TestMethod>]
55+
member _.``WantValueSome fails with the message on ValueNone`` () =
56+
let failure =
57+
failureOf (fun () ->
58+
Assert.WantValueSome (noValueInt, "the value is missing")
59+
|> ignore
60+
)
61+
62+
assertContains failure "the value is missing" "WantValueSome must fail with its message on ValueNone"
63+
64+
[<TestMethod>]
65+
member _.``IsValueSome passes for ValueSome and fails for ValueNone`` () =
66+
Assert.IsValueSome (ValueSome 5, "message")
67+
let failure = failureOf (fun () -> Assert.IsValueSome (noValueInt, "the value is missing"))
68+
assertContains failure "the value is missing" "IsValueSome must fail with its message on ValueNone"
69+
70+
[<TestMethod>]
71+
member _.``IsValueNone passes for ValueNone and fails for ValueSome`` () =
72+
Assert.IsValueNone (noValueInt, "message")
73+
let failure = failureOf (fun () -> Assert.IsValueNone (ValueSome 5, "the value is there"))
74+
assertContains failure "the value is there" "IsValueNone must fail with its message on ValueSome"
75+
76+
// ---- result ----
77+
78+
[<TestMethod>]
79+
member _.``WantOk returns the value of Ok`` () =
80+
let result : Result<int, string> = Ok 5
81+
Assert.AreEqual<int>(5, Assert.WantOk (result, "message"), "WantOk must return the value of Ok")
82+
83+
[<TestMethod>]
84+
member _.``WantOk fails with the message and the error on Error`` () =
85+
let result : Result<int, string> = Error "the error"
86+
let failure = failureOf (fun () -> Assert.WantOk (result, "the call must succeed") |> ignore)
87+
assertContains failure "the call must succeed" "WantOk must fail with its message on Error"
88+
assertContains failure "the error" "WantOk must fail with the error"
89+
90+
[<TestMethod>]
91+
member _.``WantOk fails with the error alone when it has no message`` () =
92+
let result : Result<int, string> = Error "the error"
93+
let failure = failureOf (fun () -> Assert.WantOk (result) |> ignore)
94+
assertContains failure "the error" "WantOk must fail with the error"
95+
96+
[<TestMethod>]
97+
member _.``IsOk passes for Ok and fails for Error`` () =
98+
let ok : Result<int, string> = Ok 5
99+
let error : Result<int, string> = Error "the error"
100+
Assert.IsOk (ok, "message")
101+
let failure = failureOf (fun () -> Assert.IsOk (error, "the call must succeed"))
102+
assertContains failure "the call must succeed" "IsOk must fail with its message on Error"
103+
104+
[<TestMethod>]
105+
member _.``WantError returns the error of Error`` () =
106+
let result : Result<int, string> = Error "the error"
107+
Assert.AreEqual<string>("the error", Assert.WantError (result, "message"), "WantError must return the error")
108+
109+
[<TestMethod>]
110+
member _.``WantError fails with the message and the value on Ok`` () =
111+
let result : Result<int, string> = Ok 5
112+
let failure = failureOf (fun () -> Assert.WantError (result, "the call must fail") |> ignore)
113+
assertContains failure "the call must fail" "WantError must fail with its message on Ok"
114+
assertContains failure "5" "WantError must fail with the value"
115+
116+
[<TestMethod>]
117+
member _.``IsError passes for Error and fails for Ok`` () =
118+
let ok : Result<int, string> = Ok 5
119+
let error : Result<int, string> = Error "the error"
120+
Assert.IsError (error, "message")
121+
let failure = failureOf (fun () -> Assert.IsError (ok, "the call must fail"))
122+
assertContains failure "the call must fail" "IsError must fail with its message on Ok"
123+
124+
[<TestMethod>]
125+
member _.``OkEquals passes for the expected value and fails for another value or for Error`` () =
126+
let ok : Result<int, string> = Ok 5
127+
Assert.OkEquals (5, ok, "message")
128+
let other : Result<int, string> = Ok 6
129+
let different = failureOf (fun () -> Assert.OkEquals (5, other, "the value must be 5"))
130+
assertContains different "the value must be 5" "OkEquals must fail with its message on another value"
131+
let error : Result<int, string> = Error "the error"
132+
let failure = failureOf (fun () -> Assert.OkEquals (5, error, "the call must succeed"))
133+
assertContains failure "the error" "OkEquals must fail with the error on Error"
134+
135+
[<TestMethod>]
136+
member _.``ErrorEquals passes for the expected error and fails for another error or for Ok`` () =
137+
let error : Result<int, string> = Error "the error"
138+
Assert.ErrorEquals ("the error", error, "message")
139+
let other : Result<int, string> = Error "another error"
140+
let different =
141+
failureOf (fun () -> Assert.ErrorEquals ("the error", other, "the error must be the expected one"))
142+
assertContains different "the error must be the expected one" "ErrorEquals must fail with its message on another error"
143+
let ok : Result<int, string> = Ok 5
144+
let failure = failureOf (fun () -> Assert.ErrorEquals ("the error", ok, "the call must fail"))
145+
assertContains failure "5" "ErrorEquals must fail with the value on Ok"
146+
147+
// ---- default values ----
148+
149+
[<TestMethod>]
150+
member _.``IsDefaultOf passes for the default value and fails for another one`` () =
151+
Assert.IsDefaultOf (0, "message")
152+
Assert.IsDefaultOf (noInt, "message")
153+
let number = failureOf (fun () -> Assert.IsDefaultOf (1, "the number must be the default"))
154+
assertContains number "the number must be the default" "IsDefaultOf must fail with its message on a non-default number"
155+
let option = failureOf (fun () -> Assert.IsDefaultOf (Some 1, "the option must be the default"))
156+
assertContains option "the option must be the default" "IsDefaultOf must fail with its message on a non-default option"

‎tests/FSharp.Collections.Immutable.Tests/FSharp.Collections.Immutable.Tests.fsproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20+
<Compile Include="Assert.fs" />
2021
<Compile Include="TestCategories.fs" />
22+
<Compile Include="AssertExtensionsTests.fs" />
2123
<Compile Include="Contracts\Mirror.fs" />
2224
<Compile Include="Contracts\ExceptionAssert.fs" />
2325
<Compile Include="Contracts\Inputs.fs" />

‎tests/FSharp.Collections.Immutable.Tests/TestCategories.fs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ open Microsoft.VisualStudio.TestTools.UnitTesting
88
// The attributes go on the test classes only, for the collection and the area that a class examines; test methods carry
99
// no categories (ADR 0001, D8).
1010

11+
/// The assertions that extend `Assert` (`Assert.fs`).
12+
type AssertExtensionsTestCategoryAttribute () =
13+
inherit TestCategoryBaseAttribute ()
14+
static let categories : IList<string> = [| "Assert extensions" |]
15+
override _.TestCategories = categories
16+
1117
/// The builder functions and the builder modules.
1218
type BuilderTestCategoryAttribute () =
1319
inherit TestCategoryBaseAttribute ()

0 commit comments

Comments
 (0)