diff --git a/.vscode/settings.json b/.vscode/settings.json index 4bd414b..b4ed1cd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -26,12 +26,11 @@ "C:\\Program Files\\GIMP 2\\lib\\gimp\\2.0\\python" ], "codemap.maxNestingLevel": 3, - "editor.formatOnSave": false, - "editor.formatOnPaste": false, - "editor.formatOnType": false, "autoDocstring.docstringFormat": "sphinx", "restructuredtext.sphinxBuildPath": "C:\\Python\\Scripts\\sphinx-build.exe", "restructuredtext.confPath": "${workspaceFolder}\\docs\\source", "restructuredtext.builtDocumentationPath": "${workspaceFolder}\\docs\\build\\html", "restructuredtext.languageServer.disabled": false, + "python.linting.pycodestyleEnabled": true, + "python.linting.enabled": true, } \ No newline at end of file diff --git a/src/MyMock.py b/src/MyMock.py new file mode 100644 index 0000000..415ed8b --- /dev/null +++ b/src/MyMock.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +""" +Mock class for Gimp's gimpfu import. + +Only members meant to be referenced in the main scope. +""" + + +class Gimpfu(): + PF_DIRNAME = PF_STRING = PF_TEXT = PF_BOOL = None + def register(self, **kwargs): pass + def main(self): pass diff --git a/src/blueprint.py b/src/blueprint.py index 33f0810..5d0fede 100644 --- a/src/blueprint.py +++ b/src/blueprint.py @@ -4,9 +4,6 @@ Read an XML file and produce a layout list, which is then used by the main script :mod:`cardassembler`. - -.. note:: - Run this script directly to run a :mod:`unittest`. """ @@ -15,12 +12,13 @@ __author__ = 'Martin Brajer' -import unittest - import xml.etree.ElementTree as ET -def main(path): +def main(): + path = '' + if not path: + return blueprint = Blueprint((path + '\\Blueprint.xml').decode('utf-8')) layout = blueprint.generate_layout('unique item example') palette = blueprint.generate_palette('color') @@ -37,7 +35,7 @@ class Blueprint(): """ #: Those tags are always stored in a :class:`list` & have extra treatment - # in :meth:`_step_in`. + #: in :meth:`_step_in`. SPECIAL_TAGS = ['next', 'text'] def __init__(self, file_path): @@ -231,79 +229,5 @@ def _harvest_leaves(self, color_tree): return palette -class TestCodeFormat(unittest.TestCase): - - def test_conformance(self): - """Test that we conform to PEP-8.""" - import pycodestyle - style = pycodestyle.StyleGuide() # (quiet=True) - result = style.check_files([ - 'src\\blueprint.py', - 'src\\cardassembler.py' - ]) - self.assertEqual(result.total_errors, 0, - "Found code style errors (and warnings).") - - def test_version(self): - """ Test whether :data:`__version__` follows - `Semantic Versioning 2.0.0 `_. - """ - import re - #: FAQ: Is there a suggested regular expression (RegEx) to - # check a SemVer string? - pattern = ( - r'^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0' - r'|[1-9]\d*)(?:-(?P(?:0|[1-9]\d*|\d*[a-zA-Z-][0-' - r'9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*' - r'))?(?:\+(?P[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)' - r'*))?$' - ) - self.assertTrue(re.search(pattern, __version__)) - - -class TestBlueprintMethods(unittest.TestCase): - - @classmethod - def setUpClass(cls): - - cls.blueprint = Blueprint(None) - #: Beginning of example :file:`Minimal blueprint.xml`. - XML = ( - 'image' - '800, 500' - '' - ) - cls.element_tree = ET.fromstring(XML) - #: Dict representation of :data:`XML`. - cls.DICT = {'card': {'command01_image': {'layer_type': 'image', - 'size': (800, 500)}}} - - def test_parse_int(self): - self.assertEqual(self.blueprint._parse('5', 'int'), 5) - - def test_parse_float(self): - self.assertAlmostEqual(self.blueprint._parse('1.3', 'float'), 1.3) - - def test_parse_tuple_spaces(self): - self.assertEqual( - self.blueprint._parse('3, 5', 'tuple'), - self.blueprint._parse('3,5', 'tuple')) - - def test_parse_unknown(self): - with self.assertRaises(ValueError): - self.blueprint._parse('test', 'foo') - - def test_ElementTree_to_dict(self): - self.assertEqual(self.blueprint._ElementTree_to_dict( - self.element_tree), self.DICT) - - def test_goto(self): - self.blueprint.data = self.DICT - self.assertEqual( - self.blueprint._goto('card command01_image'), - self.DICT['card']['command01_image']) - - if __name__ == '__main__': - unittest.main(exit=False) - # main('') + main() diff --git a/src/test_card_assembler.py b/src/test_card_assembler.py new file mode 100644 index 0000000..d40673a --- /dev/null +++ b/src/test_card_assembler.py @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- +""" +Testing of both scripts: :mod:`cardassembler` and :mod:`blueprint`. + +.. note:: + Run this script directly to run a :mod:`unittest`. +""" + + +import sys +import unittest + +import xml.etree.ElementTree as ET + +import blueprint +from MyMock import Gimpfu as Mock_Gimpfu # Bypass internal Gimp's python. +sys.modules['gimpfu'] = Mock_Gimpfu() +import cardassembler # nopep8 + + +class TestCodeFormat(unittest.TestCase): + + def test_conformance(self): + """ Test that we conform to PEP-8. """ + import pycodestyle + style = pycodestyle.StyleGuide() # (quiet=True) + result = style.check_files([ + 'src\\blueprint.py', + 'src\\cardassembler.py' + ]) + self.assertEqual(result.total_errors, 0, + "Found code style errors (and warnings).") + + def test_version(self): + """ Test whether :data:`__version__` follows + `Semantic Versioning 2.0.0 `_. + """ + import re + #: FAQ: Is there a suggested regular expression (RegEx) to + # check a SemVer string? + pattern = ( + r'^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0' + r'|[1-9]\d*)(?:-(?P(?:0|[1-9]\d*|\d*[a-zA-Z-][0-' + r'9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*' + r'))?(?:\+(?P[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)' + r'*))?$' + ) + self.assertTrue(re.search(pattern, blueprint.__version__)) + + def test_version_equal(self): + self.assertEqual(cardassembler.__version__, blueprint.__version__) + + def test_author_equal(self): + self.assertEqual(cardassembler.__author__, blueprint.__author__) + + +class TestBlueprintMethods(unittest.TestCase): + + @classmethod + def setUpClass(cls): + + cls.blueprint = blueprint.Blueprint(None) + #: Beginning of example :file:`Minimal blueprint.xml`. + XML = ( + 'image' + '800, 500' + '' + ) + cls.element_tree = ET.fromstring(XML) + #: Dict representation of :data:`XML`. + cls.DICT = {'card': {'command01_image': { + 'layer_type': 'image', + 'size': (800, 500), + }}} + + def test_parse_int(self): + self.assertEqual(self.blueprint._parse('5', 'int'), 5) + + def test_parse_float(self): + self.assertAlmostEqual(self.blueprint._parse('1.3', 'float'), 1.3) + + def test_parse_tuple_spaces(self): + self.assertEqual( + self.blueprint._parse('3, 5', 'tuple'), + self.blueprint._parse('3,5', 'tuple')) + + def test_parse_unknown(self): + with self.assertRaises(ValueError): + self.blueprint._parse('test', 'foo') + + def test_ElementTree_to_dict(self): + self.assertEqual( + self.blueprint._ElementTree_to_dict(self.element_tree), + self.DICT) + + def test_goto(self): + self.blueprint.data = self.DICT + self.assertEqual( + self.blueprint._goto('card command01_image'), + self.DICT['card']['command01_image']) + + +if __name__ == '__main__': + unittest.main(exit=False)