File: test-files.cpp

package info (click to toggle)
inja 3.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,620 kB
  • sloc: cpp: 30,997; python: 189; makefile: 16; sh: 3
file content (108 lines) | stat: -rw-r--r-- 3,724 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2020 Pantor. All rights reserved.

#include "inja/environment.hpp"

#include "test-common.hpp"

TEST_CASE("loading") {
  inja::Environment env;
  inja::json data;
  data["name"] = "Jeff";

  SUBCASE("Files should be loaded") {
    CHECK(env.load_file((test_file_directory / "simple.txt").string()) == "Hello {{ name }}.");
  }

  SUBCASE("Files should be rendered") {
    CHECK(env.render_file(test_file_directory / "simple.txt", data) == "Hello Jeff.");
  }

  SUBCASE("File includes should be rendered") {
    CHECK(env.render_file(test_file_directory / "include.txt", data) == "Answer: Hello Jeff.");
  }

  SUBCASE("File error should throw") {
    std::string path = (test_file_directory / "does-not-exist").string();

    std::string file_error_message = "[inja.exception.file_error] failed accessing file at '" + path + "'";
    CHECK_THROWS_WITH(env.load_file(path), file_error_message.c_str());
    CHECK_THROWS_WITH(env.load_json(path), file_error_message.c_str());
  }
}

TEST_CASE("complete-files") {
  inja::Environment env {test_file_directory};

  for (std::string test_name : {"simple-file", "nested", "nested-line", "html", "html-extend"}) {
    SUBCASE(test_name.c_str()) {
      CHECK(env.render_file_with_json_file(test_name + "/template.txt", test_name + "/data.json") == env.load_file(test_name + "/result.txt"));
    }
  }

  for (std::string test_name : {"error-unknown"}) {
    SUBCASE(test_name.c_str()) {
      CHECK_THROWS_WITH(env.render_file_with_json_file(test_name + "/template.txt", test_name + "/data.json"),
                        "[inja.exception.parser_error] (at 2:10) expected 'in', got 'ins'");
    }
  }
}

TEST_CASE("complete-files-whitespace-control") {
  inja::Environment env {test_file_directory};
  env.set_trim_blocks(true);
  env.set_lstrip_blocks(true);

  for (std::string test_name : {"nested-whitespace"}) {
    SUBCASE(test_name.c_str()) {
      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 {"./"};
  inja::json data;
  data["name"] = "Jeff";

  SUBCASE("Files should be written") {
    env.write("simple.txt", data, "global-path-result.txt");

    // Fails repeatedly on windows CI
    // CHECK(env_result.load_file("global-path-result.txt") == "Hello Jeff.");
  }
}

TEST_CASE("include-files") {
  inja::Environment env {test_file_directory};
  inja::json data;
  data["name"] = "Jeff";

  SUBCASE("from text") {
    CHECK(env.render_file("include.txt", data) == "Answer: Hello Jeff.");
    CHECK(env.render("Answer: {% include \"simple.txt\" %}", data) == "Answer: Hello Jeff.");

    CHECK_NOTHROW(env.render_file_with_json_file("html/template.txt", "html/data.json"));
  }

  SUBCASE("without local files") {
    env.set_search_included_templates_in_files(false);
    CHECK_THROWS_WITH(env.render_file_with_json_file("html/template.txt", "html/data.json"),
                    "[inja.exception.render_error] (at 3:14) include 'header.txt' not found");
  }
}

TEST_CASE("include-in-memory-and-file-template") {
  inja::Environment env {test_file_directory};

  inja::json data;
  data["name"] = "Jeff";

  std::string error_message = "[inja.exception.file_error] failed accessing file at '" + (test_file_directory / "body").string() + "'";
  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);

  CHECK(env.render_file("include-both.txt", data) == "Hello Jeff. - Bye Jeff.");
}