Skip to content

Commit f958030

Browse files
committed
select-option demo: Use prompt_for_yes_or_no
1 parent c01692a commit f958030

File tree

2 files changed

+20
-40
lines changed

2 files changed

+20
-40
lines changed

libexec/demo-core.d/select-option

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,25 @@
1717
select_option_demo() {
1818
local options=("$@")
1919
local selected
20-
local finish
2120

2221
if [[ "${#options[@]}" -eq '0' ]]; then
2322
options=('Hello, World!' 'Goodbye, World!')
2423
fi
2524

26-
while [[ -z "$finish" ]]; do
25+
while true; do
2726
@go.printf 'Please select one of the following options:\n'
2827

29-
if @go.select_option 'selected' "${options[@]}"; then
30-
@go.printf 'You selected: "%s"\n\n' "$selected"
28+
if ! @go.select_option 'selected' "${options[@]}"; then
29+
@go.printf 'You declined to select an option. Exiting...\n\n'
30+
return 1
3131
else
32-
@go.printf 'You declined to select an option.\n\n'
32+
@go.printf 'You selected: "%s"\n\n' "$selected"
3333
fi
3434

35-
@go.printf 'Would you like to select another option?\n'
36-
37-
if @go.select_option 'selected' 'Yes' 'No'; then
38-
if [[ "$selected" == 'No' ]]; then
39-
@go.printf 'Exiting...\n'
40-
finish='true'
41-
else
42-
printf '\n'
43-
fi
44-
else
45-
@go.printf 'You declined to select an option. Exiting...\n'
46-
finish='true'
35+
if ! @go.prompt_for_yes_or_no 'Would you like to select another option?' \
36+
'yes'; then
37+
@go.printf 'Exiting...\n'
38+
return 0
4739
fi
4840
done
4941
}

tests/demo-core/select-option.bats

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,28 @@ teardown() {
1212
}
1313

1414
@test "$SUITE: use default selection options" {
15-
run "$_GO_SCRIPT" demo-core select-option <<<$'1\n1\n2\n2\n'
15+
run "$_GO_SCRIPT" demo-core select-option <<<$'1\nYes\n2\nNo'
1616
assert_success
1717
split_bats_output_into_lines
18+
19+
local select_affirmative='Would you like to select another option? [Y/n] '
20+
select_affirmative+='Please select one of the following options:'
21+
1822
assert_lines_equal 'Please select one of the following options:' \
1923
'1) Hello, World!' \
2024
'2) Goodbye, World!' \
2125
"${PS3}You selected: \"Hello, World!\"" \
2226
'' \
23-
'Would you like to select another option?' \
24-
'1) Yes' \
25-
'2) No' \
26-
"${PS3}" \
27-
'Please select one of the following options:' \
27+
"${select_affirmative}" \
2828
'1) Hello, World!' \
2929
'2) Goodbye, World!' \
3030
"${PS3}You selected: \"Goodbye, World!\"" \
3131
'' \
32-
'Would you like to select another option?' \
33-
'1) Yes' \
34-
'2) No' \
35-
"${PS3}Exiting..."
32+
'Would you like to select another option? [Y/n] Exiting...'
3633
}
3734

3835
@test "$SUITE: use user-provided selection options" {
39-
run "$_GO_SCRIPT" demo-core select-option foo bar baz <<<$'2\n2\n'
36+
run "$_GO_SCRIPT" demo-core select-option foo bar baz <<<$'2\nNo'
4037
assert_success
4138
split_bats_output_into_lines
4239
assert_lines_equal 'Please select one of the following options:' \
@@ -45,30 +42,21 @@ teardown() {
4542
'3) baz' \
4643
"${PS3}You selected: \"bar\"" \
4744
'' \
48-
'Would you like to select another option?' \
49-
'1) Yes' \
50-
'2) No' \
51-
"${PS3}Exiting..."
45+
'Would you like to select another option? [Y/n] Exiting...'
5246
}
5347

54-
@test "$SUITE: exit both prompts on empty input" {
48+
@test "$SUITE: exit prompt and program on empty input terminated by EOF" {
5549
mkdir "$TEST_GO_ROOTDIR"
5650
printf '' >"$TEST_GO_ROOTDIR/input.txt"
5751
run "$_GO_SCRIPT" demo-core select-option foo bar baz \
5852
<"$TEST_GO_ROOTDIR/input.txt"
5953

60-
assert_success
54+
assert_failure
6155
split_bats_output_into_lines
6256
assert_lines_equal 'Please select one of the following options:' \
6357
'1) foo' \
6458
'2) bar' \
6559
'3) baz' \
6660
"${PS3}" \
67-
'You declined to select an option.' \
68-
'' \
69-
'Would you like to select another option?' \
70-
'1) Yes' \
71-
'2) No' \
72-
"${PS3}" \
7361
'You declined to select an option. Exiting...'
7462
}

0 commit comments

Comments
 (0)