forked from couchbaselabs/gojsonsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filterExprParserPcre_test.go
52 lines (45 loc) · 2.04 KB
/
filterExprParserPcre_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2018 Couchbase, Inc. All rights reserved.
// +build pcre
package gojsonsm
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)
func TestFilterExpressionParserPcre(t *testing.T) {
assert := assert.New(t)
fe := &FilterExpression{}
_, fe, err := NewFilterExpressionParser("REGEXP_CONTAINS(`[$%XDCRInternalKey*%$]`, \"a(?=foo)\")")
assert.Nil(err)
assert.Equal("REGEXP_CONTAINS", fe.AndConditions[0].OrConditions[0].Operand.BooleanExpr.BooleanFunc.BooleanFuncTwoArgs.BooleanFuncTwoArgsName.String())
assert.Equal("[$%XDCRInternalKey*%$]", fe.AndConditions[0].OrConditions[0].Operand.BooleanExpr.BooleanFunc.BooleanFuncTwoArgs.Argument0.Field.String())
assert.Equal("a(?=foo)", fe.AndConditions[0].OrConditions[0].Operand.BooleanExpr.BooleanFunc.BooleanFuncTwoArgs.Argument1.Argument.String())
expr, err := fe.OutputExpression()
assert.Nil(err)
assert.NotNil(expr)
// test has no pcre support
fe = &FilterExpression{}
_, fe, err = NewFilterExpressionParser("REGEXP_CONTAINS(`[$%XDCRInternalKey*%$]`, \"q(?!uit)\")")
assert.Nil(err)
assert.Equal("REGEXP_CONTAINS", fe.AndConditions[0].OrConditions[0].Operand.BooleanExpr.BooleanFunc.BooleanFuncTwoArgs.BooleanFuncTwoArgsName.String())
assert.Equal("[$%XDCRInternalKey*%$]", fe.AndConditions[0].OrConditions[0].Operand.BooleanExpr.BooleanFunc.BooleanFuncTwoArgs.Argument0.Field.String())
assert.Equal("q(?!uit)", fe.AndConditions[0].OrConditions[0].Operand.BooleanExpr.BooleanFunc.BooleanFuncTwoArgs.Argument1.Argument.String())
expr, err = fe.OutputExpression()
assert.Nil(err)
assert.NotNil(expr)
var trans Transformer
matchDef := trans.Transform([]Expression{expr})
assert.NotNil(matchDef)
m := NewFastMatcher(matchDef)
userData := map[string]interface{}{
"[$%XDCRInternalKey*%$]": "quiz",
}
udMarsh, _ := json.Marshal(userData)
match, err := m.Match(udMarsh)
assert.True(match)
fe = &FilterExpression{}
_, fe, err = NewFilterExpressionParser("REGEXP_CONTAINS(`[$%XDCRInternalKey*%$]`, \"^d\")")
assert.Nil(err)
_, err = fe.OutputExpression()
assert.Nil(err)
}