mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-14 07:08:06 +00:00
307 lines
5.1 KiB
Go
307 lines
5.1 KiB
Go
package treeops
|
|
|
|
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
|
|
}
|
|
|
|
func TestPostFixArrayEquals(t *testing.T) {
|
|
var infix = "animals(.== cat)"
|
|
var expectedOutput = `PathKey - 'animals'
|
|
--------
|
|
SELF
|
|
--------
|
|
PathKey - 'cat'
|
|
--------
|
|
Operation - EQUALS
|
|
--------
|
|
Operation - TRAVERSE
|
|
--------
|
|
`
|
|
|
|
actual, err := testExpression(infix)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
test.AssertResultComplex(t, expectedOutput, actual)
|
|
}
|
|
|
|
func TestPostFixLength(t *testing.T) {
|
|
var infix = "len(a)"
|
|
var expectedOutput = `PathKey - 'a'
|
|
--------
|
|
Operation - Length
|
|
--------
|
|
`
|
|
|
|
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'
|
|
--------
|
|
`
|
|
|
|
actual, err := testExpression(infix)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
test.AssertResultComplex(t, expectedOutput, actual)
|
|
}
|
|
|
|
func TestPostFixSimplePathExample(t *testing.T) {
|
|
var infix = "apples.bananas*.cat"
|
|
var expectedOutput = `PathKey - 'apples'
|
|
--------
|
|
PathKey - 'bananas*'
|
|
--------
|
|
Operation - TRAVERSE
|
|
--------
|
|
PathKey - 'cat'
|
|
--------
|
|
Operation - TRAVERSE
|
|
--------
|
|
`
|
|
|
|
actual, err := testExpression(infix)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
test.AssertResultComplex(t, expectedOutput, actual)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func TestPostFixOrExample(t *testing.T) {
|
|
var infix = "a OR b"
|
|
var expectedOutput = `PathKey - 'a'
|
|
--------
|
|
PathKey - 'b'
|
|
--------
|
|
Operation - OR
|
|
--------
|
|
`
|
|
|
|
actual, err := testExpression(infix)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
test.AssertResultComplex(t, expectedOutput, actual)
|
|
}
|
|
|
|
func TestPostFixOrWithEqualsExample(t *testing.T) {
|
|
var infix = "a==thing OR b==thongs"
|
|
var expectedOutput = `PathKey - 'a'
|
|
--------
|
|
PathKey - 'thing'
|
|
--------
|
|
Operation - EQUALS
|
|
--------
|
|
PathKey - 'b'
|
|
--------
|
|
PathKey - 'thongs'
|
|
--------
|
|
Operation - EQUALS
|
|
--------
|
|
Operation - OR
|
|
--------
|
|
`
|
|
|
|
actual, err := testExpression(infix)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
test.AssertResultComplex(t, expectedOutput, actual)
|
|
}
|
|
|
|
func TestPostFixOrWithEqualsPathExample(t *testing.T) {
|
|
var infix = "apples.monkeys==thing OR bogs.bobos==thongs"
|
|
var expectedOutput = `PathKey - 'apples'
|
|
--------
|
|
PathKey - 'monkeys'
|
|
--------
|
|
Operation - TRAVERSE
|
|
--------
|
|
PathKey - 'thing'
|
|
--------
|
|
Operation - EQUALS
|
|
--------
|
|
PathKey - 'bogs'
|
|
--------
|
|
PathKey - 'bobos'
|
|
--------
|
|
Operation - TRAVERSE
|
|
--------
|
|
PathKey - 'thongs'
|
|
--------
|
|
Operation - EQUALS
|
|
--------
|
|
Operation - OR
|
|
--------
|
|
`
|
|
|
|
actual, err := testExpression(infix)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
test.AssertResultComplex(t, expectedOutput, actual)
|
|
}
|