2020-12-17 03:02:54 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mikefarah/yq/v4/test"
|
|
|
|
)
|
|
|
|
|
2021-09-05 01:39:11 +00:00
|
|
|
func TestParserNoMatchingCloseCollect(t *testing.T) {
|
|
|
|
_, err := NewExpressionParser().ParseExpression("[1,2")
|
|
|
|
test.AssertResultComplex(t, "Bad expression, could not find matching `]`", err.Error())
|
|
|
|
}
|
|
|
|
func TestParserNoMatchingCloseObjectInCollect(t *testing.T) {
|
|
|
|
_, err := NewExpressionParser().ParseExpression(`[{"b": "c"]`)
|
|
|
|
test.AssertResultComplex(t, "Bad expression, could not find matching `}`", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParserNoMatchingCloseInCollect(t *testing.T) {
|
|
|
|
_, err := NewExpressionParser().ParseExpression(`[(.a]`)
|
|
|
|
test.AssertResultComplex(t, "Bad expression, could not find matching `)`", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParserNoMatchingCloseCollectObject(t *testing.T) {
|
|
|
|
_, err := NewExpressionParser().ParseExpression(`{"a": "b"`)
|
|
|
|
test.AssertResultComplex(t, "Bad expression, could not find matching `}`", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParserNoMatchingCloseCollectInCollectObject(t *testing.T) {
|
|
|
|
_, err := NewExpressionParser().ParseExpression(`{"b": [1}`)
|
|
|
|
test.AssertResultComplex(t, "Bad expression, could not find matching `]`", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParserNoMatchingCloseBracketInCollectObject(t *testing.T) {
|
|
|
|
_, err := NewExpressionParser().ParseExpression(`{"b": (1}`)
|
|
|
|
test.AssertResultComplex(t, "Bad expression, could not find matching `)`", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParserNoArgsForTwoArgOp(t *testing.T) {
|
2021-01-12 23:18:53 +00:00
|
|
|
_, err := NewExpressionParser().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) {
|
2021-01-12 23:18:53 +00:00
|
|
|
_, err := NewExpressionParser().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) {
|
2021-01-12 23:18:53 +00:00
|
|
|
_, err := NewExpressionParser().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) {
|
2021-01-12 23:18:53 +00:00
|
|
|
_, err := NewExpressionParser().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) {
|
2021-01-12 23:18:53 +00:00
|
|
|
_, err := NewExpressionParser().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) {
|
2021-01-12 23:18:53 +00:00
|
|
|
_, err := NewExpressionParser().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) {
|
2021-01-12 23:18:53 +00:00
|
|
|
_, err := NewExpressionParser().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
|
|
|
}
|