Skip to content

Commit 1cc815a

Browse files
authored
Merge pull request #10 from TypedDevs/bashunit
Bashunit
2 parents 6653e50 + 29ae86a commit 1cc815a

File tree

9 files changed

+107
-0
lines changed

9 files changed

+107
-0
lines changed

example-bashunit/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DEFAULT_PATH=tests

example-bashunit/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib/

example-bashunit/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# bashunit
2+
3+
1. Install the library: `./install.sh`
4+
2. Run the test script: `./test.sh`

example-bashunit/install.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
####################################################
4+
# More details about bashunit installation #
5+
# see: https://bashunit.typeddevs.com/installation #
6+
####################################################
7+
curl -s https://bashunit.typeddevs.com/install.sh | bash -s lib 0.11.0
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
mode=$1
4+
path=$2
5+
6+
if [ -z $path ]; then
7+
echo "Usage: ./file-report.sh [size|number-of-files] [path]"; >&2
8+
exit 1
9+
fi
10+
11+
cd "${path}"
12+
13+
case "${mode}" in
14+
number-of-files)
15+
ls -1 | wc -l ;;
16+
size)
17+
du -h | cut -f1 ;;
18+
*)
19+
>&2 echo "Usage: ./file-report.sh [size|number-of-files] [path]"; exit 1 ;;
20+
esac
21+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
function main() {
4+
numberOfPortions=$1
5+
pricePerPortion=$(calculatePrice $numberOfPortions)
6+
totalPrice=$(( numberOfPortions * pricePerPortion ))
7+
8+
echo "Total $totalPrice"
9+
}
10+
11+
function calculatePrice() {
12+
if [[ $numberOfPortions -lt 3 ]]; then
13+
echo "100"
14+
else
15+
day=$(getDay)
16+
if (( day % 2 )); then
17+
echo "80"
18+
else
19+
echo "100"
20+
fi
21+
fi
22+
}
23+
24+
function getDay() {
25+
date -d '+%d'
26+
}
27+
28+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
29+
main "$@"
30+
fi

example-bashunit/src/myfun.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
function myfun() {
4+
echo "Hello!"
5+
}

example-bashunit/test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
lib/bashunit tests
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
# set_up is called before executing each test function
4+
function set_up() {
5+
source "$(realpath ".")/src/ice_cream_price.sh"
6+
}
7+
8+
function test_ice_cream_price_should_be_100_per_portion_for_low_quantities() {
9+
result=$(main 1)
10+
11+
assert_successful_code
12+
assert_equals "${result}" "Total 100"
13+
}
14+
15+
function test_there_should_be_20_percent_discount_for_large_quantities_on_odd_days() {
16+
function getDay() {
17+
echo "1"
18+
}
19+
20+
result=$(main 4)
21+
22+
assert_successful_code
23+
assert_equals "${result}" "Total 320"
24+
}
25+
26+
function test_there_should_be_no_discount_even_for_large_quantities_on_even_days() {
27+
function getDay() {
28+
echo "2"
29+
}
30+
31+
result=$(main 4)
32+
33+
assert_successful_code
34+
assert_equals "${result}" "Total 400"
35+
}

0 commit comments

Comments
 (0)