2020-04-15 01:20:00 +02:00
|
|
|
package jsonpath
|
|
|
|
|
2021-06-06 00:41:42 +02:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
2020-04-15 01:20:00 +02:00
|
|
|
|
|
|
|
func TestEval(t *testing.T) {
|
2021-06-06 00:41:42 +02:00
|
|
|
type Scenario struct {
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
Data string
|
|
|
|
ExpectedOutput string
|
|
|
|
ExpectedOutputLength int
|
|
|
|
ExpectedError bool
|
|
|
|
}
|
|
|
|
scenarios := []Scenario{
|
|
|
|
{
|
|
|
|
Name: "simple",
|
|
|
|
Path: "key",
|
|
|
|
Data: `{"key": "value"}`,
|
|
|
|
ExpectedOutput: "value",
|
|
|
|
ExpectedOutputLength: 5,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "simple-with-invalid-data",
|
|
|
|
Path: "key",
|
|
|
|
Data: "invalid data",
|
|
|
|
ExpectedOutput: "",
|
|
|
|
ExpectedOutputLength: 0,
|
|
|
|
ExpectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "invalid-path",
|
|
|
|
Path: "key",
|
|
|
|
Data: `{}`,
|
|
|
|
ExpectedOutput: "",
|
|
|
|
ExpectedOutputLength: 0,
|
|
|
|
ExpectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "long-simple-walk",
|
|
|
|
Path: "long.simple.walk",
|
|
|
|
Data: `{"long": {"simple": {"walk": "value"}}}`,
|
|
|
|
ExpectedOutput: "value",
|
|
|
|
ExpectedOutputLength: 5,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2022-11-19 23:25:40 +01:00
|
|
|
Name: "array-of-objects",
|
2021-06-06 00:41:42 +02:00
|
|
|
Path: "ids[1].id",
|
|
|
|
Data: `{"ids": [{"id": 1}, {"id": 2}]}`,
|
|
|
|
ExpectedOutput: "2",
|
|
|
|
ExpectedOutputLength: 1,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "array-of-values",
|
|
|
|
Path: "ids[0]",
|
|
|
|
Data: `{"ids": [1, 2]}`,
|
|
|
|
ExpectedOutput: "1",
|
|
|
|
ExpectedOutputLength: 1,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
2022-11-04 01:46:35 +01:00
|
|
|
{
|
|
|
|
Name: "array-of-values-with-no-path",
|
|
|
|
Path: "",
|
|
|
|
Data: `[1, 2]`,
|
|
|
|
ExpectedOutput: "[1 2]", // the output is an array
|
|
|
|
ExpectedOutputLength: 2,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
2021-06-06 00:41:42 +02:00
|
|
|
{
|
|
|
|
Name: "array-of-values-and-invalid-index",
|
|
|
|
Path: "ids[wat]",
|
|
|
|
Data: `{"ids": [1, 2]}`,
|
|
|
|
ExpectedOutput: "",
|
|
|
|
ExpectedOutputLength: 0,
|
|
|
|
ExpectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "array-of-values-at-root",
|
|
|
|
Path: "[1]",
|
|
|
|
Data: `[1, 2]`,
|
|
|
|
ExpectedOutput: "2",
|
|
|
|
ExpectedOutputLength: 1,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2022-11-19 23:25:40 +01:00
|
|
|
Name: "array-of-objects-at-root",
|
|
|
|
Path: "[0]",
|
|
|
|
Data: `[{"id": 1}, {"id": 2}]`,
|
|
|
|
ExpectedOutput: `{"id":1}`,
|
|
|
|
ExpectedOutputLength: 8,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "array-of-objects-with-int-at-root",
|
2021-06-06 00:41:42 +02:00
|
|
|
Path: "[0].id",
|
|
|
|
Data: `[{"id": 1}, {"id": 2}]`,
|
|
|
|
ExpectedOutput: "1",
|
|
|
|
ExpectedOutputLength: 1,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2022-11-19 23:25:40 +01:00
|
|
|
Name: "array-of-objects-at-root-and-invalid-index",
|
2021-06-06 00:41:42 +02:00
|
|
|
Path: "[5].id",
|
|
|
|
Data: `[{"id": 1}, {"id": 2}]`,
|
|
|
|
ExpectedOutput: "",
|
|
|
|
ExpectedOutputLength: 0,
|
|
|
|
ExpectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "long-walk-and-array",
|
|
|
|
Path: "data.ids[0].id",
|
|
|
|
Data: `{"data": {"ids": [{"id": 1}, {"id": 2}, {"id": 3}]}}`,
|
|
|
|
ExpectedOutput: "1",
|
|
|
|
ExpectedOutputLength: 1,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "nested-array",
|
|
|
|
Path: "[3][2]",
|
|
|
|
Data: `[[1, 2], [3, 4], [], [5, 6, 7]]`,
|
|
|
|
ExpectedOutput: "7",
|
|
|
|
ExpectedOutputLength: 1,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2022-11-19 23:25:40 +01:00
|
|
|
Name: "object-with-nested-arrays",
|
2021-06-06 00:41:42 +02:00
|
|
|
Path: "data[1][1]",
|
|
|
|
Data: `{"data": [["a", "b", "c"], ["d", "eeeee", "f"]]}`,
|
|
|
|
ExpectedOutput: "eeeee",
|
|
|
|
ExpectedOutputLength: 5,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
2022-10-19 21:52:20 +02:00
|
|
|
{
|
2022-11-19 23:25:40 +01:00
|
|
|
Name: "object-with-arrays-of-objects",
|
2022-10-19 21:52:20 +02:00
|
|
|
Path: "data[0].apps[1].name",
|
|
|
|
Data: `{"data": [{"apps": [{"name":"app1"}, {"name":"app2"}, {"name":"app3"}]}]}`,
|
|
|
|
ExpectedOutput: "app2",
|
|
|
|
ExpectedOutputLength: 4,
|
|
|
|
ExpectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2022-11-19 23:25:40 +01:00
|
|
|
Name: "object-with-arrays-of-objects-with-missing-element",
|
2022-10-19 21:52:20 +02:00
|
|
|
Path: "data[0].apps[1].name",
|
|
|
|
Data: `{"data": [{"apps": []}]}`,
|
|
|
|
ExpectedOutput: "",
|
|
|
|
ExpectedOutputLength: 0,
|
|
|
|
ExpectedError: true,
|
|
|
|
},
|
2021-06-06 00:41:42 +02:00
|
|
|
{
|
2021-06-06 00:42:32 +02:00
|
|
|
Name: "partially-invalid-path-issue122",
|
2021-06-06 00:41:42 +02:00
|
|
|
Path: "data.name.invalid",
|
|
|
|
Data: `{"data": {"name": "john"}}`,
|
|
|
|
ExpectedOutput: "",
|
|
|
|
ExpectedOutputLength: 0,
|
|
|
|
ExpectedError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run(scenario.Name, func(t *testing.T) {
|
|
|
|
output, outputLength, err := Eval(scenario.Path, []byte(scenario.Data))
|
|
|
|
if (err != nil) != scenario.ExpectedError {
|
|
|
|
if scenario.ExpectedError {
|
|
|
|
t.Errorf("Expected error, got '%v'", err)
|
|
|
|
} else {
|
|
|
|
t.Errorf("Expected no error, got '%v'", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if outputLength != scenario.ExpectedOutputLength {
|
|
|
|
t.Errorf("Expected output length to be %v, but was %v", scenario.ExpectedOutputLength, outputLength)
|
|
|
|
}
|
|
|
|
if output != scenario.ExpectedOutput {
|
|
|
|
t.Errorf("Expected output to be %v, but was %v", scenario.ExpectedOutput, output)
|
|
|
|
}
|
|
|
|
})
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
}
|