Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce some bats tests #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/stub.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bats

# this is the "code under test"
# it would normally be in another file
format_date() {
date -r 222
}

setup() {
load '../load'

_DATE_ARGS='-r 222'
stub date \
"${_DATE_ARGS} : echo 'I am stubbed!'" \
"${_DATE_ARGS} : echo 'Wed Dec 31 18:03:42 CST 1969'"
}

teardown() {
unstub date
}

@test "date format util formats date with expected arguments" {
result="$(format_date)"
[ "$result" == 'I am stubbed!' ]

result="$(format_date)"
[ "$result" == 'Wed Dec 31 18:03:42 CST 1969' ]
}
37 changes: 37 additions & 0 deletions test/stub_fct.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bats

# this is the "code under test"
# it would normally be in another file
format_date() {
date -r 222
}

# this is a function used in stub return evaluation
function my_test_fct()
{
echo "This is my final stub return, receiving arguments: $*"
}
# it must be exported in the script to be accessible to its childs
export -f my_test_fct

setup() {
# Load library
load '../load'

_DATE_ARGS='-r 222'
stub date \
"${_DATE_ARGS} : echo 'I am stubbed!'" \
"${_DATE_ARGS} : my_test_fct \$*"
}

teardown() {
unstub date
}

@test "date format util formats date with expected arguments" {
result="$(format_date)"
[ "$result" == 'I am stubbed!' ]

result="$(format_date)"
[ "$result" == "This is my final stub return, receiving arguments: -r 222" ]
}