mirror of
https://github.com/pantor/inja.git
synced 2026-02-17 00:53:59 +00:00
Fix: Remove circular dependencies in headers (#295)
* Remove circular dependencies in headers * Fix unit test includes and test data directory location
This commit is contained in:
committed by
GitHub
parent
17ba53cb9e
commit
384a6bef3f
1
.gitignore
vendored
1
.gitignore
vendored
@@ -10,3 +10,4 @@ doc/latex
|
||||
examples
|
||||
|
||||
__pycache__
|
||||
/CMakeLists.txt.user
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -7,12 +7,13 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#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 {
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "inja.hpp"
|
||||
#include "json.hpp"
|
||||
|
||||
namespace inja {
|
||||
|
||||
|
||||
@@ -25,32 +25,8 @@ SOFTWARE.
|
||||
#ifndef INCLUDE_INJA_INJA_HPP_
|
||||
#define INCLUDE_INJA_INJA_HPP_
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
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 <cstdlib>
|
||||
#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"
|
||||
|
||||
14
include/inja/json.hpp
Normal file
14
include/inja/json.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef INCLUDE_INJA_JSON_HPP_
|
||||
#define INCLUDE_INJA_JSON_HPP_
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace inja {
|
||||
#ifndef INJA_DATA_TYPE
|
||||
using json = nlohmann::json;
|
||||
#else
|
||||
using json = INJA_DATA_TYPE;
|
||||
#endif
|
||||
} // namespace inja
|
||||
|
||||
#endif // INCLUDE_INJA_JSON_HPP_
|
||||
@@ -9,8 +9,8 @@
|
||||
#include <vector>
|
||||
|
||||
#include "function_storage.hpp"
|
||||
#include "inja.hpp"
|
||||
#include "utils.hpp"
|
||||
#include "json.hpp"
|
||||
|
||||
namespace inja {
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
20
include/inja/throw.hpp
Normal file
20
include/inja/throw.hpp
Normal file
@@ -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 <cstdlib>
|
||||
#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_
|
||||
@@ -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 <nlohmann/json.hpp>
|
||||
|
||||
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 <cstdlib>
|
||||
#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 <string>
|
||||
#include <string_view>
|
||||
|
||||
// #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 <utility>
|
||||
#include <vector>
|
||||
|
||||
// #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 {
|
||||
|
||||
|
||||
9
test/test-common.hpp
Normal file
9
test/test-common.hpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef INCLUDE_TEST_COMMON_HPP_
|
||||
#define INCLUDE_TEST_COMMON_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
extern const std::string test_file_directory;
|
||||
|
||||
#endif // INCLUDE_TEST_COMMON_HPP_
|
||||
@@ -1,9 +1,8 @@
|
||||
// Copyright (c) 2020 Pantor. All rights reserved.
|
||||
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
#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);
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
// Copyright (c) 2020 Pantor. All rights reserved.
|
||||
|
||||
#include <doctest/doctest.h>
|
||||
#include <string>
|
||||
|
||||
#include "inja/environment.hpp"
|
||||
#include "inja/inja.hpp"
|
||||
|
||||
#include "test-common.hpp"
|
||||
|
||||
TEST_CASE("functions") {
|
||||
inja::Environment env;
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
// Copyright (c) 2020 Pantor. All rights reserved.
|
||||
|
||||
#include <doctest/doctest.h>
|
||||
#include <string>
|
||||
|
||||
#include "inja/environment.hpp"
|
||||
#include "inja/inja.hpp"
|
||||
#include "inja/template.hpp"
|
||||
|
||||
#include "test-common.hpp"
|
||||
|
||||
TEST_CASE("types") {
|
||||
inja::Environment env;
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
// Copyright (c) 2020 Pantor. All rights reserved.
|
||||
|
||||
#include <doctest/doctest.h>
|
||||
#include <string>
|
||||
|
||||
#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
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
// Copyright (c) 2020 Pantor. All rights reserved.
|
||||
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
#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/" };
|
||||
|
||||
Reference in New Issue
Block a user