Skip to content

Commit

Permalink
Fix/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Feb 19, 2024
1 parent b9e1a2e commit a122c03
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions tests/Argu.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,47 +1022,49 @@ module ``Argu Tests Main Primitive`` =
test <@ results.Contains <@ Detach @> @>
test <@ results.GetResult <@ Main @> = "main" @>

module ``Defaulting`` =
module ``Traps for defaulting and or post-processing functions`` =
let results = parser.ParseCommandLine [| "--mandatory-arg" ; "true"; "command" |]
let failingDefThunk (): string = failwith "Defaulting Failed"

[<Fact>]
let ``Trap defaulting function exceptions`` () =
let parser value: string = if value = "default" then failwith "Parse Failed" else failwith "unexpected"
let failingDefThunk (): string = failwith "Defaulting Failed"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk, showUsage = false) @>
<| fun e -> <@ e.Message = "Defaulting Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk) @>
<@ results.GetResult(Working_Directory, failingDefThunk) @>
(fun e -> <@ e.Message.StartsWith "Defaulting Failed" && e.Message.Contains "--working-directory" @>)

module ``Trap defaulting function exceptions and parse exceptions`` =
[<Fact>]
let ``Trap defaulting function exceptions (for overloads with parse functions)`` () =
let parser value: string = failwith "should not be triggered"
let failingDefThunk (): string = failwith "Defaulting Failed"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Defaulting Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk, parser) @>
(fun e -> <@ e.Message.StartsWith "Defaulting Failed" && e.Message.Contains "--working-directory" @>)

let results = parser.ParseCommandLine [| "--mandatory-arg" ; "true"; "command" |]
[<Fact>]
let ``Trap post processing exceptions for GetResult overloads with defaulting functions`` () =
let parser value: string = if value = "default" then failwith "Parse Failed" else failwith "unexpected"
let okDefThunk (): string = "default"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, okDefThunk, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Parse Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, okDefThunk, parser) @>
(fun e -> <@ e.Message.StartsWith "Parse Failed" && e.Message.Contains "--working-directory" @>)

[<Fact>]
let ``Trap post processing exceptions and/or defaulting function exceptions`` () =
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Defaulting Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, failingDefThunk, parser) @>
(fun e -> <@ e.Message.StartsWith "Defaulting Failed" && e.Message.Contains "--working-directory" @>)

let okDefThunk (): string = "default"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, okDefThunk, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Parse Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, okDefThunk, parser) @>
(fun e -> <@ e.Message.StartsWith "Parse Failed" && e.Message.Contains "--working-directory" @>)

[<Fact>]
let ``Trap post processing exceptions for default values`` () =
let def: string = "default"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, def, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Parse Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, def, parser) @>
(fun e -> <@ e.Message.StartsWith "Parse Failed" && e.Message.Contains "--working-directory" @>)
[<Fact>]
let ``Trap post processing exceptions for GetResult overloads with default values`` () =
let def: string = "default"
let parser value: string = if value = "default" then failwith "Parse Failed" else failwith "unexpected"
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, def, parser, showUsage = false) @>
<| fun e -> <@ e.Message = "Parse Failed" && e.ErrorCode = ErrorCode.PostProcess @>
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, def, parser) @>
(fun e -> <@ e.Message.StartsWith "Parse Failed" && e.Message.Contains "--working-directory" @>)

0 comments on commit a122c03

Please sign in to comment.