diff --git a/.gitignore b/.gitignore index 8532006..442c94d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ doc/latex examples __pycache__ +/CMakeLists.txt.user diff --git a/CMakeLists.txt b/CMakeLists.txt index a46cef6..45c17d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,8 @@ execute_process(COMMAND scripts/update_single_include.sh WORKING_DIRECTORY ${PRO if(BUILD_TESTING AND INJA_BUILD_TESTS) enable_testing() + add_definitions(-D__TEST_DIR__=${CMAKE_CURRENT_SOURCE_DIR}/test) + add_executable(inja_test test/test.cpp) target_link_libraries(inja_test PRIVATE inja) target_include_directories(inja_test PRIVATE include third_party/include) @@ -104,7 +106,8 @@ if(BUILD_TESTING AND INJA_BUILD_TESTS) add_test(single_inja_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/single_inja_test) - add_executable(inja_benchmark test/benchmark.cpp) + add_executable(inja_benchmark test/benchmark.cpp + test/test-common.hpp) target_link_libraries(inja_benchmark PRIVATE inja) target_include_directories(inja_benchmark PRIVATE third_party/include) endif() diff --git a/include/inja/environment.hpp b/include/inja/environment.hpp index 239dd39..8bebfbf 100644 --- a/include/inja/environment.hpp +++ b/include/inja/environment.hpp @@ -7,12 +7,13 @@ #include #include +#include "json.hpp" #include "config.hpp" #include "function_storage.hpp" -#include "inja.hpp" #include "parser.hpp" #include "renderer.hpp" #include "template.hpp" +#include "throw.hpp" namespace inja { diff --git a/include/inja/function_storage.hpp b/include/inja/function_storage.hpp index 60bf073..2a265e0 100644 --- a/include/inja/function_storage.hpp +++ b/include/inja/function_storage.hpp @@ -8,7 +8,7 @@ #include #include -#include "inja.hpp" +#include "json.hpp" namespace inja { diff --git a/include/inja/inja.hpp b/include/inja/inja.hpp index dcbcf22..e366763 100644 --- a/include/inja/inja.hpp +++ b/include/inja/inja.hpp @@ -25,32 +25,8 @@ SOFTWARE. #ifndef INCLUDE_INJA_INJA_HPP_ #define INCLUDE_INJA_INJA_HPP_ -#include - -namespace inja { -#ifndef INJA_DATA_TYPE -using json = nlohmann::json; -#else -using json = INJA_DATA_TYPE; -#endif -} // namespace inja - -#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION) -#ifndef INJA_THROW -#define INJA_THROW(exception) throw exception -#endif -#else -#include -#ifndef INJA_THROW -#define INJA_THROW(exception) \ - std::abort(); \ - std::ignore = exception -#endif -#ifndef INJA_NOEXCEPTION -#define INJA_NOEXCEPTION -#endif -#endif - +#include "json.hpp" +#include "throw.hpp" #include "environment.hpp" #include "exceptions.hpp" #include "parser.hpp" diff --git a/include/inja/json.hpp b/include/inja/json.hpp new file mode 100644 index 0000000..8ec1352 --- /dev/null +++ b/include/inja/json.hpp @@ -0,0 +1,14 @@ +#ifndef INCLUDE_INJA_JSON_HPP_ +#define INCLUDE_INJA_JSON_HPP_ + +#include + +namespace inja { +#ifndef INJA_DATA_TYPE +using json = nlohmann::json; +#else +using json = INJA_DATA_TYPE; +#endif +} // namespace inja + +#endif // INCLUDE_INJA_JSON_HPP_ diff --git a/include/inja/node.hpp b/include/inja/node.hpp index 501854e..409045a 100644 --- a/include/inja/node.hpp +++ b/include/inja/node.hpp @@ -9,8 +9,8 @@ #include #include "function_storage.hpp" -#include "inja.hpp" #include "utils.hpp" +#include "json.hpp" namespace inja { diff --git a/include/inja/parser.hpp b/include/inja/parser.hpp index 3c396c9..bfc5d6f 100644 --- a/include/inja/parser.hpp +++ b/include/inja/parser.hpp @@ -14,10 +14,10 @@ #include "config.hpp" #include "exceptions.hpp" #include "function_storage.hpp" -#include "inja.hpp" #include "lexer.hpp" #include "node.hpp" #include "template.hpp" +#include "throw.hpp" #include "token.hpp" namespace inja { diff --git a/include/inja/renderer.hpp b/include/inja/renderer.hpp index 3622eed..d3112ee 100644 --- a/include/inja/renderer.hpp +++ b/include/inja/renderer.hpp @@ -18,9 +18,9 @@ #include "config.hpp" #include "exceptions.hpp" #include "function_storage.hpp" -#include "inja.hpp" #include "node.hpp" #include "template.hpp" +#include "throw.hpp" #include "utils.hpp" namespace inja { diff --git a/include/inja/throw.hpp b/include/inja/throw.hpp new file mode 100644 index 0000000..456e0b5 --- /dev/null +++ b/include/inja/throw.hpp @@ -0,0 +1,20 @@ +#ifndef INCLUDE_INJA_THROW_HPP_ +#define INCLUDE_INJA_THROW_HPP_ + +#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION) +#ifndef INJA_THROW +#define INJA_THROW(exception) throw exception +#endif +#else +#include +#ifndef INJA_THROW +#define INJA_THROW(exception) \ +std::abort(); \ + std::ignore = exception +#endif +#ifndef INJA_NOEXCEPTION +#define INJA_NOEXCEPTION +#endif +#endif + +#endif // INCLUDE_INJA_THROW_HPP_ diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index b751d6d..7ea810c 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -25,6 +25,10 @@ SOFTWARE. #ifndef INCLUDE_INJA_INJA_HPP_ #define INCLUDE_INJA_INJA_HPP_ +// #include "json.hpp" +#ifndef INCLUDE_INJA_JSON_HPP_ +#define INCLUDE_INJA_JSON_HPP_ + #include namespace inja { @@ -35,6 +39,12 @@ using json = INJA_DATA_TYPE; #endif } // namespace inja +#endif // INCLUDE_INJA_JSON_HPP_ + +// #include "throw.hpp" +#ifndef INCLUDE_INJA_THROW_HPP_ +#define INCLUDE_INJA_THROW_HPP_ + #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION) #ifndef INJA_THROW #define INJA_THROW(exception) throw exception @@ -42,15 +52,17 @@ using json = INJA_DATA_TYPE; #else #include #ifndef INJA_THROW -#define INJA_THROW(exception) \ - std::abort(); \ - std::ignore = exception +#define INJA_THROW(exception) \ +std::abort(); \ + std::ignore = exception #endif #ifndef INJA_NOEXCEPTION #define INJA_NOEXCEPTION #endif #endif +#endif // INCLUDE_INJA_THROW_HPP_ + // #include "environment.hpp" #ifndef INCLUDE_INJA_ENVIRONMENT_HPP_ #define INCLUDE_INJA_ENVIRONMENT_HPP_ @@ -61,6 +73,8 @@ using json = INJA_DATA_TYPE; #include #include +// #include "json.hpp" + // #include "config.hpp" #ifndef INCLUDE_INJA_CONFIG_HPP_ #define INCLUDE_INJA_CONFIG_HPP_ @@ -98,7 +112,7 @@ using json = INJA_DATA_TYPE; #include #include -// #include "inja.hpp" +// #include "json.hpp" namespace inja { @@ -235,8 +249,6 @@ public: #endif // INCLUDE_INJA_FUNCTION_STORAGE_HPP_ -// #include "inja.hpp" - // #include "utils.hpp" #ifndef INCLUDE_INJA_UTILS_HPP_ #define INCLUDE_INJA_UTILS_HPP_ @@ -358,6 +370,8 @@ inline void replace_substring(std::string& s, const std::string& f, const std::s #endif // INCLUDE_INJA_UTILS_HPP_ +// #include "json.hpp" + namespace inja { @@ -904,8 +918,6 @@ struct RenderConfig { // #include "function_storage.hpp" -// #include "inja.hpp" - // #include "parser.hpp" #ifndef INCLUDE_INJA_PARSER_HPP_ #define INCLUDE_INJA_PARSER_HPP_ @@ -926,8 +938,6 @@ struct RenderConfig { // #include "function_storage.hpp" -// #include "inja.hpp" - // #include "lexer.hpp" #ifndef INCLUDE_INJA_LEXER_HPP_ #define INCLUDE_INJA_LEXER_HPP_ @@ -1448,6 +1458,8 @@ public: // #include "template.hpp" +// #include "throw.hpp" + // #include "token.hpp" @@ -2108,12 +2120,12 @@ public: // #include "function_storage.hpp" -// #include "inja.hpp" - // #include "node.hpp" // #include "template.hpp" +// #include "throw.hpp" + // #include "utils.hpp" @@ -2764,6 +2776,8 @@ public: // #include "template.hpp" +// #include "throw.hpp" + namespace inja { diff --git a/test/test-common.hpp b/test/test-common.hpp new file mode 100644 index 0000000..512012d --- /dev/null +++ b/test/test-common.hpp @@ -0,0 +1,9 @@ +#ifndef INCLUDE_TEST_COMMON_HPP_ +#define INCLUDE_TEST_COMMON_HPP_ + +#include +#include + +extern const std::string test_file_directory; + +#endif // INCLUDE_TEST_COMMON_HPP_ diff --git a/test/test-files.cpp b/test/test-files.cpp index 32eb9b4..3ae499d 100644 --- a/test/test-files.cpp +++ b/test/test-files.cpp @@ -1,9 +1,8 @@ // Copyright (c) 2020 Pantor. All rights reserved. -#include - #include "inja/environment.hpp" -#include "inja/inja.hpp" + +#include "test-common.hpp" TEST_CASE("loading") { inja::Environment env; @@ -99,7 +98,8 @@ TEST_CASE("include-in-memory-and-file-template") { inja::json data; data["name"] = "Jeff"; - CHECK_THROWS_WITH(env.render_file("include-both.txt", data), "[inja.exception.file_error] failed accessing file at '../test/data/body'"); + std::string error_message = "[inja.exception.file_error] failed accessing file at '" + test_file_directory + "body'"; + CHECK_THROWS_WITH(env.render_file("include-both.txt", data), error_message.c_str()); const auto parsed_body_template = env.parse("Bye {{ name }}."); env.include_template("body", parsed_body_template); diff --git a/test/test-functions.cpp b/test/test-functions.cpp index 5249fba..50ee974 100644 --- a/test/test-functions.cpp +++ b/test/test-functions.cpp @@ -1,10 +1,8 @@ // Copyright (c) 2020 Pantor. All rights reserved. -#include -#include - #include "inja/environment.hpp" -#include "inja/inja.hpp" + +#include "test-common.hpp" TEST_CASE("functions") { inja::Environment env; diff --git a/test/test-renderer.cpp b/test/test-renderer.cpp index 41430f5..36fadb4 100644 --- a/test/test-renderer.cpp +++ b/test/test-renderer.cpp @@ -1,11 +1,8 @@ // Copyright (c) 2020 Pantor. All rights reserved. -#include -#include - #include "inja/environment.hpp" -#include "inja/inja.hpp" -#include "inja/template.hpp" + +#include "test-common.hpp" TEST_CASE("types") { inja::Environment env; diff --git a/test/test-units.cpp b/test/test-units.cpp index acf79ef..e679fce 100644 --- a/test/test-units.cpp +++ b/test/test-units.cpp @@ -1,12 +1,8 @@ // Copyright (c) 2020 Pantor. All rights reserved. -#include -#include - #include "inja/environment.hpp" -#include "inja/function_storage.hpp" -#include "inja/inja.hpp" -#include "inja/utils.hpp" + +#include "test-common.hpp" TEST_CASE("source location") { std::string content = R""""(Lorem Ipsum diff --git a/test/test.cpp b/test/test.cpp index 9b1fa59..3136b2c 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,16 +1,15 @@ // Copyright (c) 2020 Pantor. All rights reserved. #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN - -#include - #define JSON_USE_IMPLICIT_CONVERSIONS 0 #define JSON_NO_IO 1 -#include "inja/inja.hpp" - -const std::string test_file_directory {"../test/data/"}; #include "test-files.cpp" #include "test-functions.cpp" #include "test-renderer.cpp" #include "test-units.cpp" + +#define xstr(s) str(s) +#define str(s) #s + +const std::string test_file_directory { xstr(__TEST_DIR__)"/data/" };