From e34099cc8d0377fc3787ea80ecbb31c55d213b79 Mon Sep 17 00:00:00 2001 From: Robert Attard Date: Wed, 19 Jun 2024 10:17:47 -0400 Subject: [PATCH] refactor some tests and trim help input --- birdie_snapshots/root_help.accepted | 5 +- src/glint.gleam | 11 ++- src/glint/internal/help.gleam | 9 +- src/glint/internal/utils.gleam | 5 +- test/glint/internal/utils_test.gleam | 8 ++ test/glint_test.gleam | 142 +++++++++++++++------------ 6 files changed, 105 insertions(+), 75 deletions(-) create mode 100644 test/glint/internal/utils_test.gleam diff --git a/birdie_snapshots/root_help.accepted b/birdie_snapshots/root_help.accepted index 797a888..caf3571 100644 --- a/birdie_snapshots/root_help.accepted +++ b/birdie_snapshots/root_help.accepted @@ -2,7 +2,7 @@ version: 1.1.5 title: root help file: ./test/glint_test.gleam -test_name: help_test +test_name: root_help_test --- Some awesome global help text! @@ -25,4 +25,5 @@ SUBCOMMANDS: cmd5 cmd8-very-very-very-very-long This is cmd8 with a very very very very very - very very long description \ No newline at end of file + very very long description. + This should show up on a new line. \ No newline at end of file diff --git a/src/glint.gleam b/src/glint.gleam index 3043a11..b90f5f4 100644 --- a/src/glint.gleam +++ b/src/glint.gleam @@ -226,7 +226,7 @@ pub fn path_help( put description: String, ) -> Glint(a) { use node <- update_at(in: glint, at: path) - CommandNode(..node, description: description) + CommandNode(..node, description: string.trim(description)) } /// Set help text for the application as a whole. @@ -235,7 +235,10 @@ pub fn path_help( /// To set help text specifically for the root command please use [`glint.command_help`](#command_help) or [`glint.path_help([],...)`](#path_help) /// pub fn global_help(in glint: Glint(a), of description: String) -> Glint(a) { - Glint(..glint, config: Config(..glint.config, description: Some(description))) + Glint( + ..glint, + config: Config(..glint.config, description: Some(string.trim(description))), + ) } /// Adds a new command to be run at the specified path. @@ -315,7 +318,7 @@ pub fn command(do runner: Runner(a)) -> Command(a) { /// Attach a helptext description to a [`Command(a)`](#Command) /// pub fn command_help(of desc: String, with f: fn() -> Command(a)) -> Command(a) { - Command(..f(), description: desc) + Command(..f(), description: string.trim(desc)) } /// Specify a specific number of unnamed args that a given command expects. @@ -966,7 +969,7 @@ type FlagEntry { /// ``` /// pub fn flag_help(for flag: Flag(a), of description: String) -> Flag(a) { - Flag(..flag, desc: description) + Flag(..flag, desc: string.trim(description)) } /// Set the default value for a flag. diff --git a/src/glint/internal/help.gleam b/src/glint/internal/help.gleam index f2dda76..c9f8b03 100644 --- a/src/glint/internal/help.gleam +++ b/src/glint/internal/help.gleam @@ -93,7 +93,7 @@ pub fn command_help_to_string(help: Command, config: Config) -> String { |> string.join("\n") [ - option.unwrap(config.description, ""), + config.description |> option.unwrap(""), command, command_description, command_help_to_usage_string(help, config), @@ -309,10 +309,9 @@ fn format_content( #( "\n" - <> string.append( - string.repeat(" ", config.indent_width), - left_formatted <> right_formatted, - ), + <> string.repeat(" ", config.indent_width) + <> left_formatted + <> right_formatted, wrapped, ) } diff --git a/src/glint/internal/utils.gleam b/src/glint/internal/utils.gleam index 8e40c7b..fb01e3a 100644 --- a/src/glint/internal/utils.gleam +++ b/src/glint/internal/utils.gleam @@ -1,3 +1,4 @@ +import gleam/bool import gleam/int import gleam/list import gleam/string @@ -16,8 +17,8 @@ pub fn max_string_length(strings: List(String)) -> Int { /// the input string are retained. /// pub fn wordwrap(s: String, max_width: Int) -> List(String) { - use line <- list.flat_map(string.split(s, "\n")) - + use <- bool.guard(s == "", []) + use line <- list.flat_map(s |> string.split("\n")) line |> string.split(" ") |> do_wordwrap(max_width, "", []) diff --git a/test/glint/internal/utils_test.gleam b/test/glint/internal/utils_test.gleam new file mode 100644 index 0000000..e564635 --- /dev/null +++ b/test/glint/internal/utils_test.gleam @@ -0,0 +1,8 @@ +import gleeunit/should +import glint/internal/utils + +pub fn wordwrap_test() { + "a b c" + |> utils.wordwrap(3) + |> should.equal(["a b", "c"]) +} diff --git a/test/glint_test.gleam b/test/glint_test.gleam index 52b4640..091b8d6 100644 --- a/test/glint_test.gleam +++ b/test/glint_test.gleam @@ -96,7 +96,7 @@ pub fn runner_test() { |> should.equal(Ok(Out(snag.error("failed")))) } -pub fn help_test() { +fn help() { let nil = fn(_, _, _) { Nil } let global_flag = glint.string_flag("global") @@ -128,57 +128,66 @@ pub fn help_test() { "This is a very long flag with a very very very very very very long description", ) - let cli = - glint.new() - |> glint.with_name("test") - |> glint.global_help("Some awesome global help text!") - |> glint.as_module - |> glint.group_flag([], global_flag) - |> glint.add(at: [], do: { - use <- glint.command_help("This is the root command") - use _arg1 <- glint.named_arg("arg1") - use _arg2 <- glint.named_arg("arg2") - use _flag <- glint.flag(flag_1) - glint.command(nil) - }) - |> glint.add(at: ["cmd1"], do: { - use <- glint.command_help("This is cmd1") - use _flag2 <- glint.flag(flag_2) - use _flag5 <- glint.flag(flag_5) - glint.command(nil) - }) - |> glint.add(at: ["cmd1", "cmd3"], do: { - use <- glint.command_help("This is cmd3") - use _flag3 <- glint.flag(flag_3) - use <- glint.unnamed_args(glint.MinArgs(2)) - use _woo <- glint.named_arg("woo") - glint.command(nil) - }) - |> glint.add(at: ["cmd1", "cmd4"], do: { - use <- glint.command_help( - "This is cmd4 which has a very very very very very very very very long description", - ) - use _flag4 <- glint.flag(flag_4) - use <- glint.unnamed_args(glint.EqArgs(0)) - glint.command(nil) - }) - |> glint.add(at: ["cmd2"], do: { - use <- glint.command_help("This is cmd2") - use <- glint.unnamed_args(glint.EqArgs(0)) - use _arg1 <- glint.named_arg("arg1") - use _arg2 <- glint.named_arg("arg2") - glint.command(nil) - }) - |> glint.add( - at: ["cmd5", "cmd6"], - do: glint.command_help("This is cmd6", fn() { glint.command(nil) }), - ) - |> glint.path_help(["cmd5", "cmd6", "cmd7"], "This is cmd7") - |> glint.path_help( - ["cmd8-very-very-very-very-long"], - "This is cmd8 with a very very very very very very very long description", + glint.new() + |> glint.with_name("test") + |> glint.global_help("Some awesome global help text!") + |> glint.as_module + |> glint.group_flag([], global_flag) + |> glint.add(at: [], do: { + use <- glint.command_help("This is the root command") + use _arg1 <- glint.named_arg("arg1") + use _arg2 <- glint.named_arg("arg2") + use _flag <- glint.flag(flag_1) + glint.command(nil) + }) + |> glint.add(at: ["cmd1"], do: { + use <- glint.command_help("This is cmd1") + use _flag2 <- glint.flag(flag_2) + use _flag5 <- glint.flag(flag_5) + glint.command(nil) + }) + |> glint.add(at: ["cmd1", "cmd3"], do: { + use <- glint.command_help("This is cmd3") + use _flag3 <- glint.flag(flag_3) + use <- glint.unnamed_args(glint.MinArgs(2)) + use _woo <- glint.named_arg("woo") + glint.command(nil) + }) + |> glint.add(at: ["cmd1", "cmd4"], do: { + use <- glint.command_help( + "This is cmd4 which has a very very very very very very very very long description", ) + use _flag4 <- glint.flag(flag_4) + use <- glint.unnamed_args(glint.EqArgs(0)) + glint.command(nil) + }) + |> glint.add(at: ["cmd2"], do: { + use <- glint.command_help("This is cmd2") + use <- glint.unnamed_args(glint.EqArgs(0)) + use _arg1 <- glint.named_arg("arg1") + use _arg2 <- glint.named_arg("arg2") + glint.command(nil) + }) + |> glint.add( + at: ["cmd5", "cmd6"], + do: glint.command_help("This is cmd6", fn() { glint.command(nil) }), + ) + |> glint.path_help(["cmd5", "cmd6", "cmd7"], "This is cmd7") + |> glint.path_help( + ["cmd8-very-very-very-very-long"], + "This is cmd8 with a very very very very very very very long description. +This should show up on a new line.", + ) +} + +fn assert_unwrap_help(res: Result(glint.Out(a), String)) -> String { + let assert Ok(Help(help)) = res + help +} + +pub fn help_test() { + let cli = help() // execute root command glint.execute(cli, ["a", "b"]) |> should.equal(Ok(Out(Nil))) @@ -200,45 +209,54 @@ pub fn help_test() { glint.execute(cli, ["cmd2", "1", "2", "3"]) |> should.be_error() +} - let assert_unwrap_help = fn(res: Result(glint.Out(a), String)) -> String { - let assert Ok(Help(help)) = res - help - } - +pub fn root_help_test() { // help message for root command - glint.execute(cli, ["--help"]) + glint.execute(help(), ["--help"]) |> assert_unwrap_help |> birdie.snap("root help") +} +pub fn cmd1_help_test() { // help message for command - glint.execute(cli, ["cmd1", "--help"]) + glint.execute(help(), ["cmd1", "--help"]) |> assert_unwrap_help |> birdie.snap("cmd1 help") +} +pub fn cmd4_help_test() { // help message for nested command - glint.execute(cli, ["cmd1", "cmd4", "--help"]) + glint.execute(help(), ["cmd1", "cmd4", "--help"]) |> assert_unwrap_help |> birdie.snap("cmd4 help") +} +pub fn cmd2_help_test() { // help message for command with no additional flags - glint.execute(cli, ["cmd2", "--help"]) + glint.execute(help(), ["cmd2", "--help"]) |> assert_unwrap_help |> birdie.snap("cmd2 help") +} +pub fn cmd3_help_test() { // help message for command with no additional flags - glint.execute(cli, ["cmd1", "cmd3", "--help"]) + glint.execute(help(), ["cmd1", "cmd3", "--help"]) |> assert_unwrap_help |> birdie.snap("cmd3 help") +} +pub fn cmd6_help_test() { // help message for command a subcommand whose help was set with glint.path_help - glint.execute(cli, ["cmd5", "cmd6", "--help"]) + glint.execute(help(), ["cmd5", "cmd6", "--help"]) |> assert_unwrap_help |> birdie.snap("cmd6 help") +} +pub fn cmd7_help_test() { // help message for command that had help_text set with glint.glint.path_help // has no children or command runner set so no other details are available - glint.execute(cli, ["cmd5", "cmd6", "cmd7", "--help"]) + glint.execute(help(), ["cmd5", "cmd6", "cmd7", "--help"]) |> assert_unwrap_help |> birdie.snap("cmd7 help") }