From 1cdccdd2a56398b2f19f620eb4f6373e8e72a33f Mon Sep 17 00:00:00 2001 From: adnan360 Date: Mon, 29 Mar 2021 23:04:37 +0600 Subject: [PATCH] Add tests Add tests based on bats, a TAP-compliant testing framework for bash --- README.md | 10 ++++++++++ tests/common.bash | 3 +++ tests/pemdas.bats | 43 +++++++++++++++++++++++++++++++++++++++++++ tests/run.sh | 4 ++++ tests/simple.bats | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100755 tests/common.bash create mode 100755 tests/pemdas.bats create mode 100755 tests/run.sh create mode 100755 tests/simple.bats 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." ] +}