yq/pkg/yqlib/treeops/path_postfix_test.go

283 lines
4.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 {
formatted = formatted + path.toString() + "--------\n"
}
return formatted, nil
}
2020-10-09 06:07:53 +00:00
func TestPostFixArrayEquals(t *testing.T) {
var infix = "a"
var expectedOutput = `PathKey - 'a'
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-09-24 03:20:02 +00:00
func TestPostFixSimpleExample(t *testing.T) {
2020-09-20 12:40:09 +00:00
var infix = "a"
2020-09-24 03:20:02 +00:00
var expectedOutput = `PathKey - 'a'
2020-09-20 12:47:53 +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 TestPostFixSimplePathExample(t *testing.T) {
2020-09-20 12:47:53 +00:00
var infix = "apples.bananas*.cat"
2020-09-24 03:20:02 +00:00
var expectedOutput = `PathKey - 'apples'
--------
PathKey - 'bananas*'
--------
Operation - TRAVERSE
--------
PathKey - 'cat'
--------
Operation - TRAVERSE
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-10 04:24:37 +00:00
func TestPostFixSimpleAssign(t *testing.T) {
var infix = "a.b := frog"
var expectedOutput = `PathKey - 'a'
--------
PathKey - 'b'
--------
Operation - TRAVERSE
--------
PathKey - 'frog'
--------
Operation - ASSIGN
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
2020-09-24 03:28:47 +00:00
func TestPostFixSimplePathNumbersExample(t *testing.T) {
var infix = "apples[0].cat"
var expectedOutput = `PathKey - 'apples'
--------
PathKey - '0'
--------
Operation - TRAVERSE
--------
PathKey - 'cat'
--------
Operation - TRAVERSE
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
func TestPostFixSimplePathAppendArrayExample(t *testing.T) {
var infix = "apples[+].cat"
var expectedOutput = `PathKey - 'apples'
--------
PathKey - '[+]'
--------
Operation - TRAVERSE
--------
PathKey - 'cat'
--------
Operation - TRAVERSE
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
func TestPostFixSimplePathSplatArrayExample(t *testing.T) {
var infix = "apples.[*]cat"
var expectedOutput = `PathKey - 'apples'
--------
PathKey - '[*]'
--------
Operation - TRAVERSE
--------
PathKey - 'cat'
--------
Operation - TRAVERSE
--------
`
actual, err := testExpression(infix)
if err != nil {
t.Error(err)
}
test.AssertResultComplex(t, expectedOutput, actual)
}
func TestPostFixDeepMatchExample(t *testing.T) {
var infix = "apples.**.cat"
var expectedOutput = `PathKey - 'apples'
--------
PathKey - '**'
--------
Operation - TRAVERSE
--------
PathKey - 'cat'
--------
Operation - TRAVERSE
--------
`
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-09-20 12:40:09 +00:00
var infix = "a OR b"
2020-09-24 03:20:02 +00:00
var expectedOutput = `PathKey - 'a'
2020-09-20 12:40:09 +00:00
--------
2020-09-24 03:20:02 +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) {
var infix = "(animal == 3)"
var expectedOutput = `PathKey - 'animal'
--------
PathKey - '3'
--------
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-09-20 12:40:09 +00:00
var infix = "a==thing OR b==thongs"
2020-09-24 03:20:02 +00:00
var expectedOutput = `PathKey - 'a'
2020-09-20 12:40:09 +00:00
--------
2020-09-24 03:20:02 +00:00
PathKey - 'thing'
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
PathKey - 'b'
2020-09-20 12:40:09 +00:00
--------
2020-09-24 03:20:02 +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-09-20 12:47:53 +00:00
var infix = "apples.monkeys==thing OR bogs.bobos==thongs"
2020-09-24 03:20:02 +00:00
var expectedOutput = `PathKey - 'apples'
--------
PathKey - 'monkeys'
--------
Operation - TRAVERSE
--------
PathKey - 'thing'
--------
Operation - EQUALS
2020-09-20 12:47:53 +00:00
--------
2020-09-24 03:20:02 +00:00
PathKey - 'bogs'
2020-09-20 12:47:53 +00:00
--------
2020-09-24 03:20:02 +00:00
PathKey - 'bobos'
2020-09-20 12:47:53 +00:00
--------
2020-09-24 03:20:02 +00:00
Operation - TRAVERSE
2020-09-20 12:47:53 +00:00
--------
2020-09-24 03:20:02 +00:00
PathKey - 'thongs'
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)
}