Skip to content

Commit 7f0b362

Browse files
committed
Tests of bind, map and mapError
1 parent 973f648 commit 7f0b362

File tree

1 file changed

+45
-3
lines changed
  • src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Core

1 file changed

+45
-3
lines changed

src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Core/ResultTests.fs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ type EmailValidation=
1313
| Empty
1414
| NoAt
1515

16-
module Results=
17-
let bind f m = match m with Error e -> Error e | Ok x -> f x
1816

19-
open Results
17+
open Result
2018

2119
[<TestFixture>]
2220
type ResultTests() =
@@ -42,3 +40,47 @@ type ResultTests() =
4240
test_validate_email "something_else" (Error NoAt)
4341
test_validate_email "some@email.com" (Ok "some@email.com")
4442

43+
let toUpper (v:string) = v.ToUpper()
44+
45+
let shouldBeOkWithValue expected maybeOk = match maybeOk with | Error e-> failwith "Expected Ok, got Error!" | Ok v->Assert.AreEqual(expected, v)
46+
47+
let shouldBeErrorWithValue expected maybeError = match maybeError with | Error e-> Assert.AreEqual(expected, e) | Ok v-> failwith "Expected Error, got Ok!"
48+
49+
[<Test>]
50+
member this.MapWillTransformOkValues() =
51+
Ok "some@email.com"
52+
|> map toUpper
53+
|> shouldBeOkWithValue "SOME@EMAIL.COM"
54+
55+
[<Test>]
56+
member this.MapWillNotTransformErrorValues() =
57+
Error "my error"
58+
|> map toUpper
59+
|> shouldBeErrorWithValue "my error"
60+
61+
[<Test>]
62+
member this.MapErrorWillTransformErrorValues() =
63+
Error "my error"
64+
|> mapError toUpper
65+
|> shouldBeErrorWithValue "MY ERROR"
66+
67+
[<Test>]
68+
member this.MapErrorWillNotTransformOkValues() =
69+
Ok "some@email.com"
70+
|> mapError toUpper
71+
|> shouldBeOkWithValue "some@email.com"
72+
73+
let addOneOk (v:int) = Ok (v+1)
74+
[<Test>]
75+
member this.BindShouldModifyOkValue() =
76+
Ok 42
77+
|> bind addOneOk
78+
|> shouldBeOkWithValue 43
79+
80+
[<Test>]
81+
member this.BindErrorShouldNotModifyError() =
82+
Error "Error"
83+
|> bind addOneOk
84+
|> shouldBeErrorWithValue "Error"
85+
86+

0 commit comments

Comments
 (0)