mirror of
https://github.com/pantor/inja.git
synced 2026-03-05 00:36:26 +00:00
* throw exception if a file cannot be opened * Add a new function in utils.hpp: open_file_or_throw: This function returns an opened std::ifstream or throws by calling `inja_throw`. * Use this function in Parser::load_file which previously returned an empty string if the file couldn't be opened. * Use this function in Environment::load_json which previously threw a `nlohmann::detail::parse_error` if the file couldn't be opened. * In Parser::parse_statement: When including files through `include`, do not attempt to (re-)parse templates from files that were already included. Additionally, this prevents inja from attempting to load in-memory templates by their name from disk. * Add tests that check if an exception is thrown when attempting to open files that do not exist. * cmake: enable C++11 * cmake: require C++11 when depending on single_inja * code style
55 lines
1.6 KiB
C++
55 lines
1.6 KiB
C++
#include "catch/catch.hpp"
|
|
#include "inja/inja.hpp"
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
|
const std::string test_file_directory {"../test/data/"};
|
|
|
|
TEST_CASE("loading") {
|
|
inja::Environment env;
|
|
json data;
|
|
data["name"] = "Jeff";
|
|
|
|
SECTION("Files should be loaded") {
|
|
CHECK( env.load_file(test_file_directory + "simple.txt") == "Hello {{ name }}." );
|
|
}
|
|
|
|
SECTION("Files should be rendered") {
|
|
CHECK( env.render_file(test_file_directory + "simple.txt", data) == "Hello Jeff." );
|
|
}
|
|
|
|
SECTION("File includes should be rendered") {
|
|
CHECK( env.render_file(test_file_directory + "include.txt", data) == "Answer: Hello Jeff." );
|
|
}
|
|
|
|
SECTION("File error should throw") {
|
|
std::string path(test_file_directory + "does-not-exist");
|
|
CHECK_THROWS_WITH( env.load_file(path), "[inja.exception.file_error] failed accessing file at '" + path + "'" );
|
|
CHECK_THROWS_WITH( env.load_json(path), "[inja.exception.file_error] failed accessing file at '" + path + "'" );
|
|
}
|
|
}
|
|
|
|
TEST_CASE("complete-files") {
|
|
inja::Environment env {test_file_directory};
|
|
|
|
for (std::string test_name : {"simple-file", "nested", "nested-line", "html"}) {
|
|
SECTION(test_name) {
|
|
CHECK( env.render_file_with_json_file(test_name + "/template.txt", test_name + "/data.json") == env.load_file(test_name + "/result.txt") );
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_CASE("global-path") {
|
|
inja::Environment env {test_file_directory, "./"};
|
|
inja::Environment env_result {"./"};
|
|
json data;
|
|
data["name"] = "Jeff";
|
|
|
|
SECTION("Files should be written") {
|
|
env.write("simple.txt", data, "global-path-result.txt");
|
|
CHECK( env_result.load_file("global-path-result.txt") == "Hello Jeff." );
|
|
}
|
|
}
|