Skip to content

Commit 753d5f0

Browse files
committed
Added tryParse with Error test
1 parent 3f7bf68 commit 753d5f0

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

src/Tests/SimpleAppTest.fs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ open Utils
77
open Input
88
open System.CommandLine.Parsing
99

10-
let mutable handlerCalled = false
10+
let mutable actionCalled = false
1111
[<SetUp>]
12-
let setup () = handlerCalled <- false
13-
[<TearDown>]
14-
let tearDown () = handlerCalled =! true
12+
let setup () = actionCalled <- false
1513

1614
[<Test>]
1715
let ``01 --word Hello -w World -s * return unit`` () =
@@ -26,9 +24,10 @@ let ``01 --word Hello -w World -s * return unit`` () =
2624
setAction (fun (words, separator) ->
2725
words =! [| "Hello"; "World" |]
2826
separator =! Some "*"
29-
handlerCalled <- true
27+
actionCalled <- true
3028
)
3129
} =! 0
30+
actionCalled =! true
3231

3332
[<Test>]
3433
let ``02 --word Hello -w World return unit`` () =
@@ -41,9 +40,10 @@ let ``02 --word Hello -w World return unit`` () =
4140
setAction (fun (words, separator) ->
4241
words =! [| "Hello"; "World" |]
4342
separator =! None
44-
handlerCalled <- true
43+
actionCalled <- true
4544
)
4645
} =! 0
46+
actionCalled =! true
4747

4848
[<Test>]
4949
let ``03 --word Hello -w World -s * return int`` () =
@@ -56,10 +56,11 @@ let ``03 --word Hello -w World -s * return int`` () =
5656
setAction (fun (words, separator) ->
5757
words =! [| "Hello"; "World" |]
5858
separator =! Some "*"
59-
handlerCalled <- true
59+
actionCalled <- true
6060
5
6161
)
6262
} =! 5
63+
actionCalled =! true
6364

6465
[<Test>]
6566
let ``04 --word Hello -w World -s * return int using manual configured options`` () =
@@ -72,10 +73,11 @@ let ``04 --word Hello -w World -s * return int using manual configured options``
7273
setAction (fun (words, separator) ->
7374
words =! [| "Hello"; "World" |]
7475
separator =! Some "*"
75-
handlerCalled <- true
76+
actionCalled <- true
7677
5
7778
)
7879
} =! 5
80+
actionCalled =! true
7981

8082
[<Test>]
8183
let ``05 empty array`` () =
@@ -88,9 +90,10 @@ let ``05 empty array`` () =
8890
setAction (fun (words, separator) ->
8991
words =! [||]
9092
separator =! Some "*"
91-
handlerCalled <- true
93+
actionCalled <- true
9294
)
9395
} =! 0
96+
actionCalled =! true
9497

9598
/// In beta5, the action handler is never called if an input starts with "@", even if ResponseFileTokenReplacer is set to null.
9699
[<Test>]
@@ -104,7 +107,7 @@ let ``06 - rootCommand should use configuration`` () =
104107
//inputs (Input.Option<string>("package", [ "--package"; "-p" ], "A package with a leading @ name"))
105108
inputs (option "--package" |> aliases ["-p"] |> desc "A package with a leading @ name")
106109
setAction (fun (package: string) ->
107-
handlerCalled <- true
110+
actionCalled <- true
108111
if package.StartsWith("@") then
109112
printfn $"{package}"
110113
0 // success
@@ -113,6 +116,7 @@ let ``06 - rootCommand should use configuration`` () =
113116
1 // failure
114117
)
115118
} =! 0
119+
actionCalled =! true
116120

117121
[<Test>]
118122
let ``07 - Child command should use configuration`` () =
@@ -124,7 +128,7 @@ let ``07 - Child command should use configuration`` () =
124128
|> desc "A package with a leading @ name"
125129
)
126130
setAction (fun (package: string) ->
127-
handlerCalled <- true
131+
actionCalled <- true
128132
if package.StartsWith("@") then
129133
printfn $"{package}"
130134
0 // success
@@ -143,10 +147,10 @@ let ``07 - Child command should use configuration`` () =
143147
noAction
144148
addCommand getCmd
145149
} =! 0
150+
actionCalled =! true
146151

147152
[<Test>]
148153
let ``08 - Validators`` () =
149-
handlerCalled <- true // Set to true to avoid false negatives in the test
150154
let args = args "-w delete -s *"
151155
let cfg =
152156
commandLineConfiguration {
@@ -174,4 +178,26 @@ let ``08 - Validators`` () =
174178

175179
printfn $"{cfg.Error}"
176180
let result = cfg.Invoke(args)
177-
result =! 1 // Expecting a failure due to the separator validation
181+
result =! 1 // Expecting a failure due to the separator validation
182+
actionCalled =! false
183+
184+
[<Test>]
185+
let ``09 tryParser Directory Info`` () =
186+
testRootCommand "--directory .." {
187+
description "Custom parser for directory info"
188+
inputs (
189+
option<System.IO.DirectoryInfo> "--directory"
190+
|> desc "A directory path"
191+
|> required
192+
|> tryParse (fun res ->
193+
let path = res.Tokens[0].Value
194+
if path = ".."
195+
then Error "'..' is not a valid directory"
196+
else Ok (System.IO.DirectoryInfo path)
197+
)
198+
)
199+
setAction (fun dir ->
200+
printfn $"Directory: {dir}"
201+
)
202+
} =! 1 // Should fail
203+
actionCalled =! false

0 commit comments

Comments
 (0)