Skip to content

Commit

Permalink
Fix: Remove circular dependencies in headers (#295)
Browse files Browse the repository at this point in the history
* Remove circular dependencies in headers

* Fix unit test includes and test data directory location
  • Loading branch information
navrocky authored Feb 11, 2025
1 parent 17ba53c commit 384a6be
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ doc/latex
examples

__pycache__
/CMakeLists.txt.user
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion include/inja/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
2 changes: 1 addition & 1 deletion include/inja/function_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <utility>
#include <vector>

#include "inja.hpp"
#include "json.hpp"

namespace inja {

Expand Down
28 changes: 2 additions & 26 deletions include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions include/inja/json.hpp
Original file line number Diff line number Diff line change
@@ -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_
2 changes: 1 addition & 1 deletion include/inja/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include <vector>

#include "function_storage.hpp"
#include "inja.hpp"
#include "utils.hpp"
#include "json.hpp"

namespace inja {

Expand Down
2 changes: 1 addition & 1 deletion include/inja/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions include/inja/throw.hpp
Original file line number Diff line number Diff line change
@@ -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_
38 changes: 26 additions & 12 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,22 +39,30 @@ 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
#endif
#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_
Expand All @@ -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_
Expand Down Expand Up @@ -98,7 +112,7 @@ using json = INJA_DATA_TYPE;
#include <utility>
#include <vector>

// #include "inja.hpp"
// #include "json.hpp"


namespace inja {
Expand Down Expand Up @@ -235,8 +249,6 @@ class FunctionStorage {

#endif // INCLUDE_INJA_FUNCTION_STORAGE_HPP_

// #include "inja.hpp"

// #include "utils.hpp"
#ifndef INCLUDE_INJA_UTILS_HPP_
#define INCLUDE_INJA_UTILS_HPP_
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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_
Expand All @@ -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_
Expand Down Expand Up @@ -1448,6 +1458,8 @@ class Lexer {

// #include "template.hpp"

// #include "throw.hpp"

// #include "token.hpp"


Expand Down Expand Up @@ -2108,12 +2120,12 @@ class Parser {

// #include "function_storage.hpp"

// #include "inja.hpp"

// #include "node.hpp"

// #include "template.hpp"

// #include "throw.hpp"

// #include "utils.hpp"


Expand Down Expand Up @@ -2764,6 +2776,8 @@ class Renderer : public NodeVisitor {

// #include "template.hpp"

// #include "throw.hpp"


namespace inja {

Expand Down
9 changes: 9 additions & 0 deletions test/test-common.hpp
Original file line number Diff line number Diff line change
@@ -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_
8 changes: 4 additions & 4 deletions test/test-files.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions test/test-functions.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 2 additions & 5 deletions test/test-renderer.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 2 additions & 6 deletions test/test-units.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 5 additions & 6 deletions test/test.cpp
Original file line number Diff line number Diff line change
@@ -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/" };

0 comments on commit 384a6be

Please sign in to comment.