Skip to content

Commit 823ea91

Browse files
committed
echo-(filter|unique), is-needle: fix tests
/ref befefb0 also add `__is_stdinargs_option_or_argument` to make filtering out arguments easier, used by `echo-filter`
1 parent befefb0 commit 823ea91

5 files changed

Lines changed: 151 additions & 29 deletions

File tree

commands/echo-filter

Lines changed: 110 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,119 @@ function echo_filter_test() (
44
source "$DOROTHY/sources/bash.bash"
55
eval-tester --ignore-stderr --status=22 -- echo-filter --help
66

7-
eval-tester --name='no args' -- \
7+
## FILTER
8+
9+
eval-tester --name='no args' --status=22 --ignore-stderr -- \
810
echo-filter
911

10-
eval-tester --name='no args' -- \
12+
eval-tester --name='no args' --status=22 --ignore-stderr -- \
1113
echo-filter --
1214

15+
local \
16+
inputs=(a B 'c C' 'd D' 'e E' 'f F') \
17+
filters=(--any --each --value='a' --value='b' --prefix='c' --prefix='D' --suffix='e' --suffix='F')
18+
1319
# respect case
14-
eval-tester --stdout=$'A\nC\ne\na\nb\n' --trailing-newlines -- \
15-
echo-filter -- A C e a b a b
20+
eval-tester --stdout=$'a\nc C\nf F\n' --trailing-newlines -- \
21+
echo-filter "${filters[@]}" -- "${inputs[@]}"
1622

17-
__print_lines A C e a b a b | eval-tester --stdout=$'A\nC\ne\na\nb\n' --trailing-newlines -- \
18-
echo-filter --stdin
23+
__print_lines "${inputs[@]}" | eval-tester --stdout=$'a\nc C\nf F\n' --trailing-newlines -- \
24+
echo-filter --stdin "${filters[@]}"
1925

2026
# ignore case
21-
eval-tester --stdout=$'a\nc\ne\nb\n' --trailing-newlines -- \
22-
echo-filter --ignore-case -- A C e a b a b
27+
eval-tester --stdout=$'a\nB\nc C\nd D\ne E\nf F\n' --trailing-newlines -- \
28+
echo-filter --ignore-case "${filters[@]}" -- "${inputs[@]}"
29+
30+
__print_lines "${inputs[@]}" | eval-tester --stdout=$'a\nB\nc C\nd D\ne E\nf F\n' --trailing-newlines -- \
31+
echo-filter --stdin --ignore-case "${filters[@]}"
32+
33+
# lowercase
34+
eval-tester --stdout=$'a\nb\nc c\nd d\ne e\nf f\n' --trailing-newlines -- \
35+
echo-filter --lowercase "${filters[@]}" -- "${inputs[@]}"
36+
37+
__print_lines "${inputs[@]}" | eval-tester --stdout=$'a\nb\nc c\nd d\ne e\nf f\n' --trailing-newlines -- \
38+
echo-filter --stdin --lowercase "${filters[@]}"
39+
40+
# uppercase
41+
eval-tester --stdout=$'A\nB\nC C\nD D\nE E\nF F\n' --trailing-newlines -- \
42+
echo-filter --uppercase "${filters[@]}" -- "${inputs[@]}"
43+
44+
__print_lines "${inputs[@]}" | eval-tester --stdout=$'A\nB\nC C\nD D\nE E\nF F\n' --trailing-newlines -- \
45+
echo-filter --stdin --uppercase "${filters[@]}"
46+
47+
## HAS
48+
49+
return 0 # @todo finish these tests
50+
51+
eval-tester --status=22 --ignore-stderr -- \
52+
echo-filter --has
53+
54+
eval-tester --status=22 --ignore-stderr -- \
55+
echo-filter --has --
56+
57+
eval-tester --status=22 --ignore-stderr -- \
58+
echo-filter --has -- a b c
59+
60+
eval-tester --status=22 --ignore-stderr -- \
61+
echo-filter --has a b -- a b c
62+
63+
eval-tester --status=22 --ignore-stderr -- \
64+
echo-filter --has a b --
65+
66+
eval-tester --status=22 --ignore-stderr -- \
67+
echo-filter --has a b
68+
69+
eval-tester --status=1 -- \
70+
echo-filter --has a --
71+
72+
eval-tester --status=1 -- \
73+
echo-filter --has --any a b --
74+
75+
eval-tester --status=1 -- \
76+
echo-filter --has --all a b --
77+
78+
# capture what [[ " ${items[*]} " =~ " $item " ]] cannot
79+
eval-tester --status=1 -- \
80+
echo-filter --has b -- a 'b b b'
81+
82+
eval-tester --status=1 -- \
83+
echo-filter --has c -- a 'b b b'
84+
85+
eval-tester --status=1 -- \
86+
echo-filter --has --any b c -- a 'b b b'
87+
88+
eval-tester --status=1 -- \
89+
echo-filter --has --all b c -- a 'b b b'
90+
91+
eval-tester -- \
92+
echo-filter --has --any a 'b b b' -- a 'b b b'
93+
94+
eval-tester -- \
95+
echo-filter --has --all a 'b b b' -- a 'b b b'
96+
97+
eval-tester -- \
98+
echo-filter --has --any --needle=a --needle='b b b' -- a 'b b b'
99+
100+
eval-tester -- \
101+
echo-filter --has --all --needle=a --needle='b b b' -- a 'b b b'
102+
103+
eval-tester -- \
104+
echo-filter --has --any a a -- a 'b b b'
105+
106+
eval-tester -- \
107+
echo-filter --has --all a a -- a 'b b b'
108+
109+
eval-tester -- \
110+
echo-filter --has --any a c -- a 'b b b'
111+
112+
eval-tester --status=1 -- \
113+
echo-filter --has --all a c -- a 'b b b'
114+
115+
eval-tester -- \
116+
echo-filter --has --any a c -- a 'b b b'
23117

24-
__print_lines A c e a b a b | eval-tester --stdout=$'a\nc\ne\nb\n' --trailing-newlines -- \
25-
echo-filter --stdin --ignore-case
118+
eval-tester -- \
119+
echo-filter --has --all --ignore-case a C -- A 'b b b' c
26120

27121
return 0
28122
)
@@ -57,15 +151,14 @@ function echo_filter() (
57151
}
58152

59153
# process
60-
local item options_filter=() option_stdinargs=()
154+
local options_filter=() option_stdinargs=()
61155
while [[ $# -ne 0 ]]; do
62-
item="$1"
156+
if __is_stdinargs_option_or_argument "$1"; then
157+
option_stdinargs+=("$1")
158+
else
159+
options_filter+=("$1")
160+
fi
63161
shift
64-
case "$item" in
65-
'--no-color'* | '--color' | '--no-stdin'* | '--stdin' | '--no-timeout'* | '--timeout' | '--no-inline'* | '--inline' | '--max-args=' | '-' | '--') option_stdinargs+=("$item") ;;
66-
'--'*) options_filter+=("$item") ;;
67-
*) option_stdinargs+=("$item") ;;
68-
esac
69162
done
70163

71164
# =====================================

commands/echo-unique

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,26 @@ function echo_unique_test() (
1818
echo-unique --stdin
1919

2020
# ignore case
21-
eval-tester --stdout=$'a\nc\ne\nb\n' --trailing-newlines -- \
22-
echo-unique --ignore-case -- A C e a b a b
21+
eval-tester --stdout=$'A\nC\ne\nb\n' --trailing-newlines -- \
22+
echo-unique --ignore-case -- A C e a b a B
2323

24-
__print_lines A c e a b a b | eval-tester --stdout=$'a\nc\ne\nb\n' --trailing-newlines -- \
24+
__print_lines A C e a b a B | eval-tester --stdout=$'A\nC\ne\nb\n' --trailing-newlines -- \
2525
echo-unique --stdin --ignore-case
2626

27+
# lowercase
28+
eval-tester --stdout=$'a\nc\ne\nb\n' --trailing-newlines -- \
29+
echo-unique --lowercase -- A C e a b a B
30+
31+
__print_lines A C e a b a B | eval-tester --stdout=$'a\nc\ne\nb\n' --trailing-newlines -- \
32+
echo-unique --stdin --lowercase
33+
34+
# uppercase
35+
eval-tester --stdout=$'A\nC\nE\nB\n' --trailing-newlines -- \
36+
echo-unique --uppercase -- A C e a b a B
37+
38+
__print_lines A C e a b a B | eval-tester --stdout=$'A\nC\nE\nB\n' --trailing-newlines -- \
39+
echo-unique --stdin --uppercase
40+
2741
return 0
2842
)
2943
function echo_unique() (

commands/is-needle

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ function is_needle() (
100100
--all | --any
101101
Required if multiple needles are provided: which technique to use for comparisons.
102102
103-
--[no-]ignore-case
104-
If provided, ignore case when comparing needle to inputs.
103+
--case=ignore | --ignore-case
104+
Ignore case when comparing each <needle> to each to each <input>.
105+
--case=sensitive
106+
The default. Respect case when comparing each <needle> to each to each <input>.
105107
106108
--needle=<needle> | <needle>
107-
Verify this <needle> exists within the <input>s
108-
Note that you should always use \`--needle=<needle>\` as just doing <needle> will fail if the <needle> looks like a flag.
109+
Verify this <needle> exists within the <input>s.
110+
Note that you should always use \`--needle=<needle>\` as just doing <needle> will fail if the <needle> starts with a dash \`-\`.
109111
110112
RETURNS:
111113
[0] if the <needle> is found within the <input>s.
@@ -115,7 +117,7 @@ function is_needle() (
115117
}
116118

117119
# process our arguments
118-
local item option_mode='' option_ignore_case='no' option_lookups=() option_inputs=() option_inputs_defined='no'
120+
local item option_mode='' option_case='sensitive' option_lookups=() option_inputs=() option_inputs_defined='no'
119121
while [[ $# -ne 0 ]]; do
120122
item="$1"
121123
shift
@@ -127,7 +129,10 @@ function is_needle() (
127129
--any) option_mode=any ;;
128130
--each) option_mode=each ;;
129131
--all) option_mode=all ;;
130-
'--no-ignore-case'* | '--ignore-case'*) __flag --source={item} --target={option_ignore_case} --affirmative --coerce ;;
132+
# case mode
133+
'--case=ignore' | '--ignore-case' | '--ignore-case=yes') option_case='ignore' ;; # compare
134+
'--case=sensitive' | '--ignore-case=no') option_case='sensitive' ;;
135+
'--case=') : ;;
131136
--)
132137
option_inputs_defined=yes
133138
option_inputs+=("$@")
@@ -160,7 +165,7 @@ function is_needle() (
160165
# =====================================
161166
# Action
162167

163-
__has --source={option_inputs} --ignore-case="$option_ignore_case" --"$option_mode" --overlap "${option_lookups[@]}" || return 1
168+
__has --source={option_inputs} --case="$option_case" --"$option_mode" --overlap "${option_lookups[@]}" || return 1
164169
)
165170

166171
# fire if invoked standalone

sources/bash.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,8 +1029,8 @@ else
10291029
BASH_NATIVE_UPPERCASE_SUFFIX=''
10301030
BASH_NATIVE_LOWERCASE_SUFFIX=''
10311031
# bash versions prior to v4 also do not have:
1032-
# `declare -u`: -u to convert NAMEs to upper case on assignment
1033-
# `declare -l`: -l to convert NAMEs to lower case on assignment
1032+
# `declare -u`: -u to convert NAMEs to uppercase on assignment
1033+
# `declare -l`: -l to convert NAMEs to lowercase on assignment
10341034
function __get_uppercase_first_letter {
10351035
# trim -- prefix
10361036
if [[ ${1-} == '--' ]]; then

sources/stdinargs.bash

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ function __print_piece {
113113
fi
114114
}
115115

116+
# This allows the caller to filter out our arguments from theirs
117+
function __is_stdinargs_option_or_argument {
118+
case "$1" in
119+
'--no-color'* | '--color' | '--no-stdin'* | '--stdin' | '--no-timeout'* | '--timeout' | '--no-inline'* | '--inline' | '--max-args=' | '-' | '--') return 0 ;;
120+
'--'*) return 1 ;;
121+
*) return 0 ;;
122+
esac
123+
}
124+
# consider `__segregate_options --stdinargs={option_stdinargs} --ours={options_filter}`
125+
116126
# This processes the arguments.
117127
# This cannot become a safety function, as it needs to support unsafe functions, which safety functions cannot.
118128
# Only if unsafe is hard deprecated, could it become a safety function, but that doesn't make sense.

0 commit comments

Comments
 (0)