Skip to content

Commit

Permalink
Implement utils/simple_dict.sh, add bats-support and bats-assert
Browse files Browse the repository at this point in the history
  • Loading branch information
juice500ml committed Jul 22, 2023
1 parent 675342d commit bedd205
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ check_autopep8
.coverage
htmlcov
coverage.xml*
bats-core/
test_utils/bats-core/
test_utils/bats-support/
test_utils/bats-assert/
shellcheck*
check_shellcheck*
test_spm.vocab
Expand Down
2 changes: 1 addition & 1 deletion ci/test_python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

set -euo pipefail

exclude="egs2/TEMPLATE/asr1/utils,egs2/TEMPLATE/asr1/steps,egs2/TEMPLATE/tts1/sid,doc,tools,bats-core"
exclude="egs2/TEMPLATE/asr1/utils,egs2/TEMPLATE/asr1/steps,egs2/TEMPLATE/tts1/sid,doc,tools,test_utils/bats-core,test_utils/bats-support,test_utils/bats-assert"

# flake8
# "$(dirname $0)"/test_flake8.sh
Expand Down
4 changes: 3 additions & 1 deletion ci/test_shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ fi
PATH=$(pwd)/bats-core/bin:$(pwd)/shellcheck-stable:$PATH
if ! [ -x "$(command -v bats)" ]; then
echo "=== install bats ==="
git clone https://github.com/bats-core/bats-core.git
git clone https://github.com/bats-core/bats-core.git test_utils
git clone https://github.com/bats-core/bats-support.git test_utils
git clone https://github.com/bats-core/bats-assert.git test_utils
fi
if ! [ -x "$(command -v shellcheck)" ]; then
echo "=== install shellcheck ==="
Expand Down
4 changes: 3 additions & 1 deletion ci/test_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ echo "=== run integration tests at test_utils ==="
PATH=$(pwd)/bats-core/bin:$PATH
if ! [ -x "$(command -v bats)" ]; then
echo "=== install bats ==="
git clone https://github.com/bats-core/bats-core.git
git clone https://github.com/bats-core/bats-core.git test_utils
git clone https://github.com/bats-core/bats-support.git test_utils
git clone https://github.com/bats-core/bats-assert.git test_utils
fi
bats test_utils/integration_test_*.bats

Expand Down
72 changes: 72 additions & 0 deletions test_utils/test_simple_dict.bats
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)
}
63 changes: 63 additions & 0 deletions utils/simple_dict.sh
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}"
}

0 comments on commit bedd205

Please sign in to comment.