Skip to content

Commit

Permalink
fix gtest reindent
Browse files Browse the repository at this point in the history
  • Loading branch information
matepek committed Oct 19, 2024
1 parent b009d92 commit f581bbc
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 50 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

## [4.12.1]

### Fixed

- google test indentation [issue](https://github.com/matepek/vscode-catch2-test-adapter/issues/437)

## [4.12.0] - 2024-03-29

### Added
Expand Down
50 changes: 30 additions & 20 deletions src/TestResultBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export class TestResultBuilder<T extends AbstractTest = AbstractTest> {
if (this.addBeginEndMsg) {
const locStr = this.getLocationAtStr(this.test.file, this.test.line, true);
if (this.level === 0) {
this.addOutputLine(0, ansi.bold(`[ RUN ] \`${ansi.italic(this.test.label)}\``) + `${locStr}`);
this.addReindentedOutput(0, ansi.bold(`[ RUN ] \`${ansi.italic(this.test.label)}\``) + `${locStr}`);
} else {
this.addOutputLine(-1, ansi.dim('├') + '`' + ansi.italic(this.test.label) + '`' + locStr);
this.addReindentedOutput(-1, ansi.dim('├') + '`' + ansi.italic(this.test.label) + '`' + locStr);
}
}
}
Expand Down Expand Up @@ -63,12 +63,23 @@ export class TestResultBuilder<T extends AbstractTest = AbstractTest> {
}

failedByTimeout(timeoutMilisec: number): void {
this.addOutputLine(1, '⌛️ Timed out: "testMate.cpp.test.runtimeLimit": ' + timeoutMilisec / 1000 + ' second(s).');
this.addReindentedOutput(
1,
'⌛️ Timed out: "testMate.cpp.test.runtimeLimit": ' + timeoutMilisec / 1000 + ' second(s).',
);
this.failed();
}

addOutputLine(indent: number, ...msgs: string[]): void {
const lines = indentStr(this.level + indent, ...msgs);
addReindentedOutput(indent: number, ...msgs: string[]): void {
const lines = formatStr(this.level + indent, true, ...msgs);

//this._outputLines.push(...lines);

this.testRun.appendOutput(lines.map(x => this.runPrefix + x + '\r\n').join(''));
}

addOutput(indent: number, ...msgs: string[]): void {
const lines = formatStr(this.level + indent, false, ...msgs);

//this._outputLines.push(...lines);

Expand Down Expand Up @@ -128,9 +139,9 @@ export class TestResultBuilder<T extends AbstractTest = AbstractTest> {
this.addMessage(file, line, 'Expanded: `' + expanded + '`');

const loc = this.getLocationAtStr(file, line, false);
this.addOutputLine(1, 'Expression ' + ansi.red('failed') + loc + ':');
this.addOutputLine(2, '❕Original: ' + original);
this.addOutputLine(2, '❗️Expanded: ' + expanded);
this.addReindentedOutput(1, 'Expression ' + ansi.red('failed') + loc + ':');
this.addReindentedOutput(2, '❕Original: ' + original);
this.addReindentedOutput(2, '❗️Expanded: ' + expanded);
}

addMessageWithOutput(
Expand All @@ -142,8 +153,8 @@ export class TestResultBuilder<T extends AbstractTest = AbstractTest> {
file = this.test.exec.findSourceFilePath(file);
this.addMessage(file, line, [`${title}`, ...message].join('\r\n'));
const loc = this.getLocationAtStr(file, line, false);
this.addOutputLine(1, `${title}${loc}`);
this.addOutputLine(2, ...message);
this.addReindentedOutput(1, `${title}${loc}`);
this.addReindentedOutput(2, ...message);
}

addMessage(file: string | undefined, line: number | string | undefined, ...message: string[]): void {
Expand All @@ -168,8 +179,8 @@ export class TestResultBuilder<T extends AbstractTest = AbstractTest> {
): void {
file = this.test.exec.findSourceFilePath(file);
const loc = this.getLocationAtStr(file, line, false);
this.addOutputLine(1, `${title}${loc}${message.length ? ':' : ''}`);
this.addOutputLine(2, ...message);
this.addReindentedOutput(1, `${title}${loc}${message.length ? ':' : ''}`);
this.addReindentedOutput(2, ...message);
}

///
Expand All @@ -194,7 +205,7 @@ export class TestResultBuilder<T extends AbstractTest = AbstractTest> {
const d = this._duration ? ansi.dim(` in ${Math.round(this._duration * 1000) / 1000000} second(s)`) : '';

if (this.level === 0) {
this.addOutputLine(0, `${this.coloredResult()} \`${ansi.italic(this.test.label)}\`` + `${d}`, '');
this.addReindentedOutput(0, `${this.coloredResult()} \`${ansi.italic(this.test.label)}\`` + `${d}`, '');
}
}
}
Expand Down Expand Up @@ -270,8 +281,12 @@ export class TestResultBuilder<T extends AbstractTest = AbstractTest> {

const indentPrefix = (level: number) => ansi.dim('│ '.repeat(level));

const indentStr = (indent: number, ...strs: string[]) => {
return reindentStr(...strs).map(l => indentPrefix(indent) + l);
const formatStr = (indent_lvl: number, reindent: boolean, ...strs: string[]) => {
strs = strs.flatMap(x => x.split(/\r?\n/));
if (reindent) {
strs = reindentLines(strs);
}
return strs.map(l => indentPrefix(indent_lvl) + l);
};

const reindentLines = (lines: string[]): string[] => {
Expand All @@ -284,8 +299,3 @@ const reindentLines = (lines: string[]): string[] => {
const reindented = lines.map(l => l.substring(indent).trimEnd());
return reindented;
};

const reindentStr = (...strs: string[]): string[] => {
const lines = strs.flatMap(x => x.split(/\r?\n/));
return reindentLines(lines);
};
8 changes: 4 additions & 4 deletions src/framework/AbstractExecutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,28 +757,28 @@ export abstract class AbstractExecutable<TestT extends AbstractTest = AbstractTe
case ExecutableRunResultValue.OK:
{
this.shared.log.errorS('builder should not left behind if no problem', this, leftBehindBuilder);
leftBehindBuilder.addOutputLine(0, '❗️ Test run has been cancelled by user.');
leftBehindBuilder.addReindentedOutput(0, '❗️ Test run has been cancelled by user.');
leftBehindBuilder.errored();
}
break;
case ExecutableRunResultValue.CancelledByUser:
{
this.shared.log.info('Test run has been cancelled by user. ✋', leftBehindBuilder);
leftBehindBuilder.addOutputLine(0, '❓ Test run has been cancelled by user.');
leftBehindBuilder.addReindentedOutput(0, '❓ Test run has been cancelled by user.');
leftBehindBuilder.errored();
}
break;
case ExecutableRunResultValue.TimeoutByUser:
{
this.shared.log.info('Test has timed out. See `test.runtimeLimit` for details.', leftBehindBuilder);
leftBehindBuilder.addOutputLine(0, '❗️ Test has timed out. See `test.runtimeLimit` for details.');
leftBehindBuilder.addReindentedOutput(0, '❗️ Test has timed out. See `test.runtimeLimit` for details.');
leftBehindBuilder.errored();
}
break;
case ExecutableRunResultValue.Errored:
{
this.shared.log.warn('Test has ended unexpectedly.', result, leftBehindBuilder);
leftBehindBuilder.addOutputLine(0, '❗️ Test has ended unexpectedly: ' + result.toString());
leftBehindBuilder.addReindentedOutput(0, '❗️ Test has ended unexpectedly: ' + result.toString());
leftBehindBuilder.errored();
}
break;
Expand Down
25 changes: 14 additions & 11 deletions src/framework/Catch2/Catch2Executable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ abstract class TagProcessorBase implements XmlTagProcessor {
// known tag, do nothing
} else {
this.shared.log.errorS('unhandled tag:' + tag.name);
this.builder.addOutputLine(1, `Unknown XML tag: ${tag.name} with ${JSON.stringify(tag.attribs)}`);
this.builder.addReindentedOutput(1, `Unknown XML tag: ${tag.name} with ${JSON.stringify(tag.attribs)}`);
}
}
}
Expand All @@ -449,7 +449,7 @@ abstract class TagProcessorBase implements XmlTagProcessor {
return processor(dataTrimmed, parentTag, this.builder, this.shared);
} catch (e) {
this.shared.log.exceptionS(e);
this.builder.addOutputLine(1, 'Unknown fatal error: ' + inspect(e));
this.builder.addReindentedOutput(1, 'Unknown fatal error: ' + inspect(e));
this.builder.errored();
}
} else if (processor === null) {
Expand All @@ -460,7 +460,10 @@ abstract class TagProcessorBase implements XmlTagProcessor {
// known tag, do nothing
} else {
this.shared.log.errorS('unhandled tag:' + parentTag.name, parentTag);
this.builder.addOutputLine(1, `Unknown XML tag: ${parentTag.name} with ${JSON.stringify(parentTag.attribs)}`);
this.builder.addReindentedOutput(
1,
`Unknown XML tag: ${parentTag.name} with ${JSON.stringify(parentTag.attribs)}`,
);
}
}
}
Expand Down Expand Up @@ -782,7 +785,7 @@ class BenchmarkResultsProcessor implements XmlTagProcessor {

end(): void {
if (this.failed) {
this.builder.addOutputLine(1, 'Failed: `' + this.failed.message + '`');
this.builder.addReindentedOutput(1, 'Failed: `' + this.failed.message + '`');
this.builder.failed();
} else {
{
Expand All @@ -793,32 +796,32 @@ class BenchmarkResultsProcessor implements XmlTagProcessor {
.filter(n => n !== 'name')
.map(key => `- ${key}: ${attribs[key]}`);

this.builder.addOutputLine(1, ...params);
this.builder.addReindentedOutput(1, ...params);
}
if (this.mean) {
const mean = this.mean;
const params = Object.keys(mean)
.filter(n => n !== 'value')
.map(key => `- ${key}: ${mean[key]} ns`);

this.builder.addOutputLine(1, `Mean: ${mean.value} ns:`);
this.builder.addOutputLine(1, ...params);
this.builder.addReindentedOutput(1, `Mean: ${mean.value} ns:`);
this.builder.addReindentedOutput(1, ...params);
}
if (this.standardDeviation) {
const standardDeviation = this.standardDeviation;
const params = Object.keys(standardDeviation)
.filter(n => n !== 'value')
.map(key => `- ${key}: ${standardDeviation[key]} ns`);

this.builder.addOutputLine(1, `Standard Deviation: ${standardDeviation.value} ns:`);
this.builder.addOutputLine(1, ...params);
this.builder.addReindentedOutput(1, `Standard Deviation: ${standardDeviation.value} ns:`);
this.builder.addReindentedOutput(1, ...params);
}
if (this.outliers) {
const outliers = this.outliers;
const params = Object.keys(outliers).map(key => `- ${key}: ${outliers[key]} ns`);

this.builder.addOutputLine(1, `Outliers:`);
this.builder.addOutputLine(1, ...params);
this.builder.addReindentedOutput(1, `Outliers:`);
this.builder.addReindentedOutput(1, ...params);
}

this.builder.setDurationMilisec(Date.now() - this.started);
Expand Down
10 changes: 5 additions & 5 deletions src/framework/GoogleBenchmark/GoogleBenchmarkExecutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function parseAndProcessTestCase(

try {
if (metric['error_occurred']) {
builder.addOutputLine(1, '❌ Error occurred:', (metric['error_occurred'] as string).toString());
builder.addReindentedOutput(1, '❌ Error occurred:', (metric['error_occurred'] as string).toString());
builder.errored();
}

Expand All @@ -232,21 +232,21 @@ function parseAndProcessTestCase(
typeof builder.test.failIfExceedsLimitNs === 'number' &&
builder.test.failIfExceedsLimitNs < value * timeUnitMultiplier
) {
builder.addOutputLine(1, `❌ Failed: "${key}" exceeded limit: ${builder.test.failIfExceedsLimitNs} ns.`);
builder.addOutputLine(1, ' ');
builder.addReindentedOutput(1, `❌ Failed: "${key}" exceeded limit: ${builder.test.failIfExceedsLimitNs} ns.`);
builder.addReindentedOutput(1, ' ');
builder.failed();
}
}

Object.keys(metric).forEach(key => {
const value = metric[key];
const value2 = typeof value === 'string' ? '"' + value + '"' : value;
builder.addOutputLine(1, key + ': ' + value2);
builder.addReindentedOutput(1, key + ': ' + value2);
});
} catch (e) {
log.exceptionS(e, metric);

builder.addOutputLine(
builder.addReindentedOutput(
1,
'❌ Unexpected ERROR while parsing',
`Exception: "${e}"`,
Expand Down
14 changes: 7 additions & 7 deletions src/framework/GoogleTest/GoogleTestExecutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ class TestCaseProcessor implements LineProcessor {
this.testCaseShared.builder.test.line,
true,
);
this.testCaseShared.builder.addOutputLine(0, ansi.bold(line) + loc);
this.testCaseShared.builder.addReindentedOutput(0, ansi.bold(line) + loc);
}

online(line: string): void | true | LineProcessor {
Expand All @@ -434,15 +434,15 @@ class TestCaseProcessor implements LineProcessor {
}

if (this.testCaseShared.gMockWarningCount) {
this.testCaseShared.builder.addOutputLine(
this.testCaseShared.builder.addReindentedOutput(
1,
'⚠️' + this.testCaseShared.gMockWarningCount + ' GMock warning(s) in the output!',
);
}

this.testCaseShared.builder.build();

this.testCaseShared.builder.addOutputLine(
this.testCaseShared.builder.addReindentedOutput(
0,
testEndMatch[1] +
styleFunc(testEndMatch[2]) +
Expand All @@ -464,7 +464,7 @@ class TestCaseProcessor implements LineProcessor {
const fullMsg = failureMatch[5];
const failureMsg = failureMatch[7];

this.testCaseShared.builder.addOutputLine(
this.testCaseShared.builder.addReindentedOutput(
1,
ansi.red(type) + failureMsg + this.builder.getLocationAtStr(file, line, false),
);
Expand All @@ -481,7 +481,7 @@ class TestCaseProcessor implements LineProcessor {
}
}

this.testCaseShared.builder.addOutputLine(1, line);
this.testCaseShared.builder.addOutput(1, line);
}
}

Expand Down Expand Up @@ -535,7 +535,7 @@ class FailureProcessor implements LineProcessor {
}

end(): void {
this.testCaseShared.builder.addOutputLine(2, ...this.lines);
this.testCaseShared.builder.addReindentedOutput(2, ...this.lines);

if (isDecorationEnabled) {
this.testCaseShared.builder.addMarkdownMsg(
Expand Down Expand Up @@ -614,7 +614,7 @@ class ExpectCallProcessor implements LineProcessor {
}

end(): void {
this.testCaseShared.builder.addOutputLine(2, ...this.lines);
this.testCaseShared.builder.addReindentedOutput(2, ...this.lines);

this.testCaseShared.builder.addMessage(this.file, this.line, this.expected, ...this.actual);
}
Expand Down
6 changes: 3 additions & 3 deletions src/framework/doctest/DOCExecutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ abstract class TagProcessorBase implements XmlTagProcessor {
// known tag, do nothing
} else {
this.shared.log.errorS('unhandled tag:' + tag.name);
this.builder.addOutputLine(1, `Unknown XML tag: ${tag.name} with ${JSON.stringify(tag.attribs)}`);
this.builder.addReindentedOutput(1, `Unknown XML tag: ${tag.name} with ${JSON.stringify(tag.attribs)}`);
}
}
}
Expand All @@ -387,13 +387,13 @@ abstract class TagProcessorBase implements XmlTagProcessor {
return processor(dataTrimmed, parentTag, this.builder, this.caseData, this.shared);
} catch (e) {
this.shared.log.exceptionS(e);
this.builder.addOutputLine(1, 'Unknown fatal error: ' + inspect(e));
this.builder.addReindentedOutput(1, 'Unknown fatal error: ' + inspect(e));
this.builder.errored(); //TODO: check this is really working
}
} else if (processor === null) {
// known tag, do nothing
} else {
this.builder.addOutputLine(1, dataTrimmed);
this.builder.addReindentedOutput(1, dataTrimmed);
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/cpp/gtest/gtest1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,13 @@ TEST(Std, NoOutput)
std::cout << "ENV_TEST: " << env_p << "\n";
std::cout << "cout\n";
std::cerr << "cerr\n";
}

TEST(CppMateTest, LeadingWhitespace)
{
std::cout << "First line\n"
<< " Second line\n"
<< "- Third line\n"
<< " Fourth line\n";
FAIL() << "bad whitespace";
}

0 comments on commit f581bbc

Please sign in to comment.