Skip to content

Commit

Permalink
Testing moved to separate script (+loading gimpfu)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-brajer committed Dec 21, 2020
1 parent e01f484 commit a783b15
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 85 deletions.
5 changes: 2 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
12 changes: 12 additions & 0 deletions src/MyMock.py
Original file line number Diff line number Diff line change
@@ -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
88 changes: 6 additions & 82 deletions src/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
"""


Expand All @@ -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')
Expand All @@ -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):
Expand Down Expand Up @@ -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 <https://semver.org/>`_.
"""
import re
#: FAQ: Is there a suggested regular expression (RegEx) to
# check a SemVer string?
pattern = (
r'^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0'
r'|[1-9]\d*)(?:-(?P<prerelease>(?: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<buildmetadata>[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 = (
'<data><card><command01_image><layer_type>image</layer_type>'
'<size parse="tuple">800, 500</size></command01_image>'
'</card></data>'
)
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()
104 changes: 104 additions & 0 deletions src/test_card_assembler.py
Original file line number Diff line number Diff line change
@@ -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 <https://semver.org/>`_.
"""
import re
#: FAQ: Is there a suggested regular expression (RegEx) to
# check a SemVer string?
pattern = (
r'^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0'
r'|[1-9]\d*)(?:-(?P<prerelease>(?: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<buildmetadata>[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 = (
'<data><card><command01_image><layer_type>image</layer_type>'
'<size parse="tuple">800, 500</size></command01_image>'
'</card></data>'
)
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)

0 comments on commit a783b15

Please sign in to comment.