Skip to content

Commit 528e67d

Browse files
committed
demo-core: Add prompt subcommand for lib/prompt
1 parent f958030 commit 528e67d

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

libexec/demo-core.d/prompt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#! /usr/bin/env bash
2+
#
3+
# Demonstration of `lib/prompt` capabilities
4+
#
5+
# Usage:
6+
# {{go}} {{cmd}}
7+
#
8+
# Use this program to get a feel for the core `@go.prompt_for_input` and
9+
# `@go.prompt_for_yes_or_no` functions, and for examples of how to use them in
10+
# your own scripts.
11+
12+
. "$_GO_USE_MODULES" 'prompt'
13+
14+
_@go.prompt_demo() {
15+
local name
16+
local quest='To seek the grail!'
17+
18+
# No default value; returns error on no input.
19+
if ! @go.prompt_for_input 'name' $'What is your name?\n' '' \
20+
'Run away, Sir or Madam Not Appearing in this Film! Run away!'; then
21+
return 1
22+
fi
23+
@go.printf 'Nice to meet you, %s!\n' "$name"
24+
25+
if @go.prompt_for_yes_or_no 'Do you have a quest?' 'yes'; then
26+
# Default value applies if input is empty.
27+
if ! @go.prompt_for_input 'quest' $'What is your quest?\n' "$quest"; then
28+
return 1
29+
fi
30+
elif ! @go.prompt_for_yes_or_no "Might I suggest: $quest" 'yes'; then
31+
@go.printf 'OK, no quest. Suit yourself!\n'
32+
return 1
33+
fi
34+
@go.printf 'Your quest is: %s\n' "$quest"
35+
}
36+
37+
_@go.prompt_demo "$@"

tests/demo-core/prompt.bats

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#! /usr/bin/env bats
2+
3+
load ../environment
4+
5+
setup() {
6+
test_filter
7+
}
8+
9+
teardown() {
10+
@go.remove_test_go_rootdir
11+
}
12+
13+
@test "$SUITE: use default quest option" {
14+
run "$_GO_SCRIPT" demo-core prompt <<<$'Mike\n\n'
15+
assert_success
16+
split_bats_output_into_lines
17+
18+
local default_quest='Do you have a quest? [Y/n] '
19+
default_quest+='What is your quest? [default: To seek the grail!]'
20+
21+
assert_lines_equal 'What is your name?' \
22+
'Nice to meet you, Mike!' \
23+
"${default_quest}" \
24+
'Your quest is: To seek the grail!'
25+
}
26+
27+
@test "$SUITE: return error if name not specified" {
28+
run "$_GO_SCRIPT" demo-core prompt <<<''
29+
assert_failure 'What is your name?' \
30+
'Run away, Sir or Madam Not Appearing in this Film! Run away!'
31+
}
32+
33+
@test "$SUITE: specify a different quest" {
34+
run "$_GO_SCRIPT" demo-core prompt \
35+
<<<$'Mike\nyes\nTo go back and face the peril!'
36+
assert_success
37+
split_bats_output_into_lines
38+
39+
local default_quest='Do you have a quest? [Y/n] '
40+
default_quest+='What is your quest? [default: To seek the grail!]'
41+
42+
assert_lines_equal 'What is your name?' \
43+
'Nice to meet you, Mike!' \
44+
"${default_quest}" \
45+
'Your quest is: To go back and face the peril!'
46+
}
47+
48+
@test "$SUITE: decline a quest" {
49+
run "$_GO_SCRIPT" demo-core prompt <<<$'Mike\nno\nno'
50+
assert_failure
51+
split_bats_output_into_lines
52+
53+
local default_quest='Do you have a quest? [Y/n] '
54+
default_quest+='Might I suggest: To seek the grail! [Y/n]'
55+
56+
assert_lines_equal 'What is your name?' \
57+
'Nice to meet you, Mike!' \
58+
"${default_quest} OK, no quest. Suit yourself!"
59+
}
60+
61+
@test "$SUITE: suggest a quest and accept it" {
62+
run "$_GO_SCRIPT" demo-core prompt <<<$'Mike\nno\nyes'
63+
assert_success
64+
split_bats_output_into_lines
65+
66+
local default_quest='Do you have a quest? [Y/n] '
67+
default_quest+='Might I suggest: To seek the grail! [Y/n]'
68+
69+
assert_lines_equal 'What is your name?' \
70+
'Nice to meet you, Mike!' \
71+
"${default_quest} Your quest is: To seek the grail!"
72+
}

0 commit comments

Comments
 (0)