-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement utils/simple_dict.sh, add bats-support and bats-assert
- Loading branch information
1 parent
675342d
commit bedd205
Showing
6 changed files
with
145 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
setup() { | ||
load 'bats-support/load' | ||
load 'bats-assert/load' | ||
|
||
cd $BATS_TEST_DIRNAME/../egs2/mls/asr2 | ||
utils=$(cd $BATS_TEST_DIRNAME/..; pwd)/utils | ||
echo $utils | ||
} | ||
|
||
@test "simple_dict.sh: can import" { | ||
source $utils/simple_dict.sh | ||
} | ||
|
||
@test "simple_dict.sh: check various dict_init edge cases" { | ||
run bash -c "source $utils/simple_dict.sh; dict_init good_argument" | ||
assert_success | ||
|
||
run bash -c "source $utils/simple_dict.sh; dict_init bad number_of_arguments" | ||
assert_failure | ||
|
||
run bash -c "source $utils/simple_dict.sh; dict_init bad/argument" | ||
assert_failure | ||
|
||
run bash -c "source $utils/simple_dict.sh; dict_init duplicate; dict_init duplicate" | ||
assert_failure | ||
|
||
run bash -c "source $utils/simple_dict.sh; dict_init duplicate; dict_init not_duplicate" | ||
assert_success | ||
} | ||
|
||
@test "simple_dict.sh: key existence test" { | ||
run bash -c "source $utils/simple_dict.sh; dict_get d b" | ||
assert_failure | ||
run bash -c "source $utils/simple_dict.sh; dict_remove d b" | ||
assert_failure | ||
|
||
run bash -c "source $utils/simple_dict.sh; dict_init d; dict_get d b" | ||
assert_failure | ||
run bash -c "source $utils/simple_dict.sh; dict_init d; dict_remove d b" | ||
assert_failure | ||
|
||
run bash -c "source $utils/simple_dict.sh; dict_init d; dict_put d a 1; dict_get d b" | ||
assert_failure | ||
run bash -c "source $utils/simple_dict.sh; dict_init d; dict_put d a 1; dict_remove d b" | ||
assert_failure | ||
|
||
run bash -c "source $utils/simple_dict.sh; dict_init d; dict_put d a 1; dict_get d a" | ||
assert_success | ||
run bash -c "source $utils/simple_dict.sh; dict_init d; dict_put d a 1; dict_remove d a" | ||
assert_success | ||
run bash -c "source $utils/simple_dict.sh; dict_init d; dict_put d a 1; dict_remove d a; dict_get d a" | ||
assert_failure | ||
} | ||
|
||
@test "simple_dict.sh: key read, write, and delete" { | ||
source $utils/simple_dict.sh | ||
dict_init d | ||
|
||
dict_put d a one | ||
assert_equal 'one' $(dict_get d a) | ||
|
||
dict_put d b 2 | ||
assert_equal 'one' $(dict_get d a) | ||
assert_equal '2' $(dict_get d b) | ||
|
||
dict_put d a 1 | ||
assert_equal '1' $(dict_get d a) | ||
assert_equal '2' $(dict_get d b) | ||
|
||
dict_remove d a | ||
assert_equal '2' $(dict_get d b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env bash | ||
|
||
dict_init() { | ||
# Parameter safety | ||
if [ "$#" != 1 ]; then | ||
echo "Bad number of arguments." | ||
exit 1 | ||
fi | ||
|
||
if [[ "$1" == *\/* ]]; then | ||
echo "Bad dictionary name." | ||
echo "Name: $1" | ||
exit 1 | ||
fi | ||
|
||
# Initialize temporary directory in /tmp | ||
if [ -z ${simple_storage+x} ]; then | ||
simple_storage=$(mktemp -d) | ||
fi | ||
|
||
dict_name=$1 | ||
mkdir "${simple_storage}/${dict_name}" | ||
} | ||
|
||
dict_put() { | ||
# Parameter safety | ||
if [ "$#" != 3 ]; then | ||
echo "Bad number of arguments." | ||
exit 1 | ||
fi | ||
|
||
dict_name=$1 | ||
key=$2 | ||
value=$3 | ||
|
||
echo ${value} > "${simple_storage}/${dict_name}/${key}" | ||
} | ||
|
||
dict_get() { | ||
# Parameter safety | ||
if [ "$#" != 2 ]; then | ||
echo "Bad number of arguments." | ||
exit 1 | ||
fi | ||
|
||
dict_name=$1 | ||
key=$2 | ||
|
||
cat "${simple_storage}/${dict_name}/${key}" | ||
} | ||
|
||
dict_remove() { | ||
# Parameter safety | ||
if [ "$#" != 2 ]; then | ||
echo "Bad number of arguments." | ||
exit 1 | ||
fi | ||
|
||
dict_name=$1 | ||
key=$2 | ||
|
||
rm "${simple_storage}/${dict_name}/${key}" | ||
} |