|
1 | 1 | module ListOptionTest |
2 | 2 |
|
3 | | - |
4 | 3 | open NUnit.Framework |
5 | 4 | open Swensen.Unquote |
6 | 5 | open FSharp.SystemCommandLine |
7 | 6 | open Utils |
8 | 7 | open Input |
9 | 8 |
|
10 | | -let mutable handlerCalled = false |
11 | | -let called() = handlerCalled <- true |
12 | | -[<SetUp>] |
13 | | -let setup () = handlerCalled <- false |
14 | | - |
15 | | -[<Test>] |
16 | | -let ``01 - No input to string list option should be empty list``() = |
17 | | - let input = option<string list> "-p" |
18 | | - let commandRunner (shouldSucceed: bool): string -> (string list -> bool) -> unit = fun command comp -> |
| 9 | +/// Runs `command` against a root command with the single list `input`. |
| 10 | +/// Returns `Some values` if the action ran, or `None` if parsing failed. |
| 11 | +let run (input: ActionInput<'T list>) (command: string) = |
| 12 | + let mutable received = None |
| 13 | + let exitCode = |
19 | 14 | testRootCommand command { |
20 | 15 | description "Test" |
21 | 16 | inputs input |
22 | | - setAction (function |
23 | | - | values when comp values -> called(); 0 |
24 | | - | _ -> 1 |
25 | | - ) |
| 17 | + setAction (fun values -> received <- Some values; 0) |
26 | 18 | } |
27 | | - |> if shouldSucceed then (=!) 0 else (<>!) 0 |
28 | | - handlerCalled =! shouldSucceed |
29 | | - handlerCalled <- false |
30 | | - let shouldSucceed = commandRunner true |
31 | | - let shouldFail = commandRunner false |
32 | | - |
33 | | - shouldSucceed "" List.isEmpty |
34 | | - shouldSucceed "-p a" (List.isEmpty >> not) |
35 | | - shouldFail "-p a" List.isEmpty |
36 | | - |
| 19 | + match received with |
| 20 | + | Some _ -> exitCode =! 0 |
| 21 | + | None -> exitCode <>! 0 |
| 22 | + received |
| 23 | + |
37 | 24 | [<Test>] |
38 | | -let ``02 - No input to int list option should be empty list``() = |
39 | | - let input = option<int list> "-p" |
40 | | - let commandRunner (shouldSucceed: bool): string -> (int list -> bool) -> unit = fun command comp -> |
41 | | - testRootCommand command { |
42 | | - description "Test" |
43 | | - inputs input |
44 | | - setAction (function |
45 | | - | values when comp values -> called(); 0 |
46 | | - | _ -> 1 |
47 | | - ) |
48 | | - } |
49 | | - |> if shouldSucceed then (=!) 0 else (<>!) 0 |
50 | | - handlerCalled =! shouldSucceed |
51 | | - handlerCalled <- false |
52 | | - let shouldSucceed = commandRunner true |
53 | | - let shouldFail = commandRunner false |
54 | | - |
55 | | - shouldSucceed "" List.isEmpty |
56 | | - shouldSucceed "-p 3" (List.isEmpty >> not) |
57 | | - shouldFail "-p a" List.isEmpty |
| 25 | +let ``01 - string list option`` () = |
| 26 | + let input = option<string list> "-p" |
| 27 | + run input "" =! Some [] |
| 28 | + run input "-p a" =! Some [ "a" ] |
| 29 | + run input "-p a -p b -p c" =! Some [ "a"; "b"; "c" ] |
58 | 30 |
|
| 31 | +[<Test>] |
| 32 | +let ``02 - int list option`` () = |
| 33 | + let input = option<int list> "-p" |
| 34 | + run input "" =! Some [] |
| 35 | + run input "-p 3 -p 1 -p 2" =! Some [ 3; 1; 2 ] |
| 36 | + run input "-p a" =! None |
| 37 | + run input "-p 99999999999" =! None |
59 | 38 |
|
60 | 39 | [<Test>] |
61 | | -let ``03 - No input to string list argument should be empty list``() = |
| 40 | +let ``03 - string list argument`` () = |
62 | 41 | let input = argument<string list> "p" |
63 | | - let commandRunner (shouldSucceed: bool): string -> (string list -> bool) -> unit = fun command comp -> |
64 | | - testRootCommand command { |
65 | | - description "Test" |
66 | | - inputs input |
67 | | - setAction (function |
68 | | - | values when comp values -> called(); 0 |
69 | | - | _ -> 1 |
70 | | - ) |
71 | | - } |
72 | | - |> if shouldSucceed then (=!) 0 else (<>!) 0 |
73 | | - handlerCalled =! shouldSucceed |
74 | | - handlerCalled <- false |
75 | | - let shouldSucceed = commandRunner true |
76 | | - let shouldFail = commandRunner false |
77 | | - |
78 | | - shouldSucceed "" List.isEmpty |
79 | | - shouldSucceed "a" (List.isEmpty >> not) |
80 | | - shouldFail "a" List.isEmpty |
81 | | - |
| 42 | + run input "" =! Some [] |
| 43 | + run input "a b" =! Some [ "a"; "b" ] |
| 44 | + |
82 | 45 | [<Test>] |
83 | | -let ``04 - No input to int list argument should be empty list``() = |
| 46 | +let ``04 - int list argument`` () = |
84 | 47 | let input = argument<int list> "p" |
85 | | - let commandRunner (shouldSucceed: bool): string -> (int list -> bool) -> unit = fun command comp -> |
86 | | - testRootCommand command { |
87 | | - description "Test" |
88 | | - inputs input |
89 | | - setAction (function |
90 | | - | values when comp values -> called(); 0 |
91 | | - | _ -> 1 |
92 | | - ) |
93 | | - } |
94 | | - |> if shouldSucceed then (=!) 0 else (<>!) 0 |
95 | | - handlerCalled =! shouldSucceed |
96 | | - handlerCalled <- false |
97 | | - let shouldSucceed = commandRunner true |
98 | | - let shouldFail = commandRunner false |
99 | | - |
100 | | - shouldSucceed "" List.isEmpty |
101 | | - shouldSucceed "3" (List.isEmpty >> not) |
102 | | - shouldFail "a" List.isEmpty |
| 48 | + run input "" =! Some [] |
| 49 | + run input "3 4" =! Some [ 3; 4 ] |
| 50 | + run input "a" =! None |
| 51 | + |
| 52 | +[<Test>] |
| 53 | +let ``05 - FileInfo list option`` () = |
| 54 | + let input = option<System.IO.FileInfo list> "-f" |
| 55 | + let names = run input "-f a.txt -f b.txt" |> Option.map (List.map _.Name) |
| 56 | + names =! Some [ "a.txt"; "b.txt" ] |
| 57 | + |
| 58 | +[<Test>] |
| 59 | +let ``06 - Guid list option reports unsupported type as a parse error`` () = |
| 60 | + let input = option<System.Guid list> "-g" |
| 61 | + run input "-g 7f3c2a10-0000-0000-0000-000000000000" =! None |
0 commit comments