|
| 1 | +module Fable.Tests.Nullness |
| 2 | + |
| 3 | +open Util.Testing |
| 4 | +open Fable.Core.JsInterop |
| 5 | + |
| 6 | +type ABNull = |
| 7 | + | A |
| 8 | + | B of (string | null) |
| 9 | + |
| 10 | +let tests = |
| 11 | + testList |
| 12 | + "Nullness" |
| 13 | + [ |
| 14 | + testCase |
| 15 | + "nullArgCheck" |
| 16 | + (fun () -> |
| 17 | + let ex = |
| 18 | + try |
| 19 | + nullArgCheck "arg" null |
| 20 | + with e -> |
| 21 | + e |
| 22 | + |
| 23 | + equal "Value cannot be null. (Parameter 'arg')" ex.Message |
| 24 | + ) |
| 25 | + |
| 26 | + testCase "Null active pattern works" |
| 27 | + <| fun () -> |
| 28 | + let getLength abnull = |
| 29 | + match abnull with |
| 30 | + | A -> 0 |
| 31 | + | B Null -> 0 |
| 32 | + | B(NonNull s) -> s.Length // `s` is derived to be `string` |
| 33 | + |
| 34 | + equal (getLength A) 0 |
| 35 | + equal (getLength (B null)) 0 |
| 36 | + |
| 37 | + testCase "NonNull active pattern works" |
| 38 | + <| fun () -> |
| 39 | + let getLength abnull = |
| 40 | + match abnull with |
| 41 | + | A -> 0 |
| 42 | + | B Null -> 0 |
| 43 | + | B(NonNull s) -> s.Length // `s` is derived to be `string` |
| 44 | + |
| 45 | + equal (getLength (B "hello")) 5 |
| 46 | + |
| 47 | + testCase "works with generics" |
| 48 | + <| fun _ -> |
| 49 | + // Generic code, note 'T must be constrained to be a reference type |
| 50 | + let findOrNull (index: int) (list: 'T list) : 'T | null when 'T: not struct = |
| 51 | + match List.tryItem index list with |
| 52 | + | Some item -> item |
| 53 | + | None -> null |
| 54 | + |
| 55 | + equal (findOrNull 1 [ "a"; "b"; "c" ]) "b" |
| 56 | + equal (findOrNull 3 [ "a"; "b"; "c" ]) null |
| 57 | + |
| 58 | +#if FABLE_COMPILER |
| 59 | + testCase "works for interop with undefined" |
| 60 | + <| fun () -> |
| 61 | + let maybeUndefined (value: string) : (string | null) = importMember "./js/nullness.js" |
| 62 | + |
| 63 | + match maybeUndefined "ok" with |
| 64 | + | NonNull _ -> equal true true |
| 65 | + | Null -> equal true false |
| 66 | + |
| 67 | + match maybeUndefined "foo" with |
| 68 | + | NonNull _ -> equal true false |
| 69 | + | Null -> equal true true |
| 70 | + |
| 71 | + testCase "works for interop with null" |
| 72 | + <| fun () -> |
| 73 | + let maybeNull (value: string) : (string | null) = importMember "./js/nullness.js" |
| 74 | + |
| 75 | + match maybeNull "ok" with |
| 76 | + | NonNull _ -> equal true true |
| 77 | + | Null -> equal true false |
| 78 | + |
| 79 | + match maybeNull "foo" with |
| 80 | + | NonNull _ -> equal true false |
| 81 | + | Null -> equal true true |
| 82 | +#endif |
| 83 | + |
| 84 | + testCase "Unchecked.nonNull works" |
| 85 | + <| fun () -> |
| 86 | + let toUpper (text: string | null) = |
| 87 | + (Unchecked.nonNull text).ToUpperInvariant() |
| 88 | + |
| 89 | + equal (toUpper "hello") "HELLO" |
| 90 | + ] |
0 commit comments