Skip to content

Commit

Permalink
Upgraded all test to use declare instead of echo (#33)
Browse files Browse the repository at this point in the history
This simplifies comparing bash builtin `getopts` and `getopts_long`, especially where one sets a blank variable, while the other one unsets it instead.
  • Loading branch information
UrsaDK authored Dec 18, 2024
1 parent 936fdd5 commit b745fcd
Show file tree
Hide file tree
Showing 32 changed files with 375 additions and 339 deletions.
72 changes: 34 additions & 38 deletions test/bats/github_13.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,78 +15,74 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-no_shortspec'
@test "${FEATURE}: long variable, silent" {
compare '-v user_val' \
'--variable=user_val' \
'/^OPTIND: /d'
expect "${bash_getopts[5]}" == 'OPTIND: 3'
expect "${getopts_long[5]}" == 'OPTIND: 2'
'/^declare -i OPTIND=/d'
expect "${bash_getopts[5]}" == 'declare -i OPTIND="3"'
expect "${getopts_long[5]}" == 'declare -i OPTIND="2"'
}
@test "${FEATURE}: long variable, verbose" {
compare '-v user_val' \
'--variable=user_val' \
'/^OPTIND: /d'
expect "${bash_getopts[5]}" == 'OPTIND: 3'
expect "${getopts_long[5]}" == 'OPTIND: 2'
'/^declare -i OPTIND=/d'
expect "${bash_getopts[5]}" == 'declare -i OPTIND="3"'
expect "${getopts_long[5]}" == 'declare -i OPTIND="2"'
}

@test "${FEATURE}: toggle followed by long variable, silent" {
compare '-t -v user_val' \
'--toggle --variable=user_val' \
'/^OPTIND: /d'
expect "${bash_getopts[6]}" == 'OPTIND: 4'
expect "${getopts_long[6]}" == 'OPTIND: 3'
'/^declare -i OPTIND=/d'
expect "${bash_getopts[6]}" == 'declare -i OPTIND="4"'
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
}
@test "${FEATURE}: toggle followed by long variable, verbose" {
compare '-t -v user_val' \
'--toggle --variable=user_val' \
'/^OPTIND: /d'
expect "${bash_getopts[6]}" == 'OPTIND: 4'
expect "${getopts_long[6]}" == 'OPTIND: 3'
'/^declare -i OPTIND=/d'
expect "${bash_getopts[6]}" == 'declare -i OPTIND="4"'
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
}

@test "${FEATURE}: long variable followed by toggle, silent" {
compare '-v user_val -t' \
'--variable=user_val --toggle' \
'/^OPTIND: /d'
expect "${bash_getopts[6]}" == 'OPTIND: 4'
expect "${getopts_long[6]}" == 'OPTIND: 3'
'/^declare -i OPTIND=/d'
expect "${bash_getopts[6]}" == 'declare -i OPTIND="4"'
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
}
@test "${FEATURE}: long variable followed by toggle, verbose" {
compare '-v user_val -t' \
'--variable=user_val --toggle' \
'/^OPTIND: /d'
expect "${bash_getopts[6]}" == 'OPTIND: 4'
expect "${getopts_long[6]}" == 'OPTIND: 3'
'/^declare -i OPTIND=/d'
expect "${bash_getopts[6]}" == 'declare -i OPTIND="4"'
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
}

@test "${FEATURE}: terminator followed by long variable, silent" {
compare '-t -- -v user_val' \
compare '-t -- -vuser_val' \
'--toggle -- --variable=user_val' \
'/^\$@: /d'
expect "${bash_getopts[6]}" == '$@: ([0]="-v" [1]="user_val")'
expect "${getopts_long[6]}" == '$@: ([0]="--variable=user_val")'
'6{s/\[0]="(-v|--variable=)/[0]="-NORMALIZED=/}'
expect "${bash_getopts[6]}" == 'declare -a $@=([0]="-vuser_val")'
expect "${getopts_long[6]}" == 'declare -a $@=([0]="--variable=user_val")'
}
@test "${FEATURE}: terminator followed by long variable, verbose" {
compare '-t -- -v user_val' \
compare '-t -- -vuser_val' \
'--toggle -- --variable=user_val' \
'/^\$@: /d'
expect "${bash_getopts[6]}" == '$@: ([0]="-v" [1]="user_val")'
expect "${getopts_long[6]}" == '$@: ([0]="--variable=user_val")'
'6{s/\[0]="(-v|--variable=)/[0]="-NORMALIZED=/}'
expect "${bash_getopts[6]}" == 'declare -a $@=([0]="-vuser_val")'
expect "${getopts_long[6]}" == 'declare -a $@=([0]="--variable=user_val")'
}

@test "${FEATURE}: long variable followed by terminator, silent" {
compare '-v user_val -- -t' \
compare '-vuser_val -- -t' \
'--variable=user_val -- --toggle' \
'/^(OPTIND|\$@): /d'
expect "${bash_getopts[5]}" == 'OPTIND: 4'
expect "${getopts_long[5]}" == 'OPTIND: 3'
expect "${bash_getopts[6]}" == '$@: ([0]="-t")'
expect "${getopts_long[6]}" == '$@: ([0]="--toggle")'
'6{s/\[0]="(-t|--toggle)"/[0]="NORMALIZED"/}'
expect "${bash_getopts[6]}" == 'declare -a $@=([0]="-t")'
expect "${getopts_long[6]}" == 'declare -a $@=([0]="--toggle")'
}
@test "${FEATURE}: long variable followed by terminator, verbose" {
compare '-v user_val -- -t' \
compare '-vuser_val -- -t' \
'--variable=user_val -- --toggle' \
'/^(OPTIND|\$@): /d'
expect "${bash_getopts[5]}" == 'OPTIND: 4'
expect "${getopts_long[5]}" == 'OPTIND: 3'
expect "${bash_getopts[6]}" == '$@: ([0]="-t")'
expect "${getopts_long[6]}" == '$@: ([0]="--toggle")'
'6{s/\[0]="(-t|--toggle)"/[0]="NORMALIZED"/}'
expect "${bash_getopts[6]}" == 'declare -a $@=([0]="-t")'
expect "${getopts_long[6]}" == 'declare -a $@=([0]="--toggle")'
}
24 changes: 12 additions & 12 deletions test/bats/github_15a.bats
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ load ../test_helper
'--toggle-- --toggle user_arg' \
'1{/^toggle triggered/d}' \
'/^INVALID OPTION/d'
expect "${bash_getopts[2]}" == 'INVALID OPTION -- OPTARG="-"'
expect "${bash_getopts[3]}" == 'INVALID OPTION -- OPTARG="-"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="toggle--"'
expect "${bash_getopts[1]}" == 'toggle triggered -- OPTARG'
expect "${bash_getopts[4]}" == 'toggle triggered -- OPTARG'
expect "${getopts_long[2]}" == 'toggle triggered -- OPTARG'
expect "${bash_getopts[2]}" == 'INVALID OPTION -- declare -- OPTARG="-"'
expect "${bash_getopts[3]}" == 'INVALID OPTION -- declare -- OPTARG="-"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="toggle--"'
expect "${bash_getopts[1]}" == 'toggle triggered -- declare -- OPTARG'
expect "${bash_getopts[4]}" == 'toggle triggered -- declare -- OPTARG'
expect "${getopts_long[2]}" == 'toggle triggered -- declare -- OPTARG'
}
@test "${FEATURE}: long toggle, verbose" {
compare '-t-- -t user_arg' \
Expand All @@ -52,12 +52,12 @@ load ../test_helper
'5{/^INVALID OPTION or MISSING ARGUMENT/d}' \
's/getopts[[:alpha:]_-]*/GETOPTS-NORMALISED/' \
's/(illegal option --) (-|toggle--)/\1 TOGGLE-NORMALISED/'
expect "${bash_getopts[1]}" == 'toggle triggered -- OPTARG'
expect "${bash_getopts[1]}" == 'toggle triggered -- declare -- OPTARG'
expect "${bash_getopts[2]}" =~ 'getopts-verbose: illegal option -- -$'
expect "${bash_getopts[4]}" =~ 'getopts-verbose: illegal option -- -$'
expect "${bash_getopts[6]}" == 'toggle triggered -- OPTARG'
expect "${bash_getopts[6]}" == 'toggle triggered -- declare -- OPTARG'
expect "${getopts_long[1]}" =~ 'getopts_long-verbose: illegal option -- toggle--$'
expect "${getopts_long[3]}" == 'toggle triggered -- OPTARG'
expect "${getopts_long[3]}" == 'toggle triggered -- declare -- OPTARG'
}

# Both implementations should see:
Expand All @@ -82,14 +82,14 @@ load ../test_helper
compare '-o-- -t user_arg' \
'--option-- --toggle user_arg' \
'1{/(option supplied|INVALID OPTION)/d}'
expect "${bash_getopts[1]}" == 'option supplied -- OPTARG="--"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="option--"'
expect "${bash_getopts[1]}" == 'option supplied -- declare -- OPTARG="--"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="option--"'
}
@test "${FEATURE}: long option, verbose" {
compare '-o-- -t user_arg' \
'--option-- --toggle user_arg' \
'1{/(option supplied|illegal option)/d}' \
'2{/^INVALID OPTION or MISSING ARGUMENT/d}'
expect "${bash_getopts[1]}" == 'option supplied -- OPTARG="--"'
expect "${bash_getopts[1]}" == 'option supplied -- declare -- OPTARG="--"'
expect "${getopts_long[1]}" =~ "getopts_long-verbose: illegal option -- option--$"
}
24 changes: 12 additions & 12 deletions test/bats/github_15b.bats
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-shortspec_with_dash'
'--toggle-- --toggle user_arg' \
'1{/^toggle triggered/d}' \
'/^INVALID OPTION/d'
expect "${bash_getopts[2]}" == 'INVALID OPTION -- OPTARG="-"'
expect "${bash_getopts[3]}" == 'INVALID OPTION -- OPTARG="-"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="toggle--"'
expect "${bash_getopts[1]}" == 'toggle triggered -- OPTARG'
expect "${bash_getopts[4]}" == 'toggle triggered -- OPTARG'
expect "${getopts_long[2]}" == 'toggle triggered -- OPTARG'
expect "${bash_getopts[2]}" == 'INVALID OPTION -- declare -- OPTARG="-"'
expect "${bash_getopts[3]}" == 'INVALID OPTION -- declare -- OPTARG="-"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="toggle--"'
expect "${bash_getopts[1]}" == 'toggle triggered -- declare -- OPTARG'
expect "${bash_getopts[4]}" == 'toggle triggered -- declare -- OPTARG'
expect "${getopts_long[2]}" == 'toggle triggered -- declare -- OPTARG'
}
@test "${FEATURE}: long toggle, verbose" {
compare '-t-- -t user_arg' \
Expand All @@ -55,12 +55,12 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-shortspec_with_dash'
'5{/^INVALID OPTION or MISSING ARGUMENT/d}' \
's/getopts[[:alpha:]_-]*/GETOPTS-NORMALISED/' \
's/(illegal option --) (-|toggle--)/\1 TOGGLE-NORMALISED/'
expect "${bash_getopts[1]}" == 'toggle triggered -- OPTARG'
expect "${bash_getopts[1]}" == 'toggle triggered -- declare -- OPTARG'
expect "${bash_getopts[2]}" =~ 'getopts-verbose: illegal option -- -$'
expect "${bash_getopts[4]}" =~ 'getopts-verbose: illegal option -- -$'
expect "${bash_getopts[6]}" == 'toggle triggered -- OPTARG'
expect "${bash_getopts[6]}" == 'toggle triggered -- declare -- OPTARG'
expect "${getopts_long[1]}" =~ 'getopts_long-\w+-verbose: illegal option -- toggle--$'
expect "${getopts_long[3]}" == 'toggle triggered -- OPTARG'
expect "${getopts_long[3]}" == 'toggle triggered -- declare -- OPTARG'
}

# Both implementations should see:
Expand All @@ -85,14 +85,14 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-shortspec_with_dash'
compare '-o-- -t user_arg' \
'--option-- --toggle user_arg' \
'1{/(option supplied|INVALID OPTION)/d}'
expect "${bash_getopts[1]}" == 'option supplied -- OPTARG="--"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="option--"'
expect "${bash_getopts[1]}" == 'option supplied -- declare -- OPTARG="--"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="option--"'
}
@test "${FEATURE}: long option, verbose" {
compare '-o-- -t user_arg' \
'--option-- --toggle user_arg' \
'1{/(option supplied|illegal option)/d}' \
'2{/^INVALID OPTION or MISSING ARGUMENT/d}'
expect "${bash_getopts[1]}" == 'option supplied -- OPTARG="--"'
expect "${bash_getopts[1]}" == 'option supplied -- declare -- OPTARG="--"'
expect "${getopts_long[1]}" =~ "getopts_long-\w+-verbose: illegal option -- option--$"
}
12 changes: 6 additions & 6 deletions test/bats/github_26a.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-longspec_with_dash'
@test "${FEATURE}: double toggle, silent" {
compare '-tt' \
'--- --toggle' \
'/^OPTIND: /d'
expect "${bash_getopts[6]}" == 'OPTIND: 2'
expect "${getopts_long[6]}" == 'OPTIND: 3'
'/^declare -i OPTIND=/d'
expect "${bash_getopts[6]}" == 'declare -i OPTIND="2"'
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
}
@test "${FEATURE}: double toggle, verbose" {
compare '-tt' \
'--- --toggle' \
'/^OPTIND: /d'
expect "${bash_getopts[6]}" == 'OPTIND: 2'
expect "${getopts_long[6]}" == 'OPTIND: 3'
'/^declare -i OPTIND=/d'
expect "${bash_getopts[6]}" == 'declare -i OPTIND="2"'
expect "${getopts_long[6]}" == 'declare -i OPTIND="3"'
}
4 changes: 2 additions & 2 deletions test/bats/github_26b.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export GETOPTS_LONG_TEST_BIN='getopts_long-longspec_with_dash_colon'
compare '-z' \
'---zz' \
'/^INVALID OPTION/d'
expect "${bash_getopts[1]}" == 'INVALID OPTION -- OPTARG="z"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="-zz"'
expect "${bash_getopts[1]}" == 'INVALID OPTION -- declare -- OPTARG="z"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="-zz"'
}
@test "${FEATURE}: option with adjacent value, verbose" {
compare '-z' \
Expand Down
32 changes: 16 additions & 16 deletions test/bats/invalid_arguments.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ load ../test_helper
compare '-i' \
'--invalid' \
'/^INVALID OPTION -- /d'
expect "${bash_getopts[1]}" == 'INVALID OPTION -- OPTARG="i"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="invalid"'
expect "${bash_getopts[1]}" == 'INVALID OPTION -- declare -- OPTARG="i"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="invalid"'
}
@test "${FEATURE}: long option, verbose" {
compare '-i' \
Expand All @@ -41,8 +41,8 @@ load ../test_helper
compare '-i user_arg' \
'--invalid user_arg' \
'/^INVALID OPTION -- /d'
expect "${bash_getopts[1]}" == 'INVALID OPTION -- OPTARG="i"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="invalid"'
expect "${bash_getopts[1]}" == 'INVALID OPTION -- declare -- OPTARG="i"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="invalid"'
}
@test "${FEATURE}: long option, extra arguments, verbose" {
compare '-i user_arg' \
Expand All @@ -55,53 +55,53 @@ load ../test_helper
@test "${FEATURE}: short option, terminator, extra arguments, silent" {
compare '-i -- user_arg' \
'-i -- user_arg'
expect "${getopts_long[6]}" == '$@: ([0]="user_arg")'
expect "${getopts_long[6]}" == 'declare -a $@=([0]="user_arg")'
}
@test "${FEATURE}: short option, terminator, extra arguments, verbose" {
compare '-i -- user_arg' \
'-i -- user_arg' \
's/getopts_long-verbose/getopts-verbose/g'
expect "${getopts_long[7]}" == '$@: ([0]="user_arg")'
expect "${getopts_long[7]}" == 'declare -a $@=([0]="user_arg")'
}

@test "${FEATURE}: long option, terminator, extra arguments, silent" {
compare '-i -- user_arg' \
'--invalid -- user_arg' \
'/^INVALID OPTION -- /d'
expect "${bash_getopts[1]}" == 'INVALID OPTION -- OPTARG="i"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG="invalid"'
expect "${getopts_long[6]}" == '$@: ([0]="user_arg")'
expect "${bash_getopts[1]}" == 'INVALID OPTION -- declare -- OPTARG="i"'
expect "${getopts_long[1]}" == 'INVALID OPTION -- declare -- OPTARG="invalid"'
expect "${getopts_long[6]}" == 'declare -a $@=([0]="user_arg")'
}
@test "${FEATURE}: long option, terminator, extra arguments, verbose" {
compare '-i -- user_arg' \
'--invalid -- user_arg' \
's/getopts_long-verbose: (.*) invalid$/getopts-verbose: \1 i/g'
expect "${getopts_long[7]}" == '$@: ([0]="user_arg")'
expect "${getopts_long[7]}" == 'declare -a $@=([0]="user_arg")'
}

# terminator followed by options

@test "${FEATURE}: terminator, short option, extra arguments, silent" {
compare '-- -i user_arg' \
'-- -i user_arg'
expect "${getopts_long[5]}" == '$@: ([0]="-i" [1]="user_arg")'
expect "${getopts_long[5]}" == 'declare -a $@=([0]="-i" [1]="user_arg")'
}
@test "${FEATURE}: terminator, short option, extra arguments, verbose" {
compare '-- -i user_arg' \
'-- -i user_arg' \
's/getopts_long-verbose/getopts-verbose/g'
expect "${getopts_long[5]}" == '$@: ([0]="-i" [1]="user_arg")'
expect "${getopts_long[5]}" == 'declare -a $@=([0]="-i" [1]="user_arg")'
}

@test "${FEATURE}: terminator, long option, extra arguments, silent" {
compare '-- -i user_arg' \
'-- --invalid user_arg' \
'/^\$@: /d'
expect "${getopts_long[5]}" == '$@: ([0]="--invalid" [1]="user_arg")'
'5{s/\[0]="(-i|--invalid)"/[0]="NORMALIZED"/}'
expect "${getopts_long[5]}" == 'declare -a $@=([0]="--invalid" [1]="user_arg")'
}
@test "${FEATURE}: terminator, long option, extra arguments, verbose" {
compare '-- -i user_arg' \
'-- --invalid user_arg' \
'/^\$@: /d'
expect "${getopts_long[5]}" == '$@: ([0]="--invalid" [1]="user_arg")'
'5{s/\[0]="(-i|--invalid)"/[0]="NORMALIZED"/}'
expect "${getopts_long[5]}" == 'declare -a $@=([0]="--invalid" [1]="user_arg")'
}
4 changes: 2 additions & 2 deletions test/bats/no_arguments.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ load ../test_helper
@test "${FEATURE}: terminator, extra arguments, silent" {
compare '-- user_arg' \
'-- user_arg'
expect "${getopts_long[5]}" == '$@: ([0]="user_arg")'
expect "${getopts_long[5]}" == 'declare -a $@=([0]="user_arg")'
}
@test "${FEATURE}: terminator, extra arguments, verbose" {
compare '-- user_arg' \
'-- user_arg'
expect "${getopts_long[5]}" == '$@: ([0]="user_arg")'
expect "${getopts_long[5]}" == 'declare -a $@=([0]="user_arg")'
}
Loading

0 comments on commit b745fcd

Please sign in to comment.