Skip to content

Commit 905bf43

Browse files
committed
Fix bad indentation calculation in the console reporter
The problem came from the console reporter trying to provide a fancy linebreaking (primarily for things like `SCENARIO` or the BDD macros), so that new lines start with extra indentation if the text being line broken starts as "{text}: ". The console reporter did not properly take into account cases where the ": " part would already be in a later line, in which case it would ask for non-sensical level of indentation (larger than single line length). We fixed this by also enforcing that the special indentation case only triggers if the ": " is found early enough in the line, so that we also avoid degenerate cases like this: ``` blablabla: F a n c y . . . ``` Fixes #2309
1 parent 0fdee1c commit 905bf43

11 files changed

Lines changed: 63 additions & 16 deletions

src/catch2/reporters/catch_reporter_console.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -559,15 +559,37 @@ void ConsoleReporter::printOpenHeader(std::string const& _name) {
559559
}
560560
}
561561

562-
// if string has a : in first line will set indent to follow it on
563-
// subsequent lines
564562
void ConsoleReporter::printHeaderString(std::string const& _string, std::size_t indent) {
565-
std::size_t i = _string.find(": ");
566-
if (i != std::string::npos)
567-
i += 2;
568-
else
569-
i = 0;
570-
stream << TextFlow::Column(_string).indent(indent + i).initialIndent(indent) << '\n';
563+
// We want to get a bit fancy with line breaking here, so that subsequent
564+
// lines start after ":" if one is present, e.g.
565+
// ```
566+
// blablabla: Fancy
567+
// linebreaking
568+
// ```
569+
// but we also want to avoid problems with overly long indentation causing
570+
// the text to take up too many lines, e.g.
571+
// ```
572+
// blablabla: F
573+
// a
574+
// n
575+
// c
576+
// y
577+
// .
578+
// .
579+
// .
580+
// ```
581+
// So we limit the prefix indentation check to first quarter of the possible
582+
// width
583+
std::size_t idx = _string.find( ": " );
584+
if ( idx != std::string::npos && idx < CATCH_CONFIG_CONSOLE_WIDTH / 4 ) {
585+
idx += 2;
586+
} else {
587+
idx = 0;
588+
}
589+
stream << TextFlow::Column( _string )
590+
.indent( indent + idx )
591+
.initialIndent( indent )
592+
<< '\n';
571593
}
572594

573595
struct SummaryColumn {

tests/SelfTest/Baselines/automake.sw.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Nor would this
191191
:test-result: FAIL Regex string matcher
192192
:test-result: PASS Regression test #1
193193
:test-result: PASS Reporter's write listings to provided stream
194+
:test-result: PASS Reproducer for #2309 - a very long description past 80 chars (default console width) with a late colon : blablabla
194195
:test-result: PASS SUCCEED counts as a test pass
195196
:test-result: PASS SUCCEED does not require an argument
196197
:test-result: PASS Scenario: BDD tests requiring Fixtures to provide commonly-accessed data or methods

tests/SelfTest/Baselines/compact.sw.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,7 @@ Reporters.tests.cpp:<line number>: passed: listingString, ContainsSubstring( "fa
14491449
</SourceInfo>
14501450
</TestCase>
14511451
</MatchingTests>" ( contains: "fake test name" and contains: "fakeTestTag" ) with 1 message: 'Tested reporter: xml'
1452+
Reporters.tests.cpp:<line number>: passed:
14521453
Message.tests.cpp:<line number>: passed: with 1 message: 'this is a success'
14531454
Message.tests.cpp:<line number>: passed:
14541455
BDD.tests.cpp:<line number>: passed: before == 0 for: 0 == 0

tests/SelfTest/Baselines/console.std.approved.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,6 @@ due to unexpected exception with message:
14261426
Why would you throw a std::string?
14271427

14281428
===============================================================================
1429-
test cases: 372 | 295 passed | 70 failed | 7 failed as expected
1430-
assertions: 2114 | 1958 passed | 129 failed | 27 failed as expected
1429+
test cases: 373 | 296 passed | 70 failed | 7 failed as expected
1430+
assertions: 2115 | 1959 passed | 129 failed | 27 failed as expected
14311431

tests/SelfTest/Baselines/console.sw.approved.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10231,6 +10231,15 @@ with expansion:
1023110231
with message:
1023210232
Tested reporter: xml
1023310233

10234+
-------------------------------------------------------------------------------
10235+
Reproducer for #2309 - a very long description past 80 chars (default console
10236+
width) with a late colon : blablabla
10237+
-------------------------------------------------------------------------------
10238+
Reporters.tests.cpp:<line number>
10239+
...............................................................................
10240+
10241+
Reporters.tests.cpp:<line number>: PASSED:
10242+
1023410243
-------------------------------------------------------------------------------
1023510244
SUCCEED counts as a test pass
1023610245
-------------------------------------------------------------------------------
@@ -17030,6 +17039,6 @@ Misc.tests.cpp:<line number>
1703017039
Misc.tests.cpp:<line number>: PASSED:
1703117040

1703217041
===============================================================================
17033-
test cases: 372 | 279 passed | 86 failed | 7 failed as expected
17034-
assertions: 2131 | 1958 passed | 146 failed | 27 failed as expected
17042+
test cases: 373 | 280 passed | 86 failed | 7 failed as expected
17043+
assertions: 2132 | 1959 passed | 146 failed | 27 failed as expected
1703517044

tests/SelfTest/Baselines/junit.sw.approved.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<testsuitesloose text artifact
33
>
4-
<testsuite name="<exe-name>" errors="17" failures="129" tests="2131" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
4+
<testsuite name="<exe-name>" errors="17" failures="129" tests="2132" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
55
<properties>
66
<property name="random-seed" value="1"/>
77
<property name="filters" value="~[!nonportable]~[!benchmark]~[approvals] *"/>
@@ -1163,6 +1163,7 @@ Matchers.tests.cpp:<line number>
11631163
<testcase classname="<exe-name>.global" name="Reporter's write listings to provided stream/xml reporter lists tags" time="{duration}" status="run"/>
11641164
<testcase classname="<exe-name>.global" name="Reporter's write listings to provided stream/xml reporter lists reporters" time="{duration}" status="run"/>
11651165
<testcase classname="<exe-name>.global" name="Reporter's write listings to provided stream/xml reporter lists tests" time="{duration}" status="run"/>
1166+
<testcase classname="<exe-name>.global" name="Reproducer for #2309 - a very long description past 80 chars (default console width) with a late colon : blablabla" time="{duration}" status="run"/>
11661167
<testcase classname="<exe-name>.global" name="SUCCEED counts as a test pass" time="{duration}" status="run"/>
11671168
<testcase classname="<exe-name>.global" name="SUCCEED does not require an argument" time="{duration}" status="run"/>
11681169
<testcase classname="<exe-name>.Fixture" name="Scenario: BDD tests requiring Fixtures to provide commonly-accessed data or methods/Given: No operations precede me" time="{duration}" status="run"/>

tests/SelfTest/Baselines/sonarqube.sw.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
<testCase name="Reporter's write listings to provided stream/xml reporter lists tags" duration="{duration}"/>
182182
<testCase name="Reporter's write listings to provided stream/xml reporter lists reporters" duration="{duration}"/>
183183
<testCase name="Reporter's write listings to provided stream/xml reporter lists tests" duration="{duration}"/>
184+
<testCase name="Reproducer for #2309 - a very long description past 80 chars (default console width) with a late colon : blablabla" duration="{duration}"/>
184185
<testCase name="The default listing implementation write to provided stream/Listing tags" duration="{duration}"/>
185186
<testCase name="The default listing implementation write to provided stream/Listing reporters" duration="{duration}"/>
186187
<testCase name="The default listing implementation write to provided stream/Listing tests" duration="{duration}"/>

tests/SelfTest/Baselines/tap.sw.approved.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,8 @@ ok {test-number} - listingString, ContainsSubstring("fake reporter"s) for: "<?xm
25902590
ok {test-number} - !(factories.empty()) for: !false
25912591
# Reporter's write listings to provided stream
25922592
ok {test-number} - listingString, ContainsSubstring( "fake test name"s ) && ContainsSubstring( "fakeTestTag"s ) for: "<?xml version="1.0" encoding="UTF-8"?> <MatchingTests> <TestCase> <Name>fake test name</Name> <ClassName/> <Tags>[fakeTestTag]</Tags> <SourceInfo> <File>fake-file.cpp</File> <Line>123456789</Line> </SourceInfo> </TestCase> </MatchingTests>" ( contains: "fake test name" and contains: "fakeTestTag" ) with 1 message: 'Tested reporter: xml'
2593+
# Reproducer for #2309 - a very long description past 80 chars (default console width) with a late colon : blablabla
2594+
ok {test-number} -
25932595
# SUCCEED counts as a test pass
25942596
ok {test-number} - with 1 message: 'this is a success'
25952597
# SUCCEED does not require an argument
@@ -4264,5 +4266,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0
42644266
ok {test-number} -
42654267
# xmlentitycheck
42664268
ok {test-number} -
4267-
1..2131
4269+
1..2132
42684270

tests/SelfTest/Baselines/teamcity.sw.approved.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ Matchers.tests.cpp:<line number>|nexpression failed|n CHECK_THAT( testStringFor
491491
##teamcity[testFinished name='Regression test #1' duration="{duration}"]
492492
##teamcity[testStarted name='Reporter|'s write listings to provided stream']
493493
##teamcity[testFinished name='Reporter|'s write listings to provided stream' duration="{duration}"]
494+
##teamcity[testStarted name='Reproducer for #2309 - a very long description past 80 chars (default console width) with a late colon : blablabla']
495+
##teamcity[testFinished name='Reproducer for #2309 - a very long description past 80 chars (default console width) with a late colon : blablabla' duration="{duration}"]
494496
##teamcity[testStarted name='SUCCEED counts as a test pass']
495497
##teamcity[testFinished name='SUCCEED counts as a test pass' duration="{duration}"]
496498
##teamcity[testStarted name='SUCCEED does not require an argument']

tests/SelfTest/Baselines/xml.sw.approved.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12248,6 +12248,9 @@ All available test cases:
1224812248
</Section>
1224912249
<OverallResult success="true"/>
1225012250
</TestCase>
12251+
<TestCase name="Reproducer for #2309 - a very long description past 80 chars (default console width) with a late colon : blablabla" tags="[console-reporter]" filename="tests/<exe-name>/IntrospectiveTests/Reporters.tests.cpp" >
12252+
<OverallResult success="true"/>
12253+
</TestCase>
1225112254
<TestCase name="SUCCEED counts as a test pass" tags="[messages]" filename="tests/<exe-name>/UsageTests/Message.tests.cpp" >
1225212255
<OverallResult success="true"/>
1225312256
</TestCase>
@@ -20027,6 +20030,6 @@ loose text artifact
2002720030
</Section>
2002820031
<OverallResult success="true"/>
2002920032
</TestCase>
20030-
<OverallResults successes="1958" failures="146" expectedFailures="27"/>
20031-
<OverallResultsCases successes="279" failures="86" expectedFailures="7"/>
20033+
<OverallResults successes="1959" failures="146" expectedFailures="27"/>
20034+
<OverallResultsCases successes="280" failures="86" expectedFailures="7"/>
2003220035
</Catch2TestRun>

0 commit comments

Comments
 (0)