Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor some tests and trim help input #47

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions birdie_snapshots/root_help.accepted
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand All @@ -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
very very long description.
This should show up on a new line.
11 changes: 7 additions & 4 deletions src/glint.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 4 additions & 5 deletions src/glint/internal/help.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
)
}
5 changes: 3 additions & 2 deletions src/glint/internal/utils.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gleam/bool
import gleam/int
import gleam/list
import gleam/string
Expand All @@ -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, "", [])
Expand Down
8 changes: 8 additions & 0 deletions test/glint/internal/utils_test.gleam
Original file line number Diff line number Diff line change
@@ -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"])
}
142 changes: 80 additions & 62 deletions test/glint_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)))
Expand All @@ -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")
}
Expand Down
Loading