-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathimport_test.cpp
69 lines (61 loc) · 2.96 KB
/
import_test.cpp
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
#include <iostream>
#include <string>
#include "test_tools.h"
// Test cases are taken from the pandor/Jinja2 tests
class ImportTest : public TemplateEnvFixture
{
protected:
void SetUp() override
{
TemplateEnvFixture::SetUp();
AddFile("module", R"(
{% macro test(extra='') %}[{{ foo }}{{ extra }}|{{ 23 }}]{% endmacro %}
{% set sbar=56 %}
{% macro __inner() %}77{% endmacro %}
{% macro test_set() %}[{{ foo }}|{{ sbar }}]{% endmacro %}
{% macro test_inner() %}[{{ foo }}|{{ __inner() }}]{% endmacro %}
)");
AddFile("header", "[{{ foo }}|{{ 23 }}]");
AddFile("o_printer", "({{ o }})");
}
};
TEST_F(ImportTest, TestContextImports)
{
jinja2::ValuesMap params{{"foo", 42}};
auto result = Render(R"({% import "module" as m %}{{ m.test() }}{{ m.test_set() }})", params);
EXPECT_EQ("[|23][|56]", result);
result = Render(R"({% import "module" as m %}{{ m.test(foo) }}{{ m.test_set() }})", params);
EXPECT_EQ("[42|23][|56]", result);
result = Render(R"({% import "module" as m without context %}{{ m.test() }}{{ m.test_set() }})", params);
EXPECT_EQ("[|23][|56]", result);
result = Render(R"({% import "module" as m without context %}{{ m.test(foo) }}{{ m.test_set() }})", params);
EXPECT_EQ("[42|23][|56]", result);
result = Render(R"({% import "module" as m with context %}{{ m.test() }}{{ m.test_set() }})", params);
EXPECT_EQ("[42|23][42|56]", result);
result = Render(R"({% import "module" as m with context %}{{ m.test(foo) }}{{ m.test_set() }})", params);
EXPECT_EQ("[4242|23][42|56]", result);
result = Render(R"({% import "module" as m without context %}{% set sbar=88 %}{{ m.test() }}{{ m.test_set() }})", params);
EXPECT_EQ("[|23][|56]", result);
result = Render(R"({% import "module" as m with context %}{% set sbar=88 %}{{ m.test() }}{{ m.test_set() }})", params);
EXPECT_EQ("[42|23][42|56]", result);
result = Render(R"({% import "module" as m without context %}{{ m.test() }}{{ m.test_inner() }})", params);
EXPECT_EQ("[|23][|77]", result);
result = Render(R"({% import "module" as m with context %}{{ m.test() }}{{ m.test_inner() }})", params);
EXPECT_EQ("[42|23][42|77]", result);
result = Render(R"({% from "module" import test %}{{ test() }})", params);
EXPECT_EQ("[|23]", result);
result = Render(R"({% from "module" import test without context %}{{ test() }})", params);
EXPECT_EQ("[|23]", result);
result = Render(R"({% from "module" import test with context %}{{ test() }})", params);
EXPECT_EQ("[42|23]", result);
}
TEST_F(ImportTest, TestImportSyntax)
{
Load(R"({% from "foo" import bar %})");
Load(R"({% from "foo" import bar, baz %})");
Load(R"({% from "foo" import bar, baz with context %})");
Load(R"({% from "foo" import bar, baz, with context %})");
Load(R"({% from "foo" import bar, with context %})");
Load(R"({% from "foo" import bar, with, context %})");
Load(R"({% from "foo" import bar, with with context %})");
}