-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
21 additions
and
55 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,4 @@ | |
t.add_variable(user_variables) | ||
t.add_builder(builders) | ||
|
||
print(t.to_json()) | ||
t.to_json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ source = src | |
omit = | ||
*test* | ||
*tox* | ||
*examples* | ||
|
||
[tool:pytest] | ||
addopts = --verbose | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,35 @@ | ||
# Copyright (c) 2012-2013, Mark Peek <mark@peek.org> | ||
# All rights reserved. | ||
# | ||
from __future__ import unicode_literals | ||
|
||
import os | ||
import re | ||
import sys | ||
import unittest | ||
import pytest | ||
from io import StringIO | ||
from importlib import import_module | ||
|
||
try: | ||
u = unicode | ||
except NameError: | ||
u = str | ||
|
||
@pytest.mark.skip | ||
class TestExamples(unittest.TestCase): | ||
class TestFileExamples(object): | ||
|
||
maxDiff = None | ||
output_files = None | ||
|
||
# those are set by create_test_class | ||
filename = None | ||
expected_output = None | ||
def load_output_files(self): | ||
self.output_files = {} | ||
regex = re.compile(r'.template', re.I) | ||
current_directory = os.path.dirname(os.path.realpath(__file__)) + '/../' + 'examples_output' | ||
example_filenames = filter(regex.search, os.listdir(current_directory)) | ||
for example_filename in example_filenames: | ||
with open(current_directory + '/' + example_filename) as f: | ||
self.output_files[example_filename] = f.read() | ||
|
||
def test_example(self): | ||
saved = sys.stdout | ||
stdout = StringIO() | ||
try: | ||
sys.stdout = stdout | ||
def load_example_file(self, example_output_filename): | ||
original_example = example_output_filename.split('.')[0] | ||
example_module = import_module('examples.' + original_example) | ||
assert self.output_files[original_example + '.template'].rstrip() == example_module.t.to_json() | ||
|
||
with open(self.filename) as f: | ||
code = compile(f.read(), self.filename, "exec") | ||
exec(code, {'__name__': '__main__'}) | ||
finally: | ||
sys.stdout = saved | ||
# rewind fake stdout so we can read it | ||
stdout.seek(0) | ||
actual_output = stdout.read() | ||
self.assertEqual(u(self.expected_output), u(actual_output)) | ||
def test_examples(self): | ||
self.load_output_files() | ||
for file_name in self.output_files.keys(): | ||
self.load_example_file(file_name) | ||
|
||
|
||
def create_test_class(testname, **kwargs): | ||
klass = type(testname, (TestExamples,), kwargs) | ||
return klass | ||
|
||
|
||
def load_tests(loader, tests, pattern): | ||
# Filter out all *.py files from the examples directory | ||
examples = 'examples' | ||
regex = re.compile(r'.py$', re.I) | ||
example_filesnames = filter(regex.search, os.listdir(examples)) | ||
|
||
suite = unittest.TestSuite() | ||
|
||
for f in example_filesnames: | ||
testname = 'test_' + f[:-3] | ||
expected_output = open('tests/examples_output/%s.template' % | ||
f[:-3]).read() | ||
test_class = create_test_class(testname, filename=examples + '/' + f, | ||
expected_output=expected_output) | ||
tests = loader.loadTestsFromTestCase(test_class) | ||
suite.addTests(tests) | ||
|
||
return suite | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |