yq/pkg/yqlib/expression_processing_test.go

217 lines
7.8 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-09-17 11:58:01 +00:00
import (
2020-10-17 11:10:47 +00:00
"fmt"
2020-09-17 11:58:01 +00:00
"testing"
2020-10-27 05:45:16 +00:00
"github.com/mikefarah/yq/v4/test"
2020-09-17 11:58:01 +00:00
)
2020-10-17 11:10:47 +00:00
var pathTests = []struct {
path string
expectedTokens []interface{}
expectedPostFix []interface{}
2020-12-01 07:08:41 +00:00
}{
2021-02-03 06:11:47 +00:00
{
`.a | .b | .c`,
append(make([]interface{}, 0), "a", "PIPE", "b", "PIPE", "c"),
append(make([]interface{}, 0), "a", "b", "c", "PIPE", "PIPE"),
},
2020-11-22 02:50:32 +00:00
{
`[]`,
append(make([]interface{}, 0), "[", "]"),
2020-12-01 07:08:41 +00:00
append(make([]interface{}, 0), "EMPTY", "COLLECT", "SHORT_PIPE"),
2020-11-22 02:50:32 +00:00
},
2021-02-03 04:51:26 +00:00
{
`.b[.a]`,
append(make([]interface{}, 0), "b", "TRAVERSE_ARRAY", "[", "a", "]"),
append(make([]interface{}, 0), "b", "a", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY"),
},
2020-12-26 10:37:08 +00:00
{
`.[]`,
2021-02-03 04:51:26 +00:00
append(make([]interface{}, 0), "SELF", "TRAVERSE_ARRAY", "[", "]"),
append(make([]interface{}, 0), "SELF", "EMPTY", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY"),
2020-12-26 10:37:08 +00:00
},
{
`.a[]`,
2021-02-03 04:51:26 +00:00
append(make([]interface{}, 0), "a", "TRAVERSE_ARRAY", "[", "]"),
append(make([]interface{}, 0), "a", "EMPTY", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY"),
},
{
`.a.[]`,
2021-02-03 04:51:26 +00:00
append(make([]interface{}, 0), "a", "TRAVERSE_ARRAY", "[", "]"),
append(make([]interface{}, 0), "a", "EMPTY", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY"),
2020-12-26 10:37:08 +00:00
},
{
`.a[0]`,
2021-02-03 04:51:26 +00:00
append(make([]interface{}, 0), "a", "TRAVERSE_ARRAY", "[", "0 (int64)", "]"),
append(make([]interface{}, 0), "a", "0 (int64)", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY"),
2020-12-26 10:37:08 +00:00
},
{
`.a.[0]`,
2021-02-03 04:51:26 +00:00
append(make([]interface{}, 0), "a", "TRAVERSE_ARRAY", "[", "0 (int64)", "]"),
append(make([]interface{}, 0), "a", "0 (int64)", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY"),
},
{
`.a[].c`,
2021-02-03 04:51:26 +00:00
append(make([]interface{}, 0), "a", "TRAVERSE_ARRAY", "[", "]", "SHORT_PIPE", "c"),
append(make([]interface{}, 0), "a", "EMPTY", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY", "c", "SHORT_PIPE"),
},
{
`[3]`,
append(make([]interface{}, 0), "[", "3 (int64)", "]"),
2020-12-01 07:08:41 +00:00
append(make([]interface{}, 0), "3 (int64)", "COLLECT", "SHORT_PIPE"),
},
{
`.key.array + .key.array2`,
append(make([]interface{}, 0), "key", "SHORT_PIPE", "array", "ADD", "key", "SHORT_PIPE", "array2"),
append(make([]interface{}, 0), "key", "array", "SHORT_PIPE", "key", "array2", "SHORT_PIPE", "ADD"),
},
{
`.key.array * .key.array2`,
append(make([]interface{}, 0), "key", "SHORT_PIPE", "array", "MULTIPLY", "key", "SHORT_PIPE", "array2"),
append(make([]interface{}, 0), "key", "array", "SHORT_PIPE", "key", "array2", "SHORT_PIPE", "MULTIPLY"),
},
{
`.key.array // .key.array2`,
append(make([]interface{}, 0), "key", "SHORT_PIPE", "array", "ALTERNATIVE", "key", "SHORT_PIPE", "array2"),
append(make([]interface{}, 0), "key", "array", "SHORT_PIPE", "key", "array2", "SHORT_PIPE", "ALTERNATIVE"),
},
2020-10-17 11:10:47 +00:00
{
2020-12-01 07:08:41 +00:00
`.a | .[].b == "apple"`,
2021-02-03 04:51:26 +00:00
append(make([]interface{}, 0), "a", "PIPE", "SELF", "TRAVERSE_ARRAY", "[", "]", "SHORT_PIPE", "b", "EQUALS", "apple (string)"),
append(make([]interface{}, 0), "a", "SELF", "EMPTY", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY", "b", "SHORT_PIPE", "apple (string)", "EQUALS", "PIPE"),
2020-10-17 11:10:47 +00:00
},
2020-12-01 07:10:10 +00:00
{
`(.a | .[].b) == "apple"`,
2021-02-03 04:51:26 +00:00
append(make([]interface{}, 0), "(", "a", "PIPE", "SELF", "TRAVERSE_ARRAY", "[", "]", "SHORT_PIPE", "b", ")", "EQUALS", "apple (string)"),
append(make([]interface{}, 0), "a", "SELF", "EMPTY", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY", "b", "SHORT_PIPE", "PIPE", "apple (string)", "EQUALS"),
2020-12-01 07:10:10 +00:00
},
2020-10-17 11:10:47 +00:00
{
`.[] | select(. == "*at")`,
2021-02-03 04:51:26 +00:00
append(make([]interface{}, 0), "SELF", "TRAVERSE_ARRAY", "[", "]", "PIPE", "SELECT", "(", "SELF", "EQUALS", "*at (string)", ")"),
append(make([]interface{}, 0), "SELF", "EMPTY", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY", "SELF", "*at (string)", "EQUALS", "SELECT", "PIPE"),
2020-10-17 11:10:47 +00:00
},
{
`[true]`,
2020-10-20 04:33:20 +00:00
append(make([]interface{}, 0), "[", "true (bool)", "]"),
2020-12-01 07:08:41 +00:00
append(make([]interface{}, 0), "true (bool)", "COLLECT", "SHORT_PIPE"),
2020-10-17 11:10:47 +00:00
},
2020-10-21 02:54:51 +00:00
{
`[true, false]`,
append(make([]interface{}, 0), "[", "true (bool)", "UNION", "false (bool)", "]"),
2020-12-01 07:08:41 +00:00
append(make([]interface{}, 0), "true (bool)", "false (bool)", "UNION", "COLLECT", "SHORT_PIPE"),
2020-10-21 02:54:51 +00:00
},
2020-10-21 01:54:58 +00:00
{
`"mike": .a`,
append(make([]interface{}, 0), "mike (string)", "CREATE_MAP", "a"),
append(make([]interface{}, 0), "mike (string)", "a", "CREATE_MAP"),
},
{
`.a: "mike"`,
append(make([]interface{}, 0), "a", "CREATE_MAP", "mike (string)"),
append(make([]interface{}, 0), "a", "mike (string)", "CREATE_MAP"),
},
2020-10-21 02:54:51 +00:00
{
`{"mike": .a}`,
append(make([]interface{}, 0), "{", "mike (string)", "CREATE_MAP", "a", "}"),
2020-12-01 07:08:41 +00:00
append(make([]interface{}, 0), "mike (string)", "a", "CREATE_MAP", "COLLECT_OBJECT", "SHORT_PIPE"),
2020-10-21 02:54:51 +00:00
},
{
`{.a: "mike"}`,
append(make([]interface{}, 0), "{", "a", "CREATE_MAP", "mike (string)", "}"),
2020-12-01 07:08:41 +00:00
append(make([]interface{}, 0), "a", "mike (string)", "CREATE_MAP", "COLLECT_OBJECT", "SHORT_PIPE"),
2020-10-21 02:54:51 +00:00
},
{
2021-02-03 04:51:26 +00:00
`{.a: .c, .b.[]: .f.g[]}`,
append(make([]interface{}, 0), "{", "a", "CREATE_MAP", "c", "UNION", "b", "TRAVERSE_ARRAY", "[", "]", "CREATE_MAP", "f", "SHORT_PIPE", "g", "TRAVERSE_ARRAY", "[", "]", "}"),
append(make([]interface{}, 0), "a", "c", "CREATE_MAP", "b", "EMPTY", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY", "f", "g", "EMPTY", "COLLECT", "SHORT_PIPE", "TRAVERSE_ARRAY", "SHORT_PIPE", "CREATE_MAP", "UNION", "COLLECT_OBJECT", "SHORT_PIPE"),
2020-10-21 02:54:51 +00:00
},
2020-11-02 00:20:38 +00:00
{
`explode(.a.b)`,
2020-12-01 07:08:41 +00:00
append(make([]interface{}, 0), "EXPLODE", "(", "a", "SHORT_PIPE", "b", ")"),
append(make([]interface{}, 0), "a", "b", "SHORT_PIPE", "EXPLODE"),
2020-11-02 00:20:38 +00:00
},
{
`.a.b style="folded"`,
2020-12-01 07:08:41 +00:00
append(make([]interface{}, 0), "a", "SHORT_PIPE", "b", "ASSIGN_STYLE", "folded (string)"),
append(make([]interface{}, 0), "a", "b", "SHORT_PIPE", "folded (string)", "ASSIGN_STYLE"),
2020-11-02 00:20:38 +00:00
},
2020-11-19 05:45:05 +00:00
{
`tag == "str"`,
append(make([]interface{}, 0), "GET_TAG", "EQUALS", "str (string)"),
append(make([]interface{}, 0), "GET_TAG", "str (string)", "EQUALS"),
},
{
`. tag= "str"`,
append(make([]interface{}, 0), "SELF", "ASSIGN_TAG", "str (string)"),
append(make([]interface{}, 0), "SELF", "str (string)", "ASSIGN_TAG"),
},
{
`lineComment == "str"`,
append(make([]interface{}, 0), "GET_COMMENT", "EQUALS", "str (string)"),
append(make([]interface{}, 0), "GET_COMMENT", "str (string)", "EQUALS"),
},
{
`. lineComment= "str"`,
append(make([]interface{}, 0), "SELF", "ASSIGN_COMMENT", "str (string)"),
append(make([]interface{}, 0), "SELF", "str (string)", "ASSIGN_COMMENT"),
},
2021-01-06 09:22:50 +00:00
{
`. lineComment |= "str"`,
append(make([]interface{}, 0), "SELF", "ASSIGN_COMMENT", "str (string)"),
append(make([]interface{}, 0), "SELF", "str (string)", "ASSIGN_COMMENT"),
},
2020-12-01 07:08:41 +00:00
{
`.a.b tag="!!str"`,
append(make([]interface{}, 0), "a", "SHORT_PIPE", "b", "ASSIGN_TAG", "!!str (string)"),
append(make([]interface{}, 0), "a", "b", "SHORT_PIPE", "!!str (string)", "ASSIGN_TAG"),
},
2020-11-02 00:20:38 +00:00
{
`""`,
append(make([]interface{}, 0), " (string)"),
append(make([]interface{}, 0), " (string)"),
},
2020-11-02 02:43:45 +00:00
{
`.foo* | (. style="flow")`,
append(make([]interface{}, 0), "foo*", "PIPE", "(", "SELF", "ASSIGN_STYLE", "flow (string)", ")"),
append(make([]interface{}, 0), "foo*", "SELF", "flow (string)", "ASSIGN_STYLE", "PIPE"),
},
2020-11-13 02:19:54 +00:00
{
`{}`,
append(make([]interface{}, 0), "{", "}"),
2020-12-01 07:08:41 +00:00
append(make([]interface{}, 0), "EMPTY", "COLLECT_OBJECT", "SHORT_PIPE"),
2020-11-13 02:19:54 +00:00
},
2020-09-17 11:58:01 +00:00
}
2021-01-12 23:18:53 +00:00
var tokeniser = newExpressionTokeniser()
var postFixer = newExpressionPostFixer()
2020-09-17 11:58:01 +00:00
2020-10-17 11:10:47 +00:00
func TestPathParsing(t *testing.T) {
for _, tt := range pathTests {
2020-09-17 11:58:01 +00:00
tokens, err := tokeniser.Tokenise(tt.path)
if err != nil {
t.Error(tt.path, err)
}
var tokenValues []interface{}
for _, token := range tokens {
2021-02-03 06:11:47 +00:00
tokenValues = append(tokenValues, token.toString(false))
2020-09-17 11:58:01 +00:00
}
2020-10-17 11:10:47 +00:00
test.AssertResultComplexWithContext(t, tt.expectedTokens, tokenValues, fmt.Sprintf("tokenise: %v", tt.path))
results, errorP := postFixer.ConvertToPostfix(tokens)
var readableResults []interface{}
for _, token := range results {
readableResults = append(readableResults, token.toString())
}
if errorP != nil {
t.Error(tt.path, err)
}
test.AssertResultComplexWithContext(t, tt.expectedPostFix, readableResults, fmt.Sprintf("postfix: %v", tt.path))
2020-09-17 11:58:01 +00:00
}
}