aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMartin Vilcans <martin@librador.com>2012-07-10 00:09:00 +0200
committerMartin Vilcans <martin@librador.com>2012-07-10 00:28:14 +0200
commit6b8ecde671306398f1c242df74ef918e12724f35 (patch)
tree9e7b0cba6e4cb4d30e8a50c4f38a21cca83f91df /tests
parent64d171de1345bc389a406cce8d980967b4202b98 (diff)
downloadscreenplain-6b8ecde671306398f1c242df74ef918e12724f35.tar.gz
Automatically create test cases from files in tests/files
This replaces the repeated test code.
Diffstat (limited to 'tests')
-rw-r--r--tests/files/README18
-rw-r--r--tests/files_test.py86
2 files changed, 70 insertions, 34 deletions
diff --git a/tests/files/README b/tests/files/README
new file mode 100644
index 0000000..b63ce04
--- /dev/null
+++ b/tests/files/README
@@ -0,0 +1,18 @@
+This directory contains test cases.
+
+There are two kinds of files in this directory:
+
+A "source file" has just one extension, e.g. 'foo.fountain'.
+An "expect file" has two extensions, e.g. 'foo.fountain.html'.
+
+For each source file, Screenplain is run to see if it produces
+the content of the corresponding expect file(s).
+
+E.g. for the following files:
+
+foo.fountain, foo.fountain.html, foo.fountain.fdx
+
+Screenplain will be run with foo.fountain as input and HTML output.
+The result is compared to foo.fountain.html.
+Then the same thing is done with FDX output, which is compared
+to foo.fountain.fdx.
diff --git a/tests/files_test.py b/tests/files_test.py
index 0a0480e..63c4fa6 100644
--- a/tests/files_test.py
+++ b/tests/files_test.py
@@ -6,6 +6,7 @@ from __future__ import with_statement
import unittest2
import tempfile
+import os
import os.path
import shutil
import re
@@ -35,7 +36,7 @@ def clean_string(s):
return spaces_re.sub(' ', line_break_re.sub('\n', s))
-class ParseTests(unittest2.TestCase):
+class FileTests(unittest2.TestCase):
"""High level tests that runs Screenplain using command line arguments.
"""
@@ -65,36 +66,53 @@ class ParseTests(unittest2.TestCase):
expected = read_file(self.source(expected_results_file))
return clean_string(actual), clean_string(expected)
- def test_fountain_to_fdx(self):
- actual, expected = self.convert(
- 'simple.fountain', 'simple.fdx', 'simple.fountain.fdx')
- self.assertMultiLineEqual(expected, actual)
-
- def test_fountain_to_html(self):
- actual, expected = self.convert(
- 'simple.fountain', 'simple.html', 'simple.fountain.html', '--bare')
- self.assertMultiLineEqual(expected, actual)
-
- def test_scene_numbers(self):
- actual, expected = self.convert(
- 'scene-numbers.fountain', 'scene-numbers.html',
- 'scene-numbers.fountain.html', '--bare')
- self.assertMultiLineEqual(expected, actual)
-
- def test_sections(self):
- actual, expected = self.convert(
- 'sections.fountain', 'sections.html',
- 'sections.fountain.html', '--bare')
- self.assertMultiLineEqual(expected, actual)
-
- def test_boneyard(self):
- actual, expected = self.convert(
- 'boneyard.fountain', 'sections.html',
- 'boneyard.fountain.html', '--bare')
- self.assertMultiLineEqual(expected, actual)
-
- def test_page_break(self):
- actual, expected = self.convert(
- 'page-break.fountain', 'page-break.html',
- 'page-break.fountain.html', '--bare')
- self.assertMultiLineEqual(expected, actual)
+ @classmethod
+ def add_file_case(cls, source_file, expected_results_file):
+ """Add a test case that compares the content
+ of a generated file with the expected results.
+
+ """
+ extension = os.path.splitext(expected_results_file)[1][1:]
+
+ def test_function(self):
+ actual, expected = self.convert(
+ source_file, source_file + '.' + extension,
+ source_file + '.' + extension,
+ '--bare'
+ )
+ self.assertMultiLineEqual(expected, actual)
+
+ func_name = (
+ 'test_' +
+ source_file.replace('.', '_') +
+ '_to_' +
+ extension
+ )
+ setattr(cls, func_name, test_function)
+
+
+def _create_tests():
+ """Creates a test case for every source/expect file pair.
+
+ Finds all the source files in the test files directory.
+ (A source file is one with just one extension, e.g. 'foo.fountain'.)
+ For each of them, finds the corresponding expect files.
+ (An expect file has two extensions, e.g. 'foo.fountain.html'
+ which contains the expected output when converting foo.fountain
+ to HTML.)
+
+ """
+ source_file_re = re.compile(r'^[^.]+\.[^.]+$')
+ expect_file_re = re.compile(r'^[^.]+\.[^.]+\.[^.]+$')
+
+ test_files = os.listdir(source_dir)
+ source_files = [f for f in test_files if source_file_re.match(f)]
+ expect_files = [f for f in test_files if expect_file_re.match(f)]
+
+ for source in source_files:
+ for expected in (
+ f for f in expect_files if f.startswith(source + '.')
+ ):
+ FileTests.add_file_case(source, expected)
+
+_create_tests()