Add test_double_quotes test

The new test ensures that no regressions are introduced in the future.
This commit is contained in:
Hennadii Stepanov 2025-04-24 16:47:18 +01:00
parent 4dffd5b55c
commit 20bb1458cc
No known key found for this signature in database
GPG Key ID: 410108112E7EA81F
2 changed files with 31 additions and 1 deletions

View File

@ -1,4 +1,4 @@
set(test_names test_subprocess test_cat test_env test_err_redirection test_exception test_split test_main test_ret_code)
set(test_names test_subprocess test_cat test_double_quotes test_env test_err_redirection test_exception test_split test_main test_ret_code)
set(test_files env_script.sh write_err.sh write_err.txt)

View File

@ -0,0 +1,30 @@
#include <subprocess.hpp>
#include <cassert>
#include <string>
namespace sp = subprocess;
// JSON requires the use of double quotes (see: https://json.org/).
// This test verifies proper handling of them.
void test_double_quotes()
{
// A simple JSON object.
const std::string expected{"{\"name\": \"value\"}"};
#ifdef __USING_WINDOWS__
const std::string command{"cmd.exe /c echo "};
#else
const std::string command{"echo "};
#endif
auto p = sp::Popen(command + expected, sp::output{sp::PIPE});
const auto out = p.communicate().first;
std::string result{out.buf.begin(), out.buf.end()};
// The `echo` command appends a newline.
result.erase(result.find_last_not_of(" \n\r\t") + 1);
assert(result == expected);
}
int main() {
test_double_quotes();
return 0;
}