| 1 |
import os |
| 2 |
|
| 3 |
from django.template import Context, Engine, TemplateSyntaxError |
| 4 |
from django.test import SimpleTestCase |
| 5 |
|
| 6 |
from .utils import ROOT |
| 7 |
|
| 8 |
RELATIVE = os.path.join(ROOT, "relative_templates") |
| 9 |
|
| 10 |
|
| 11 |
class ExtendsRelativeBehaviorTests(SimpleTestCase): |
| 12 |
def test_normal_extend(self): |
| 13 |
engine = Engine(dirs=[RELATIVE]) |
| 14 |
template = engine.get_template("one.html") |
| 15 |
output = template.render(Context({})) |
| 16 |
self.assertEqual(output.strip(), "three two one") |
| 17 |
|
| 18 |
def test_normal_extend_variable(self): |
| 19 |
engine = Engine(dirs=[RELATIVE]) |
| 20 |
template = engine.get_template("one_var.html") |
| 21 |
output = template.render(Context({"tmpl": "./two.html"})) |
| 22 |
self.assertEqual(output.strip(), "three two one") |
| 23 |
|
| 24 |
def test_dir1_extend(self): |
| 25 |
engine = Engine(dirs=[RELATIVE]) |
| 26 |
template = engine.get_template("dir1/one.html") |
| 27 |
output = template.render(Context({})) |
| 28 |
self.assertEqual(output.strip(), "three two one dir1 one") |
| 29 |
|
| 30 |
def test_dir1_extend1(self): |
| 31 |
engine = Engine(dirs=[RELATIVE]) |
| 32 |
template = engine.get_template("dir1/one1.html") |
| 33 |
output = template.render(Context({})) |
| 34 |
self.assertEqual(output.strip(), "three two one dir1 one") |
| 35 |
|
| 36 |
def test_dir1_extend2(self): |
| 37 |
engine = Engine(dirs=[RELATIVE]) |
| 38 |
template = engine.get_template("dir1/one2.html") |
| 39 |
output = template.render(Context({})) |
| 40 |
self.assertEqual(output.strip(), "three two one dir1 one") |
| 41 |
|
| 42 |
def test_dir1_extend3(self): |
| 43 |
engine = Engine(dirs=[RELATIVE]) |
| 44 |
template = engine.get_template("dir1/one3.html") |
| 45 |
output = template.render(Context({})) |
| 46 |
self.assertEqual(output.strip(), "three two one dir1 one") |
| 47 |
|
| 48 |
def test_dir2_extend(self): |
| 49 |
engine = Engine(dirs=[RELATIVE]) |
| 50 |
template = engine.get_template("dir1/dir2/one.html") |
| 51 |
output = template.render(Context({})) |
| 52 |
self.assertEqual(output.strip(), "three two one dir2 one") |
| 53 |
|
| 54 |
def test_extend_error(self): |
| 55 |
engine = Engine(dirs=[RELATIVE]) |
| 56 |
msg = ( |
| 57 |
"The relative path '\"./../two.html\"' points outside the file " |
| 58 |
"hierarchy that template 'error_extends.html' is in." |
| 59 |
) |
| 60 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 61 |
engine.render_to_string("error_extends.html") |
| 62 |
|
| 63 |
|
| 64 |
class IncludeRelativeBehaviorTests(SimpleTestCase): |
| 65 |
def test_normal_include(self): |
| 66 |
engine = Engine(dirs=[RELATIVE]) |
| 67 |
template = engine.get_template("dir1/dir2/inc2.html") |
| 68 |
output = template.render(Context({})) |
| 69 |
self.assertEqual(output.strip(), "dir2 include") |
| 70 |
|
| 71 |
def test_normal_include_variable(self): |
| 72 |
engine = Engine(dirs=[RELATIVE]) |
| 73 |
template = engine.get_template("dir1/dir2/inc3.html") |
| 74 |
output = template.render(Context({"tmpl": "./include_content.html"})) |
| 75 |
self.assertEqual(output.strip(), "dir2 include") |
| 76 |
|
| 77 |
def test_dir2_include(self): |
| 78 |
engine = Engine(dirs=[RELATIVE]) |
| 79 |
template = engine.get_template("dir1/dir2/inc1.html") |
| 80 |
output = template.render(Context({})) |
| 81 |
self.assertEqual(output.strip(), "three") |
| 82 |
|
| 83 |
def test_include_error(self): |
| 84 |
engine = Engine(dirs=[RELATIVE]) |
| 85 |
msg = ( |
| 86 |
"The relative path '\"./../three.html\"' points outside the file " |
| 87 |
"hierarchy that template 'error_include.html' is in." |
| 88 |
) |
| 89 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 90 |
engine.render_to_string("error_include.html") |
| 91 |
|
| 92 |
|
| 93 |
class ExtendsMixedBehaviorTests(SimpleTestCase): |
| 94 |
def test_mixing1(self): |
| 95 |
engine = Engine(dirs=[RELATIVE]) |
| 96 |
template = engine.get_template("dir1/two.html") |
| 97 |
output = template.render(Context({})) |
| 98 |
self.assertEqual(output.strip(), "three two one dir2 one dir1 two") |
| 99 |
|
| 100 |
def test_mixing2(self): |
| 101 |
engine = Engine(dirs=[RELATIVE]) |
| 102 |
template = engine.get_template("dir1/three.html") |
| 103 |
output = template.render(Context({})) |
| 104 |
self.assertEqual(output.strip(), "three dir1 three") |
| 105 |
|
| 106 |
def test_mixing_loop(self): |
| 107 |
engine = Engine(dirs=[RELATIVE]) |
| 108 |
msg = ( |
| 109 |
"The relative path '\"./dir2/../looped.html\"' was translated to " |
| 110 |
"template name 'dir1/looped.html', the same template in which " |
| 111 |
"the tag appears." |
| 112 |
) |
| 113 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 114 |
engine.render_to_string("dir1/looped.html") |