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 {
|
|
|
|
if ExpressionParser == nil {
|
|
|
|
ExpressionParser = newExpressionParser()
|
|
|
|
}
|
|
|
|
return ExpressionParser
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:39:11 +00:00
|
|
|
func TestParserNoMatchingCloseCollect(t *testing.T) {
|
2022-02-01 03:47:51 +00:00
|
|
|
_, err := getExpressionParser().ParseExpression("[1,2")
|
2021-09-05 01:39:11 +00:00
|
|
|
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"]`)
|
2021-09-05 01:39:11 +00:00
|
|
|
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]`)
|
2021-09-05 01:39:11 +00:00
|
|
|
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"`)
|
2021-09-05 01:39:11 +00:00
|
|
|
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}`)
|
2021-09-05 01:39:11 +00:00
|
|
|
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}`)
|
2021-09-05 01:39:11 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:39:11 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:39:11 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:39:11 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:39:11 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:39:11 +00:00
|
|
|
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
|
|
|
|
2021-09-05 01:39:11 +00:00
|
|
|
func TestParserExtraArgs(t *testing.T) {
|
2022-02-01 03:47:51 +00:00
|
|
|
_, err := getExpressionParser().ParseExpression("sortKeys(.) explode(.)")
|
2021-02-03 06:20:54 +00:00
|
|
|
test.AssertResultComplex(t, "Bad expression, please check expression syntax", err.Error())
|
2020-12-17 03:19:46 +00:00
|
|
|
}
|