You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

68 lines
890 B

package math
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseFraction(t *testing.T) {
testCases := []struct {
f string
exp Fraction
err bool
}{
{
f: "2/3",
exp: Fraction{2, 3},
err: false,
},
{
f: "15/5",
exp: Fraction{15, 5},
err: false,
},
{
f: "-1/2",
exp: Fraction{-1, 2},
err: false,
},
{
f: "1/-2",
exp: Fraction{1, -2},
err: false,
},
{
f: "2/3/4",
exp: Fraction{},
err: true,
},
{
f: "123",
exp: Fraction{},
err: true,
},
{
f: "1a2/4",
exp: Fraction{},
err: true,
},
{
f: "1/3bc4",
exp: Fraction{},
err: true,
},
}
for idx, tc := range testCases {
output, err := ParseFraction(tc.f)
if tc.err {
assert.Error(t, err, idx)
} else {
assert.NoError(t, err, idx)
}
assert.Equal(t, tc.exp, output, idx)
}
}