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

Add tests #11

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)~~
Expand Down
3 changes: 3 additions & 0 deletions tests/common.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
REPO_ROOT_PATH="$PWD"
BINARY_FILE="$PWD/bcalc"

43 changes: 43 additions & 0 deletions tests/pemdas.bats
Original file line number Diff line number Diff line change
@@ -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 ]
}
4 changes: 4 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

REPO_ROOT="$(dirname $(dirname $(realpath $0)))"
bats --tap "$REPO_ROOT/tests"
40 changes: 40 additions & 0 deletions tests/simple.bats
Original file line number Diff line number Diff line change
@@ -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." ]
}