Skip to content

Commit c2c96f9

Browse files
committed
add test coverage for subcommands
1 parent f14f2eb commit c2c96f9

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -712,40 +712,42 @@ extension HelpGenerationTests {
712712

713713
extension HelpGenerationTests {
714714
struct NonCustomUsage: ParsableCommand {
715-
static let configuration = CommandConfiguration()
715+
struct ExampleSubcommand: ParsableCommand {
716+
static let configuration = CommandConfiguration()
717+
@Argument var output: String
718+
}
719+
720+
static let configuration = CommandConfiguration(
721+
subcommands: [ExampleSubcommand.self])
716722

717723
@Argument var file: String
718724
@Flag var verboseMode = false
719725
}
720726

721727
struct CustomUsageShort: ParsableCommand {
722-
static var configuration: CommandConfiguration {
723-
CommandConfiguration(usage: """
728+
static let configuration = CommandConfiguration(
729+
usage: """
724730
example [--verbose] <file-name>
725731
""")
726-
}
727-
732+
728733
@Argument var file: String
729734
@Flag var verboseMode = false
730735
}
731736

732737
struct CustomUsageLong: ParsableCommand {
733-
static var configuration: CommandConfiguration {
734-
CommandConfiguration(usage: """
738+
static let configuration = CommandConfiguration(
739+
usage: """
735740
example <file-name>
736741
example --verbose <file-name>
737742
example --help
738743
""")
739-
}
740-
744+
741745
@Argument var file: String
742746
@Flag var verboseMode = false
743747
}
744748

745749
struct CustomUsageHidden: ParsableCommand {
746-
static var configuration: CommandConfiguration {
747-
CommandConfiguration(usage: "")
748-
}
750+
static let configuration = CommandConfiguration(usage: "")
749751

750752
@Argument var file: String
751753
@Flag var verboseMode = false
@@ -755,14 +757,32 @@ extension HelpGenerationTests {
755757
AssertEqualStrings(
756758
actual: NonCustomUsage.helpMessage(columns: 80),
757759
expected: """
758-
USAGE: non-custom-usage <file> [--verbose-mode]
760+
USAGE: non-custom-usage <file> [--verbose-mode] <subcommand>
759761
760762
ARGUMENTS:
761763
<file>
762764
763765
OPTIONS:
764766
--verbose-mode
765767
-h, --help Show help information.
768+
769+
SUBCOMMANDS:
770+
example-subcommand
771+
772+
See 'non-custom-usage help <subcommand>' for detailed help.
773+
""")
774+
775+
AssertEqualStrings(
776+
actual: NonCustomUsage.helpMessage(
777+
for: NonCustomUsage.ExampleSubcommand.self, columns: 80),
778+
expected: """
779+
USAGE: non-custom-usage example-subcommand <output>
780+
781+
ARGUMENTS:
782+
<output>
783+
784+
OPTIONS:
785+
-h, --help Show help information.
766786
767787
""")
768788

@@ -814,7 +834,7 @@ extension HelpGenerationTests {
814834
actual: NonCustomUsage.fullMessage(for: ValidationError("Test")),
815835
expected: """
816836
Error: Test
817-
Usage: non-custom-usage <file> [--verbose-mode]
837+
Usage: non-custom-usage <file> [--verbose-mode] <subcommand>
818838
See 'non-custom-usage --help' for more information.
819839
""")
820840

@@ -848,7 +868,14 @@ extension HelpGenerationTests {
848868
AssertEqualStrings(
849869
actual: NonCustomUsage.usageString(),
850870
expected: """
851-
non-custom-usage <file> [--verbose-mode]
871+
non-custom-usage <file> [--verbose-mode] <subcommand>
872+
""")
873+
874+
AssertEqualStrings(
875+
actual: NonCustomUsage.usageString(
876+
for: NonCustomUsage.ExampleSubcommand.self),
877+
expected: """
878+
non-custom-usage example-subcommand <output>
852879
""")
853880

854881
AssertEqualStrings(

0 commit comments

Comments
 (0)