Browse Source

[WIP] Test of `patatools convert FOO` works

- Errors are not tested yet
- Pylint may fail
pull/189/head
Louis 9 years ago
parent
commit
0e54d18ab1
  1. 10
      patacrep/tools/convert/__main__.py
  2. 85
      test/test_patatools/test_convert.py
  3. 12
      test/test_patatools/test_convert_errors/song.tsg
  4. 1
      test/test_patatools/test_convert_success/.gitignore
  5. 45
      test/test_patatools/test_convert_success/greensleeves.csg
  6. 62
      test/test_patatools/test_convert_success/greensleeves.csg.tsg.control

10
patacrep/tools/convert/__main__.py

@ -25,15 +25,15 @@ def confirm(destname):
def main(args=None):
"""Main function: run from command line."""
if args is None:
args = sys.argv[1:]
if len(args) < 3:
args = sys.argv
if len(args) < 4:
LOGGER.error("Invalid number of arguments.")
LOGGER.error("Usage: %s", _usage())
sys.exit(1)
source = args[0]
dest = args[1]
song_files = args[2:]
source = args[1]
dest = args[2]
song_files = args[3:]
renderers = files.load_plugins(
datadirs=DEFAULT_CONFIG.get('datadir', []),

85
test/test_patatools/test_convert.py

@ -0,0 +1,85 @@
"""Tests of the patatools-convert command."""
# pylint: disable=too-few-public-methods
from pkg_resources import resource_filename
import contextlib
import glob
import os
import shutil
import unittest
from patacrep import files
from patacrep.tools.__main__ import main as tools_main
from patacrep.encoding import open_read
from patacrep.tools.convert.__main__ import main as convert_main
from patacrep.songbook.__main__ import main as songbook_main
from .. import dynamic
from .. import logging_reduced
class TestConvert(unittest.TestCase, metaclass=dynamic.DynamicTest):
"""Test of the "patatools convert" subcommand"""
def _system(self, main, args):
try:
main(args)
except SystemExit as systemexit:
return systemexit.code
return 0
def assertConvert(self, basename, in_format, out_format):
"""Test of the "patatools convert" subcommand"""
sourcename = "{}.{}".format(basename, in_format)
destname = "{}.{}".format(basename, out_format)
controlname = "{}.{}.control".format(sourcename, out_format)
for main, args in [
(tools_main, ["patatools", "convert"]),
(convert_main, ["patatools-convert"]),
]:
with self.subTest(main=main, args=args):
with self.chdir("test_convert_success"):
with open_read(controlname) as controlfile:
with logging_reduced():
if os.path.exists(destname):
os.remove(destname)
self._system(main, args + [in_format, out_format, sourcename])
expected = controlfile.read().strip().replace(
"@TEST_FOLDER@",
files.path2posix(resource_filename(__name__, "")),
)
with open_read(destname) as destfile:
self.assertMultiLineEqual(
destfile.read().strip(),
expected,
)
@staticmethod
@contextlib.contextmanager
def chdir(*path):
"""Context to temporarry change current directory, relative to this file directory
"""
with files.chdir(resource_filename(__name__, ""), *path):
yield
@classmethod
def _iter_testmethods(cls):
"""Iterate over song files to test."""
with cls.chdir("test_convert_success"):
for control in sorted(glob.glob('*.*.*.control')):
[*base, in_format, out_format, _] = control.split('.')
base = '.'.join(base)
yield (
"test_{}_{}_{}".format(base, in_format, out_format),
cls._create_test(base, in_format, out_format),
)
@classmethod
def _create_test(cls, base, in_format, out_format):
"""Return a function testing that `base` compilation from `in_format` to `out_format` format.
"""
test_parse_render = lambda self: self.assertConvert(base, in_format, out_format)
test_parse_render.__doc__ = (
"Test that '{base}.{in_format}' is correctly converted to '{out_format}'."
).format(base=os.path.basename(base), in_format=in_format, out_format=out_format)
return test_parse_render

12
test/test_patatools/test_convert_errors/song.tsg

@ -0,0 +1,12 @@
\beginsong{Wonderful song}
[by={Anonymous}]
\begin{verse}
\[A]La la la\\
\end{verse}
\begin{chorus}
\[C]La la la\\
\end{chorus}
\endsong

1
test/test_patatools/test_convert_success/.gitignore

@ -0,0 +1 @@
greensleeves.tsg

45
test/test_patatools/test_convert_success/greensleeves.csg

@ -0,0 +1,45 @@
{lang: en}
{columns: 2}
{title: Greensleeves}
{title: Un autre sous-titre}
{title: Un sous titre}
{artist: Traditionnel}
{album: Angleterre}
A[Am]las, my love, ye [G]do me wrong
To [Am]cast me oft dis[E]curteously
And [Am]I have loved [G]you so long
De[Am]lighting [E]in your [Am]companie
{start_of_chorus}
[C]Green[B]sleeves was [G]all my joy
[Am]Greensleeves was [E]my delight
[C]Greensleeves was my [G]heart of gold
And [Am]who but [E]Ladie [Am]Greensleeves
{end_of_chorus}
I [Am]have been ready [G]at your hand
To [Am]grant what ever [E]you would crave
I [Am]have both waged [G]life and land
Your [Am]love and [E]good will [Am]for to have
I [Am]bought thee kerchers [G]to thy head
That [Am]were wrought fine and [E]gallantly
I [Am]kept thee both at [G]boord and bed
Which [Am]cost my [E]purse well [Am]favouredly
I [Am]bought thee peticotes [G]of the best
The [Am]cloth so fine as [E]fine might be
I [Am]gave thee jewels [G]for thy chest
And [Am]all this [E]cost I [Am]spent on thee
Thy [Am]smock of silke, both [G]faire and white
With [Am]gold embrodered [E]gorgeously
Thy [Am]peticote of [G]sendall right
And [Am]this I [E]bought thee [Am]gladly

62
test/test_patatools/test_convert_success/greensleeves.csg.tsg.control

@ -0,0 +1,62 @@
\selectlanguage{english}
\songcolumns{2}
\beginsong{Greensleeves\\
Un autre sous-titre\\
Un sous titre}[
by={
Traditionnel },
album={Angleterre},
]
\begin{verse}
A\[Am]las, my love, ye \[G]do me wrong
To \[Am]cast me oft dis\[E]curteously
And \[Am]I have loved \[G]you so long
De\[Am]lighting \[E]in your \[Am]companie
\end{verse}
\begin{chorus}
\[C]Green\[B]sleeves was \[G]all my joy
\[Am]Greensleeves was \[E]my delight
\[C]Greensleeves was my \[G]heart of gold
And \[Am]who but \[E]Ladie \[Am]Greensleeves
\end{chorus}
\begin{verse}
I \[Am]have been ready \[G]at your hand
To \[Am]grant what ever \[E]you would crave
I \[Am]have both waged \[G]life and land
Your \[Am]love and \[E]good will \[Am]for to have
\end{verse}
\begin{verse}
I \[Am]bought thee kerchers \[G]to thy head
That \[Am]were wrought fine and \[E]gallantly
I \[Am]kept thee both at \[G]boord and bed
Which \[Am]cost my \[E]purse well \[Am]favouredly
\end{verse}
\begin{verse}
I \[Am]bought thee peticotes \[G]of the best
The \[Am]cloth so fine as \[E]fine might be
I \[Am]gave thee jewels \[G]for thy chest
And \[Am]all this \[E]cost I \[Am]spent on thee
\end{verse}
\begin{verse}
Thy \[Am]smock of silke, both \[G]faire and white
With \[Am]gold embrodered \[E]gorgeously
Thy \[Am]peticote of \[G]sendall right
And \[Am]this I \[E]bought thee \[Am]gladly
\end{verse}
\endsong
Loading…
Cancel
Save