diff --git a/.editorconfig b/.editorconfig index acf47a9b..2593929d 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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] diff --git a/tests/Argu.Tests/Tests.fs b/tests/Argu.Tests/Tests.fs index bbf13450..691394b0 100644 --- a/tests/Argu.Tests/Tests.fs +++ b/tests/Argu.Tests/Tests.fs @@ -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] [] @@ -31,7 +31,7 @@ module ``Argu Tests Main List`` = | [] Force | [] 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" @@ -39,21 +39,21 @@ module ``Argu Tests Main List`` = type NewArgs = | [] Name of string interface IArgParserTemplate with - member this.Usage = + member this.Usage = match this with | Name _ -> "New name" type TagArgs = | New of ParseResults interface IArgParserTemplate with - member this.Usage = + member this.Usage = match this with | New _ -> "New tag" type CheckoutArgs = | [] Branch of string interface IArgParserTemplate with - member this.Usage = + member this.Usage = match this with | Branch _ -> "push changes to remote repository and branch" @@ -176,8 +176,8 @@ module ``Argu Tests Main List`` = [] 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 ] @@ -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 @> @@ -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] @> - + [] let ``Help String`` () = @@ -296,7 +296,7 @@ module ``Argu Tests Main List`` = let ``First Parameter not placed at beginning`` () = raisesWith <@ parser.ParseCommandLine [| "--mandatory-arg" ; "true" ; "--first-parameter" ; "foo" |] @> (fun e -> <@ e.Message.Contains "should precede all other" @>) - + [] let ``Rest Parameter`` () = @@ -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" @> - + [] 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" @> - + [] let ``Parse equals or space assignment with colon fails`` () = raises <@ parser.Parse([|"--flex-equals-assignment:../../my-relative-path"; "--dir==foo"|], ignoreMissing = true) @> - + [] 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" @> - + [] 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) @@ -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 = | [] [] Flex_Equals_Assignment of string interface IArgParserTemplate with @@ -393,7 +393,7 @@ module ``Argu Tests Main List`` = [] let ``Disallowed equals assignment combination throws`` () = raisesWith <@ ArgumentParser.Create (programName = "gadget") @> - + type DisallowedArityWithAssignmentOrSpaced = | [] Flex_Equals_Assignment of string * int interface IArgParserTemplate with @@ -404,7 +404,7 @@ module ``Argu Tests Main List`` = [] let ``EqualsAssignmentOrSpaced and arity not one combination throws`` () = raisesWith <@ ArgumentParser.Create (programName = "gadget1") @> - + [] let ``Should fail on incorrect assignment 1`` () = raises <@ parser.Parse([|"--dir:foo"|], ignoreMissing = true) @> @@ -412,8 +412,8 @@ module ``Argu Tests Main List`` = [] 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 ] @@ -447,8 +447,9 @@ 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 @> @> @@ -456,8 +457,9 @@ module ``Argu Tests Main List`` = 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] @> [] @@ -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 @> [] let ``Required subcommand attribute should fail on missing subcommand`` () = @@ -531,17 +533,17 @@ module ``Argu Tests Main List`` = test <@ results.TryGetSubCommand() = Some Nullary_Sub @> [] - 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 @> [] - 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 <@ parser.ParseCommandLine(args, ignoreMissing = true) @> - (fun e -> <@ e.FirstLine.Contains "subcommand" @>) + (fun e -> <@ e.FirstLine.Contains "cannot run multiple subcommands" @>) [] let ``GatherUnrecognized attribute`` () = @@ -757,7 +759,7 @@ module ``Argu Tests Main List`` = [] let ``Use single dash prefix as default`` () = let parser = ArgumentParser.Create("usage string") - let args = + let args = [| "-argument" ; "bar" ; "-levels-deep" ; "3" |] let expected_outcome = set [ Argument "bar" ; Levels_Deep 3 ] @@ -799,7 +801,7 @@ module ``Argu Tests Main List`` = let args = [| "--mandatory-arg" ; "true" ; "/D" |] let results = parser.ParseCommandLine args test <@ results.Contains <@ Detach @> @> - + [] let ``Should fail when Usage, Mandatory and raiseOnUsage = true`` () = raisesWith <@ parser.ParseCommandLine ([|"--help"|], raiseOnUsage = true) @> @@ -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() 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 = @@ -955,8 +955,8 @@ module ``Argu Tests Main Primitive`` = [] 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 ] @@ -972,7 +972,7 @@ module ``Argu Tests Main Primitive`` = let ``Help String`` () = raisesWith <@ parser.ParseCommandLine [| "--help" |] @> (fun e -> <@ e.Message.Contains "USAGE:" @>) - + [] let ``First Parameter not placed at beginning`` () = raisesWith <@ parser.ParseCommandLine [| "--mandatory-arg" ; "true" ; "--first-parameter" ; "foo" |] @> @@ -980,8 +980,8 @@ module ``Argu Tests Main Primitive`` = [] 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 ] @@ -1007,7 +1007,7 @@ module ``Argu Tests Main Primitive`` = test <@ results.UnrecognizedCliParams = ["foobar"] @> test <@ results.Contains <@ Detach @> @> test <@ results.GetResult <@ Main @> = "main" @> - + [] let ``Trap defaulting function exceptions`` () = let results = parser.ParseCommandLine [| "--mandatory-arg" ; "true"; "command" |] @@ -1018,4 +1018,3 @@ module ``Argu Tests Main Primitive`` = raisesWith <@ results.GetResult(Working_Directory, defThunk) @> (fun e -> <@ e.Message.StartsWith "Defaulting Failed" && e.Message.Contains "--working-directory" @>) - \ No newline at end of file