diff --git a/README.md b/README.md index 2457b20..0dfc9dd 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,16 @@ calculation = 100-50 - `operation_array` = an array of all the operations - `calculation` = the calculation to be used +## Tests + +Make sure you have [`bats`](https://github.com/bats-core/bats-core) installed from system repo or otherwise. Then run: + +```sh +tests/run.sh +# or +path/to/bats --tap tests +``` + ## TODO - ~~Better formatted ouput (perhaps `1,000,000` instead of `1000000` for example)~~ diff --git a/tests/common.bash b/tests/common.bash new file mode 100755 index 0000000..9c3b1f3 --- /dev/null +++ b/tests/common.bash @@ -0,0 +1,3 @@ +REPO_ROOT_PATH="$PWD" +BINARY_FILE="$PWD/bcalc" + diff --git a/tests/pemdas.bats b/tests/pemdas.bats new file mode 100755 index 0000000..4eb51e3 --- /dev/null +++ b/tests/pemdas.bats @@ -0,0 +1,43 @@ +#!/usr/bin/env bats + +load common + +@test "Single brackets test" { + result="$($BINARY_FILE '(2+2)')" + [ "$result" -eq 4 ] +} + +@test "Single brackets with basic math test" { + result="$($BINARY_FILE '(5+5)/2')" + [ "$result" -eq 5 ] +} + +@test "Exponent in bracket with math test" { + result="$($BINARY_FILE '(2^2)*2')" + [ "$result" -eq 8 ] +} + +@test "2 levels of brackets test" { + result="$($BINARY_FILE '((2+2)+2)')" + [ "$result" -eq 6 ] +} + +@test "3 levels of brackets test" { + result="$($BINARY_FILE '(((2+2)+2)+2)')" + [ "$result" -eq 8 ] +} + +@test "Multiple digits without bracket first test" { + result="$($BINARY_FILE '1+2*3')" + [ "$result" -eq 7 ] +} + +@test "Multiple digits without bracket second test" { + result="$($BINARY_FILE '6-1*0+3/3')" + [ "$result" -eq 7 ] +} + +@test "Multiple digits with bracket and operations test" { + result="$($BINARY_FILE '500-(4*2^2+12)')" + [ "$result" -eq 472 ] +} diff --git a/tests/run.sh b/tests/run.sh new file mode 100755 index 0000000..79369b2 --- /dev/null +++ b/tests/run.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +REPO_ROOT="$(dirname $(dirname $(realpath $0)))" +bats --tap "$REPO_ROOT/tests" diff --git a/tests/simple.bats b/tests/simple.bats new file mode 100755 index 0000000..c3a68b4 --- /dev/null +++ b/tests/simple.bats @@ -0,0 +1,40 @@ +#!/usr/bin/env bats + +load common + +@test "Addition test" { + result="$($BINARY_FILE 2+2)" + [ "$result" -eq 4 ] +} + +@test "Subtraction test" { + result="$($BINARY_FILE 4-2)" + [ "$result" -eq 2 ] +} + +@test "Multiplication test" { + result="$($BINARY_FILE 4*2)" + [ "$result" -eq 8 ] +} + +@test "Division test" { + result="$($BINARY_FILE 4/2)" + [ "$result" -eq 2 ] +} + +@test "Division with remainder test" { + result="$($BINARY_FILE 5%2)" + [ "$result" -eq 1 ] +} + +@test "Exponent test" { + result="$($BINARY_FILE 3^2)" + [ "$result" -eq 9 ] +} + +@test "Decimal not supported error test" { + run "$BINARY_FILE" 1.1+2.2 + [ $status -eq 1 ] + [ "${lines[0]}" == "Only digits, parenthesis, '^', '*', '%', '/', '+', and '-', are supported." ] + [ "${lines[1]}" == "'.' is currently unsupported." ] +}