catchorg / catchorg/Catch2

Add string escape function or escape functionality built into CAPTURE macro

Open
#2,967 13 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

# The problem

This:
```c++
const auto asciiControlsString =
"null \0 tab \t carriage return \r line feed \n whatever this is \xf quotation mark \" chars"sv;
CAPTURE(asciiControlsString);
```
results in a message like this:
```
with message:
line feed rolsString := "null tab carriage return
whatever this is  quotation mark " chars"
```
Notice that because of carriage return `\r` character it garbles up the message:
```
line feed rolsString := "null tab carriage return
^ ^
text remnants of the variable name
after \r
```

# Proposed solution

Having an `escape()` function to escape non-printable and some special characters (like carriage return) provided by Catch2 would be wonderful so people don't have to reinvent the wheel every time.

The result of code like this:
```c++
CAPTURE(escape(asciiControlsString));
```
can look like this:
```
with message:
escape(asciiControlsString) := "null \0 tab \t carriage return \r
line feed \n whatever this is  quotation mark " chars"
```

Alternatively `CAPTURE()` macro can incorporate this functionality, or Catch2 can provide additional macro with such functionality, e.g. `CAPTURE_ESCAPED()`.

Contributor guide

Open the contributing guide

Research direction

Start by examining the CAPTURE macro and the issue's examples of control-character output. Compare the proposed escape() function, built-in CAPTURE behavior, and CAPTURE_ESCAPED() alternative, then define which approach should produce readable escaped output without carriage returns garbling the message.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.