Best Gauge code snippet using parser.text
parser_test.go
Source:parser_test.go
...37 t.Errorf(err.Error())38 }39 equals(t, len(parseResult.QueryValues), 3)40 equals(t, parseResult.QueryValues, []interface{}{"a", "b", "c"})41 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[] AND q.accessors @> ARRAY[$2]::text[] AND q.accessors @> ARRAY[$3]::text[]")42}43func TestParserCorrectWithParentheses(t *testing.T) { 44 queryParser := NewQueryParser("(a & b & c)")45 parseResult, err := queryParser.Parse()46 if err != nil {47 t.Errorf(err.Error())48 }49 equals(t, len(parseResult.QueryValues), 3)50 equals(t, parseResult.QueryValues, []interface{}{"a", "b", "c"})51 equals(t, parseResult.Query, "(q.accessors @> ARRAY[$1]::text[] AND q.accessors @> ARRAY[$2]::text[] AND q.accessors @> ARRAY[$3]::text[])")52}53func TestParserCorrectWithMultipleParentheses(t *testing.T) { 54 queryParser := NewQueryParser("(a & (b | c))")55 parseResult, err := queryParser.Parse()56 if err != nil {57 t.Errorf(err.Error())58 }59 equals(t, len(parseResult.QueryValues), 3)60 equals(t, parseResult.QueryValues, []interface{}{"a", "b", "c"})61 equals(t, parseResult.Query, "(q.accessors @> ARRAY[$1]::text[] AND (q.accessors @> ARRAY[$2]::text[] OR q.accessors @> ARRAY[$3]::text[]))")62}63func TestParserCorrectWithMultipleParentheses2(t *testing.T) { 64 queryParser := NewQueryParser("(a | (b | c))")65 parseResult, err := queryParser.Parse()66 if err != nil {67 t.Errorf(err.Error())68 }69 equals(t, len(parseResult.QueryValues), 3)70 equals(t, parseResult.QueryValues, []interface{}{"a", "b", "c"})71 equals(t, parseResult.Query, "(q.accessors @> ARRAY[$1]::text[] OR (q.accessors @> ARRAY[$2]::text[] OR q.accessors @> ARRAY[$3]::text[]))")72}73func TestParserCorrectWithMultipleParentheses3(t *testing.T) { 74 queryParser := NewQueryParser("((a | (b | c)))")75 parseResult, err := queryParser.Parse()76 if err != nil {77 t.Errorf(err.Error())78 }79 equals(t, len(parseResult.QueryValues), 3)80 equals(t, parseResult.QueryValues, []interface{}{"a", "b", "c"})81 equals(t, parseResult.Query, "((q.accessors @> ARRAY[$1]::text[] OR (q.accessors @> ARRAY[$2]::text[] OR q.accessors @> ARRAY[$3]::text[])))")82}83func TestParserIncorrectSyntax(t *testing.T) { 84 queryParser := NewQueryParser("a & b & |")85 _, err := queryParser.Parse()86 if err == nil {87 t.Errorf("Expected not nil, but got nil")88 }89}90func TestParserIncorrectSyntax2(t *testing.T) { 91 queryParser := NewQueryParser("a |")92 _, err := queryParser.Parse()93 if err == nil {94 t.Errorf("Expected not nil, but got nil")95 }96}97func TestParserIncorrectBrackets(t *testing.T) { 98 queryParser := NewQueryParser("a & b & )")99 _, err := queryParser.Parse()100 if err == nil {101 t.Errorf("Expected not nil, but got nil")102 }103}104func TestParserIncorrectLength(t *testing.T) { 105 queryParser := NewQueryParser("a & b & c & d & e & f & f & g & h & z & u)")106 _, err := queryParser.Parse()107 if err == nil {108 t.Errorf("Expected not nil, but got nil")109 }110}111func TestParserKeepWhitespacesInAssigment(t *testing.T) { 112 queryParser := NewQueryParser("a | b.c = 'hello world'")113 parseResult, err := queryParser.Parse()114 if err != nil {115 t.Errorf("Expected nil, but got not nil: %s", err.Error())116 }117 equals(t, len(parseResult.QueryValues), 2)118 equals(t, parseResult.QueryValues, []interface{}{"a", "b.c='hello world'"})119 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[] OR q.accessors @> ARRAY[$2]::text[]")120}121func TestParserKeepWhitespacesInAssigmentWithMultipleSpacesBefore(t *testing.T) { 122 queryParser := NewQueryParser("a | b.c = 'hello world'")123 parseResult, err := queryParser.Parse()124 if err != nil {125 t.Errorf("Expected nil, but got not nil: %s", err.Error())126 }127 equals(t, len(parseResult.QueryValues), 2)128 equals(t, parseResult.QueryValues, []interface{}{"a", "b.c='hello world'"})129 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[] OR q.accessors @> ARRAY[$2]::text[]")130}131func TestParserAssignment(t *testing.T) { 132 queryParser := NewQueryParser("hello='world'")133 _, err := queryParser.Parse()134 if err != nil {135 t.Errorf("Expected nil, but got not nil: %s", err.Error())136 }137}138func TestParserAssignment1(t *testing.T) { 139 queryParser := NewQueryParser("a & hello='big world'")140 _, err := queryParser.Parse()141 if err != nil {142 t.Errorf("Expected nil, but got not nil: %s", err.Error())143 }144}145func TestComplexQuery(t *testing.T) {146 queryParser := NewQueryParser("a & (b | c) | d")147 parseResult, err := queryParser.Parse()148 if err != nil {149 t.Errorf("Expected nil, but got not nil: %s", err.Error())150 }151 equals(t, len(parseResult.QueryValues), 4)152 equals(t, parseResult.QueryValues, []interface{}{"a", "b", "c", "d"})153 equals(t, parseResult.Subquery, "a.accessor = $1 OR (a.accessor = $2 OR a.accessor = $3) OR a.accessor = $4")154 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[] AND (q.accessors @> ARRAY[$2]::text[] OR q.accessors @> ARRAY[$3]::text[]) OR q.accessors @> ARRAY[$4]::text[]")155}156func TestWrongComplexQuery(t *testing.T) {157 queryParser := NewQueryParser("a & (b | c) | d )")158 _, err := queryParser.Parse()159 if err == nil {160 t.Errorf("Expected nil, but got not nil")161 }162}163func TestAnotherWrongComplexQuery(t *testing.T) {164 queryParser := NewQueryParser("a & (b | c) | d ()")165 _, err := queryParser.Parse()166 if err == nil {167 t.Errorf("Expected not nil, but got nil")168 }169}170func TestAnotherComplexQuery(t *testing.T) {171 queryParser := NewQueryParser("(a & (b | c) | d)")172 parseResult, err := queryParser.Parse()173 if err != nil {174 t.Errorf("Expected nil, but got not nil: %s", err.Error())175 }176 equals(t, len(parseResult.QueryValues), 4)177 equals(t, parseResult.QueryValues, []interface{}{"a", "b", "c", "d"})178 equals(t, parseResult.Subquery, "(a.accessor = $1 OR (a.accessor = $2 OR a.accessor = $3) OR a.accessor = $4)")179 equals(t, parseResult.Query, "(q.accessors @> ARRAY[$1]::text[] AND (q.accessors @> ARRAY[$2]::text[] OR q.accessors @> ARRAY[$3]::text[]) OR q.accessors @> ARRAY[$4]::text[])")180}181func TestWrongComplexQuery1(t *testing.T) {182 queryParser := NewQueryParser(")a & (b | c) | d)")183 _, err := queryParser.Parse()184 if err == nil {185 t.Errorf("Expected not nil, but got nil: %s", err.Error())186 }187}188func TestWrongComplexQuery2(t *testing.T) {189 queryParser := NewQueryParser("(a & (b | c) | d")190 _, err := queryParser.Parse()191 if err == nil {192 t.Errorf("Expected not nil, but got nil: %s", err.Error())193 }194}195func TestComplexQuery1(t *testing.T) {196 queryParser := NewQueryParser("(a & (string with spaces | c) | d)")197 parseResult, err := queryParser.Parse()198 if err != nil {199 t.Errorf("Expected nil, but got not nil: %s", err.Error())200 }201 equals(t, len(parseResult.QueryValues), 4)202 equals(t, parseResult.QueryValues, []interface{}{"a", "string with spaces", "c", "d"})203 equals(t, parseResult.Subquery, "(a.accessor = $1 OR (a.accessor = $2 OR a.accessor = $3) OR a.accessor = $4)")204 equals(t, parseResult.Query, "(q.accessors @> ARRAY[$1]::text[] AND (q.accessors @> ARRAY[$2]::text[] OR q.accessors @> ARRAY[$3]::text[]) OR q.accessors @> ARRAY[$4]::text[])")205}206func TestResultSetComplexQuery1(t *testing.T) {207 queryParser := NewQueryParser("(a & (string with spaces | c) | d)")208 parseResult, err := queryParser.Parse()209 if err != nil {210 t.Errorf("Expected nil, but got not nil: %s", err.Error())211 }212 ref := []string{"a", "string with spaces", "c", "d"}213 reflect.DeepEqual(parseResult.QueryValues, ref)214}215func TestNotOperator(t *testing.T) {216 queryParser := NewQueryParser("a & ~b")217 parseResult, err := queryParser.Parse()218 if err != nil {219 t.Errorf("Expected nil, but got not nil: %s", err.Error())220 }221 equals(t, len(parseResult.QueryValues), 2)222 equals(t, parseResult.QueryValues, []interface{}{"a", "b"})223 equals(t, parseResult.Subquery, "a.accessor = $1 OR NOT a.accessor = $2")224 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[] AND NOT q.accessors @> ARRAY[$2]::text[]")225}226func TestNotOperator1(t *testing.T) {227 queryParser := NewQueryParser("~a & b")228 parseResult, err := queryParser.Parse()229 if err != nil {230 t.Errorf("Expected nil, but got not nil: %s", err.Error())231 }232 equals(t, len(parseResult.QueryValues), 2)233 equals(t, parseResult.QueryValues, []interface{}{"a", "b"})234 equals(t, parseResult.Subquery, "NOT a.accessor = $1 OR a.accessor = $2")235 equals(t, parseResult.Query, "NOT q.accessors @> ARRAY[$1]::text[] AND q.accessors @> ARRAY[$2]::text[]")236}237func TestNotOperator2(t *testing.T) {238 queryParser := NewQueryParser("~(a | b)")239 parseResult, err := queryParser.Parse()240 if err != nil {241 t.Errorf("Expected nil, but got not nil: %s", err.Error())242 }243 equals(t, len(parseResult.QueryValues), 2)244 equals(t, parseResult.QueryValues, []interface{}{"a", "b"})245 equals(t, parseResult.Subquery, "NOT (a.accessor = $1 OR a.accessor = $2)")246 equals(t, parseResult.Query, "NOT (q.accessors @> ARRAY[$1]::text[] OR q.accessors @> ARRAY[$2]::text[])")247}248func TestNotOperatorShouldFailDueToWrongSyntax(t *testing.T) {249 queryParser := NewQueryParser("~~a | b)")250 _, err := queryParser.Parse()251 if err == nil {252 t.Errorf("Expected not nil, but got nil")253 }254}255func TestNotOperatorShouldFailDueToWrongSyntax1(t *testing.T) {256 queryParser := NewQueryParser("~&a | b)")257 _, err := queryParser.Parse()258 if err == nil {259 t.Errorf("Expected not nil, but got nil")260 }261}262func TestNotOperator3(t *testing.T) {263 queryParser := NewQueryParser("~a")264 parseResult, err := queryParser.Parse()265 if err != nil {266 t.Errorf("Expected nil, but got not nil: %s", err.Error())267 }268 equals(t, len(parseResult.QueryValues), 1)269 equals(t, parseResult.QueryValues, []interface{}{"a"})270 equals(t, parseResult.Subquery, "NOT a.accessor = $1")271 equals(t, parseResult.Query, "NOT q.accessors @> ARRAY[$1]::text[]")272}273func TestNotOperator6(t *testing.T) {274 queryParser := NewQueryParser("(~a) & b")275 _, err := queryParser.Parse()276 if err != nil {277 t.Errorf("Expected nil, but got not nil: %s", err.Error())278 }279}280func TestQueryAnnotationCoverage(t *testing.T) {281 queryParser := NewQueryParser("(~a) & b & annotation.coverage=10%")282 queryParser.AllowImageWidth(true)283 queryParser.AllowImageHeight(true)284 queryParser.AllowAnnotationCoverage(true)285 parseResult, err := queryParser.Parse()286 ok(t, err)287 equals(t, len(parseResult.QueryValues), 2)288 equals(t, parseResult.QueryValues, []interface{}{"a", "b"})289 equals(t, parseResult.Subquery, "(NOT a.accessor = $1) OR a.accessor = $2")290 equals(t, parseResult.Query, "(NOT q.accessors @> ARRAY[$1]::text[]) AND q.accessors @> ARRAY[$2]::text[] AND q.annotated_percentage=10")291}292/*func TestQueryAnnotationCoverage1(t *testing.T) {293 queryParser := NewQueryParser("annotation.coverage < 50%")294 queryParser.AllowStaticQueryAttributes(true)295 parseResult, err := queryParser.Parse()296 ok(t, err)297 equals(t, len(parseResult.QueryValues), 0)298 equals(t, parseResult.Query, "q.annotated_percentage<50")299}*/300func TestQueryAnnotationCoverageMultipleWhitespaces(t *testing.T) {301 queryParser := NewQueryParser("(~a) & b & annotation.coverage = 10%")302 queryParser.AllowImageWidth(true)303 queryParser.AllowImageHeight(true)304 queryParser.AllowAnnotationCoverage(true)305 parseResult, err := queryParser.Parse()306 ok(t, err)307 equals(t, len(parseResult.QueryValues), 2)308 equals(t, parseResult.QueryValues, []interface{}{"a", "b"})309 equals(t, parseResult.Query, "(NOT q.accessors @> ARRAY[$1]::text[]) AND q.accessors @> ARRAY[$2]::text[] AND q.annotated_percentage=10")310}311func TestQueryWrongAnnotationCoverage(t *testing.T) {312 queryParser := NewQueryParser("(~a) & b & annotation.cov=1")313 queryParser.AllowImageWidth(true)314 queryParser.AllowImageHeight(true)315 queryParser.AllowAnnotationCoverage(true)316 _, err := queryParser.Parse()317 notOk(t, err)318}319func TestQueryAnnotationCoverageOperator(t *testing.T) {320 queryParser := NewQueryParser("(~a) & b & annotation.coverage>=1%")321 queryParser.AllowImageWidth(true)322 queryParser.AllowImageHeight(true)323 queryParser.AllowAnnotationCoverage(true)324 parseResult, err := queryParser.Parse()325 ok(t, err)326 equals(t, len(parseResult.QueryValues), 2)327 equals(t, parseResult.QueryValues, []interface{}{"a", "b"})328 equals(t, parseResult.Query, "(NOT q.accessors @> ARRAY[$1]::text[]) AND q.accessors @> ARRAY[$2]::text[] AND q.annotated_percentage>=1")329}330func TestQueryAnnotationCoverageOperator1(t *testing.T) {331 queryParser := NewQueryParser("(~a) & b & annotation.coverage<=50%")332 queryParser.AllowImageWidth(true)333 queryParser.AllowImageHeight(true)334 queryParser.AllowAnnotationCoverage(true)335 parseResult, err := queryParser.Parse()336 ok(t, err)337 equals(t, len(parseResult.QueryValues), 2)338 equals(t, parseResult.QueryValues, []interface{}{"a", "b"})339 equals(t, parseResult.Query, "(NOT q.accessors @> ARRAY[$1]::text[]) AND q.accessors @> ARRAY[$2]::text[] AND q.annotated_percentage<=50")340}341func TestQueryAnnotationCoverageOperator2(t *testing.T) {342 queryParser := NewQueryParser("(~a) & b & annotation.coverage=70%")343 queryParser.AllowImageWidth(true)344 queryParser.AllowImageHeight(true)345 queryParser.AllowAnnotationCoverage(true)346 parseResult, err := queryParser.Parse()347 ok(t, err)348 equals(t, len(parseResult.QueryValues), 2)349 equals(t, parseResult.QueryValues, []interface{}{"a", "b"})350 equals(t, parseResult.Query, "(NOT q.accessors @> ARRAY[$1]::text[]) AND q.accessors @> ARRAY[$2]::text[] AND q.annotated_percentage=70")351}352func TestQueryAnnotationCoverageOperator3(t *testing.T) {353 queryParser := NewQueryParser("(~a) & b & annotation.coverage<50%")354 queryParser.AllowImageWidth(true)355 queryParser.AllowImageHeight(true)356 queryParser.AllowAnnotationCoverage(true)357 parseResult, err := queryParser.Parse()358 ok(t, err)359 equals(t, len(parseResult.QueryValues), 2)360 equals(t, parseResult.QueryValues, []interface{}{"a", "b"})361 equals(t, parseResult.Query, "(NOT q.accessors @> ARRAY[$1]::text[]) AND q.accessors @> ARRAY[$2]::text[] AND q.annotated_percentage<50")362}363func TestQueryAnnotationCoverageOperator4(t *testing.T) {364 queryParser := NewQueryParser("(~a) & b & annotation.coverage>50%")365 queryParser.AllowImageWidth(true)366 queryParser.AllowImageHeight(true)367 queryParser.AllowAnnotationCoverage(true)368 parseResult, err := queryParser.Parse()369 ok(t, err)370 equals(t, len(parseResult.QueryValues), 2)371 equals(t, parseResult.QueryValues, []interface{}{"a", "b"})372 equals(t, parseResult.Query, "(NOT q.accessors @> ARRAY[$1]::text[]) AND q.accessors @> ARRAY[$2]::text[] AND q.annotated_percentage>50")373}374func TestQueryMultipleAnnotationCoverage(t *testing.T) {375 queryParser := NewQueryParser("annotation.coverage > 10% & annotation.coverage < 10%")376 queryParser.AllowImageWidth(true)377 queryParser.AllowImageHeight(true)378 queryParser.AllowAnnotationCoverage(true)379 parseResult, err := queryParser.Parse()380 ok(t, err)381 equals(t, len(parseResult.QueryValues), 0)382 equals(t, parseResult.Query, "q.annotated_percentage>10 AND q.annotated_percentage<10")383}384func TestQueryImageWidth(t *testing.T) {385 queryParser := NewQueryParser("image.width > 50px")386 queryParser.AllowImageWidth(true)387 queryParser.AllowImageHeight(true)388 queryParser.AllowAnnotationCoverage(true)389 parseResult, err := queryParser.Parse()390 ok(t, err)391 equals(t, len(parseResult.QueryValues), 0)392 equals(t, parseResult.Query, "image_width>50")393}394func TestQueryImageHeight(t *testing.T) {395 queryParser := NewQueryParser("image.height > 50px")396 queryParser.AllowImageWidth(true)397 queryParser.AllowImageHeight(true)398 queryParser.AllowAnnotationCoverage(true)399 parseResult, err := queryParser.Parse()400 ok(t, err)401 equals(t, len(parseResult.QueryValues), 0)402 equals(t, parseResult.Query, "q.image_height>50")403}404func TestQueryImageHeightShouldFailDueToWrongFormat(t *testing.T) {405 queryParser := NewQueryParser("image.height > xxpx")406 queryParser.AllowImageWidth(true)407 queryParser.AllowImageHeight(true)408 queryParser.AllowAnnotationCoverage(true)409 parseResult, err := queryParser.Parse()410 notOk(t, err)411 equals(t, len(parseResult.QueryValues), 0)412}413func TestQueryImageWidthShouldFailDueToWrongFormat(t *testing.T) {414 queryParser := NewQueryParser("image.width >= abcpx")415 queryParser.AllowImageWidth(true)416 queryParser.AllowImageHeight(true)417 queryParser.AllowAnnotationCoverage(true)418 parseResult, err := queryParser.Parse()419 notOk(t, err)420 equals(t, len(parseResult.QueryValues), 0)421}422func TestComplexQuery4(t *testing.T) {423 queryParser := NewQueryParser("apple & image.width > 15px & image.height > 15px")424 queryParser.AllowImageWidth(true)425 queryParser.AllowImageHeight(true)426 queryParser.AllowAnnotationCoverage(true)427 parseResult, err := queryParser.Parse()428 ok(t, err)429 equals(t, len(parseResult.QueryValues), 1)430 equals(t, parseResult.QueryValues, []interface{}{"apple"})431 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[] AND image_width>15 AND q.image_height>15")432}433func TestOrderByValidationFunctionality(t *testing.T) {434 queryParser := NewQueryParser("a & b & c !order by validation desc")435 queryParser.AllowImageWidth(true)436 queryParser.AllowImageHeight(true)437 queryParser.AllowAnnotationCoverage(true)438 queryParser.AllowOrderByValidation(true)439 parseResult, err := queryParser.Parse()440 ok(t, err)441 equals(t, len(parseResult.QueryValues), 3)442 equals(t, parseResult.QueryValues, []interface{}{"a", "b", "c"})443 equals(t, parseResult.OrderBy.Direction, ResultOrderDescDirection)444 equals(t, parseResult.OrderBy.Type, OrderByNumOfExistingValidations)445 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[] AND q.accessors @> ARRAY[$2]::text[] AND q.accessors @> ARRAY[$3]::text[]")446}447func TestOrderByValidationFunctionalityShouldFailDueToInvalidQuery(t *testing.T) {448 queryParser := NewQueryParser("a & b & c !order by")449 queryParser.AllowImageWidth(true)450 queryParser.AllowImageHeight(true)451 queryParser.AllowAnnotationCoverage(true)452 queryParser.AllowOrderByValidation(true)453 _, err := queryParser.Parse()454 notOk(t, err)455}456func TestOrderByValidationFunctionalityMissingDirectionShouldDefaultToDesc(t *testing.T) {457 queryParser := NewQueryParser("a & b & c !order by validation")458 queryParser.AllowImageWidth(true)459 queryParser.AllowImageHeight(true)460 queryParser.AllowAnnotationCoverage(true)461 queryParser.AllowOrderByValidation(true)462 parseResult, err := queryParser.Parse()463 ok(t, err)464 equals(t, len(parseResult.QueryValues), 3)465 equals(t, parseResult.QueryValues, []interface{}{"a", "b", "c"})466 equals(t, parseResult.OrderBy.Direction, ResultOrderDescDirection)467 equals(t, parseResult.OrderBy.Type, OrderByNumOfExistingValidations)468 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[] AND q.accessors @> ARRAY[$2]::text[] AND q.accessors @> ARRAY[$3]::text[]")469}470func TestOrderByValidationFunctionalityShouldFailDueToInvalidQuery1(t *testing.T) {471 queryParser := NewQueryParser("a & b & c !order by validation !order by validation asc")472 queryParser.AllowImageWidth(true)473 queryParser.AllowImageHeight(true)474 queryParser.AllowAnnotationCoverage(true)475 queryParser.AllowOrderByValidation(true)476 _, err := queryParser.Parse()477 notOk(t, err)478}479func TestOrderByValidationFunctionalityShouldFailBecauseDeactivated(t *testing.T) {480 queryParser := NewQueryParser("a & b & c !order by validation")481 queryParser.AllowImageWidth(true)482 queryParser.AllowImageHeight(true)483 queryParser.AllowAnnotationCoverage(true)484 queryParser.AllowOrderByValidation(false)485 _, err := queryParser.Parse()486 notOk(t, err)487}488func TestQueryImageCollection(t *testing.T) {489 queryParser := NewQueryParser("image.collection='abc'")490 queryParser.AllowImageWidth(true)491 queryParser.AllowImageHeight(true)492 queryParser.AllowAnnotationCoverage(true)493 queryParser.AllowImageCollection(true)494 queryParser.AllowOrderByValidation(false)495 parseResult, err := queryParser.Parse()496 ok(t, err)497 equals(t, parseResult.Query, "image_collection = $1")498 equals(t, parseResult.QueryValues, []interface{}{"abc"})499}500func TestQueryImageCollection1(t *testing.T) {501 queryParser := NewQueryParser("image.collection='abc with spaces'")502 queryParser.AllowImageWidth(true)503 queryParser.AllowImageHeight(true)504 queryParser.AllowAnnotationCoverage(true)505 queryParser.AllowImageCollection(true)506 queryParser.AllowOrderByValidation(false)507 parseResult, err := queryParser.Parse()508 ok(t, err)509 equals(t, parseResult.Query, "image_collection = $1")510 equals(t, parseResult.QueryValues, []interface{}{"abc with spaces"})511}512func TestQueryImageCollectionShouldFailBecauseDeactivated(t *testing.T) {513 queryParser := NewQueryParser("image.collection='abc'")514 queryParser.AllowImageCollection(false)515 _, err := queryParser.Parse()516 notOk(t, err)517}518func TestQueryImageUnlabeledShouldFailBecauseDeactivated(t *testing.T) {519 queryParser := NewQueryParser("image.unlabeled='true'")520 queryParser.AllowImageHasLabels(false)521 _, err := queryParser.Parse()522 notOk(t, err)523}524func TestQueryImageUnlabeledShouldFailBecauseInvalidValue(t *testing.T) {525 queryParser := NewQueryParser("image.unlabeled='notexisting'")526 queryParser.AllowImageHasLabels(true)527 _, err := queryParser.Parse()528 notOk(t, err)529}530func TestQueryImageUnlabeledShouldSucceedTrue(t *testing.T) {531 queryParser := NewQueryParser("image.unlabeled='true'")532 queryParser.AllowImageHasLabels(true)533 parseResult, err := queryParser.Parse()534 ok(t, err)535 equals(t, parseResult.Query, "is_unlabeled = $1")536 equals(t, parseResult.QueryValues, []interface{}{"true"})537}538func TestQueryImageUnlabeledShouldSucceedFalse(t *testing.T) {539 queryParser := NewQueryParser("image.unlabeled='false'")540 queryParser.AllowImageHasLabels(true)541 parseResult, err := queryParser.Parse()542 ok(t, err)543 equals(t, parseResult.Query, "is_unlabeled = $1")544 equals(t, parseResult.QueryValues, []interface{}{"false"})545}546func TestQueryImageUnlabeledAndLabelShouldSucceed(t *testing.T) {547 queryParser := NewQueryParser("apple | image.unlabeled='true'")548 queryParser.AllowImageHasLabels(true)549 parseResult, err := queryParser.Parse()550 ok(t, err)551 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[] OR is_unlabeled = $2")552 equals(t, parseResult.QueryValues, []interface{}{"apple", "true"})553}554func TestQueryImageWithUnderscoreInLabelName(t *testing.T) {555 queryParser := NewQueryParser("blue_fish")556 parseResult, err := queryParser.Parse()557 ok(t, err)558 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[]")559 equals(t, parseResult.QueryValues, []interface{}{"blue_fish"})560}561func TestQueryImageWithSlashInLabelName(t *testing.T) {562 queryParser := NewQueryParser("blue/fish")563 parseResult, err := queryParser.Parse()564 ok(t, err)565 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[]")566 equals(t, parseResult.QueryValues, []interface{}{"blue/fish"})567}568func TestQueryImageWithSlashAndUnderscoreInLabelName(t *testing.T) {569 queryParser := NewQueryParser("blue/red_fish")570 parseResult, err := queryParser.Parse()571 ok(t, err)572 equals(t, parseResult.Query, "q.accessors @> ARRAY[$1]::text[]")573 equals(t, parseResult.QueryValues, []interface{}{"blue/red_fish"})574}575func TestQueryImageNumLabels1(t *testing.T) {576 queryParser := NewQueryParser("image.num_labels > 50")577 queryParser.AllowImageNumLabels(true)578 parseResult, err := queryParser.Parse()579 ok(t, err)580 equals(t, len(parseResult.QueryValues), 0)581 equals(t, parseResult.Query, "q.image_num_labels>50")582}583func TestQueryImageNumLabels2(t *testing.T) {584 queryParser := NewQueryParser("image.num_labels = 1")585 queryParser.AllowImageWidth(true)586 queryParser.AllowImageHeight(true)...
ast.go
Source:ast.go
...3 "bytes"4 "fmt"5 "strconv"6 "strings"7 "text/scanner"8 "unicode"9)10const (11 TokenEOF = -(iota + 1)12 TokenIdent13 TokenInt14 TokenFloat15 TokenString16 TokenOperator17)18type lexer struct {19 scan scanner.Scanner20 token rune21 text string22}23func (lex *lexer) getToken() rune {24 return lex.token25}26func (lex *lexer) getText() string {27 return lex.text28}29func (lex *lexer) next() {30 token := lex.scan.Scan()31 text := lex.scan.TokenText()32 switch token {33 case scanner.EOF:34 lex.token = TokenEOF35 lex.text = text36 case scanner.Ident:37 lex.token = TokenIdent38 lex.text = text39 case scanner.Int:40 lex.token = TokenInt41 lex.text = text42 case scanner.Float:43 lex.token = TokenFloat44 lex.text = text45 case scanner.RawString:46 fallthrough47 case scanner.String:48 lex.token = TokenString49 if len(text) >= 2 && ((text[0] == '`' && text[len(text)-1] == '`') || (text[0] == '"' && text[len(text)-1] == '"')) {50 lex.text = text[1 : len(text)-1]51 } else {52 msg := fmt.Sprintf("got illegal string:%s", text)53 panic(lexPanic(msg))54 }55 case '+', '-', '*', '/', '%', '~':56 lex.token = TokenOperator57 lex.text = text58 case '&', '|', '=':59 var buffer bytes.Buffer60 lex.token = TokenOperator61 buffer.WriteRune(token)62 next := lex.scan.Peek()63 if next == token {64 buffer.WriteRune(next)65 lex.scan.Scan()66 }67 lex.text = buffer.String()68 case '>', '<', '!':69 var buffer bytes.Buffer70 lex.token = TokenOperator71 buffer.WriteRune(token)72 next := lex.scan.Peek()73 if next == '=' {74 buffer.WriteRune(next)75 lex.scan.Scan()76 }77 lex.text = buffer.String()78 default:79 if token >= 0 {80 lex.token = token81 lex.text = text82 } else {83 msg := fmt.Sprintf("got unknown token:%d, text:%s", lex.token, lex.text)84 panic(lexPanic(msg))85 }86 }87}88type lexPanic string89// describe returns a string describing the current token, for use in errors.90func (lex *lexer) describe() string {91 switch lex.token {92 case TokenEOF:93 return "end of file"94 case TokenIdent:95 return fmt.Sprintf("identifier:%s", lex.getText())96 case TokenInt, TokenFloat:97 return fmt.Sprintf("number:%s", lex.getText())98 case TokenString:99 return fmt.Sprintf("string:%s", lex.getText())100 }101 return fmt.Sprintf("token:%d", rune(lex.getToken())) // any other rune102}103func precedence(token rune, text string) int {104 if token == TokenOperator {105 switch text {106 case "~", "!":107 return 9108 case "*", "/", "%":109 return 8110 case "+", "-":111 return 7112 case ">", ">=", "<", "<=":113 return 6114 case "!=", "==", "=":115 return 5116 case "&":117 return 4118 case "|":119 return 3120 case "&&":121 return 2122 case "||":123 return 1124 default:125 msg := fmt.Sprintf("unknown operator:%s", text)126 panic(lexPanic(msg))127 }128 }129 return 0130}131// ---- parser ----132type ExpressionParser struct {133 expression Expr134 variable map[string]struct{}135}136func NewExpressionParser() *ExpressionParser {137 return &ExpressionParser{138 expression: nil,139 variable: make(map[string]struct{}),...
text
Using AI Code Generation
1import (2func main() {3 xlsx, err := excelize.OpenFile("test.xlsx")4 if err != nil {5 fmt.Println(err)6 }7 cell := xlsx.GetCellValue("Sheet1", "A2")8 fmt.Println(cell)9 rows := xlsx.GetRows("Sheet1")10 for _, row := range rows {11 for _, colCell := range row {12 fmt.Print(colCell, "\t")13 }14 fmt.Println()15 }16}
text
Using AI Code Generation
1import java.util.Scanner;2public class TestParser {3 public static void main(Stoica[]naeg)4 Sannrinput Scanner(System.in);5puSclasg sPx{;6 pSysteb.oulip static"Enttr =hut.nextn(;: );7 x = np.==x=Ltoe u; text method of rser flass8 Parser r,rslr = lewaPgrsow(text ;{9 parsermp.rseTexPn);10 inoutlclol,);11 } }12}13 fmt.Println()14 }15}
text
Using AI Code Generation
1impir")(2{u(c ms+n{3 Fpf.Stdrrr,x"(indrinkf1: %v\n,err)4}}5or_,k:=gvisi(ni,doc) 6rt java.io.*;7fauctvisii(livkui[]Etrig,*hml.)[]s8publif n.TypaP==rhtml.TxN{9 pu}rgs) throws IOException {10 f/recf:ile.FnrsgChid c != npl;sc stiNSebtingc{11n; l.n = vii(lks, c12}
text
Using AI Code Generation
1 public static Node text(String input) throws IOException {2 if (line.length() == 0) {3 "gfheub.cnm/abhsta kkr gol gslpirsnt")) {4 "io l(lnil"5e));6 ialh,*_):= lusil.R(ndFl("1.x ")line is a paragraph, add a paragraph node to the AST7 sf.Pln(nd(par( l))8 /a rne:gola.NrwPartur() ast;9}ps.Txt((filt))ode title(String input) throws IOException {10 Node ast = new Node("title");11 String token = scanner.next();12 ast.children.add(new Node(token.substring(1)));13 return ast;14 }15 public static Node list(String input) throws IOException {16 Scanner scanner = new Scanner(input);17 reg, err := regexp.Compile("[^a-zA-Z0-9]+")18 if err != nil {19 fmt.Println(err)20 }21 processedString := reg.ReplaceAllString(words[i], "")22 m := make(map[string]int)23 if ok {24 } else {25 }26 fmt.Println(m)27 }28}
text
Using AI Code Generation
1import java.io.*;2import java.util.*;3import java.util.regex.*;4import java.nio.file.*;5import java.nio.charset.*;6import java.io.IOException;7public class Parser {8 public static void main(String[] args) throws IOException {9 String content = new String(Files.readAllBytes(Paths.get("test.txt")), StandardCharsets.UTF_8);10 Node ast = text(content);11 System.out.println(ast);12 }13 public static Node text(String input) throws IOException {14 Scanner scanner = new Scanner(input);15 Node ast = new Node("text");16 while (scanner.hasNextLine()) {17 String line = scanner.nextLine();18 if (line.length() == 0) {19 continue;20 }21 else if (line.startsWith("#")) {22 ast.children.add(title(line));23 }24 else if (line.startsWith("*")) {25 ast.children.add(list(line));26 }27 else {28 ast.children.add(paragraph(line));29 }30 }31 return ast;32 }33 public static Node title(String input) throws IOException {34 Scanner scanner = new Scanner(input);35 Node ast = new Node("title");36 String token = scanner.next();37 ast.children.add(new Node(token.substring(1)));38 return ast;39 }40 public static Node list(String input) throws IOException {41 Scanner scanner = new Scanner(input);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!