Skip to content

Commit b745fcd

Browse files
authored
Upgraded all test to use declare instead of echo (#33)
This simplifies comparing bash builtin `getopts` and `getopts_long`, especially where one sets a blank variable, while the other one unsets it instead.
1 parent 936fdd5 commit b745fcd

32 files changed

+375
-339
lines changed

test/bats/github_13.bats

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,78 +15,74 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-no_shortspec'
1515
@test "${FEATURE}: long variable, silent" {
1616
compare '-v user_val' \
1717
'--variable=user_val' \
18-
'/^OPTIND: /d'
19-
expect "${bash_getopts[5]}" == 'OPTIND: 3'
20-
expect "${getopts_long[5]}" == 'OPTIND: 2'
18+
'/^declare -i OPTIND=/d'
19+
expect "${bash_getopts[5]}" == 'declare -i OPTIND="3"'
20+
expect "${getopts_long[5]}" == 'declare -i OPTIND="2"'
2121
}
2222
@test "${FEATURE}: long variable, verbose" {
2323
compare '-v user_val' \
2424
'--variable=user_val' \
25-
'/^OPTIND: /d'
26-
expect "${bash_getopts[5]}" == 'OPTIND: 3'
27-
expect "${getopts_long[5]}" == 'OPTIND: 2'
25+
'/^declare -i OPTIND=/d'
26+
expect "${bash_getopts[5]}" == 'declare -i OPTIND="3"'
27+
expect "${getopts_long[5]}" == 'declare -i OPTIND="2"'
2828
}
2929

3030
@test "${FEATURE}: toggle followed by long variable, silent" {
3131
compare '-t -v user_val' \
3232
'--toggle --variable=user_val' \
33-
'/^OPTIND: /d'
34-
expect "${bash_getopts[6]}" == 'OPTIND: 4'
35-
expect "${getopts_long[6]}" == 'OPTIND: 3'
33+
'/^declare -i OPTIND=/d'
34+
expect "${bash_getopts[6]}" == 'declare -i OPTIND="4"'
35+
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
3636
}
3737
@test "${FEATURE}: toggle followed by long variable, verbose" {
3838
compare '-t -v user_val' \
3939
'--toggle --variable=user_val' \
40-
'/^OPTIND: /d'
41-
expect "${bash_getopts[6]}" == 'OPTIND: 4'
42-
expect "${getopts_long[6]}" == 'OPTIND: 3'
40+
'/^declare -i OPTIND=/d'
41+
expect "${bash_getopts[6]}" == 'declare -i OPTIND="4"'
42+
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
4343
}
4444

4545
@test "${FEATURE}: long variable followed by toggle, silent" {
4646
compare '-v user_val -t' \
4747
'--variable=user_val --toggle' \
48-
'/^OPTIND: /d'
49-
expect "${bash_getopts[6]}" == 'OPTIND: 4'
50-
expect "${getopts_long[6]}" == 'OPTIND: 3'
48+
'/^declare -i OPTIND=/d'
49+
expect "${bash_getopts[6]}" == 'declare -i OPTIND="4"'
50+
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
5151
}
5252
@test "${FEATURE}: long variable followed by toggle, verbose" {
5353
compare '-v user_val -t' \
5454
'--variable=user_val --toggle' \
55-
'/^OPTIND: /d'
56-
expect "${bash_getopts[6]}" == 'OPTIND: 4'
57-
expect "${getopts_long[6]}" == 'OPTIND: 3'
55+
'/^declare -i OPTIND=/d'
56+
expect "${bash_getopts[6]}" == 'declare -i OPTIND="4"'
57+
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
5858
}
5959

6060
@test "${FEATURE}: terminator followed by long variable, silent" {
61-
compare '-t -- -v user_val' \
61+
compare '-t -- -vuser_val' \
6262
'--toggle -- --variable=user_val' \
63-
'/^\$@: /d'
64-
expect "${bash_getopts[6]}" == '$@: ([0]="-v" [1]="user_val")'
65-
expect "${getopts_long[6]}" == '$@: ([0]="--variable=user_val")'
63+
'6{s/\[0]="(-v|--variable=)/[0]="-NORMALIZED=/}'
64+
expect "${bash_getopts[6]}" == 'declare -a $@=([0]="-vuser_val")'
65+
expect "${getopts_long[6]}" == 'declare -a $@=([0]="--variable=user_val")'
6666
}
6767
@test "${FEATURE}: terminator followed by long variable, verbose" {
68-
compare '-t -- -v user_val' \
68+
compare '-t -- -vuser_val' \
6969
'--toggle -- --variable=user_val' \
70-
'/^\$@: /d'
71-
expect "${bash_getopts[6]}" == '$@: ([0]="-v" [1]="user_val")'
72-
expect "${getopts_long[6]}" == '$@: ([0]="--variable=user_val")'
70+
'6{s/\[0]="(-v|--variable=)/[0]="-NORMALIZED=/}'
71+
expect "${bash_getopts[6]}" == 'declare -a $@=([0]="-vuser_val")'
72+
expect "${getopts_long[6]}" == 'declare -a $@=([0]="--variable=user_val")'
7373
}
7474

7575
@test "${FEATURE}: long variable followed by terminator, silent" {
76-
compare '-v user_val -- -t' \
76+
compare '-vuser_val -- -t' \
7777
'--variable=user_val -- --toggle' \
78-
'/^(OPTIND|\$@): /d'
79-
expect "${bash_getopts[5]}" == 'OPTIND: 4'
80-
expect "${getopts_long[5]}" == 'OPTIND: 3'
81-
expect "${bash_getopts[6]}" == '$@: ([0]="-t")'
82-
expect "${getopts_long[6]}" == '$@: ([0]="--toggle")'
78+
'6{s/\[0]="(-t|--toggle)"/[0]="NORMALIZED"/}'
79+
expect "${bash_getopts[6]}" == 'declare -a $@=([0]="-t")'
80+
expect "${getopts_long[6]}" == 'declare -a $@=([0]="--toggle")'
8381
}
8482
@test "${FEATURE}: long variable followed by terminator, verbose" {
85-
compare '-v user_val -- -t' \
83+
compare '-vuser_val -- -t' \
8684
'--variable=user_val -- --toggle' \
87-
'/^(OPTIND|\$@): /d'
88-
expect "${bash_getopts[5]}" == 'OPTIND: 4'
89-
expect "${getopts_long[5]}" == 'OPTIND: 3'
90-
expect "${bash_getopts[6]}" == '$@: ([0]="-t")'
91-
expect "${getopts_long[6]}" == '$@: ([0]="--toggle")'
85+
'6{s/\[0]="(-t|--toggle)"/[0]="NORMALIZED"/}'
86+
expect "${bash_getopts[6]}" == 'declare -a $@=([0]="-t")'
87+
expect "${getopts_long[6]}" == 'declare -a $@=([0]="--toggle")'
9288
}

test/bats/github_15a.bats

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ load ../test_helper
3737
'--toggle-- --toggle user_arg' \
3838
'1{/^toggle triggered/d}' \
3939
'/^INVALID OPTION/d'
40-
expect "${bash_getopts[2]}" == 'INVALID OPTION -- OPTARG="-"'
41-
expect "${bash_getopts[3]}" == 'INVALID OPTION -- OPTARG="-"'
42-
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="toggle--"'
43-
expect "${bash_getopts[1]}" == 'toggle triggered -- OPTARG'
44-
expect "${bash_getopts[4]}" == 'toggle triggered -- OPTARG'
45-
expect "${getopts_long[2]}" == 'toggle triggered -- OPTARG'
40+
expect "${bash_getopts[2]}" == 'INVALID OPTION -- declare -- OPTARG="-"'
41+
expect "${bash_getopts[3]}" == 'INVALID OPTION -- declare -- OPTARG="-"'
42+
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="toggle--"'
43+
expect "${bash_getopts[1]}" == 'toggle triggered -- declare -- OPTARG'
44+
expect "${bash_getopts[4]}" == 'toggle triggered -- declare -- OPTARG'
45+
expect "${getopts_long[2]}" == 'toggle triggered -- declare -- OPTARG'
4646
}
4747
@test "${FEATURE}: long toggle, verbose" {
4848
compare '-t-- -t user_arg' \
@@ -52,12 +52,12 @@ load ../test_helper
5252
'5{/^INVALID OPTION or MISSING ARGUMENT/d}' \
5353
's/getopts[[:alpha:]_-]*/GETOPTS-NORMALISED/' \
5454
's/(illegal option --) (-|toggle--)/\1 TOGGLE-NORMALISED/'
55-
expect "${bash_getopts[1]}" == 'toggle triggered -- OPTARG'
55+
expect "${bash_getopts[1]}" == 'toggle triggered -- declare -- OPTARG'
5656
expect "${bash_getopts[2]}" =~ 'getopts-verbose: illegal option -- -$'
5757
expect "${bash_getopts[4]}" =~ 'getopts-verbose: illegal option -- -$'
58-
expect "${bash_getopts[6]}" == 'toggle triggered -- OPTARG'
58+
expect "${bash_getopts[6]}" == 'toggle triggered -- declare -- OPTARG'
5959
expect "${getopts_long[1]}" =~ 'getopts_long-verbose: illegal option -- toggle--$'
60-
expect "${getopts_long[3]}" == 'toggle triggered -- OPTARG'
60+
expect "${getopts_long[3]}" == 'toggle triggered -- declare -- OPTARG'
6161
}
6262

6363
# Both implementations should see:
@@ -82,14 +82,14 @@ load ../test_helper
8282
compare '-o-- -t user_arg' \
8383
'--option-- --toggle user_arg' \
8484
'1{/(option supplied|INVALID OPTION)/d}'
85-
expect "${bash_getopts[1]}" == 'option supplied -- OPTARG="--"'
86-
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="option--"'
85+
expect "${bash_getopts[1]}" == 'option supplied -- declare -- OPTARG="--"'
86+
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="option--"'
8787
}
8888
@test "${FEATURE}: long option, verbose" {
8989
compare '-o-- -t user_arg' \
9090
'--option-- --toggle user_arg' \
9191
'1{/(option supplied|illegal option)/d}' \
9292
'2{/^INVALID OPTION or MISSING ARGUMENT/d}'
93-
expect "${bash_getopts[1]}" == 'option supplied -- OPTARG="--"'
93+
expect "${bash_getopts[1]}" == 'option supplied -- declare -- OPTARG="--"'
9494
expect "${getopts_long[1]}" =~ "getopts_long-verbose: illegal option -- option--$"
9595
}

test/bats/github_15b.bats

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-shortspec_with_dash'
4040
'--toggle-- --toggle user_arg' \
4141
'1{/^toggle triggered/d}' \
4242
'/^INVALID OPTION/d'
43-
expect "${bash_getopts[2]}" == 'INVALID OPTION -- OPTARG="-"'
44-
expect "${bash_getopts[3]}" == 'INVALID OPTION -- OPTARG="-"'
45-
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="toggle--"'
46-
expect "${bash_getopts[1]}" == 'toggle triggered -- OPTARG'
47-
expect "${bash_getopts[4]}" == 'toggle triggered -- OPTARG'
48-
expect "${getopts_long[2]}" == 'toggle triggered -- OPTARG'
43+
expect "${bash_getopts[2]}" == 'INVALID OPTION -- declare -- OPTARG="-"'
44+
expect "${bash_getopts[3]}" == 'INVALID OPTION -- declare -- OPTARG="-"'
45+
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="toggle--"'
46+
expect "${bash_getopts[1]}" == 'toggle triggered -- declare -- OPTARG'
47+
expect "${bash_getopts[4]}" == 'toggle triggered -- declare -- OPTARG'
48+
expect "${getopts_long[2]}" == 'toggle triggered -- declare -- OPTARG'
4949
}
5050
@test "${FEATURE}: long toggle, verbose" {
5151
compare '-t-- -t user_arg' \
@@ -55,12 +55,12 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-shortspec_with_dash'
5555
'5{/^INVALID OPTION or MISSING ARGUMENT/d}' \
5656
's/getopts[[:alpha:]_-]*/GETOPTS-NORMALISED/' \
5757
's/(illegal option --) (-|toggle--)/\1 TOGGLE-NORMALISED/'
58-
expect "${bash_getopts[1]}" == 'toggle triggered -- OPTARG'
58+
expect "${bash_getopts[1]}" == 'toggle triggered -- declare -- OPTARG'
5959
expect "${bash_getopts[2]}" =~ 'getopts-verbose: illegal option -- -$'
6060
expect "${bash_getopts[4]}" =~ 'getopts-verbose: illegal option -- -$'
61-
expect "${bash_getopts[6]}" == 'toggle triggered -- OPTARG'
61+
expect "${bash_getopts[6]}" == 'toggle triggered -- declare -- OPTARG'
6262
expect "${getopts_long[1]}" =~ 'getopts_long-\w+-verbose: illegal option -- toggle--$'
63-
expect "${getopts_long[3]}" == 'toggle triggered -- OPTARG'
63+
expect "${getopts_long[3]}" == 'toggle triggered -- declare -- OPTARG'
6464
}
6565

6666
# Both implementations should see:
@@ -85,14 +85,14 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-shortspec_with_dash'
8585
compare '-o-- -t user_arg' \
8686
'--option-- --toggle user_arg' \
8787
'1{/(option supplied|INVALID OPTION)/d}'
88-
expect "${bash_getopts[1]}" == 'option supplied -- OPTARG="--"'
89-
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="option--"'
88+
expect "${bash_getopts[1]}" == 'option supplied -- declare -- OPTARG="--"'
89+
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="option--"'
9090
}
9191
@test "${FEATURE}: long option, verbose" {
9292
compare '-o-- -t user_arg' \
9393
'--option-- --toggle user_arg' \
9494
'1{/(option supplied|illegal option)/d}' \
9595
'2{/^INVALID OPTION or MISSING ARGUMENT/d}'
96-
expect "${bash_getopts[1]}" == 'option supplied -- OPTARG="--"'
96+
expect "${bash_getopts[1]}" == 'option supplied -- declare -- OPTARG="--"'
9797
expect "${getopts_long[1]}" =~ "getopts_long-\w+-verbose: illegal option -- option--$"
9898
}

test/bats/github_26a.bats

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-longspec_with_dash'
1515
@test "${FEATURE}: double toggle, silent" {
1616
compare '-tt' \
1717
'--- --toggle' \
18-
'/^OPTIND: /d'
19-
expect "${bash_getopts[6]}" == 'OPTIND: 2'
20-
expect "${getopts_long[6]}" == 'OPTIND: 3'
18+
'/^declare -i OPTIND=/d'
19+
expect "${bash_getopts[6]}" == 'declare -i OPTIND="2"'
20+
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
2121
}
2222
@test "${FEATURE}: double toggle, verbose" {
2323
compare '-tt' \
2424
'--- --toggle' \
25-
'/^OPTIND: /d'
26-
expect "${bash_getopts[6]}" == 'OPTIND: 2'
27-
expect "${getopts_long[6]}" == 'OPTIND: 3'
25+
'/^declare -i OPTIND=/d'
26+
expect "${bash_getopts[6]}" == 'declare -i OPTIND="2"'
27+
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
2828
}

test/bats/github_26b.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-longspec_with_dash_colon'
1818
compare '-z' \
1919
'---zz' \
2020
'/^INVALID OPTION/d'
21-
expect "${bash_getopts[1]}" == 'INVALID OPTION -- OPTARG="z"'
22-
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="-zz"'
21+
expect "${bash_getopts[1]}" == 'INVALID OPTION -- declare -- OPTARG="z"'
22+
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="-zz"'
2323
}
2424
@test "${FEATURE}: option with adjacent value, verbose" {
2525
compare '-z' \

test/bats/invalid_arguments.bats

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ load ../test_helper
1616
compare '-i' \
1717
'--invalid' \
1818
'/^INVALID OPTION -- /d'
19-
expect "${bash_getopts[1]}" == 'INVALID OPTION -- OPTARG="i"'
20-
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="invalid"'
19+
expect "${bash_getopts[1]}" == 'INVALID OPTION -- declare -- OPTARG="i"'
20+
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="invalid"'
2121
}
2222
@test "${FEATURE}: long option, verbose" {
2323
compare '-i' \
@@ -41,8 +41,8 @@ load ../test_helper
4141
compare '-i user_arg' \
4242
'--invalid user_arg' \
4343
'/^INVALID OPTION -- /d'
44-
expect "${bash_getopts[1]}" == 'INVALID OPTION -- OPTARG="i"'
45-
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="invalid"'
44+
expect "${bash_getopts[1]}" == 'INVALID OPTION -- declare -- OPTARG="i"'
45+
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="invalid"'
4646
}
4747
@test "${FEATURE}: long option, extra arguments, verbose" {
4848
compare '-i user_arg' \
@@ -55,53 +55,53 @@ load ../test_helper
5555
@test "${FEATURE}: short option, terminator, extra arguments, silent" {
5656
compare '-i -- user_arg' \
5757
'-i -- user_arg'
58-
expect "${getopts_long[6]}" == '$@: ([0]="user_arg")'
58+
expect "${getopts_long[6]}" == 'declare -a $@=([0]="user_arg")'
5959
}
6060
@test "${FEATURE}: short option, terminator, extra arguments, verbose" {
6161
compare '-i -- user_arg' \
6262
'-i -- user_arg' \
6363
's/getopts_long-verbose/getopts-verbose/g'
64-
expect "${getopts_long[7]}" == '$@: ([0]="user_arg")'
64+
expect "${getopts_long[7]}" == 'declare -a $@=([0]="user_arg")'
6565
}
6666

6767
@test "${FEATURE}: long option, terminator, extra arguments, silent" {
6868
compare '-i -- user_arg' \
6969
'--invalid -- user_arg' \
7070
'/^INVALID OPTION -- /d'
71-
expect "${bash_getopts[1]}" == 'INVALID OPTION -- OPTARG="i"'
72-
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="invalid"'
73-
expect "${getopts_long[6]}" == '$@: ([0]="user_arg")'
71+
expect "${bash_getopts[1]}" == 'INVALID OPTION -- declare -- OPTARG="i"'
72+
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="invalid"'
73+
expect "${getopts_long[6]}" == 'declare -a $@=([0]="user_arg")'
7474
}
7575
@test "${FEATURE}: long option, terminator, extra arguments, verbose" {
7676
compare '-i -- user_arg' \
7777
'--invalid -- user_arg' \
7878
's/getopts_long-verbose: (.*) invalid$/getopts-verbose: \1 i/g'
79-
expect "${getopts_long[7]}" == '$@: ([0]="user_arg")'
79+
expect "${getopts_long[7]}" == 'declare -a $@=([0]="user_arg")'
8080
}
8181

8282
# terminator followed by options
8383

8484
@test "${FEATURE}: terminator, short option, extra arguments, silent" {
8585
compare '-- -i user_arg' \
8686
'-- -i user_arg'
87-
expect "${getopts_long[5]}" == '$@: ([0]="-i" [1]="user_arg")'
87+
expect "${getopts_long[5]}" == 'declare -a $@=([0]="-i" [1]="user_arg")'
8888
}
8989
@test "${FEATURE}: terminator, short option, extra arguments, verbose" {
9090
compare '-- -i user_arg' \
9191
'-- -i user_arg' \
9292
's/getopts_long-verbose/getopts-verbose/g'
93-
expect "${getopts_long[5]}" == '$@: ([0]="-i" [1]="user_arg")'
93+
expect "${getopts_long[5]}" == 'declare -a $@=([0]="-i" [1]="user_arg")'
9494
}
9595

9696
@test "${FEATURE}: terminator, long option, extra arguments, silent" {
9797
compare '-- -i user_arg' \
9898
'-- --invalid user_arg' \
99-
'/^\$@: /d'
100-
expect "${getopts_long[5]}" == '$@: ([0]="--invalid" [1]="user_arg")'
99+
'5{s/\[0]="(-i|--invalid)"/[0]="NORMALIZED"/}'
100+
expect "${getopts_long[5]}" == 'declare -a $@=([0]="--invalid" [1]="user_arg")'
101101
}
102102
@test "${FEATURE}: terminator, long option, extra arguments, verbose" {
103103
compare '-- -i user_arg' \
104104
'-- --invalid user_arg' \
105-
'/^\$@: /d'
106-
expect "${getopts_long[5]}" == '$@: ([0]="--invalid" [1]="user_arg")'
105+
'5{s/\[0]="(-i|--invalid)"/[0]="NORMALIZED"/}'
106+
expect "${getopts_long[5]}" == 'declare -a $@=([0]="--invalid" [1]="user_arg")'
107107
}

test/bats/no_arguments.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ load ../test_helper
2121
@test "${FEATURE}: terminator, extra arguments, silent" {
2222
compare '-- user_arg' \
2323
'-- user_arg'
24-
expect "${getopts_long[5]}" == '$@: ([0]="user_arg")'
24+
expect "${getopts_long[5]}" == 'declare -a $@=([0]="user_arg")'
2525
}
2626
@test "${FEATURE}: terminator, extra arguments, verbose" {
2727
compare '-- user_arg' \
2828
'-- user_arg'
29-
expect "${getopts_long[5]}" == '$@: ([0]="user_arg")'
29+
expect "${getopts_long[5]}" == 'declare -a $@=([0]="user_arg")'
3030
}

0 commit comments

Comments
 (0)