Browse Source

[WIP][test] Improve clrf test

pull/160/head
Louis 9 years ago
parent
commit
5cc173fd0b
  1. 96
      test/test_chordpro/test_parser.py

96
test/test_chordpro/test_parser.py

@ -2,6 +2,7 @@
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
import contextlib
import glob import glob
import os import os
import unittest import unittest
@ -33,6 +34,28 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
maxDiff = None maxDiff = None
@staticmethod
@contextlib.contextmanager
def chdir():
"""Context to temporarry change current directory to this file directory
"""
olddir = os.getcwd()
os.chdir(resource_filename(__name__, ""))
yield
os.chdir(olddir)
def assertRender(self, destformat, sourcename, destname): # pylint: disable=invalid-name
"""Assert that `sourcename` is correctly rendered as `destname` in `destformat`.
"""
with self.chdir():
with open_read(destname) as expectfile:
with disable_logging():
song = self.song_plugins[LANGUAGES[destformat]]['sgc'](sourcename, self.config)
self.assertMultiLineEqual(
song.render(output=sourcename).strip(),
expectfile.read().strip(),
)
@classmethod @classmethod
def _iter_testmethods(cls): def _iter_testmethods(cls):
"""Iterate over song files to test.""" """Iterate over song files to test."""
@ -40,33 +63,25 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
cls.config = DEFAULT_CONFIG cls.config = DEFAULT_CONFIG
if 'datadir' not in cls.config: if 'datadir' not in cls.config:
cls.config['datadir'] = [] cls.config['datadir'] = []
cls.config['datadir'].append(resource_filename(__name__, 'datadir')) cls.config['datadir'].append('datadir')
cls.song_plugins = files.load_plugins( cls.song_plugins = files.load_plugins(
datadirs=cls.config['datadir'], datadirs=cls.config['datadir'],
root_modules=['songs'], root_modules=['songs'],
keyword='SONG_RENDERERS', keyword='SONG_RENDERERS',
) )
for source in sorted(glob.glob(os.path.join( with cls.chdir():
os.path.dirname(__file__), for source in sorted(glob.glob('*.source')):
'*.source', base = source[:-len(".source")]
))): for dest in LANGUAGES:
base = os.path.relpath(source, os.getcwd())[:-len(".source")] destname = "{}.{}".format(base, dest)
for dest in LANGUAGES: if not os.path.exists(destname):
destname = "{}.{}".format(base, dest) continue
if not os.path.exists(destname):
continue
yield (
"test_{}_{}".format(os.path.basename(base), dest),
cls._create_test(base, dest),
)
if os.path.basename(base) == 'newline':
yield ( yield (
"test_crlf_{}_{}".format(os.path.basename(base), dest), "test_{}_{}".format(base, dest),
cls._create_crlf_test(base, dest), cls._create_test(base, dest),
) )
@classmethod @classmethod
def _create_test(cls, base, dest): def _create_test(cls, base, dest):
"""Return a function testing that `base` compilation in `dest` format. """Return a function testing that `base` compilation in `dest` format.
@ -76,51 +91,24 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
"""Test that `base` is correctly parsed and rendered.""" """Test that `base` is correctly parsed and rendered."""
if base is None or dest is None: if base is None or dest is None:
return return
destname = "{}.{}".format(base, dest) self.assertRender(dest, "{}.source".format(base), "{}.{}".format(base, dest))
with open_read(destname) as expectfile:
chordproname = "{}.source".format(base)
with disable_logging():
self.assertMultiLineEqual(
self.song_plugins[LANGUAGES[dest]]['sgc'](chordproname, self.config).render(
output=chordproname,
).strip(),
expectfile.read().strip(),
)
test_parse_render.__doc__ = ( test_parse_render.__doc__ = (
"Test that '{base}' is correctly parsed and rendererd into '{format}' format." "Test that '{base}' is correctly parsed and rendererd into '{format}' format."
).format(base=os.path.basename(base), format=dest) ).format(base=os.path.basename(base), format=dest)
return test_parse_render return test_parse_render
@classmethod def test_clrf(self):
def _create_crlf_test(cls, base, dest): """Test that source is correctly parsed and rendered when line endings are CRLF.
"""Transform the `base` line endings into CRLF and test the compilation.
""" """
originalname = "newline.source"
def test_parse_render(self): chordproname = "newline.crlf.source"
"""Test that `base` is correctly parsed and rendered when line endings are CRLF. with self.chdir():
"""
if base is None or dest is None:
return
originalname = "{}.source".format(base)
chordproname = "{}.crlf.source".format(base)
with open_read(originalname) as originalfile: with open_read(originalname) as originalfile:
with open(chordproname, 'w') as chordprofile: with open(chordproname, 'w') as chordprofile:
for line in originalfile: for line in originalfile:
chordprofile.write(line.replace('\n', '\r\n')) chordprofile.write(line.replace('\n', '\r\n'))
destname = "{}.{}".format(base, dest) for dest in LANGUAGES:
with open_read(destname) as expectfile: with self.subTest(dest):
with disable_logging(): self.assertRender(dest, chordproname, "newline.{}".format(dest))
self.assertMultiLineEqual(
self.song_plugins[LANGUAGES[dest]]['sgc'](chordproname, self.config).render(
output=chordproname,
).strip(),
expectfile.read().strip(),
)
os.remove(chordproname) os.remove(chordproname)
test_parse_render.__doc__ = (
"Test that '{base}' is correctly parsed and rendererd into '{format}' format with CRLF."
).format(base=os.path.basename(base), format=dest)
return test_parse_render

Loading…
Cancel
Save