yq/pkg/yqlib/treeops/path_postfix_test.go

355 lines
5.8 KiB
Go
Raw Normal View History

2020-10-08 23:59:03 +00:00
package treeops
2020-09-20 12:40:09 +00:00
import (
"testing"
"github.com/mikefarah/yq/v3/test"
)
// var tokeniser = NewPathTokeniser()
var postFixer = NewPathPostFixer()
func testExpression(expression string) (string, error) {
tokens, err := tokeniser.Tokenise(expression)
if err != nil {
return "", err
}
results, errorP := postFixer.ConvertToPostfix(tokens)
if errorP != nil {
return "", errorP
}
formatted := ""
for _, path := range results {
2020-10-16 01:29:26 +00:00
formatted = formatted + path.toString() + "\n--------\n"
2020-09-20 12:40:09 +00:00
}
return formatted, nil
}
2020-10-13 03:37:01 +00:00
func TestPostFixTraverseBar(t *testing.T) {
2020-10-16 01:29:26 +00:00
var infix = ".animals | [.]"
var expectedOutput = `PathKey - animals
2020-10-13 03:37:01 +00:00
--------
SELF
--------
Operation - COLLECT
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-10-13 03:37:01 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-10-16 01:29:26 +00:00
func TestPostFixPipeEquals(t *testing.T) {
var infix = `.animals | (. == "cat") `
var expectedOutput = `PathKey - animals
2020-10-11 00:24:22 +00:00
--------
SELF
--------
2020-10-16 01:29:26 +00:00
Value - cat (string)
2020-10-11 00:24:22 +00:00
--------
Operation - EQUALS
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-10-09 06:07:53 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-10-16 01:29:26 +00:00
func TestPostFixCollect(t *testing.T) {
var infix = "[.a]"
var expectedOutput = `PathKey - a
2020-10-12 01:24:59 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - COLLECT
2020-10-12 01:24:59 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-10-16 01:29:26 +00:00
func TestPostFixSplatSearch(t *testing.T) {
var infix = `.a | (.[].b == "apple")`
var expectedOutput = `PathKey - a
--------
PathKey - []
--------
PathKey - b
--------
Operation - PIPE
--------
Value - apple (string)
--------
Operation - EQUALS
--------
Operation - PIPE
2020-09-20 12:47:53 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-10-16 01:29:26 +00:00
func TestPostFixCollectWithExpression(t *testing.T) {
var infix = `[ (.a == "fred") | (.d, .f)]`
var expectedOutput = `PathKey - a
--------
Value - fred (string)
--------
Operation - EQUALS
--------
PathKey - d
2020-09-24 03:20:02 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - f
2020-09-24 03:20:02 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - OR
2020-09-24 03:20:02 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-09-24 03:20:02 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - COLLECT
2020-09-20 12:47:53 +00:00
--------
`
2020-09-20 12:40:09 +00:00
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-10-16 01:29:26 +00:00
func TestPostFixLength(t *testing.T) {
var infix = ".a | length"
var expectedOutput = `PathKey - a
2020-10-10 04:24:37 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - LENGTH
2020-10-10 04:24:37 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-10-10 04:24:37 +00:00
--------
2020-10-16 01:29:26 +00:00
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
func TestPostFixSimpleExample(t *testing.T) {
var infix = ".a"
var expectedOutput = `PathKey - a
2020-10-10 04:24:37 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-10-16 01:29:26 +00:00
func TestPostFixSimplePathExample(t *testing.T) {
var infix = ".apples.bananas*.cat"
var expectedOutput = `PathKey - apples
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - bananas*
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - cat
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-09-24 03:28:47 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-10-16 01:29:26 +00:00
func TestPostFixSimpleAssign(t *testing.T) {
var infix = ".a.b |= \"frog\""
var expectedOutput = `PathKey - a
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - b
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
Value - frog (string)
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - ASSIGN
2020-09-24 03:28:47 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-10-16 01:29:26 +00:00
func TestPostFixSimplePathNumbersExample(t *testing.T) {
var infix = ".apples[0].cat"
var expectedOutput = `PathKey - apples
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - 0
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - cat
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-09-24 03:28:47 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-10-16 01:29:26 +00:00
func TestPostFixSimplePathSplatArrayExample(t *testing.T) {
var infix = ".apples[].cat"
var expectedOutput = `PathKey - apples
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - []
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - cat
2020-09-24 03:28:47 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-09-24 03:28:47 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-09-24 03:20:02 +00:00
func TestPostFixOrExample(t *testing.T) {
2020-10-16 01:29:26 +00:00
var infix = ".a, .b"
var expectedOutput = `PathKey - a
2020-09-20 12:40:09 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - b
2020-09-20 12:40:09 +00:00
--------
2020-09-24 03:20:02 +00:00
Operation - OR
2020-09-20 12:40:09 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
2020-10-09 06:07:53 +00:00
test.AssertResultComplex(t, expectedOutput, actual)
}
func TestPostFixEqualsNumberExample(t *testing.T) {
2020-10-16 01:29:26 +00:00
var infix = ".animal == 3"
var expectedOutput = `PathKey - animal
2020-10-09 06:07:53 +00:00
--------
2020-10-16 01:29:26 +00:00
Value - 3 (int64)
2020-10-09 06:07:53 +00:00
--------
Operation - EQUALS
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
2020-09-20 12:40:09 +00:00
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-09-24 03:20:02 +00:00
func TestPostFixOrWithEqualsExample(t *testing.T) {
2020-10-16 01:29:26 +00:00
var infix = ".a==\"thing\", .b==.thongs"
var expectedOutput = `PathKey - a
2020-09-20 12:40:09 +00:00
--------
2020-10-16 01:29:26 +00:00
Value - thing (string)
2020-09-20 12:40:09 +00:00
--------
2020-09-24 03:20:02 +00:00
Operation - EQUALS
2020-09-20 12:40:09 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - b
2020-09-20 12:40:09 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - thongs
2020-09-20 12:40:09 +00:00
--------
2020-09-24 03:20:02 +00:00
Operation - EQUALS
2020-09-20 12:40:09 +00:00
--------
2020-09-24 03:20:02 +00:00
Operation - OR
2020-09-20 12:40:09 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-09-20 12:47:53 +00:00
2020-09-24 03:20:02 +00:00
func TestPostFixOrWithEqualsPathExample(t *testing.T) {
2020-10-16 01:29:26 +00:00
var infix = ".apples.monkeys==\"thing\", .bogs.bobos==true"
var expectedOutput = `PathKey - apples
2020-09-24 03:20:02 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - monkeys
2020-09-24 03:20:02 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-09-24 03:20:02 +00:00
--------
2020-10-16 01:29:26 +00:00
Value - thing (string)
2020-09-24 03:20:02 +00:00
--------
Operation - EQUALS
2020-09-20 12:47:53 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - bogs
2020-09-20 12:47:53 +00:00
--------
2020-10-16 01:29:26 +00:00
PathKey - bobos
2020-09-20 12:47:53 +00:00
--------
2020-10-16 01:29:26 +00:00
Operation - PIPE
2020-09-20 12:47:53 +00:00
--------
2020-10-16 01:29:26 +00:00
Value - true (bool)
2020-09-20 12:47:53 +00:00
--------
2020-09-24 03:20:02 +00:00
Operation - EQUALS
2020-09-20 12:47:53 +00:00
--------
2020-09-24 03:20:02 +00:00
Operation - OR
2020-09-20 12:47:53 +00:00
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}