mirror of
https://github.com/pantor/inja.git
synced 2026-04-04 15:18:53 +00:00
introduce INJA_DATA_TYPE
This commit is contained in:
@@ -44,7 +44,6 @@ Inja is a headers only library, which can be downloaded from the [releases](http
|
||||
|
||||
// Just for convenience
|
||||
using namespace inja;
|
||||
using json = nlohmann::json;
|
||||
```
|
||||
|
||||
If you are using the [Meson Build System](http://mesonbuild.com), then you can wrap this repository as a subproject.
|
||||
|
||||
@@ -28,7 +28,11 @@ SOFTWARE.
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace inja {
|
||||
#ifndef INJA_DATA_TYPE
|
||||
using json = nlohmann::json;
|
||||
#else
|
||||
using json = INJA_DATA_TYPE;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION)
|
||||
|
||||
@@ -28,7 +28,11 @@ SOFTWARE.
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace inja {
|
||||
#ifndef INJA_DATA_TYPE
|
||||
using json = nlohmann::json;
|
||||
#else
|
||||
using json = INJA_DATA_TYPE;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
TEST_CASE("loading") {
|
||||
inja::Environment env;
|
||||
json data;
|
||||
inja::json data;
|
||||
data["name"] = "Jeff";
|
||||
|
||||
SUBCASE("Files should be loaded") { CHECK(env.load_file(test_file_directory + "simple.txt") == "Hello {{ name }}."); }
|
||||
@@ -58,7 +58,7 @@ TEST_CASE("complete-files-whitespace-control") {
|
||||
TEST_CASE("global-path") {
|
||||
inja::Environment env {test_file_directory, "./"};
|
||||
inja::Environment env_result {"./"};
|
||||
json data;
|
||||
inja::json data;
|
||||
data["name"] = "Jeff";
|
||||
|
||||
SUBCASE("Files should be written") {
|
||||
@@ -79,7 +79,7 @@ TEST_CASE("include-without-local-files") {
|
||||
TEST_CASE("include-in-memory-and-file-template") {
|
||||
inja::Environment env {test_file_directory};
|
||||
|
||||
json data;
|
||||
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'");
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
TEST_CASE("functions") {
|
||||
inja::Environment env;
|
||||
|
||||
json data;
|
||||
inja::json data;
|
||||
data["name"] = "Peter";
|
||||
data["city"] = "New York";
|
||||
data["names"] = {"Jeff", "Seb", "Peter", "Tom"};
|
||||
@@ -199,7 +199,7 @@ TEST_CASE("functions") {
|
||||
|
||||
TEST_CASE("assignments") {
|
||||
inja::Environment env;
|
||||
json data;
|
||||
inja::json data;
|
||||
data["age"] = 28;
|
||||
|
||||
CHECK(env.render("{% set new_hour=23 %}{{ new_hour }}", data) == "23");
|
||||
@@ -209,7 +209,7 @@ TEST_CASE("assignments") {
|
||||
|
||||
TEST_CASE("callbacks") {
|
||||
inja::Environment env;
|
||||
json data;
|
||||
inja::json data;
|
||||
data["age"] = 28;
|
||||
|
||||
env.add_callback("double", 1, [](inja::Arguments &args) {
|
||||
@@ -262,7 +262,7 @@ TEST_CASE("callbacks") {
|
||||
|
||||
SUBCASE("Variadic") {
|
||||
env.add_callback("argmax", [](inja::Arguments& args) {
|
||||
auto result = std::max_element(args.begin(), args.end(), [](const json* a, const json* b) { return *a < *b;});
|
||||
auto result = std::max_element(args.begin(), args.end(), [](const inja::json* a, const inja::json* b) { return *a < *b;});
|
||||
return std::distance(args.begin(), result);
|
||||
});
|
||||
|
||||
@@ -273,7 +273,7 @@ TEST_CASE("callbacks") {
|
||||
|
||||
TEST_CASE("combinations") {
|
||||
inja::Environment env;
|
||||
json data;
|
||||
inja::json data;
|
||||
data["name"] = "Peter";
|
||||
data["city"] = "Brunswick";
|
||||
data["age"] = 29;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
TEST_CASE("types") {
|
||||
inja::Environment env;
|
||||
json data;
|
||||
inja::json data;
|
||||
data["name"] = "Peter";
|
||||
data["city"] = "Brunswick";
|
||||
data["age"] = 29;
|
||||
@@ -74,7 +74,7 @@ TEST_CASE("types") {
|
||||
}
|
||||
|
||||
SUBCASE("nested loops") {
|
||||
auto ldata = json::parse(R""""(
|
||||
auto ldata = inja::json::parse(R""""(
|
||||
{ "outer" : [
|
||||
{ "inner" : [
|
||||
{ "in2" : [ 1, 2 ] },
|
||||
@@ -158,7 +158,7 @@ Yeah!
|
||||
}
|
||||
|
||||
TEST_CASE("templates") {
|
||||
json data;
|
||||
inja::json data;
|
||||
data["name"] = "Peter";
|
||||
data["city"] = "Brunswick";
|
||||
data["is_happy"] = true;
|
||||
@@ -186,8 +186,8 @@ TEST_CASE("templates") {
|
||||
}
|
||||
|
||||
SUBCASE("include-in-loop") {
|
||||
json loop_data;
|
||||
loop_data["cities"] = json::array({{{"name", "Munich"}}, {{"name", "New York"}}});
|
||||
inja::json loop_data;
|
||||
loop_data["cities"] = inja::json::array({{{"name", "Munich"}}, {{"name", "New York"}}});
|
||||
|
||||
inja::Environment env;
|
||||
env.include_template("city.tpl", env.parse("{{ loop.index }}:{{ city.name }};"));
|
||||
@@ -246,7 +246,7 @@ TEST_CASE("templates") {
|
||||
}
|
||||
|
||||
TEST_CASE("other syntax") {
|
||||
json data;
|
||||
inja::json data;
|
||||
data["name"] = "Peter";
|
||||
data["city"] = "Brunswick";
|
||||
data["age"] = 29;
|
||||
|
||||
@@ -36,16 +36,16 @@ TEST_CASE("copy environment") {
|
||||
env.include_template("tpl", t1);
|
||||
std::string test_tpl = "{% include \"tpl\" %}";
|
||||
|
||||
REQUIRE(env.render(test_tpl, json()) == "4");
|
||||
REQUIRE(env.render(test_tpl, inja::json()) == "4");
|
||||
|
||||
inja::Environment copy(env);
|
||||
CHECK(copy.render(test_tpl, json()) == "4");
|
||||
CHECK(copy.render(test_tpl, inja::json()) == "4");
|
||||
|
||||
// overwrite template in source env
|
||||
inja::Template t2 = env.parse("{{ double(4) }}");
|
||||
env.include_template("tpl", t2);
|
||||
REQUIRE(env.render(test_tpl, json()) == "8");
|
||||
REQUIRE(env.render(test_tpl, inja::json()) == "8");
|
||||
|
||||
// template is unchanged in copy
|
||||
CHECK(copy.render(test_tpl, json()) == "4");
|
||||
CHECK(copy.render(test_tpl, inja::json()) == "4");
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
#include "doctest/doctest.h"
|
||||
#include "inja/inja.hpp"
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
const std::string test_file_directory {"../test/data/"};
|
||||
|
||||
#include "test-files.cpp"
|
||||
|
||||
Reference in New Issue
Block a user