Skip to content

Commit

Permalink
#12 add some tests to expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
thegodenage committed Mar 17, 2024
1 parent 4825ffd commit 196198b
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ inpackage: True
packages:
waffle/internal/rule:
interfaces:
Builder:
Builder:
node:
174 changes: 174 additions & 0 deletions internal/rule/expression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package rule

import (
"testing"

"github.com/stretchr/testify/mock"

"waffle/internal/request"
)

func Test_and_Eval(t *testing.T) {
type fields struct {
leftFunc func() node
rightFunc func() node
}
type args struct {
r request.Wrapper
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "both nodes true, true returned",
fields: fields{
leftFunc: func() node {
n := NewMocknode(t)

n.EXPECT().Eval(mock.Anything).Return(true)

return n
},
rightFunc: func() node {
n := NewMocknode(t)

n.EXPECT().Eval(mock.Anything).Return(true)

return n
},
},
want: true,
},
{
name: "one of nodes false, false returned",
fields: fields{
leftFunc: func() node {
n := NewMocknode(t)

n.EXPECT().Eval(mock.Anything).Return(true)

return n
},
rightFunc: func() node {
n := NewMocknode(t)

n.EXPECT().Eval(mock.Anything).Return(false)

return n
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var left, right node
if tt.fields.leftFunc != nil {
left = tt.fields.leftFunc()
}
if tt.fields.rightFunc != nil {
right = tt.fields.rightFunc()
}

a := and{
left: left,
right: right,
}
if got := a.Eval(tt.args.r); got != tt.want {
t.Errorf("Eval() = %v, want %v", got, tt.want)
}
})
}
}

func Test_or_Eval(t *testing.T) {
type fields struct {
leftFunc func() node
rightFunc func() node
}
type args struct {
r request.Wrapper
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "first node true, returns true",
fields: fields{
leftFunc: func() node {
n := NewMocknode(t)

n.EXPECT().Eval(mock.Anything).Return(true)

return n
},
},
want: true,
},
{
name: "both nodes false, returns false",
fields: fields{
leftFunc: func() node {
n := NewMocknode(t)

n.EXPECT().Eval(mock.Anything).Return(false)

return n
},
rightFunc: func() node {
n := NewMocknode(t)

n.EXPECT().Eval(mock.Anything).Return(false)

return n
},
},
want: false,
},
{
name: "first node false, second true, returns true",
fields: fields{
leftFunc: func() node {
n := NewMocknode(t)

n.EXPECT().Eval(mock.Anything).Return(false)

return n
},
rightFunc: func() node {
n := NewMocknode(t)

n.EXPECT().Eval(mock.Anything).Return(true)

return n
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var left, right node
if tt.fields.leftFunc != nil {
left = tt.fields.leftFunc()
}
if tt.fields.rightFunc != nil {
right = tt.fields.rightFunc()
}

o := or{
left: left,
right: right,
}
if got := o.Eval(tt.args.r); got != tt.want {
t.Errorf("Eval() = %v, want %v", got, tt.want)
}
})
}
}
82 changes: 82 additions & 0 deletions internal/rule/mock_node.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 19 additions & 21 deletions internal/rule/predicate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package rule

import (
"fmt"
"io"
"waffle/internal/request"
)

Expand Down Expand Up @@ -56,24 +54,24 @@ type predicateBuilder struct {
var _ Builder = (*predicateBuilder)(nil)

func (p *predicateBuilder) Build(name, variable, expression string) (*Predicate, error) {
tree := and{
left: gt{
valueFunc: func(r request.Wrapper) (int, error) {
bytes, err := io.ReadAll(r.Request.Body)
if err != nil {
return 0, fmt.Errorf("cannot read bytes: %w", err)
}

return len(bytes), nil
},
check: 0,
},
right: gt{
valueFunc: func(r request.Wrapper) (int, error) {
return len(r.Request.Header), nil
},
check: 0,
},
}
//tree := and{
// left: gt{
// valueFunc: func(r request.Wrapper) (int, error) {
// bytes, err := io.ReadAll(r.Request.Body)
// if err != nil {
// return 0, fmt.Errorf("cannot read bytes: %w", err)
// }
//
// return len(bytes), nil
// },
// check: 0,
// },
// right: gt{
// valueFunc: func(r request.Wrapper) (int, error) {
// return len(r.Request.Header), nil
// },
// check: 0,
// },
//}
return nil, nil
}

0 comments on commit 196198b

Please sign in to comment.