From 7c8653d8b2194538974966e77b44adc922c77cd5 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Thu, 19 Nov 2015 09:43:31 +0100 Subject: [PATCH] Create content tests --- test/test_content/__init__.py | 0 test/test_content/datadir/songs/chordpro.csg | 51 +++++++++ test/test_content/datadir/songs/exsong.sg | 10 ++ test/test_content/datadir/songs/intersong.is | 6 + test/test_content/datadir/songs/jsonlist.json | 0 .../datadir/songs/subdir/chordpro.csg | 51 +++++++++ test/test_content/datadir/songs/texfile.tex | 70 ++++++++++++ test/test_content/datadir/songs/texsong.tsg | 70 ++++++++++++ test/test_content/sections.control | 1 + test/test_content/sections.source | 6 + test/test_content/songs.control | 1 + test/test_content/songs.source | 1 + test/test_content/sorted.control | 1 + test/test_content/sorted.source | 1 + test/test_content/test_content.py | 106 ++++++++++++++++++ 15 files changed, 375 insertions(+) create mode 100644 test/test_content/__init__.py create mode 100644 test/test_content/datadir/songs/chordpro.csg create mode 100644 test/test_content/datadir/songs/exsong.sg create mode 100644 test/test_content/datadir/songs/intersong.is create mode 100644 test/test_content/datadir/songs/jsonlist.json create mode 100644 test/test_content/datadir/songs/subdir/chordpro.csg create mode 100644 test/test_content/datadir/songs/texfile.tex create mode 100644 test/test_content/datadir/songs/texsong.tsg create mode 100644 test/test_content/sections.control create mode 100644 test/test_content/sections.source create mode 100644 test/test_content/songs.control create mode 100644 test/test_content/songs.source create mode 100644 test/test_content/sorted.control create mode 100644 test/test_content/sorted.source create mode 100644 test/test_content/test_content.py diff --git a/test/test_content/__init__.py b/test/test_content/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/test_content/datadir/songs/chordpro.csg b/test/test_content/datadir/songs/chordpro.csg new file mode 100644 index 00000000..c1295968 --- /dev/null +++ b/test/test_content/datadir/songs/chordpro.csg @@ -0,0 +1,51 @@ +{lang : en} +{columns : 2} +{ title : Greensleeves} +{subtitle: Test of the chordpro format} +{artist: Traditionnel} +{artist: Prénom Nom} +{cover : traditionnel.jpg } +{album :Angleterre} + +{partition : greensleeves.ly} + + +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]Greensleeves 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 + +{c:test of comment} + +{gc: test of guitar comment} + +{image: traditionnel.jpg} + +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 + + diff --git a/test/test_content/datadir/songs/exsong.sg b/test/test_content/datadir/songs/exsong.sg new file mode 100644 index 00000000..f37f351f --- /dev/null +++ b/test/test_content/datadir/songs/exsong.sg @@ -0,0 +1,10 @@ +\beginsong{Image included from a different datadir\\\LaTeX} + [cover={img/datadir2}] + + \cover + + \lilypond{scores/datadir2.ly} + + \image{img/datadir2} + +\endsong diff --git a/test/test_content/datadir/songs/intersong.is b/test/test_content/datadir/songs/intersong.is new file mode 100644 index 00000000..dbeeb547 --- /dev/null +++ b/test/test_content/datadir/songs/intersong.is @@ -0,0 +1,6 @@ +\selectlanguage{french} +\sortassong{}[by={QQ}] +\begin{intersong} + +Lorem ipsum +\end{intersong} diff --git a/test/test_content/datadir/songs/jsonlist.json b/test/test_content/datadir/songs/jsonlist.json new file mode 100644 index 00000000..e69de29b diff --git a/test/test_content/datadir/songs/subdir/chordpro.csg b/test/test_content/datadir/songs/subdir/chordpro.csg new file mode 100644 index 00000000..c1295968 --- /dev/null +++ b/test/test_content/datadir/songs/subdir/chordpro.csg @@ -0,0 +1,51 @@ +{lang : en} +{columns : 2} +{ title : Greensleeves} +{subtitle: Test of the chordpro format} +{artist: Traditionnel} +{artist: Prénom Nom} +{cover : traditionnel.jpg } +{album :Angleterre} + +{partition : greensleeves.ly} + + +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]Greensleeves 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 + +{c:test of comment} + +{gc: test of guitar comment} + +{image: traditionnel.jpg} + +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 + + diff --git a/test/test_content/datadir/songs/texfile.tex b/test/test_content/datadir/songs/texfile.tex new file mode 100644 index 00000000..81a725ef --- /dev/null +++ b/test/test_content/datadir/songs/texfile.tex @@ -0,0 +1,70 @@ +\selectlanguage{french} +\songcolumns{2} +\beginsong{Chevaliers de la table ronde} + [by={Traditionnel},cover={traditionnel},album={France}] + + \cover + \gtab{C}{X32010} + \gtab{G7}{320001} + \gtab{F}{1:022100} + + \begin{verse} + Cheva\[C]liers de la Table Ronde + Goûtons \[G7]voir si le vin est \[C]bon + \rep{2} + \end{verse} + + \begin{chorus} + Goûtons \[F]voir, \echo{oui, oui, oui} + Goûtons \[C]voir, \echo{non, non, non} + Goûtons \[G7]voir si le vin est bon + \rep{2} + \end{chorus} + + \begin{verse} + S'il est bon, s'il est agréable + J'en boirai jusqu'à mon plaisir + \end{verse} + + \begin{verse} + J'en boirai cinq à six bouteilles + Et encore, ce n'est pas beaucoup + \end{verse} + + \begin{verse} + Si je meurs, je veux qu'on m'enterre + Dans une cave où il y a du bon vin + \end{verse} + + \begin{verse} + Les deux pieds contre la muraille + Et la tête sous le robinet + \end{verse} + + \begin{verse} + Et les quatre plus grands ivrognes + Porteront les quatre coins du drap + \end{verse} + + \begin{verse} + Pour donner le discours d'usage + On prendra le bistrot du coin + \end{verse} + + \begin{verse} + Et si le tonneau se débouche + J'en boirai jusqu'à mon plaisir + \end{verse} + + \begin{verse} + Et s'il en reste quelques gouttes + Ce sera pour nous rafraîchir + \end{verse} + + \begin{verse} + Sur ma tombe, je veux qu'on inscrive + \emph{Ici gît le roi des buveurs} + \end{verse} + +\endsong + diff --git a/test/test_content/datadir/songs/texsong.tsg b/test/test_content/datadir/songs/texsong.tsg new file mode 100644 index 00000000..81a725ef --- /dev/null +++ b/test/test_content/datadir/songs/texsong.tsg @@ -0,0 +1,70 @@ +\selectlanguage{french} +\songcolumns{2} +\beginsong{Chevaliers de la table ronde} + [by={Traditionnel},cover={traditionnel},album={France}] + + \cover + \gtab{C}{X32010} + \gtab{G7}{320001} + \gtab{F}{1:022100} + + \begin{verse} + Cheva\[C]liers de la Table Ronde + Goûtons \[G7]voir si le vin est \[C]bon + \rep{2} + \end{verse} + + \begin{chorus} + Goûtons \[F]voir, \echo{oui, oui, oui} + Goûtons \[C]voir, \echo{non, non, non} + Goûtons \[G7]voir si le vin est bon + \rep{2} + \end{chorus} + + \begin{verse} + S'il est bon, s'il est agréable + J'en boirai jusqu'à mon plaisir + \end{verse} + + \begin{verse} + J'en boirai cinq à six bouteilles + Et encore, ce n'est pas beaucoup + \end{verse} + + \begin{verse} + Si je meurs, je veux qu'on m'enterre + Dans une cave où il y a du bon vin + \end{verse} + + \begin{verse} + Les deux pieds contre la muraille + Et la tête sous le robinet + \end{verse} + + \begin{verse} + Et les quatre plus grands ivrognes + Porteront les quatre coins du drap + \end{verse} + + \begin{verse} + Pour donner le discours d'usage + On prendra le bistrot du coin + \end{verse} + + \begin{verse} + Et si le tonneau se débouche + J'en boirai jusqu'à mon plaisir + \end{verse} + + \begin{verse} + Et s'il en reste quelques gouttes + Ce sera pour nous rafraîchir + \end{verse} + + \begin{verse} + Sur ma tombe, je veux qu'on inscrive + \emph{Ici gît le roi des buveurs} + \end{verse} + +\endsong + diff --git a/test/test_content/sections.control b/test/test_content/sections.control new file mode 100644 index 00000000..3666bbce --- /dev/null +++ b/test/test_content/sections.control @@ -0,0 +1 @@ +["section:Traditional", "exsong.sg", "section:Example", "texsong.tsg", "chordpro.csg", "exsong.sg"] \ No newline at end of file diff --git a/test/test_content/sections.source b/test/test_content/sections.source new file mode 100644 index 00000000..339815be --- /dev/null +++ b/test/test_content/sections.source @@ -0,0 +1,6 @@ +[["section", "Traditional"], + "exsong.sg", + ["section", "Example"], + "texsong.tsg", + "chordpro.csg", + "exsong.sg"] \ No newline at end of file diff --git a/test/test_content/songs.control b/test/test_content/songs.control new file mode 100644 index 00000000..e85dfea7 --- /dev/null +++ b/test/test_content/songs.control @@ -0,0 +1 @@ +["exsong.sg", "texsong.tsg", "chordpro.csg", "subdir/chordpro.csg"] \ No newline at end of file diff --git a/test/test_content/songs.source b/test/test_content/songs.source new file mode 100644 index 00000000..dcd19323 --- /dev/null +++ b/test/test_content/songs.source @@ -0,0 +1 @@ +["exsong.sg", "intersong.is", "jsonlist.json", "texfile.tex", "texsong.tsg", "chordpro.csg", "subdir/chordpro.csg"] \ No newline at end of file diff --git a/test/test_content/sorted.control b/test/test_content/sorted.control new file mode 100644 index 00000000..5cfaa29d --- /dev/null +++ b/test/test_content/sorted.control @@ -0,0 +1 @@ +["chordpro.csg", "exsong.sg", "subdir/chordpro.csg", "texsong.tsg"] \ No newline at end of file diff --git a/test/test_content/sorted.source b/test/test_content/sorted.source new file mode 100644 index 00000000..5b2bbf3a --- /dev/null +++ b/test/test_content/sorted.source @@ -0,0 +1 @@ +[["sorted(fullpath)"]] \ No newline at end of file diff --git a/test/test_content/test_content.py b/test/test_content/test_content.py new file mode 100644 index 00000000..c396ccf5 --- /dev/null +++ b/test/test_content/test_content.py @@ -0,0 +1,106 @@ +"""Tests for the content plugins.""" + +# pylint: disable=too-few-public-methods + +import glob +import os +import unittest +import json + +from patacrep.songs import DataSubpath, DEFAULT_CONFIG +from patacrep import content, files +from patacrep.content.song import SongRenderer +from patacrep.content.section import Section + +from .. import dynamic # pylint: disable=unused-import + +class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest): + """Test of the content plugins. + + For any given `foo.source`, it parses the content as a json "content" + argument of a .sb file. + It controls that the generated file list is equal to the one in `foo.control`. + """ + + maxDiff = None + config = None + + @classmethod + def setUpClass(cls): + cls._generate_config() + + @classmethod + def _iter_testmethods(cls): + """Iterate over dynamically generated test methods""" + for source in sorted(glob.glob(os.path.join( + os.path.dirname(__file__), + '*.source', + ))): + base = source[:-len(".source")] + yield ( + "test_content_{}".format(os.path.basename(base)), + cls._create_content_test(base), + ) + + @classmethod + def _create_content_test(cls, base): + """Return a function that `base.source` produces the correct file list""" + + def test_content(self): + """Test that `base.source` produces the correct file list""" + sourcename = "{}.source".format(base) + with open(sourcename, mode="r", encoding="utf8") as sourcefile: + sbcontent = json.load(sourcefile) + + expandedlist = content.process_content(sbcontent, cls.config.copy()) + sourcelist = list(map(cls._clean_path, expandedlist)) + + controlname = "{}.control".format(base) + if not os.path.exists(controlname): + raise Exception("Missing control:" + str(sourcelist).replace("'",'"')) + with open(controlname, mode="r", encoding="utf8") as controlfile: + controllist = json.load(controlfile) + + self.assertEqual(controllist, sourcelist) + + test_content.__doc__ = ( + "Test that '{base}.source' produces the correct file list""" + ).format(base=os.path.basename(base)) + return test_content + + @classmethod + def _clean_path(cls, elem): + """Shorten the path relative to the `songs` directory""" + if isinstance(elem, SongRenderer): + songpath = os.path.join(os.path.dirname(__file__), 'datadir', 'songs') + return files.relpath(elem.song.fullpath, songpath) + elif isinstance(elem, Section): + return "{}:{}".format(elem.keyword, elem.name) + else: + raise Exception(elem) + + @classmethod + def _generate_config(cls): + """Generate the config to process the content""" + + config = DEFAULT_CONFIG.copy() + + datadirpath = os.path.join(os.path.dirname(__file__), 'datadir') + + config['datadir'] = [datadirpath] + + config['_songdir'] = [ + DataSubpath(path, 'songs') + for path in config['datadir'] + ] + config['_content_plugins'] = files.load_plugins( + datadirs=config.get('datadir', []), + root_modules=['content'], + keyword='CONTENT_PLUGINS', + ) + config['_song_plugins'] = files.load_plugins( + datadirs=config.get('datadir', []), + root_modules=['songs'], + keyword='SONG_RENDERERS', + )['tsg'] + cls.config = config