goparsify/calc/calc_test.go
2017-08-08 23:11:47 +10:00

55 lines
1.1 KiB
Go

package calc
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestNumbers(t *testing.T) {
result, err := Calc(`1`)
require.NoError(t, err)
require.EqualValues(t, 1, result)
}
func TestAddition(t *testing.T) {
result, err := Calc(`1+1`)
require.NoError(t, err)
require.EqualValues(t, 2, result)
}
func TestSubtraction(t *testing.T) {
result, err := Calc(`1-1`)
require.NoError(t, err)
require.EqualValues(t, 0, result)
}
func TestDivision(t *testing.T) {
result, err := Calc(`1/2`)
require.NoError(t, err)
require.EqualValues(t, .5, result)
}
func TestMultiplication(t *testing.T) {
result, err := Calc(`1*2`)
require.NoError(t, err)
require.EqualValues(t, 2, result)
}
func TestOrderOfOperations(t *testing.T) {
result, err := Calc(`1+10*2`)
require.NoError(t, err)
require.EqualValues(t, 21, result)
}
func TestParenthesis(t *testing.T) {
result, err := Calc(`(1+10)*2`)
require.NoError(t, err)
require.EqualValues(t, 22, result)
}
func TestRecursive(t *testing.T) {
result, err := Calc(`(1+(2*(3-(4/(5)))))`)
require.NoError(t, err)
require.EqualValues(t, 5.4, result)
}