Skip to content

Commit

Permalink
Implements the modulos
Browse files Browse the repository at this point in the history
  • Loading branch information
jamillosantos committed Dec 7, 2017
1 parent a5ef363 commit b068ac0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
11 changes: 11 additions & 0 deletions expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func (e *ExpressionMultiple) Solve(ctx Context) (interface{}, error) {
return nil, err
}
switch rr := rTemp.(type) {
case int:
r = float64(rr)
case float64:
r = rr
}
Expand Down Expand Up @@ -159,6 +161,15 @@ func (e *ExpressionMultiplePart) Solve(ctx Context) (interface{}, error) {
default:
return nil, NewWrongTypeError(v)
}
case "%":
switch vv := v.(type) {
case int:
return int(accumulated+0.5) % vv, nil
case float64:
return int(accumulated+0.5) % int(vv+0.5), nil
default:
return nil, NewWrongTypeError(v)
}
case "^":
switch vv := v.(type) {
case int:
Expand Down
48 changes: 45 additions & 3 deletions expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestExpressions(t *testing.T) {

g.It("should solve an expression", func() {
v := expressions.NewExpressionField("field")
resolver := expressions.NewMapResolver(map[string]float64{
resolver := expressions.NewMapResolver(map[string]interface{}{
"field": 1.2345,
})
ctx := expressions.NewContext(resolver, nil)
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestExpressions(t *testing.T) {
Expect(err).NotTo(BeNil())
})

g.It("should fail due to invalid oeprator", func() {
g.It("should fail due to invalid operator", func() {
expr := expressions.NewExpressionMultiplePart("invalid operator", expressions.NewExpressionValue(4))
Expect(expr).NotTo(BeNil())
_, err := expr.Solve(expressions.NewContext(nil, nil))
Expand Down Expand Up @@ -361,7 +361,49 @@ func TestExpressions(t *testing.T) {
})
})

g.Describe("Power", func() {
g.Describe("Module", func() {
g.It("should solve an expression with integer params", func() {
expr := expressions.NewExpressionMultiplePart("%", expressions.NewExpressionValue(2))
Expect(expr).NotTo(BeNil())
ctx := expressions.NewContext(nil, nil)
ctx.SetAccumulated(5)
v, err := expr.Solve(ctx)
Expect(err).To(BeNil())
Expect(v).To(Equal(1))
})

g.It("should solve an expression with float params", func() {
expr := expressions.NewExpressionMultiplePart("%", expressions.NewExpressionValue(float64(3)))
Expect(expr).NotTo(BeNil())
ctx := expressions.NewContext(nil, nil)
ctx.SetAccumulated(5)
v, err := expr.Solve(ctx)
Expect(err).To(BeNil())
Expect(v).To(Equal(2))
})

g.It("should fail with injected expression solving failure", func() {
expr := expressions.NewExpressionMultiplePart("%+", &ExpressionFail{})
Expect(expr).NotTo(BeNil())
ctx := expressions.NewContext(nil, nil)
ctx.SetAccumulated(3.5)
_, err := expr.Solve(ctx)
Expect(err).NotTo(BeNil())
Expect(fmt.Sprint(err)).To(Equal("failed"))
})

g.It("should fail with wrong data type", func() {
expr := expressions.NewExpressionMultiplePart("%", expressions.NewExpressionValue("wrong data type"))
Expect(expr).NotTo(BeNil())
ctx := expressions.NewContext(nil, nil)
ctx.SetAccumulated(3.5)
_, err := expr.Solve(ctx)
Expect(err).NotTo(BeNil())
Expect(fmt.Sprint(err)).To(ContainSubstring("not a valid type"))
})
})

g.Describe("Exponentiation", func() {
g.It("should solve an expression with integer params", func() {
expr := expressions.NewExpressionMultiplePart("^", expressions.NewExpressionValue(2))
Expect(expr).NotTo(BeNil())
Expand Down
24 changes: 18 additions & 6 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
. "github.com/franela/goblin"
. "github.com/onsi/gomega"
"math"
"time"
)

func TestCompile(t *testing.T) {
Expand Down Expand Up @@ -99,7 +100,7 @@ func TestCompile(t *testing.T) {
Expect(v).To(Equal(float64(11)))
})

g.It("should resolve a pow", func() {
g.It("should resolve a exponentiation", func() {
expr, err := expressions.Compile("2^3")
Expect(err).To(BeNil())
Expect(expr).NotTo(BeNil())
Expand All @@ -108,17 +109,28 @@ func TestCompile(t *testing.T) {
Expect(v).To(Equal(float64(8)))
})

g.It("should resolve a pow (with brackets)", func() {
expr, err := expressions.Compile("4^(2^3)")
g.It("should resolve a modulus", func() {
expr, err := expressions.Compile("2%3")
Expect(err).To(BeNil())
Expect(expr).NotTo(BeNil())
v, err := expr.Solve(expressions.NewContext(nil, nil))
Expect(err).To(BeNil())
Expect(v).To(Equal(float64(65536)))
Expect(v).To(Equal(float64(2)))
})

g.It("should resolve a modulus (with brackets)", func() {
g.Timeout(time.Hour)

expr, err := expressions.Compile("4%(6-3)")
Expect(err).To(BeNil())
Expect(expr).NotTo(BeNil())
v, err := expr.Solve(expressions.NewContext(nil, nil))
Expect(err).To(BeNil())
Expect(v).To(Equal(float64(1)))
})

g.It("should resolve a equation with variables (without brackets)", func() {
resolver := expressions.NewMapResolver(map[string]float64{
resolver := expressions.NewMapResolver(map[string]interface{}{
"x": 4.5,
"y": 2,
})
Expand All @@ -133,7 +145,7 @@ func TestCompile(t *testing.T) {
})

g.It("should resolve a equation with variables (with brackets)", func() {
resolver := expressions.NewMapResolver(map[string]float64{
resolver := expressions.NewMapResolver(map[string]interface{}{
"x": 4.5,
"y": 2,
})
Expand Down
4 changes: 2 additions & 2 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ type Resolver interface {
}

type MapResolver struct {
m map[string]float64
m map[string]interface{}
}

func NewMapResolver(m map[string]float64) *MapResolver {
func NewMapResolver(m map[string]interface{}) *MapResolver {
return &MapResolver{
m: m,
}
Expand Down
2 changes: 1 addition & 1 deletion resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMapResolver(t *testing.T) {
g.Describe("Resolvers", func() {
g.Describe("MapResolver", func() {
g.It("should resolve two variables", func() {
resolver := expressions.NewMapResolver(map[string]float64{
resolver := expressions.NewMapResolver(map[string]interface{}{
"t": float64(1),
"c": float64(2.34),
})
Expand Down

0 comments on commit b068ac0

Please sign in to comment.