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
|
|
|
|
}
|
|
|
|
|
2024-09-29 05:06:34 +00:00
|
|
|
func TestParserCreateMapColonOnItsOwn(t *testing.T) {
|
|
|
|
_, err := getExpressionParser().ParseExpression(":")
|
|
|
|
test.AssertResultComplex(t, "':' expects 2 args but there is 0", err.Error())
|
|
|
|
}
|
|
|
|
|
2022-10-08 01:44:47 +00:00
|
|
|
func TestParserNoMatchingCloseBracket(t *testing.T) {
|
|
|
|
_, err := getExpressionParser().ParseExpression(".cat | with(.;.bob")
|
|
|
|
test.AssertResultComplex(t, "bad expression - probably missing close bracket on WITH", err.Error())
|
|
|
|
}
|
|
|
|
|
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")
|
2022-10-08 01:44:47 +00:00
|
|
|
test.AssertResultComplex(t, "bad expression, could not find matching `]`", err.Error())
|
2021-09-05 01:39:11 +00:00
|
|
|
}
|
|
|
|
func TestParserNoMatchingCloseObjectInCollect(t *testing.T) {
|
2022-02-01 03:47:51 +00:00
|
|
|
_, err := getExpressionParser().ParseExpression(`[{"b": "c"]`)
|
2022-10-08 01:44:47 +00:00
|
|
|
test.AssertResultComplex(t, "bad expression, could not find matching `}`", err.Error())
|
2021-09-05 01:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParserNoMatchingCloseInCollect(t *testing.T) {
|
2022-02-01 03:47:51 +00:00
|
|
|
_, err := getExpressionParser().ParseExpression(`[(.a]`)
|
2022-10-08 01:44:47 +00:00
|
|
|
test.AssertResultComplex(t, "bad expression, could not find matching `)`", err.Error())
|
2021-09-05 01:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParserNoMatchingCloseCollectObject(t *testing.T) {
|
2022-02-01 03:47:51 +00:00
|
|
|
_, err := getExpressionParser().ParseExpression(`{"a": "b"`)
|
2022-10-08 01:44:47 +00:00
|
|
|
test.AssertResultComplex(t, "bad expression, could not find matching `}`", err.Error())
|
2021-09-05 01:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParserNoMatchingCloseCollectInCollectObject(t *testing.T) {
|
2022-02-01 03:47:51 +00:00
|
|
|
_, err := getExpressionParser().ParseExpression(`{"b": [1}`)
|
2022-10-08 01:44:47 +00:00
|
|
|
test.AssertResultComplex(t, "bad expression, could not find matching `]`", err.Error())
|
2021-09-05 01:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParserNoMatchingCloseBracketInCollectObject(t *testing.T) {
|
2022-02-01 03:47:51 +00:00
|
|
|
_, err := getExpressionParser().ParseExpression(`{"b": (1}`)
|
2022-10-08 01:44:47 +00:00
|
|
|
test.AssertResultComplex(t, "bad expression, could not find matching `)`", err.Error())
|
2021-09-05 01:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(.)")
|
2022-10-08 01:44:47 +00:00
|
|
|
test.AssertResultComplex(t, "bad expression, please check expression syntax", err.Error())
|
2020-12-17 03:19:46 +00:00
|
|
|
}
|