Skip to content

Commit

Permalink
-added a custom name for generated raw string literal
Browse files Browse the repository at this point in the history
  • Loading branch information
kamchatka-volcano committed Oct 6, 2024
1 parent 465fc96 commit edfc6e9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/textnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TextNode::TextNode(std::string value)

std::string TextNode::renderingCode() const
{
return "out << R\"(" + content_ + ")\";";
return "out << R\"_htcpp_str_(" + content_ + ")_htcpp_str_\";";
}

std::string TextNode::content() const
Expand Down
4 changes: 2 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ std::string transformRawStrings(const std::string& cppCode, const StreamReaderPo
if (res == "`"){
if (!insideString) {
rawStringPos = stream.position();
result += "R\"(";
result += "R\"_htcpp_str_(";
}
else
result += ")\"";
result += ")_htcpp_str_\"";
insideString = !insideString;
}
else
Expand Down
14 changes: 7 additions & 7 deletions tests/test_codenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ TEST(ExpressionNode, Basic)
TEST(ExpressionNode, WithStringOutput)
{
test<htcpp::ExpressionNode>
("$( isVisible ? \"Hello:)\" : defaultValue())",
"out << ( isVisible ? \"Hello:)\" : defaultValue());");
("$( isVisible ? \"Hello:)_htcpp_str_\" : defaultValue())",
"out << ( isVisible ? \"Hello:)_htcpp_str_\" : defaultValue());");
}

TEST(ExpressionNode, WithRawStringOutput)
{
test<htcpp::ExpressionNode>
("$( isVisible ? R\"(Hello:))\" : defaultValue())",
"out << ( isVisible ? R\"(Hello:))\" : defaultValue());");
("$( isVisible ? R\"_htcpp_str_(Hello:))_htcpp_str_\" : defaultValue())",
"out << ( isVisible ? R\"_htcpp_str_(Hello:))_htcpp_str_\" : defaultValue());");
}

TEST(ExpressionNode, WithCustomRawStringOutput)
{
test<htcpp::ExpressionNode>
("$( isVisible ? `Hello:)` : defaultValue())",
"out << ( isVisible ? R\"(Hello:))\" : defaultValue());");
"out << ( isVisible ? R\"_htcpp_str_(Hello:))_htcpp_str_\" : defaultValue());");
}

TEST(ExpressionNode, WithCharOutput)
Expand Down Expand Up @@ -115,7 +115,7 @@ TEST(StatementNode, BasicWithString)
{
test<htcpp::StatementNode>
("${ auto a = 0; {auto str = `Hello world's{}`;} }",
" auto a = 0; {auto str = R\"(Hello world's{})\";} ");
" auto a = 0; {auto str = R\"_htcpp_str_(Hello world's{})_htcpp_str_\";} ");
}

TEST(GlobalStatementNode, Basic)
Expand All @@ -129,7 +129,7 @@ TEST(GlobalStatementNode, BasicWithString)
{
test<htcpp::GlobalStatementNode>
("#{ auto a = 0; {auto str = `Hello {}`;} }",
" auto a = 0; {auto str = R\"(Hello {})\";} ");
" auto a = 0; {auto str = R\"_htcpp_str_(Hello {})_htcpp_str_\";} ");
}

TEST(InvalidExpressionNode, Unclosed)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_procedurenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TEST(ProcedureNode, Basic)
{
test("#hello_world(){ <p>Hello World! </p> }",
"hello_world",
"out << R\"( <p>Hello World! </p> )\";");
"out << R\"_htcpp_str_( <p>Hello World! </p> )_htcpp_str_\";");
}


Expand Down
20 changes: 10 additions & 10 deletions tests/test_sectionnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,52 +38,52 @@ void testError(const std::string& input, const std::string& expectedErrorMsg)
TEST(SectionNode, Basic)
{
test("[[ Hello world! ]]",
"out << R\"( Hello world! )\";");
"out << R\"_htcpp_str_( Hello world! )_htcpp_str_\";");
}

TEST(SectionNode, BasicWithConditionalExtension)
{
test("[[?(isVisible) Hello world! ]]",
"if (isVisible){ out << R\"( Hello world! )\"; } ");
"if (isVisible){ out << R\"_htcpp_str_( Hello world! )_htcpp_str_\"; } ");
}

TEST(SectionNode, BasicWithLoopExtension)
{
test("[[@(auto i = 0; i < 5; ++i) Hello world! ]]",
"for (auto i = 0; i < 5; ++i){ out << R\"( Hello world! )\"; } ");
"for (auto i = 0; i < 5; ++i){ out << R\"_htcpp_str_( Hello world! )_htcpp_str_\"; } ");
}

TEST(SectionNode, BasicWithConditionalExtensionOnClosingBraces)
{
test("[[ Hello world! ]]?(isVisible)",
"if (isVisible){ out << R\"( Hello world! )\"; } ");
"if (isVisible){ out << R\"_htcpp_str_( Hello world! )_htcpp_str_\"; } ");
}

TEST(SectionNode, BasicWithLoopExtensionOnClosingBraces)
{
test("[[ Hello world! ]]@(auto i = 0; i < 5; ++i)",
"for (auto i = 0; i < 5; ++i){ out << R\"( Hello world! )\"; } ");
"for (auto i = 0; i < 5; ++i){ out << R\"_htcpp_str_( Hello world! )_htcpp_str_\"; } ");
}


TEST(SectionNode, Nested)
{
test("[[ Hello <p>world</p> [[!]] ]]",
"out << R\"( Hello <p>world</p> ! )\";");
"out << R\"_htcpp_str_( Hello <p>world</p> ! )_htcpp_str_\";");
}

TEST(SectionNode, NestedWithConditionalExtension)
{
test("[[ Hello <p>?(isVisible)world</p> [[!]]?(isVisible) ]]?(isVisible)",
"if (isVisible){ out << R\"( Hello )\";if (isVisible){ out << R\"(<p>world</p>)\"; } out << R\"( )\";if "
"(isVisible){ out << R\"(!)\"; } out << R\"( )\"; } ");
"if (isVisible){ out << R\"_htcpp_str_( Hello )_htcpp_str_\";if (isVisible){ out << R\"_htcpp_str_(<p>world</p>)_htcpp_str_\"; } out << R\"_htcpp_str_( )_htcpp_str_\";if "
"(isVisible){ out << R\"_htcpp_str_(!)_htcpp_str_\"; } out << R\"_htcpp_str_( )_htcpp_str_\"; } ");
}

TEST(SectionNode, NestedWithLoopExtension)
{
test("[[ Hello <p>@(auto i = 0; i < 5; ++i)world</p> [[!]]@(auto i = 0; i < 3; ++i) ]]@(auto i = 0; i < 5; ++i)",
"for (auto i = 0; i < 5; ++i){ out << R\"( Hello )\";for (auto i = 0; i < 5; ++i){ out << "
"R\"(<p>world</p>)\"; } out << R\"( )\";for (auto i = 0; i < 3; ++i){ out << R\"(!)\"; } out << R\"( )\"; } ");
"for (auto i = 0; i < 5; ++i){ out << R\"_htcpp_str_( Hello )_htcpp_str_\";for (auto i = 0; i < 5; ++i){ out << "
"R\"_htcpp_str_(<p>world</p>)_htcpp_str_\"; } out << R\"_htcpp_str_( )_htcpp_str_\";for (auto i = 0; i < 3; ++i){ out << R\"_htcpp_str_(!)_htcpp_str_\"; } out << R\"_htcpp_str_( )_htcpp_str_\"; } ");
}


Expand Down
34 changes: 17 additions & 17 deletions tests/test_tagnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,89 +41,89 @@ void testError(const std::string& input, const std::string& expectedErrorMsg)
TEST(TagNode, BasicNoAttributes)
{
test("<p> Hello world! </p>",
"out << R\"(<p> Hello world! </p>)\";");
"out << R\"_htcpp_str_(<p> Hello world! </p>)_htcpp_str_\";");
}

TEST(TagNode, Basic)
{
test("<div id=\"9\"> Hello world! </div>",
"out << R\"(<div id=\"9\"> Hello world! </div>)\";");
"out << R\"_htcpp_str_(<div id=\"9\"> Hello world! </div>)_htcpp_str_\";");
}

TEST(TagNode, BasicWithConditionalExtension)
{
test("<div id=\"9\">?(isVisible) Hello world! </div>",
"if (isVisible){ out << R\"(<div id=\"9\"> Hello world! </div>)\"; } ");
"if (isVisible){ out << R\"_htcpp_str_(<div id=\"9\"> Hello world! </div>)_htcpp_str_\"; } ");
}

TEST(TagNode, BasicWithConditionalExtensionOnClosingTag)
{
test("<div id=\"9\">Hello world! </div>?(isVisible)",
"if (isVisible){ out << R\"(<div id=\"9\">Hello world! </div>)\"; } ");
"if (isVisible){ out << R\"_htcpp_str_(<div id=\"9\">Hello world! </div>)_htcpp_str_\"; } ");
}

TEST(TagNode, BasicWithLoopExtension)
{
test("<div id=\"9\">@(auto i = 0; i < 5; ++i) Hello world! </div>",
"for (auto i = 0; i < 5; ++i){ out << R\"(<div id=\"9\"> Hello world! </div>)\"; } ");
"for (auto i = 0; i < 5; ++i){ out << R\"_htcpp_str_(<div id=\"9\"> Hello world! </div>)_htcpp_str_\"; } ");
}

TEST(TagNode, BasicWithLoopExtensionOnClosingTag)
{
test("<div id=\"9\">Hello world! </div>@(auto i = 0; i < 5; ++i)",
"for (auto i = 0; i < 5; ++i){ out << R\"(<div id=\"9\">Hello world! </div>)\"; } ");
"for (auto i = 0; i < 5; ++i){ out << R\"_htcpp_str_(<div id=\"9\">Hello world! </div>)_htcpp_str_\"; } ");
}

TEST(TagNode, WithOtherNodes)
{
test("<a href=\"$(cfg.url)\" [[class=\"visible\"]]?(isVisible)></a>",
"out << R\"(<a href=\")\";out << (cfg.url);out << R\"(\" )\";if (isVisible){ out << R\"(class=\"visible\")\"; "
"} out << R\"(></a>)\";");
"out << R\"_htcpp_str_(<a href=\")_htcpp_str_\";out << (cfg.url);out << R\"_htcpp_str_(\" )_htcpp_str_\";if (isVisible){ out << R\"_htcpp_str_(class=\"visible\")_htcpp_str_\"; "
"} out << R\"_htcpp_str_(></a>)_htcpp_str_\";");
}


TEST(TagNode, NestedWithAttributes)
{
test("<div id=\"9\"> <p>Hello world!</p> </div>",
"out << R\"(<div id=\"9\"> <p>Hello world!</p> </div>)\";");
"out << R\"_htcpp_str_(<div id=\"9\"> <p>Hello world!</p> </div>)_htcpp_str_\";");
}

TEST(TagNode, NestedWithAttributesWithConditionalExtension)
{
test("<div id=\"9\">?(isVisible) <p>?(isVisible)Hello world!</p> </div>",
"if (isVisible){ out << R\"(<div id=\"9\"> )\";if (isVisible){ out << R\"(<p>Hello world!</p>)\"; } out << "
"R\"( </div>)\"; } ");
"if (isVisible){ out << R\"_htcpp_str_(<div id=\"9\"> )_htcpp_str_\";if (isVisible){ out << R\"_htcpp_str_(<p>Hello world!</p>)_htcpp_str_\"; } out << "
"R\"_htcpp_str_( </div>)_htcpp_str_\"; } ");
}

TEST(TagNode, NestedWithAttributesWithLoopExtension)
{
test("<div id=\"9\">@(auto i = 0; i < 5; ++i) <p>@(auto i = 0; i < 5; ++i)Hello world!</p> </div>",
"for (auto i = 0; i < 5; ++i){ out << R\"(<div id=\"9\"> )\";for (auto i = 0; i < 5; ++i){ out << "
"R\"(<p>Hello world!</p>)\"; } out << R\"( </div>)\"; } ");
"for (auto i = 0; i < 5; ++i){ out << R\"_htcpp_str_(<div id=\"9\"> )_htcpp_str_\";for (auto i = 0; i < 5; ++i){ out << "
"R\"_htcpp_str_(<p>Hello world!</p>)_htcpp_str_\"; } out << R\"_htcpp_str_( </div>)_htcpp_str_\"; } ");
}

TEST(TagNode, EmptyElementNoAttributes)
{
test("<br>",
"out << R\"(<br>)\";");
"out << R\"_htcpp_str_(<br>)_htcpp_str_\";");
}

TEST(TagNode, EmptyElement)
{
test("<img src=\"1.jpg\">",
"out << R\"(<img src=\"1.jpg\">)\";");
"out << R\"_htcpp_str_(<img src=\"1.jpg\">)_htcpp_str_\";");
}

TEST(TagNode, EmptyElementWithConditionalExtension)
{
test("<img src=\"1.jpg\">?(isVisible)",
"if (isVisible){ out << R\"(<img src=\"1.jpg\">)\"; } ");
"if (isVisible){ out << R\"_htcpp_str_(<img src=\"1.jpg\">)_htcpp_str_\"; } ");
}

TEST(TagNode, EmptyElementWithLoopExtension)
{
test("<img src=\"1.jpg\">@(auto i = 0; i < 5; ++i)",
"for (auto i = 0; i < 5; ++i){ out << R\"(<img src=\"1.jpg\">)\"; } ");
"for (auto i = 0; i < 5; ++i){ out << R\"_htcpp_str_(<img src=\"1.jpg\">)_htcpp_str_\"; } ");
}

TEST(InvalidTagNode, UnclosedTag)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ TEST(Utils, TransformRawString)
{
auto code = "auto x = `Hello World`;";
EXPECT_EQ(htcpp::utils::transformRawStrings(code, {1, 1}),
"auto x = R\"(Hello World)\";");
"auto x = R\"_htcpp_str_(Hello World)_htcpp_str_\";");
}
{
auto code = "auto x = ``;";
EXPECT_EQ(htcpp::utils::transformRawStrings(code, {1, 1}),
"auto x = R\"()\";");
"auto x = R\"_htcpp_str_()_htcpp_str_\";");
}
}

Expand Down

0 comments on commit edfc6e9

Please sign in to comment.