Skip to content

Commit

Permalink
refactor: tweak tests (fsprojects#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Feb 19, 2024
1 parent 0aa2e50 commit c05ebbd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ end_of_line = lf
# Project files
[*.{csproj,fsproj,props}]
indent_size = 2
# Rider does not implement this yet (https://youtrack.jetbrains.com/issue/RIDER-25867) so for now no space before
# resharper_space_before_self_closing = true

# YAML files
[*.yml]
Expand Down
85 changes: 42 additions & 43 deletions tests/Argu.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ open Argu
module ``Argu Tests Main List`` =

type Exception with
member inline x.FirstLine =
member inline x.FirstLine =
x.Message.Split([|Environment.NewLine|], StringSplitOptions.RemoveEmptyEntries).[0]

[<Flags>]
Expand All @@ -31,29 +31,29 @@ module ``Argu Tests Main List`` =
| [<AltCommandLine("-f")>] Force
| [<MainCommand("COMMAND"); ExactlyOnce>] Remote of repo_name:string * branch_name:string
interface IArgParserTemplate with
member this.Usage =
member this.Usage =
match this with
| Force -> "force changes in remote repo"
| Remote _ -> "push changes to remote repository and branch"

type NewArgs =
| [<Mandatory>] Name of string
interface IArgParserTemplate with
member this.Usage =
member this.Usage =
match this with
| Name _ -> "New name"

type TagArgs =
| New of ParseResults<NewArgs>
interface IArgParserTemplate with
member this.Usage =
member this.Usage =
match this with
| New _ -> "New tag"

type CheckoutArgs =
| [<Mandatory>] Branch of string
interface IArgParserTemplate with
member this.Usage =
member this.Usage =
match this with
| Branch _ -> "push changes to remote repository and branch"

Expand Down Expand Up @@ -176,8 +176,8 @@ module ``Argu Tests Main List`` =

[<Fact>]
let ``Simple command line parsing`` () =
let args =
[| "--first-parameter" ; "bar" ; "--mandatory-arg" ; "true" ; "-D" ;
let args =
[| "--first-parameter" ; "bar" ; "--mandatory-arg" ; "true" ; "-D" ;
"--listener" ; "localhost" ; "8080" ; "--log-level" ; "2" |]

let expected_outcome = [ First_Parameter "bar" ; Mandatory_Arg true ; Detach ; Listener ("localhost", 8080) ; Log_Level 2 ]
Expand Down Expand Up @@ -210,7 +210,7 @@ module ``Argu Tests Main List`` =
let args = [ Mandatory_Arg true ; Detach ; Listener ("localhost", 8080) ; Log_Level 2 ]
let xmlSource = parser.PrintAppSettingsArguments args
let usages = List.map (fun a -> (a :> IArgParserTemplate).Usage) args

test <@ xmlSource.Contains usages[0] = true @>
test <@ xmlSource.Contains usages[1] = true @>
test <@ xmlSource.Contains usages[2] = true @>
Expand Down Expand Up @@ -251,7 +251,7 @@ module ``Argu Tests Main List`` =
let ``AppSettings List param single`` () =
let results = parseFunc true (function "list" -> Some "42" | _ -> None)
test <@ results.GetResult List = [42] @>


[<Fact>]
let ``Help String`` () =
Expand Down Expand Up @@ -296,7 +296,7 @@ module ``Argu Tests Main List`` =
let ``First Parameter not placed at beginning`` () =
raisesWith<ArguParseException> <@ parser.ParseCommandLine [| "--mandatory-arg" ; "true" ; "--first-parameter" ; "foo" |] @>
(fun e -> <@ e.Message.Contains "should precede all other" @>)


[<Fact>]
let ``Rest Parameter`` () =
Expand Down Expand Up @@ -356,21 +356,21 @@ module ``Argu Tests Main List`` =
let ``Parse equals assignment 2`` () =
let result = parser.Parse([|"--dir==foo"|], ignoreMissing = true)
test <@ result.GetResult Dir = "=foo" @>

[<Fact>]
let ``Parse equals or space assignment with equals`` () =
let result = parser.Parse([|"--flex-equals-assignment=../../my-relative-path"; "--dir==foo"|], ignoreMissing = true)
test <@ result.GetResult Flex_Equals_Assignment = "../../my-relative-path" @>

[<Fact>]
let ``Parse equals or space assignment with colon fails`` () =
raises<ArguParseException> <@ parser.Parse([|"--flex-equals-assignment:../../my-relative-path"; "--dir==foo"|], ignoreMissing = true) @>

[<Fact>]
let ``Parse equals or space assignment with space`` () =
let result = parser.Parse([|"--flex-equals-assignment"; "../../my-relative-path"; "--dir==foo"|], ignoreMissing = true)
test <@ result.GetResult Flex_Equals_Assignment = "../../my-relative-path" @>

[<Fact>]
let ``Parse equals or space assignment with space and optional type`` () =
let result = parser.Parse([|"--flex-equals-assignment-with-option"; "../../my-relative-path"; "--dir==foo"|], ignoreMissing = true)
Expand All @@ -382,7 +382,7 @@ module ``Argu Tests Main List`` =
// EitherSpaceOrColonAssignmentAttribute share the same underlying implementation.
let result = parser.Parse([|"--flex-colon-assignment:../../my-relative-path"; "--dir==foo"|], ignoreMissing = true)
test <@ result.GetResult Flex_Colon_Assignment = "../../my-relative-path" @>

type DisallowedAssignmentArgs =
| [<EqualsAssignmentOrSpaced>] [<EqualsAssignment>] Flex_Equals_Assignment of string
interface IArgParserTemplate with
Expand All @@ -393,7 +393,7 @@ module ``Argu Tests Main List`` =
[<Fact>]
let ``Disallowed equals assignment combination throws`` () =
raisesWith<ArguException> <@ ArgumentParser.Create<DisallowedAssignmentArgs> (programName = "gadget") @>

type DisallowedArityWithAssignmentOrSpaced =
| [<EqualsAssignmentOrSpaced>] Flex_Equals_Assignment of string * int
interface IArgParserTemplate with
Expand All @@ -404,16 +404,16 @@ module ``Argu Tests Main List`` =
[<Fact>]
let ``EqualsAssignmentOrSpaced and arity not one combination throws`` () =
raisesWith<ArguException> <@ ArgumentParser.Create<DisallowedArityWithAssignmentOrSpaced> (programName = "gadget1") @>

[<Fact>]
let ``Should fail on incorrect assignment 1`` () =
raises<ArguParseException> <@ parser.Parse([|"--dir:foo"|], ignoreMissing = true) @>


[<Fact>]
let ``Ignore Unrecognized parameters`` () =
let args =
[| "--first-parameter" ; "bar" ; "--junk-param" ; "42" ; "--mandatory-arg" ; "true" ; "-D" ;
let args =
[| "--first-parameter" ; "bar" ; "--junk-param" ; "42" ; "--mandatory-arg" ; "true" ; "-D" ;
"--listener" ; "localhost" ; "8080" ; "--log-level" ; "2" |]

let expected_outcome = [ First_Parameter "bar" ; Mandatory_Arg true ; Detach ; Listener ("localhost", 8080) ; Log_Level 2 ]
Expand Down Expand Up @@ -447,17 +447,19 @@ module ``Argu Tests Main List`` =
let ``Simple subcommand parsing 1`` () =
let args = [|"push" ; "-f" ; "origin" ; "master"|]
let results = parser.ParseCommandLine(args, ignoreMissing = true)
let nested = results.GetResult <@ Push @>
test <@ match results.TryGetSubCommand() with Some (Push _) -> true | _ -> false @>
let nested = trap <@ match results.GetSubCommand() with Push x -> x | _ -> failwith "unexpected" @>
let nested2 = results.GetResult <@ Push @>
test <@ obj.ReferenceEquals(nested, nested2) @>
test <@ nested.GetResults <@ Remote @> = [("origin", "master")] @>
test <@ nested.Contains <@ Force @> @>

[<Fact>]
let ``Simple subcommand parsing 2`` () =
let args = [|"clean"; "-fdx"|]
let results = parser.ParseCommandLine(args, ignoreMissing = true)
let nested = results.GetResult Clean
test <@ match results.TryGetSubCommand() with Some (Clean _) -> true | _ -> false @>
let nested = trap <@ match results.GetSubCommand() with Clean a -> a | _ -> failwith "unexpected" @>
let nested2 = results.GetResult Clean
test <@ obj.ReferenceEquals(nested, nested2) @>
test <@ nested.GetAllResults() = [F; D; X] @>

[<Fact>]
Expand Down Expand Up @@ -516,7 +518,7 @@ module ``Argu Tests Main List`` =
let results = parser.ParseCommandLine(args, ignoreMissing = true)
let nested = results.GetResult <@ Required @>
let nested' = nested.GetResult <@ Sub @>
test <@ nested'.Contains <@ F @> @>
test <@ nested'.Contains F @>

[<Fact>]
let ``Required subcommand attribute should fail on missing subcommand`` () =
Expand All @@ -531,17 +533,17 @@ module ``Argu Tests Main List`` =
test <@ results.TryGetSubCommand() = Some Nullary_Sub @>

[<Fact>]
let ``Required subcommand should succeed on nullary subcommand`` () =
let ``Required subcommand nullary subcommand can be parsed`` () =
let args = [|"required"; "null-sub"|]
let results = parser.ParseCommandLine(args, ignoreMissing = true)
let nested = results.GetResult <@ Required @>
test <@ nested.TryGetSubCommand() = Some Null_Sub @>
test <@ nested.GetSubCommand() = Null_Sub @>

[<Fact>]
let ``Calling both a nullary subcommand a normal one should fail`` () =
let ``Calling multiple sibling subcommands is not permitted`` () =
let args = [|"required"; "null-sub"; "sub"; "-fdx"|]
raisesWith<ArguParseException> <@ parser.ParseCommandLine(args, ignoreMissing = true) @>
(fun e -> <@ e.FirstLine.Contains "subcommand" @>)
(fun e -> <@ e.FirstLine.Contains "cannot run multiple subcommands" @>)

[<Fact>]
let ``GatherUnrecognized attribute`` () =
Expand Down Expand Up @@ -757,7 +759,7 @@ module ``Argu Tests Main List`` =
[<Fact>]
let ``Use single dash prefix as default`` () =
let parser = ArgumentParser.Create<ArgumentSingleDash>("usage string")
let args =
let args =
[| "-argument" ; "bar" ; "-levels-deep" ; "3" |]

let expected_outcome = set [ Argument "bar" ; Levels_Deep 3 ]
Expand Down Expand Up @@ -799,7 +801,7 @@ module ``Argu Tests Main List`` =
let args = [| "--mandatory-arg" ; "true" ; "/D" |]
let results = parser.ParseCommandLine args
test <@ results.Contains <@ Detach @> @>

[<Fact>]
let ``Should fail when Usage, Mandatory and raiseOnUsage = true`` () =
raisesWith<ArguParseException> <@ parser.ParseCommandLine ([|"--help"|], raiseOnUsage = true) @>
Expand Down Expand Up @@ -896,16 +898,14 @@ module ``Argu Tests Main List`` =
let ``Hidden inherited parameters are not printed in help text with subcommand`` () =
let parser = ArgumentParser.Create<BaseCommand>()
let results = parser.ParseCommandLine([|"sub"|])
match results.GetSubCommand() with
| Sub r ->
test <@ r.Parser.PrintUsage().Contains "will be shown" @>
test <@ r.Parser.PrintUsage().Contains "will be hidden" |> not @>
| _ -> failwithf "never should get here"
let r = trap <@ results.GetSubCommand() |> function Sub r -> r | _ -> failwith "unexpected" @>
test <@ r.Parser.PrintUsage().Contains "will be shown" @>
test <@ r.Parser.PrintUsage().Contains "will be hidden" |> not @>

module ``Argu Tests Main Primitive`` =

type Exception with
member inline x.FirstLine =
member inline x.FirstLine =
x.Message.Split([|Environment.NewLine|], StringSplitOptions.RemoveEmptyEntries).[0]

type ArgumentPrimitive =
Expand Down Expand Up @@ -955,8 +955,8 @@ module ``Argu Tests Main Primitive`` =

[<Fact>]
let ``Simple command line parsing`` () =
let args =
[| "--first-parameter" ; "bar" ; "--mandatory-arg" ; "true" ; "-D" ;
let args =
[| "--first-parameter" ; "bar" ; "--mandatory-arg" ; "true" ; "-D" ;
"--listener" ; "localhost" ; "8080" ; "--log-level" ; "2" |]

let expected_outcome = [ First_Parameter "bar" ; Mandatory_Arg true ; Detach ; Listener ("localhost", 8080) ; Log_Level 2 ]
Expand All @@ -972,16 +972,16 @@ module ``Argu Tests Main Primitive`` =
let ``Help String`` () =
raisesWith<ArguParseException> <@ parser.ParseCommandLine [| "--help" |] @>
(fun e -> <@ e.Message.Contains "USAGE:" @>)

[<Fact>]
let ``First Parameter not placed at beginning`` () =
raisesWith<ArguParseException> <@ parser.ParseCommandLine [| "--mandatory-arg" ; "true" ; "--first-parameter" ; "foo" |] @>
(fun e -> <@ e.Message.Contains "should precede all other" @>)

[<Fact>]
let ``Ignore Unrecognized parameters`` () =
let args =
[| "--first-parameter" ; "bar" ; "--junk-param" ; "42" ; "--mandatory-arg" ; "true" ; "-D" ;
let args =
[| "--first-parameter" ; "bar" ; "--junk-param" ; "42" ; "--mandatory-arg" ; "true" ; "-D" ;
"--listener" ; "localhost" ; "8080" ; "--log-level" ; "2" |]

let expected_outcome = [ First_Parameter "bar" ; Mandatory_Arg true ; Detach ; Listener ("localhost", 8080) ; Log_Level 2 ]
Expand All @@ -1007,7 +1007,7 @@ module ``Argu Tests Main Primitive`` =
test <@ results.UnrecognizedCliParams = ["foobar"] @>
test <@ results.Contains <@ Detach @> @>
test <@ results.GetResult <@ Main @> = "main" @>

[<Fact>]
let ``Trap defaulting function exceptions`` () =
let results = parser.ParseCommandLine [| "--mandatory-arg" ; "true"; "command" |]
Expand All @@ -1018,4 +1018,3 @@ module ``Argu Tests Main Primitive`` =
raisesWith<ArguParseException>
<@ results.GetResult(Working_Directory, defThunk) @>
(fun e -> <@ e.Message.StartsWith "Defaulting Failed" && e.Message.Contains "--working-directory" @>)

0 comments on commit c05ebbd

Please sign in to comment.