1
0
Fork 0
mirror of https://github.com/Oreolek/gamebookformat.git synced 2024-05-16 16:08:20 +03:00

Adding first output unit tests. Doing some refactoring to improve testability.

This commit is contained in:
Pelle Nilsson 2013-06-14 22:49:11 +02:00
parent 5bd01ef289
commit b0f70ff39f
3 changed files with 51 additions and 12 deletions

View file

@ -45,10 +45,10 @@ checkexpected: clean all
diff -r -x "*.aux" -x "*.gamebook" -x "*.log" -x "*.out" -x "*.png" \
-x "*.pdf" -x .gitignore -q examples expected
unittests=test_sections
unittests=$(wildcard test_*.py)
unittest: *.py
python2.7 -m unittest $(unittests)
python2.7 -m unittest $(unittests:.py=)
upload: html png pdf rtf
if [ -n "$(uploadto)" ]; then \

View file

@ -20,7 +20,8 @@ class OutputFormat (object):
def write_intro_section(self, section, shuffled_sections, output):
# FIXME some serious code-duplication here
refs = []
refsdict = ReferenceFormatter(section, shuffled_sections,
refsdict = ReferenceFormatter(section.nr,
shuffled_sections.name_to_nr,
self.format_with_template("section_ref"),
self.quote)
formatted_text = self.format_section(section, refsdict)
@ -42,7 +43,8 @@ class OutputFormat (object):
def write_section(self, section, shuffled_sections, output):
refs = []
refsdict = ReferenceFormatter(section, shuffled_sections,
refsdict = ReferenceFormatter(section.nr,
shuffled_sections.name_to_nr,
self.format_with_template("section_ref"),
self.quote)
formatted_text = self.format_section(section, refsdict)
@ -124,23 +126,23 @@ class OutputFormat (object):
class ReferenceFormatter (object):
"There is probably a better way, but this hack seems to work."
def __init__(self, section, shuffled_sections, ref_template, quote):
self.section = section
self.shuffled_sections = shuffled_sections
def __init__(self, from_nr, name_to_nr, ref_template, quote):
self.from_nr = from_nr
self.name_to_nr = name_to_nr
self.found = set()
self.ref_template = ref_template
self.items = {'nr' : section.nr}
self.items = {'nr' : from_nr}
self.quote = quote
def __getitem__(self, key):
if key in self.items:
return self.quote(self.items[key])
to_section = self.shuffled_sections.from_name[key]
to_nr = self.name_to_nr[key]
res = self.ref_template % {
'nr' : to_section.nr,
'from_nr' : self.section.nr
'nr' : to_nr,
'from_nr' : self.from_nr
}
if key in self.shuffled_sections.name_to_nr:
if key in self.name_to_nr:
self.found.add(res)
return res

37
test_output.py Executable file
View file

@ -0,0 +1,37 @@
#!/usr/bin/env python2.7
import unittest
from unittest import TestCase
import output
class TestOutputFormat(TestCase):
def setUp(self):
pass
def test_create(self):
pass
class TestReferenceFormatter(TestCase):
def setUp(self):
pass
def test_create(self):
rf = output.ReferenceFormatter(1, {}, "", str)
def test_get_item(self):
rf = output.ReferenceFormatter(1, {'a' : 1, 'b' : 2}, "%(nr)d", int)
self.assertEqual(rf['nr'], 1)
def test_get_quoted_item(self):
rf = output.ReferenceFormatter(1, {'a' : 1, 'b' : 2}, "%(nr)d", str)
self.assertEqual(rf['nr'], '1')
def test_get_reference(self):
rf = output.ReferenceFormatter(1, {'a' : 1, 'b' : 2},
"%(from_nr)d to %(nr)d", None)
self.assertEqual(rf['b'], '1 to 2')
self.assertEquals(rf.found, set(['1 to 2']))
if __name__ == '__main__':
unittest.main()