Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit 3940b04

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Improve stack name checks and tests
1 parent 518ab89 commit 3940b04

File tree

3 files changed

+111
-13
lines changed

3 files changed

+111
-13
lines changed

lib/psst/basic/assert.inc.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ assert_fail_psst()
3030
# conflict when defining local variables.
3131

3232
# msg=$1
33-
printf "Assertion fail: %s!\n" "$1" >&2
33+
printf "Assertion fail: %s\n" "$1" >&2
3434
exit 127
3535
}
3636

lib/psst/basic/stack.inc.sh

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-}:stack:"
2525
# e.g. must not contain spaces.
2626
#
2727
# PARAMETERS
28-
# stackName: Name of the stack (no spaces, only letters and underscore).
28+
# stackName: Name of the stack (only letters, digits, and underscore).
2929
# newValue: Value to push on stack.
3030
#
31+
# NOTE
32+
# `stackName` must not start with a digit!
33+
#
3134
# SAMPLE
3235
# stack_push_psst 'someStackName' "$value"
3336
#
@@ -43,6 +46,13 @@ stack_push_psst()
4346
#stackName=$1
4447
#newValue=$2
4548

49+
case $1 in
50+
[0-9]*) assert_func_fail_psst 'stack_push_psst' \
51+
'stackName must not start witha digit' ;;
52+
*[!a-zA-Z0-9_]*) assert_func_fail_psst 'stack_push_psst' \
53+
'stackName must only contain letters, digits, and underscore'
54+
esac
55+
4656
_count_stack_psst=
4757
_countName_stack_psst="${1}__count_stack_psst"
4858
eval "_count_stack_psst=\${$_countName_stack_psst-0}"
@@ -98,6 +108,13 @@ stack_pop_psst()
98108
# stackName=$1
99109
# resultVarName=$2
100110

111+
case $1 in
112+
[0-9]*) assert_func_fail_psst 'stack_push_psst' \
113+
'stackName must not start witha digit' ;;
114+
*[!a-zA-Z0-9_]*) assert_func_fail_psst 'stack_pop_psst' \
115+
'stackName must only contain letters, digits, and underscore'
116+
esac
117+
101118
_count_stack_psst=
102119
_countName_stack_psst="${1}__count_stack_psst"
103120
eval "_count_stack_psst=\${$_countName_stack_psst-}"
@@ -156,8 +173,18 @@ stack_exists_psst()
156173
# variables in the main shell. Thus we need to be careful to not conflict
157174
# when defining local variables.
158175

176+
assert_argc_psst 'stack_exists_psst' 1 $#
177+
assert_hasarg_psst 'stack_exists_psst' 'stackName' "$1"
178+
159179
#stackName=$1
160180

181+
case $1 in
182+
[0-9]*) assert_func_fail_psst 'stack_exists_psst' \
183+
'stackName must not start witha digit' ;;
184+
*[!a-zA-Z0-9_]*) assert_func_fail_psst 'stack_exists_psst' \
185+
'stackName must only contain letters, digits, and underscore'
186+
esac
187+
161188
_count_stack_psst=
162189
_countName_stack_psst="${1}__count_stack_psst"
163190
eval "_count_stack_psst=\${$_countName_stack_psst-}"
@@ -196,8 +223,18 @@ stack_count_psst()
196223
# variables in the main shell. Thus we need to be careful to not conflict
197224
# when defining local variables.
198225

226+
assert_argc_psst 'stack_count_psst' 1 $#
227+
assert_hasarg_psst 'stack_count_psst' 'stackName' "$1"
228+
199229
#stackName=$1
200230

231+
case $1 in
232+
[0-9]*) assert_func_fail_psst 'stack_count_psst' \
233+
'stackName must not start witha digit' ;;
234+
*[!a-zA-Z0-9_]*) assert_func_fail_psst 'stack_count_psst' \
235+
'stackName must only contain letters, digits, and underscore'
236+
esac
237+
201238
_count_stack_psst=
202239
_countName_stack_psst="${1}__count_stack_psst"
203240
eval "_count_stack_psst=\${$_countName_stack_psst-0}"

test/basic/bin/test_stack.sh

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,18 @@ set +e
2525
[ $? = 127 ] || test_fail_psst $LINENO
2626
set -e
2727

28-
# Stack name must not be empty, value can be empty
28+
# Stack name must not be empty or start with a digit or have spaces.
29+
# Value can be empty.
2930
set +e
3031
( stack_push_psst '' 1 2>/dev/null )
3132
[ $? = 127 ] || test_fail_psst $LINENO
3233

34+
( stack_push_psst '1stack' '' 2>/dev/null )
35+
[ $? = 127 ] || test_fail_psst $LINENO
36+
37+
( stack_puth_psst 'stack name' '' 2>/dev/null )
38+
[ $? = 127 ] || test_fail_psst $LINENO
39+
3340
( stack_push_psst 'stack' '' 2>/dev/null ) || test_fail_psst $LINENO
3441
set -e
3542

@@ -43,31 +50,84 @@ set +e
4350
[ $? = 127 ] || test_fail_psst $LINENO
4451
set -e
4552

46-
# Neither one may be empty
53+
# Stack name must not be empty or start with a digit or have spaces.
54+
# outVar name must not be empty.
4755
set +e
4856
( stack_pop_psst '' 2>/dev/null )
4957
[ $? = 127 ] || test_fail_psst $LINENO
5058

59+
( stack_pop_psst '1stack' '' 2>/dev/null )
60+
[ $? = 127 ] || test_fail_psst $LINENO
61+
62+
( stack_pop_psst 'stack name' 2>/dev/null )
63+
[ $? = 127 ] || test_fail_psst $LINENO
64+
5165
( stack_pop_psst 'stack' '' 2>/dev/null )
5266
[ $? = 127 ] || test_fail_psst $LINENO
5367
set -e
5468

5569

70+
# Exists must accept exactly one argument
71+
set +e
72+
( stack_exists_psst 2>/dev/null )
73+
[ $? = 127 ] || test_fail_psst $LINENO
74+
75+
( stack_exists_psst 'stack' 1 2>/dev/null )
76+
[ $? = 127 ] || test_fail_psst $LINENO
77+
set -e
78+
79+
# Stack name must not be empty or start with a digit or have spaces
80+
set +e
81+
( stack_exists_psst '' 2>/dev/null )
82+
[ $? = 127 ] || test_fail_psst $LINENO
83+
84+
( stack_exists_psst '1stack' 2>/dev/null )
85+
[ $? = 127 ] || test_fail_psst $LINENO
86+
87+
( stack_exists_psst 'stack name' '' 2>/dev/null )
88+
[ $? = 127 ] || test_fail_psst $LINENO
89+
set -e
90+
91+
92+
93+
# Count must accept exactly one argument
94+
set +e
95+
( stack_count_psst 2>/dev/null )
96+
[ $? = 127 ] || test_fail_psst $LINENO
97+
98+
( stack_count_psst 'stack' 1 2>/dev/null )
99+
[ $? = 127 ] || test_fail_psst $LINENO
100+
set -e
101+
102+
# Stack name must not be empty or start with a digit or have spaces
103+
set +e
104+
( stack_count_psst '' 2>/dev/null )
105+
[ $? = 127 ] || test_fail_psst $LINENO
106+
107+
( stack_count_psst '1stack' '' 2>/dev/null )
108+
[ $? = 127 ] || test_fail_psst $LINENO
109+
110+
( stack_count_psst 'stack name' '' 2>/dev/null )
111+
[ $? = 127 ] || test_fail_psst $LINENO
112+
set -e
113+
114+
115+
56116
# Test push creates and pops destroys
57117
(
58118
! stack_exists_psst 'stack' || test_fail_psst $LINENO
59-
# Verify that the gloabl namespace has not been polluted
60-
! ( set | grep '^_.*_psst' ) || test_fail_psst $LINENO
119+
# Verify that the gloabl namespace has not been polluted
120+
! ( set | grep '^_.*_psst' ) || test_fail_psst $LINENO
61121

62122
stack_push_psst 'stack' 1 || test_fail_psst $LINENO
63123
stack_exists_psst 'stack' || test_fail_psst $LINENO
64-
# Verify that the gloabl namespace has not been polluted
65-
! ( set | grep "^_.*_psst" ) || test_fail_psst $LINENO
124+
# Verify that the gloabl namespace has not been polluted
125+
! ( set | grep "^_.*_psst" ) || test_fail_psst $LINENO
66126

67127
stack_pop_psst 'stack' || test_fail_psst $LINENO
68128
! stack_exists_psst 'stack' || test_fail_psst $LINENO
69-
# Verify that the gloabl namespace has not been polluted
70-
! ( set | grep '^_.*_psst' ) || test_fail_psst $LINENO
129+
# Verify that the gloabl namespace has not been polluted
130+
! ( set | grep '^_.*_psst' ) || test_fail_psst $LINENO
71131
)
72132

73133

@@ -80,7 +140,7 @@ set -e
80140
stack_exists_psst 'stack' || test_fail_psst $LINENO
81141

82142
stack_pop_psst 'stack' || test_fail_psst $LINENO
83-
stack_exists_psst 'stack' test_fail_psst $LINENO
143+
stack_exists_psst 'stack' || test_fail_psst $LINENO
84144

85145
stack_pop_psst 'stack' || test_fail_psst $LINENO
86146
! stack_exists_psst 'stack' || test_fail_psst $LINENO
@@ -136,11 +196,12 @@ test4Res=
136196
! stack_pop_psst 'stack' test4Res || test_fail_psst $LINENO
137197
[ -z "$test4Res" ] || test_fail_psst $LINENO
138198

139-
140199
# Verify that the gloabl namespace has not been polluted
141200
! ( set | grep '^_.*_psst' ) || test_fail_psst $LINENO
142201

143-
# Test count
202+
203+
204+
# Test stack count
144205
(
145206
[ "$( stack_count_psst 'stack' )" -eq 0 ] || test_fail_psst $LINENO
146207
stack_push_psst 'stack' 'a' || test_fail_psst $LINENO

0 commit comments

Comments
 (0)