Skip to content
Snippets Groups Projects
Commit 420a6bae authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add tests for EscapeString utilities

Actually fixed a few issues and make functions escapeString() and
unescapeString() reciprocal.

This later change leads to a grammar change, now literal do not
store quotes: before `"foo"` was stored as the string "\"foo\"", now
it is just stored as "foo".
parent 525ced2d
No related branches found
No related tags found
1 merge request!63Feature/utils coverage
......@@ -55,11 +55,13 @@ struct character : if_must_else< one< '\\' >, escaped_c, ascii::any> {};
struct open_parent : seq< one< '(' >, ignored > {};
struct close_parent : seq< one< ')' >, ignored > {};
struct literal : if_must< one< '"' >, until< one< '"' >, character > > {};
struct literal : star< minus<character, one < '"' > > >{};
struct quoted_literal : if_must< one< '"' >, seq< literal, one< '"' > > >{};
struct import_kw : TAO_PEGTL_KEYWORD("import") {};
struct LITERAL : seq< literal, ignored >{};
struct LITERAL : seq< quoted_literal, ignored >{};
struct REAL : seq< real, ignored >{};
......
......@@ -14,9 +14,10 @@ ASTPrinter::_print(std::ostream& os, const ASTNode& node) const
}
os << rang::fg::reset;
if (node.is_type<language::name>() or node.is_type<language::literal>() or node.is_type<language::integer>() or
node.is_type<language::real>()) {
if (node.is_type<language::name>() or node.is_type<language::integer>() or node.is_type<language::real>()) {
os << ':' << rang::fgB::green << node.string() << rang::fg::reset;
} else if (node.is_type<language::literal>()) {
os << ":\"" << rang::fgB::green << node.string() << rang::fg::reset << '"';
}
if (m_info & static_cast<InfoBaseType>(Info::data_type)) {
......
#ifndef ESCAPED_STRING_HPP
#define ESCAPED_STRING_HPP
#include <utils/Exceptions.hpp>
#include <utils/PugsMacros.hpp>
#include <sstream>
......@@ -11,7 +12,7 @@ PUGS_INLINE std::string
unescapeString(std::string_view input_string)
{
std::stringstream ss;
for (size_t i = 1; i < input_string.size() - 1; ++i) {
for (size_t i = 0; i < input_string.size(); ++i) {
char c = input_string[i];
if (c == '\\') {
++i;
......@@ -81,11 +82,15 @@ escapeString(std::string_view input_string)
ss << R"(\\)";
break;
}
case '\'': {
ss << R"(\')";
break;
}
case '\"': {
ss << R"(\")";
break;
}
case '?': {
case '\?': {
ss << R"(\?)";
break;
}
......
......@@ -60,6 +60,7 @@ add_executable (unit_tests
test_Demangle.cpp
test_DoWhileProcessor.cpp
test_EmbeddedData.cpp
test_EscapedString.cpp
test_ExecutionPolicy.cpp
test_FakeProcessor.cpp
test_ForProcessor.cpp
......
#ifndef TEST_ESCAPED_STRING_HPP
#define TEST_ESCAPED_STRING_HPP
#include <catch2/catch.hpp>
#include <utils/EscapedString.hpp>
// clazy:excludeall=non-pod-global-static
TEST_CASE("EscapedString", "[utils]")
{
SECTION("escape string")
{
const std::string s = "foo\'\\\"\?\a\b\f\n\r\t\vbar";
REQUIRE(escapeString(s) == R"(foo\'\\\"\?\a\b\f\n\r\t\vbar)");
}
SECTION("unescape string")
{
const std::string s = R"(foo\'\\\"\?\a\b\f\n\r\t\vbar)";
REQUIRE(unescapeString(s) == std::string{"foo\'\\\"\?\a\b\f\n\r\t\vbar"});
}
}
#endif // TEST_ESCAPED_STRING_HPP
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment