yq/pkg/yqlib/expression_parser_test.go

82 lines
2.9 KiB
Go
Raw Normal View History

2020-12-17 03:02:54 +00:00
package yqlib
import (
"testing"
"github.com/mikefarah/yq/v4/test"
)
2022-02-01 03:47:51 +00:00
func getExpressionParser() ExpressionParserInterface {
2022-03-15 02:28:52 +00:00
InitExpressionParser()
2022-02-01 03:47:51 +00:00
return ExpressionParser
}
func TestParserNoMatchingCloseBracket(t *testing.T) {
_, err := getExpressionParser().ParseExpression(".cat | with(.;.bob")
test.AssertResultComplex(t, "bad expression - probably missing close bracket on WITH", err.Error())
}
func TestParserNoMatchingCloseCollect(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression("[1,2")
test.AssertResultComplex(t, "bad expression, could not find matching `]`", err.Error())
}
func TestParserNoMatchingCloseObjectInCollect(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression(`[{"b": "c"]`)
test.AssertResultComplex(t, "bad expression, could not find matching `}`", err.Error())
}
func TestParserNoMatchingCloseInCollect(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression(`[(.a]`)
test.AssertResultComplex(t, "bad expression, could not find matching `)`", err.Error())
}
func TestParserNoMatchingCloseCollectObject(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression(`{"a": "b"`)
test.AssertResultComplex(t, "bad expression, could not find matching `}`", err.Error())
}
func TestParserNoMatchingCloseCollectInCollectObject(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression(`{"b": [1}`)
test.AssertResultComplex(t, "bad expression, could not find matching `]`", err.Error())
}
func TestParserNoMatchingCloseBracketInCollectObject(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression(`{"b": (1}`)
test.AssertResultComplex(t, "bad expression, could not find matching `)`", err.Error())
}
func TestParserNoArgsForTwoArgOp(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression("=")
2020-12-17 03:02:54 +00:00
test.AssertResultComplex(t, "'=' expects 2 args but there is 0", err.Error())
}
func TestParserOneLhsArgsForTwoArgOp(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression(".a =")
2020-12-17 03:02:54 +00:00
test.AssertResultComplex(t, "'=' expects 2 args but there is 1", err.Error())
}
func TestParserOneRhsArgsForTwoArgOp(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression("= .a")
2020-12-17 03:02:54 +00:00
test.AssertResultComplex(t, "'=' expects 2 args but there is 1", err.Error())
}
func TestParserTwoArgsForTwoArgOp(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression(".a = .b")
2020-12-17 03:02:54 +00:00
test.AssertResultComplex(t, nil, err)
}
func TestParserNoArgsForOneArgOp(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression("explode")
2020-12-17 03:02:54 +00:00
test.AssertResultComplex(t, "'explode' expects 1 arg but received none", err.Error())
}
func TestParserOneArgForOneArgOp(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression("explode(.)")
2020-12-17 03:02:54 +00:00
test.AssertResultComplex(t, nil, err)
}
2020-12-17 03:19:46 +00:00
func TestParserExtraArgs(t *testing.T) {
2022-02-01 03:47:51 +00:00
_, err := getExpressionParser().ParseExpression("sortKeys(.) explode(.)")
test.AssertResultComplex(t, "bad expression, please check expression syntax", err.Error())
2020-12-17 03:19:46 +00:00
}