|
| 1 | +module MapFromAmongTest |
| 2 | + |
| 3 | + |
| 4 | +open System |
| 5 | +open NUnit.Framework |
| 6 | +open Swensen.Unquote |
| 7 | +open FSharp.SystemCommandLine |
| 8 | +open Utils |
| 9 | +open Input |
| 10 | + |
| 11 | +let mutable actionCalled = false |
| 12 | +let callAction() = actionCalled <- true |
| 13 | +[<SetUp>] |
| 14 | +let setup () = actionCalled <- false |
| 15 | + |
| 16 | +type DUType = |
| 17 | + | A |
| 18 | + | B |
| 19 | + |
| 20 | +let duChoices = [ |
| 21 | + "a", A |
| 22 | + B.ToString() (* "B" *), B |
| 23 | +] |
| 24 | + |
| 25 | +[<Test>] |
| 26 | +let ``01 - mapFromAmong requires input``() = |
| 27 | + let input = |
| 28 | + option<DUType> "--du" |> mapFromAmong duChoices |
| 29 | + testRootCommand "--du" { |
| 30 | + description "Test" |
| 31 | + inputs input |
| 32 | + setAction (ignore >> callAction) |
| 33 | + } <>! 0 |
| 34 | + actionCalled <>! true |
| 35 | + |
| 36 | +[<Test>] |
| 37 | +let ``02 - mapFromAmong returns correct typed DU value``() = |
| 38 | + let input = option<DUType> "--du" |> mapFromAmong duChoices |
| 39 | + let compareAgainst (shouldSucceed: bool): string -> DUType -> unit = fun cmd v -> |
| 40 | + testRootCommand cmd { |
| 41 | + description "Test" |
| 42 | + inputs input |
| 43 | + setAction (fun o -> |
| 44 | + if shouldSucceed |
| 45 | + then o =! v |> callAction; 0 |
| 46 | + else o <>! v; 1 |
| 47 | + ) |
| 48 | + } |
| 49 | + |> if shouldSucceed then (=!) 0 else (<>!) 0 |
| 50 | + actionCalled =! shouldSucceed |
| 51 | + actionCalled <- false |
| 52 | + let shouldSucceed = compareAgainst true |
| 53 | + let shouldFail = compareAgainst false |
| 54 | + // valid casing |
| 55 | + shouldSucceed "--du a" A |
| 56 | + shouldSucceed "--du B" B |
| 57 | + // invalid casing |
| 58 | + shouldFail "--du A" A |
| 59 | + shouldFail "--du b" B |
| 60 | + // invalid input |
| 61 | + shouldFail "--du c" A |
| 62 | + shouldFail "--du c" B |
| 63 | + // invalid map |
| 64 | + shouldFail "--du a" B |
| 65 | + shouldFail "--du B" A |
| 66 | + |
| 67 | + |
| 68 | +[<Test>] |
| 69 | +let ``03 - mapFromAmongWith returns correct typed DU - case insensitive``() = |
| 70 | + let input = option<DUType> "--du" |> mapFromAmongWith StringComparer.OrdinalIgnoreCase duChoices |
| 71 | + let compareAgainst (shouldSucceed: bool): string -> DUType -> unit = fun cmd v -> |
| 72 | + testRootCommand cmd { |
| 73 | + description "Test" |
| 74 | + inputs input |
| 75 | + setAction (fun o -> |
| 76 | + if shouldSucceed |
| 77 | + then o =! v |> callAction; 0 |
| 78 | + else o <>! v; 1 |
| 79 | + ) |
| 80 | + } |
| 81 | + |> if shouldSucceed then (=!) 0 else (<>!) 0 |
| 82 | + actionCalled =! shouldSucceed |
| 83 | + actionCalled <- false |
| 84 | + let shouldSucceed = compareAgainst true |
| 85 | + let shouldFail = compareAgainst false |
| 86 | + shouldSucceed "--du a" A |
| 87 | + shouldSucceed "--du A" A |
| 88 | + shouldSucceed "--du b" B |
| 89 | + shouldSucceed "--du B" B |
| 90 | + // invalid input |
| 91 | + shouldFail "--du c" A |
| 92 | + shouldFail "--du c" B |
| 93 | + // invalid map |
| 94 | + shouldFail "--du a" B |
| 95 | + shouldFail "--du B" A |
| 96 | + |
| 97 | +[<Test>] |
| 98 | +let ``04 - mapFromAmong followed by different tryParse will override``() = |
| 99 | + let input = option<DUType> "--du" |> mapFromAmong duChoices |> tryParse (fun _ -> Ok A) |
| 100 | + let compareAgainst (shouldSucceed: bool): string -> DUType -> unit = fun cmd v -> |
| 101 | + testRootCommand cmd { |
| 102 | + description "Test" |
| 103 | + inputs input |
| 104 | + setAction (fun o -> |
| 105 | + if shouldSucceed |
| 106 | + then o =! v |> callAction; 0 |
| 107 | + else o <>! v; 1 |
| 108 | + ) |
| 109 | + } |
| 110 | + |> if shouldSucceed then (=!) 0 else (<>!) 0 |
| 111 | + actionCalled =! shouldSucceed |
| 112 | + actionCalled <- false |
| 113 | + let shouldSucceed = compareAgainst true |
| 114 | + let shouldFail = compareAgainst false |
| 115 | + shouldSucceed "--du a" A |
| 116 | + shouldSucceed "--du A" A |
| 117 | + shouldSucceed "--du b" A |
| 118 | + shouldSucceed "--du B" A |
| 119 | + shouldFail "--du a" B |
| 120 | + shouldFail "--du A" B |
| 121 | + shouldFail "--du b" B |
| 122 | + shouldFail "--du B" B |
| 123 | + // invalid input still processes |
| 124 | + // completions will still show correctly |
| 125 | + // undefined behaviour |
| 126 | + shouldSucceed "--du c" A |
| 127 | + shouldFail "--du c" B |
0 commit comments