|
| 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" |
0 commit comments