grep tests: create a helper function for "BRE" or "ERE"

Refactor the repeated test code for finding out whether a given set of configuration will pick basic, extended or fixed into a new "test_pattern_type" helper function. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Feb 16, 2022 at 01:00 UTC ccb1fccc21331122ad1c7c247122cb7c48787361
1 file changed +54 -80
t/t7810-grep.sh
+54 -80
@@ -98,6 +98,37 @@ test_expect_success 'grep should not segfault with a bad input' '
98
99 test_invalid_grep_expression --and -e A
100
101 +test_pattern_type () {
102 + H=$1 &&
103 + HC=$2 &&
104 + L=$3 &&
105 + type=$4 &&
106 + shift 4 &&
107 +
108 + expected_str= &&
109 + case "$type" in
110 + BRE)
111 + expected_str="${HC}ab:a+bc"
112 + ;;
113 + ERE)
114 + expected_str="${HC}ab:abc"
115 + ;;
116 + FIX)
117 + expected_str="${HC}ab:a+b*c"
118 + ;;
119 + *)
120 + BUG "unknown pattern type '$type'"
121 + ;;
122 + esac &&
123 + config_str="$@" &&
124 +
125 + test_expect_success "grep $L with '$config_str' interpreted as $type" '
126 + echo $expected_str >expected &&
127 + git $config_str grep "a+b*c" $H ab >actual &&
128 + test_cmp expected actual
129 + '
130 +}
131 +
132 for H in HEAD ''
133 do
134 case "$H" in
@@ -393,35 +424,13 @@ do
424 git grep --no-recursive -n -e vvv $H -- t . >actual &&
425 test_cmp expected actual
426 '
396 - test_expect_success "grep $L with grep.extendedRegexp=false" '
397 - echo "${HC}ab:a+bc" >expected &&
398 - git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
399 - test_cmp expected actual
400 - '
427
402 - test_expect_success "grep $L with grep.extendedRegexp=true" '
403 - echo "${HC}ab:abc" >expected &&
404 - git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
405 - test_cmp expected actual
406 - '
428
408 - test_expect_success "grep $L with grep.patterntype=basic" '
409 - echo "${HC}ab:a+bc" >expected &&
410 - git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
411 - test_cmp expected actual
412 - '
413 -
414 - test_expect_success "grep $L with grep.patterntype=extended" '
415 - echo "${HC}ab:abc" >expected &&
416 - git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
417 - test_cmp expected actual
418 - '
419 -
420 - test_expect_success "grep $L with grep.patterntype=fixed" '
421 - echo "${HC}ab:a+b*c" >expected &&
422 - git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
423 - test_cmp expected actual
424 - '
429 + test_pattern_type "$H" "$HC" "$L" BRE -c grep.extendedRegexp=false
430 + test_pattern_type "$H" "$HC" "$L" ERE -c grep.extendedRegexp=true
431 + test_pattern_type "$H" "$HC" "$L" BRE -c grep.patternType=basic
432 + test_pattern_type "$H" "$HC" "$L" ERE -c grep.patternType=extended
433 + test_pattern_type "$H" "$HC" "$L" FIX -c grep.patternType=fixed
434
435 test_expect_success PCRE "grep $L with grep.patterntype=perl" '
436 echo "${HC}ab:a+b*c" >expected &&
@@ -433,59 +442,24 @@ do
442 test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
443 '
444
436 - test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
437 - echo "${HC}ab:abc" >expected &&
438 - git \
439 - -c grep.patternType=default \
440 - -c grep.extendedRegexp=true \
441 - grep "a+b*c" $H ab >actual &&
442 - test_cmp expected actual
443 - '
444 -
445 - test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
446 - echo "${HC}ab:abc" >expected &&
447 - git \
448 - -c grep.extendedRegexp=true \
449 - -c grep.patternType=default \
450 - grep "a+b*c" $H ab >actual &&
451 - test_cmp expected actual
452 - '
453 -
454 - test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
455 - echo "${HC}ab:abc" >expected &&
456 - git \
457 - -c grep.patternType=extended \
458 - -c grep.extendedRegexp=false \
459 - grep "a+b*c" $H ab >actual &&
460 - test_cmp expected actual
461 - '
462 -
463 - test_expect_success "grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
464 - echo "${HC}ab:a+bc" >expected &&
465 - git \
466 - -c grep.patternType=basic \
467 - -c grep.extendedRegexp=true \
468 - grep "a+b*c" $H ab >actual &&
469 - test_cmp expected actual
470 - '
471 -
472 - test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
473 - echo "${HC}ab:abc" >expected &&
474 - git \
475 - -c grep.extendedRegexp=false \
476 - -c grep.patternType=extended \
477 - grep "a+b*c" $H ab >actual &&
478 - test_cmp expected actual
479 - '
480 -
481 - test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
482 - echo "${HC}ab:a+bc" >expected &&
483 - git \
484 - -c grep.extendedRegexp=true \
485 - -c grep.patternType=basic \
486 - grep "a+b*c" $H ab >actual &&
487 - test_cmp expected actual
488 - '
445 + test_pattern_type "$H" "$HC" "$L" ERE \
446 + -c grep.patternType=default \
447 + -c grep.extendedRegexp=true
448 + test_pattern_type "$H" "$HC" "$L" ERE \
449 + -c grep.extendedRegexp=true \
450 + -c grep.patternType=default
451 + test_pattern_type "$H" "$HC" "$L" ERE \
452 + -c grep.patternType=extended \
453 + -c grep.extendedRegexp=false
454 + test_pattern_type "$H" "$HC" "$L" BRE \
455 + -c grep.patternType=basic \
456 + -c grep.extendedRegexp=true
457 + test_pattern_type "$H" "$HC" "$L" ERE \
458 + -c grep.extendedRegexp=false \
459 + -c grep.patternType=extended
460 + test_pattern_type "$H" "$HC" "$L" BRE \
461 + -c grep.extendedRegexp=true \
462 + -c grep.patternType=basic
463
464 test_expect_success "grep --count $L" '
465 echo ${HC}ab:3 >expected &&