Skip to content

Commit

Permalink
Add a test for setting evaluated fields
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Oct 1, 2024
1 parent ff73190 commit 174ec83
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
24 changes: 24 additions & 0 deletions dencoding/map.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dencoding

import "reflect"

// NewMap returns a new *Map that has its values initialised.
func NewMap() *Map {
keys := make([]string, 0)
Expand Down Expand Up @@ -29,6 +31,28 @@ type Map struct {
data map[string]any
}

func (m *Map) Len() int {
return len(m.keys)
}

func (m *Map) Equal(other *Map) bool {
if m.Len() != other.Len() {
return false
}

for i, k := range m.keys {
if k != other.keys[i] {
return false
}

if !reflect.DeepEqual(m.data[k], other.data[k]) {
return false
}
}

return true
}

// Get returns the value found under the given key.
func (m *Map) Get(key string) (any, bool) {
v, ok := m.data[key]
Expand Down
44 changes: 22 additions & 22 deletions execution/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func TestExecuteSelector_HappyPath(t *testing.T) {
if tc.inFn != nil {
in = tc.inFn()
}
if in == nil {
in = model.NewValue(nil)
}
exp := tc.out
if tc.outFn != nil {
exp = tc.outFn()
Expand All @@ -48,7 +51,9 @@ func TestExecuteSelector_HappyPath(t *testing.T) {
}
expV, gotV := toInterface(exp), toInterface(res)

if !cmp.Equal(expV, gotV, cmpopts.IgnoreUnexported(dencoding.Map{})) {
if !cmp.Equal(expV, gotV,
cmpopts.IgnoreUnexported(dencoding.Map{}),
) {
t.Errorf("unexpected result: %v", cmp.Diff(expV, gotV))
}
}
Expand All @@ -58,70 +63,57 @@ func TestExecuteSelector_HappyPath(t *testing.T) {
t.Run("math", func(t *testing.T) {
t.Run("literals", func(t *testing.T) {
t.Run("addition", runTest(testCase{
in: model.NewValue(nil),
s: `1 + 2`,
out: model.NewIntValue(3),
}))
t.Run("subtraction", runTest(testCase{
in: model.NewValue(nil),
s: `5 - 2`,
out: model.NewIntValue(3),
}))
t.Run("multiplication", runTest(testCase{
in: model.NewValue(nil),
s: `5 * 2`,
out: model.NewIntValue(10),
}))
t.Run("division", runTest(testCase{
in: model.NewValue(nil),
s: `10 / 2`,
out: model.NewIntValue(5),
}))
t.Run("modulus", runTest(testCase{
in: model.NewValue(nil),
s: `10 % 3`,
out: model.NewIntValue(1),
}))
t.Run("ordering", runTest(testCase{
in: model.NewValue(nil),
s: `45.2 + 5 * 4 - 2 / 2`, // 45.2 + (5 * 4) - (2 / 2) = 45.2 + 20 - 1 = 64.2
out: model.NewFloatValue(64.2),
}))
t.Run("ordering with groups", runTest(testCase{
in: model.NewValue(nil),
s: `(45.2 + 5) * ((4 - 2) / 2)`, // (45.2 + 5) * ((4 - 2) / 2) = (50.2) * ((2) / 2) = (50.2) * (1) = 50.2
out: model.NewFloatValue(50.2),
}))
})
})
t.Run("comparison", func(t *testing.T) {
t.Run("equal", runTest(testCase{
in: model.NewValue(nil),
s: `1 == 1`,
out: model.NewBoolValue(true),
}))
t.Run("not equal", runTest(testCase{
in: model.NewValue(nil),
s: `1 != 1`,
out: model.NewBoolValue(false),
}))
t.Run("greater than", runTest(testCase{
in: model.NewValue(nil),
s: `2 > 1`,
out: model.NewBoolValue(true),
}))
t.Run("greater than or equal", runTest(testCase{
in: model.NewValue(nil),
s: `2 >= 2`,
out: model.NewBoolValue(true),
}))
t.Run("less than", runTest(testCase{
in: model.NewValue(nil),
s: `1 < 2`,
out: model.NewBoolValue(true),
}))
t.Run("less than or equal", runTest(testCase{
in: model.NewValue(nil),
s: `2 <= 2`,
out: model.NewBoolValue(true),
}))
Expand All @@ -130,27 +122,22 @@ func TestExecuteSelector_HappyPath(t *testing.T) {

t.Run("literal", func(t *testing.T) {
t.Run("string", runTest(testCase{
in: model.NewValue(nil),
s: `"hello"`,
out: model.NewStringValue("hello"),
}))
t.Run("int", runTest(testCase{
in: model.NewValue(nil),
s: `123`,
out: model.NewIntValue(123),
}))
t.Run("float", runTest(testCase{
in: model.NewValue(nil),
s: `123.4`,
out: model.NewFloatValue(123.4),
}))
t.Run("true", runTest(testCase{
in: model.NewValue(nil),
s: `true`,
out: model.NewBoolValue(true),
}))
t.Run("false", runTest(testCase{
in: model.NewValue(nil),
s: `false`,
out: model.NewBoolValue(false),
}))
Expand All @@ -159,17 +146,14 @@ func TestExecuteSelector_HappyPath(t *testing.T) {
t.Run("function", func(t *testing.T) {
t.Run("add", func(t *testing.T) {
t.Run("int", runTest(testCase{
in: model.NewValue(nil),
s: `add(1, 2, 3)`,
out: model.NewIntValue(6),
}))
t.Run("float", runTest(testCase{
in: model.NewValue(nil),
s: `add(1f, 2.5, 3.5)`,
out: model.NewFloatValue(7),
}))
t.Run("mixed", runTest(testCase{
in: model.NewValue(nil),
s: `add(1, 2f)`,
out: model.NewFloatValue(3),
}))
Expand All @@ -181,6 +165,7 @@ func TestExecuteSelector_HappyPath(t *testing.T) {
return model.NewValue(
dencoding.NewMap().
Set("title", "Mr").
Set("age", int64(31)).
Set("name", dencoding.NewMap().
Set("first", "Tom").
Set("last", "Wright")),
Expand All @@ -201,6 +186,21 @@ func TestExecuteSelector_HappyPath(t *testing.T) {
s: `title + " " + (name.first) + " " + (name.last)`,
out: model.NewStringValue("Mr Tom Wright"),
}))
t.Run("add evaluated fields", runTest(testCase{
in: inputMap(),
s: `{..., over30 = age > 30}`,
outFn: func() *model.Value {
return model.NewValue(
dencoding.NewMap().
Set("title", "Mr").
Set("age", int64(31)).
Set("name", dencoding.NewMap().
Set("first", "Tom").
Set("last", "Wright")).
Set("over30", true),
)
},
}))
})

t.Run("object", func(t *testing.T) {
Expand Down

0 comments on commit 174ec83

Please sign in to comment.