diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e8437f..fe39a9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,52 +3,8 @@ project(mstch) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall") -find_package(Boost 1.54 COMPONENTS program_options REQUIRED) -include_directories(${CMAKE_BINARY_DIR} src include vendor/include ${Boost_INCLUDE_DIR}) +add_subdirectory(src) +add_subdirectory(test) -set(SRC - src/mstch.cpp - src/utils.cpp - src/token.cpp - src/render_context.cpp - src/state/in_section.cpp - src/state/in_inverted_section.cpp - src/state/outside_section.cpp - src/visitor/is_node_empty.cpp - src/visitor/render_node.cpp - src/visitor/render_section.cpp - src/visitor/to_json.cpp) - -add_library(mstch STATIC ${SRC}) - -add_executable(mstch_test test/test_main.cpp) -target_link_libraries(mstch_test mstch) - -add_executable(filetoheader test/filetoheader.cpp) -target_link_libraries(filetoheader ${Boost_PROGRAM_OPTIONS_LIBRARY}) - -file(GLOB data_files RELATIVE - "${CMAKE_SOURCE_DIR}/test/data" - "${CMAKE_SOURCE_DIR}/test/data/*.data") - -foreach(data_file ${data_files}) - list(APPEND genargs "-D${data_file}") -endforeach(data_file) - -file(GLOB string_files RELATIVE - "${CMAKE_SOURCE_DIR}/test/data" - "${CMAKE_SOURCE_DIR}/test/data/*.mustache" - "${CMAKE_SOURCE_DIR}/test/data/*.txt") - -foreach(string_file ${string_files}) - list(APPEND genargs "-S${string_file}") -endforeach(string_file) - -add_custom_command( - OUTPUT ${CMAKE_BINARY_DIR}/test_data.h - COMMAND ${CMAKE_BINARY_DIR}/filetoheader --output ${CMAKE_BINARY_DIR}/test_data.h --namespace mstchtest ${genargs} - DEPENDS ${CMAKE_BINARY_DIR}/filetoheader - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test/data/) -set_source_files_properties(${CMAKE_BINARY_DIR}/test_data.h PROPERTIES GENERATED TRUE) -add_custom_target(test_data_h DEPENDS ${CMAKE_BINARY_DIR}/test_data.h) -add_dependencies(mstch test_data_h) +enable_testing() +add_test(NAME mstch_tests COMMAND mstch_test) diff --git a/include/mstch.h b/include/mstch.h deleted file mode 100644 index 93bdba8..0000000 --- a/include/mstch.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef _MSTCH_H_ -#define _MSTCH_H_ - -#include "types.h" - -namespace mstch { - std::string render( - const std::string& tmplt, - const object& context, - const std::map& partials = - std::map()); -} - -#endif // _MSTCH_H_ diff --git a/include/types.h b/include/mstch/mstch.h similarity index 60% rename from include/types.h rename to include/mstch/mstch.h index 9ffb49e..faa9f57 100644 --- a/include/types.h +++ b/include/mstch/mstch.h @@ -1,5 +1,5 @@ -#ifndef _MSTCH_TYPES_H_ -#define _MSTCH_TYPES_H_ +#ifndef _MSTCH_H_ +#define _MSTCH_H_ #include #include @@ -14,6 +14,12 @@ namespace mstch { std::vector>::type; using object = std::map; using array = std::vector; + + std::string render( + const std::string& tmplt, + const object& context, + const std::map& partials = + std::map()); } -#endif //_MSTCH_TYPES_H_ +#endif // _MSTCH_H_ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..e2e5384 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,21 @@ +find_package(Boost 1.54 REQUIRED) + +include_directories( + ${CMAKE_SOURCE_DIR}/include + ${CMAKE_CURRENT_SOURCE_DIR} + ${Boost_INCLUDE_DIR}) + +set(SRC + mstch.cpp + utils.cpp + token.cpp + render_context.cpp + state/in_section.cpp + state/in_inverted_section.cpp + state/outside_section.cpp + visitor/is_node_empty.cpp + visitor/render_node.cpp + visitor/render_section.cpp + visitor/to_json.cpp) + +add_library(mstch STATIC ${SRC}) diff --git a/src/mstch.cpp b/src/mstch.cpp index 7bacca8..7d5d7fe 100644 --- a/src/mstch.cpp +++ b/src/mstch.cpp @@ -1,7 +1,7 @@ #include #include -#include "mstch.h" +#include "mstch/mstch.h" #include "render_context.h" using namespace mstch; @@ -16,7 +16,7 @@ std::string strip_whitespace(std::string tmplt) { std::regex whitespace_match("^\\s*$"); while (std::getline(in, line)) { std::string no_tags = std::regex_replace(line, tag_match, ""); - if(no_tags != line && std::regex_match(no_tags, whitespace_match)) { + if (no_tags != line && std::regex_match(no_tags, whitespace_match)) { out << std::regex_replace(line, std::regex("\\s"), ""); } else { out << line; diff --git a/src/render_context.h b/src/render_context.h index 337d618..579d5b7 100644 --- a/src/render_context.h +++ b/src/render_context.h @@ -5,7 +5,7 @@ #include #include -#include "types.h" +#include "mstch/mstch.h" #include "state/render_state.h" namespace mstch { diff --git a/src/token.cpp b/src/token.cpp index 8b6a853..7d1611b 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -1,6 +1,7 @@ #include "token.h" #include "utils.h" +#include #include using namespace mstch; @@ -9,7 +10,7 @@ token::token(const std::string& raw_token): raw_val(raw_token) { std::regex token_match("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}"); if(std::regex_match(raw_token, token_match)) { std::string inside = raw_token.substr(2, raw_token.size() - 4); - inside = trim(inside); + boost::trim(inside); if (inside.size() > 0 && inside.at(0) == '#') { type_val = token_type::section_open; content_val = inside.substr(1); @@ -37,7 +38,7 @@ token::token(const std::string& raw_token): raw_val(raw_token) { type_val = token_type::variable; content_val = inside; } - content_val = trim(content_val); + boost::trim(content_val); } else { type_val = token_type::text; content_val = raw_token; diff --git a/src/utils.cpp b/src/utils.cpp index 36ce021..eec8860 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,46 +1,13 @@ #include "utils.h" -#include - -std::string& mstch::ltrim(std::string& s) { - s.erase(s.begin(), std::find_if( - s.begin(), - s.end(), - std::not1(std::ptr_fun(std::isspace)))); - return s; -} - -std::string& mstch::rtrim(std::string& s) { - s.erase(std::find_if( - s.rbegin(), - s.rend(), - std::not1(std::ptr_fun(std::isspace))).base(), s.end()); - return s; -} - -std::string& mstch::trim(std::string& s) { - return ltrim(rtrim(s)); -} - -std::string mstch::replace_all( - std::string str, - const std::string& from, - const std::string& to) -{ - size_t start_pos = 0; - while((start_pos = str.find(from, start_pos)) != std::string::npos) { - str.replace(start_pos, from.length(), to); - start_pos += to.length(); - } - return str; -} +#include std::string mstch::html_escape(std::string str) { - str = replace_all(str, "&", "&"); - str = replace_all(str, "'", "'"); - str = replace_all(str, "\"", """); - str = replace_all(str, "<", "<"); - str = replace_all(str, ">", ">"); - str = replace_all(str, "/", "/"); + boost::replace_all(str, "&", "&"); + boost::replace_all(str, "'", "'"); + boost::replace_all(str, "\"", """); + boost::replace_all(str, "<", "<"); + boost::replace_all(str, ">", ">"); + boost::replace_all(str, "/", "/"); return str; } diff --git a/src/utils.h b/src/utils.h index 7777d5c..ff7cdaa 100644 --- a/src/utils.h +++ b/src/utils.h @@ -4,13 +4,6 @@ #include namespace mstch { - std::string& ltrim(std::string& s); - std::string& rtrim(std::string& s); - std::string& trim(std::string& s); - std::string replace_all( - std::string str, - const std::string& from, - const std::string& to); std::string html_escape(std::string str); } diff --git a/src/visitor/is_node_empty.h b/src/visitor/is_node_empty.h index 4843619..68a47c6 100644 --- a/src/visitor/is_node_empty.h +++ b/src/visitor/is_node_empty.h @@ -4,7 +4,7 @@ #include #include -#include "types.h" +#include "mstch/mstch.h" namespace mstch { namespace visitor { diff --git a/src/visitor/render_node.h b/src/visitor/render_node.h index 821c797..ecc4045 100644 --- a/src/visitor/render_node.h +++ b/src/visitor/render_node.h @@ -4,7 +4,7 @@ #include #include -#include "types.h" +#include "mstch/mstch.h" #include namespace mstch { diff --git a/src/visitor/render_section.h b/src/visitor/render_section.h index 2a74529..1c6c84a 100644 --- a/src/visitor/render_section.h +++ b/src/visitor/render_section.h @@ -6,7 +6,7 @@ #include #include -#include "types.h" +#include "mstch/mstch.h" namespace mstch { namespace visitor { diff --git a/src/visitor/to_json.h b/src/visitor/to_json.h index 90562a4..a36419e 100644 --- a/src/visitor/to_json.h +++ b/src/visitor/to_json.h @@ -5,7 +5,7 @@ #include #include -#include "types.h" +#include "mstch/mstch.h" namespace mstch { namespace visitor { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..737051d --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,39 @@ +find_package(Boost 1.54 COMPONENTS program_options REQUIRED) + +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_SOURCE_DIR}/include + ${CMAKE_CURRENT_SOURCE_DIR} + ${Boost_INCLUDE_DIR}) + +add_executable(filetoheader filetoheader.cpp) +target_link_libraries(filetoheader ${Boost_PROGRAM_OPTIONS_LIBRARY}) + +file(GLOB data_files RELATIVE + "${CMAKE_SOURCE_DIR}/test/data" + "${CMAKE_SOURCE_DIR}/test/data/*.data") + +foreach(data_file ${data_files}) + list(APPEND genargs "-D${data_file}") +endforeach(data_file) + +file(GLOB string_files RELATIVE + "${CMAKE_SOURCE_DIR}/test/data" + "${CMAKE_SOURCE_DIR}/test/data/*.mustache" + "${CMAKE_SOURCE_DIR}/test/data/*.txt") + +foreach(string_file ${string_files}) + list(APPEND genargs "-S${string_file}") +endforeach(string_file) + +add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/test_data.h + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/filetoheader --output ${CMAKE_CURRENT_BINARY_DIR}/test_data.h --namespace mstchtest ${genargs} + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/filetoheader + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test/data/) +set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/test_data.h PROPERTIES GENERATED TRUE) +add_custom_target(test_data_h DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/test_data.h) + +add_executable(mstch_test test_main.cpp) +target_link_libraries(mstch_test mstch) +add_dependencies(mstch_test test_data_h) diff --git a/vendor/include/catch.hpp b/test/catch.hpp similarity index 100% rename from vendor/include/catch.hpp rename to test/catch.hpp diff --git a/test/test_main.cpp b/test/test_main.cpp index f6439ce..aebdf16 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -1,7 +1,8 @@ #define CATCH_CONFIG_MAIN -#include "catch.hpp" -#include "mstch.h" +#include +#include + #include "test_data.h" #define MSTCH_TEST(x,y) TEST_CASE(x) { REQUIRE(y ## _txt == mstch::render(y ## _mustache, y ## _data)); }