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