Skip to content

Commit

Permalink
Refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoholiveira committed Dec 9, 2024
1 parent 800dac4 commit 827da35
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 438 deletions.
102 changes: 35 additions & 67 deletions arrays_test.go
Original file line number Diff line number Diff line change
@@ -1,111 +1,79 @@
package jsonlogic
package jsonlogic_test

import (
"encoding/json"
"bytes"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/diegoholiveira/jsonlogic/v3"
)

func TestFilterParseTheSubjectFromFirstPosition(t *testing.T) {
var parsed interface{}

err := json.Unmarshal([]byte(`[
rule := strings.NewReader(`{"filter": [
[1,2,3,4,5],
{"%":[{"var":""},2]}
]`), &parsed)
if err != nil {
panic(err)
}

result := filter(parsed, nil)

var expected interface{}
]}`)

json.Unmarshal([]byte(`[1,3,5]`), &expected) // nolint:errcheck
var result bytes.Buffer

assert.Equal(t, expected, result)
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `[1,3,5]`, result.String())
}

func TestFilterParseTheSubjectFromNullValue(t *testing.T) {
var parsed interface{}

err := json.Unmarshal([]byte(`[
rule := strings.NewReader(`{"filter": [
null,
{"%":[{"var":""},2]}
]`), &parsed)
if err != nil {
panic(err)
}

result := filter(parsed, nil)
]}`)

var expected interface{}
var result bytes.Buffer

json.Unmarshal([]byte(`[]`), &expected) // nolint:errcheck

assert.Equal(t, expected, result)
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `[]`, result.String())
}

func TestReduceSkipNullValues(t *testing.T) {
var parsed interface{}

err := json.Unmarshal([]byte(`[
rule := strings.NewReader(`{"reduce": [
[1,2,null,4,5],
{"+":[{"var":"current"}, {"var":"accumulator"}]},
0
]`), &parsed)
if err != nil {
panic(err)
}
]}`)

result := reduce(parsed, nil)
var result bytes.Buffer

var expected interface{}

json.Unmarshal([]byte(`12`), &expected) // nolint:errcheck

assert.Equal(t, expected, result)
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `12`, result.String())
}

func TestReduceBoolValues(t *testing.T) {
var parsed interface{}

err := json.Unmarshal([]byte(`[
rule := strings.NewReader(`{"reduce": [
[true,false,true,null],
{"or":[{"var":"current"}, {"var":"accumulator"}]},
false
]`), &parsed)
if err != nil {
panic(err)
}

result := reduce(parsed, nil)
]}`)

var expected interface{}
var result bytes.Buffer

json.Unmarshal([]byte(`true`), &expected) // nolint:errcheck

assert.Equal(t, expected, result)
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `true`, result.String())
}

func TestReduceStringValues(t *testing.T) {
var parsed interface{}

err := json.Unmarshal([]byte(`[
rule := strings.NewReader(`{"reduce": [
["a",null,"b"],
{"cat":[{"var":"current"}, {"var":"accumulator"}]},
""
]`), &parsed)
if err != nil {
panic(err)
}

result := reduce(parsed, nil)

var expected interface{}
]}`)

json.Unmarshal([]byte(`"ba"`), &expected) // nolint:errcheck
var result bytes.Buffer

assert.Equal(t, expected, result)
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `"ba"`, result.String())
}
37 changes: 0 additions & 37 deletions internal/issues/0083_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions internal/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"reflect"
Expand Down Expand Up @@ -42,7 +41,7 @@ func GetScenariosFromOfficialTestSuite() Tests {
return tests
}

buffer, _ := ioutil.ReadAll(response.Body)
buffer, _ := io.ReadAll(response.Body)

Check failure on line 44 in internal/testing.go

View workflow job for this annotation

GitHub Actions / Running with Go 1.14

undefined: io.ReadAll

Check failure on line 44 in internal/testing.go

View workflow job for this annotation

GitHub Actions / Running with Go 1.15

undefined: io.ReadAll

response.Body.Close()

Expand Down
Loading

0 comments on commit 827da35

Please sign in to comment.