diff --git a/moban_velocity/engine.py b/moban_velocity/engine.py index 11988b6..4711f3e 100644 --- a/moban_velocity/engine.py +++ b/moban_velocity/engine.py @@ -1,4 +1,3 @@ -import sys import codecs from airspeed import Template @@ -19,7 +18,7 @@ def get_template(self, template_file): def get_template_from_string(self, string): return Template(string) - + def apply_template(self, template, data, output): rendered_content = template.merge(data) return rendered_content diff --git a/tests/fixtures/velocity_tests/expected_output.txt b/tests/fixtures/velocity_tests/expected_output.txt new file mode 100644 index 0000000..f2b6b2f --- /dev/null +++ b/tests/fixtures/velocity_tests/expected_output.txt @@ -0,0 +1,8 @@ +Old people: + + Bill + + Bob + + +Third person is Mark diff --git a/tests/fixtures/velocity_tests/file_tests.json b/tests/fixtures/velocity_tests/file_tests.json new file mode 100644 index 0000000..90be32a --- /dev/null +++ b/tests/fixtures/velocity_tests/file_tests.json @@ -0,0 +1,8 @@ +{ + "people":[ + {"name": "Bill", "age": 100}, + {"name": "Bob", "age": 90}, + {"name": "Mark", "age": 25} + ], + "hello": "World!" +} \ No newline at end of file diff --git a/tests/fixtures/velocity_tests/file_tests.velocity b/tests/fixtures/velocity_tests/file_tests.velocity new file mode 100644 index 0000000..3721265 --- /dev/null +++ b/tests/fixtures/velocity_tests/file_tests.velocity @@ -0,0 +1,8 @@ +Old people: +#foreach ($person in $people) + #if($person.age > 70) + $person.name + #end +#end + +Third person is $people[2].name diff --git a/tests/test_velocity_engine.py b/tests/test_velocity_engine.py new file mode 100644 index 0000000..4973641 --- /dev/null +++ b/tests/test_velocity_engine.py @@ -0,0 +1,39 @@ +import os + +from nose.tools import eq_ +from moban.plugins import ENGINES, BaseEngine +from moban_velocity.engine import EngineVelocity + + +def test_velocity_engine_type(): + engine = ENGINES.get_engine("velocity", [], "") + assert engine.engine_cls == EngineVelocity + pass + + +def test_velocity_file_test(): + output = "test.txt" + path = os.path.join("tests", "fixtures", "velocity_tests") + engine = BaseEngine(path, path, EngineVelocity) + engine.render_to_file("file_tests.velocity", "file_tests.json", output) + with open(output, "r") as output_file: + expected_path = os.path.join("tests", "fixtures", "velocity_tests", + "expected_output.txt") + with open(expected_path) as expected_file: + expected = expected_file.read() + content = output_file.read() + eq_(content, expected) + os.unlink(output) + + +def test_velocity_string_template(): + string_template = "Hello $hello" + output = "test.txt" + path = os.path.join("tests", "fixtures", "velocity_tests") + engine = BaseEngine(path, path, EngineVelocity) + engine.render_string_to_file(string_template, "file_tests.json", output) + with open(output, "r") as output_file: + expected = "Hello World!" + content = output_file.read() + eq_(content, expected) + os.unlink(output)