Skip to content
Permalink
Browse files

Merge pull request #13416 from aeslaughter/MOOSEDocs-unit

Create tools for unit testing MOOSEDocs extensions
  • Loading branch information...
milljm committed Jun 10, 2019
2 parents ca64a63 + a0b8e79 commit d508c4ec7f0126735212be073da5fac93d0c1651
@@ -24,6 +24,6 @@
if MOOSE_PYTHON_DIR not in sys.path:
sys.path.append(MOOSE_PYTHON_DIR)

from MooseDocs import main
from MooseDocs import main #pylint: disable=wrong-import-position
if __name__ == '__main__':
sys.exit(main.run())
@@ -56,6 +56,8 @@ def read(self, page):
if isinstance(page, pages.Source) and page.source and os.path.exists(page.source):
LOG.debug('READ %s', page.source)
return common.read(page.source).lstrip('\n')
elif isinstance(page, pages.Text):
return page.content

def tokenize(self, root, content, page, group=None, line=1, report=True):
"""
@@ -80,8 +82,8 @@ def tokenize(self, root, content, page, group=None, line=1, report=True):
if token.name == 'ErrorToken':
msg = common.report_error(token['message'],
page.source,
token.info.line,
token.info[0],
token.info.line if token.info else None,
token.info[0] if token.info else token.text(),
token['traceback'],
u'TOKENIZE ERROR')
LOG.error(msg)
@@ -141,6 +141,9 @@ def write(self, page, result=None):
elif isinstance(page, pages.Directory):
create_directory(page.destination)

elif isinstance(page, pages.Text):
pass

else:
LOG.error('Unknown Node type: %s', type(page))

@@ -0,0 +1,101 @@
import unittest
from MooseDocs.common import load_extensions
from MooseDocs import base
from MooseDocs.tree import tokens, pages, html, latex

class MooseDocsTestCase(unittest.TestCase):
"""
A class to aid in creating unit tests for MOOSEDocs.
"""
EXTENSIONS = []
READER = base.MarkdownReader
RENDERER = base.HTMLRenderer
EXECUTIONER = None

def __init__(self, *args, **kwargs):
super(MooseDocsTestCase, self).__init__(*args, **kwargs)
self.__node = None
self.__ast = None
self.__meta = None
self.__result = None
self.__translator = None

@property
def meta(self):
"""Return the current Meta data"""
return self.__meta

@property
def ast(self):
"""Return the current AST"""
return self.__ast

@property
def result(self):
"""Return the current rendered result"""
return self.__result

def setup(self, content=None, reader=None, renderer=None, extensions=None, executioner=None):
"""Helper method for setting up MOOSEDocs objects"""
content = content or []
reader = self.READER() if reader is None else reader
renderer = self.RENDERER() if renderer is None else renderer
extensions = extensions or self.EXTENSIONS
executioner = executioner or self.EXECUTIONER

ext = load_extensions(extensions)
self.__translator = base.Translator(content, reader, renderer, ext, executioner)
self.__translator.init()

def tokenize(self, text, *args, **kwargs):
"""Helper for tokenization"""
if args or kwargs or (self.__translator is None):
self.setup(*args, **kwargs)

self.__node = pages.Text(text)
self.__ast, self.__meta = self.__translator.executioner.tokenize(self.__node)
return self.__ast

def render(self, ast, *args, **kwargs):
"""Helper for rendering AST"""
if args or kwargs or (self.__translator is None):
self.setup(*args, **kwargs)
self.__result = self.__translator.executioner.render(self.__node, ast, self.__meta)
return self.__result

def execute(self, text, *args, **kwargs):
"""Helper for tokenization and renderering"""
self.tokenize(text, *args, **kwargs)
self.render(self.__ast)
return self.__ast, self.__result

def assertToken(self, token, tname, **kwargs):
"""Helper for checking type and attributes of a token"""
self.assertEqual(token.name, tname)
self.assertAttributes(token, **kwargs)

def assertHTMLTag(self, tag, tname, **kwargs):
"""Helper for checking HTML tree nodes"""
self.assertIsInstance(tag, html.Tag)
self.assertEqual(tag.name, tname)
self.assertAttributes(tag, **kwargs)

def assertHTMLString(self, node, content, **kwargs):
self.assertIsInstance(node, html.String)
self.assertEqual(node.get('content'), content)
self.assertAttributes(node, **kwargs)

def assertLatex(self, node, tname, name, **kwargs):
self.assertEqual(node.__class__.__name__, tname)
self.assertEqual(node.name, name)
self.assertAttributes(node, **kwargs)

def assertLatexString(self, node, content, **kwargs):
self.assertIsInstance(node, latex.String)
self.assertEqual(node.get('content'), content)
self.assertAttributes(node, **kwargs)

def assertAttributes(self, node, **kwargs):
for key, value in kwargs.iteritems():
key = key.rstrip('_')
self.assertEqual(node[key], value)
@@ -1822,7 +1822,7 @@
skip = 'WIP'
[]
[]
</code></pre><ul><li>Unit</li><li>MooseDocs shall include a TokenComponent and RenderComponent objects for creating extensions.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/base:components</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include lexer object capable of recursively processing text.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/base:lexer</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include reader object for reading files.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/base:reader</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall have a storage structure for holding ordered items by name.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:storage</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include utility for parsing key, value pairs from a string.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:parse_settings</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include special exceptions for use during tokenization and rendering.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:exceptions</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a utility for creating text boxes suitable for terminal output.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:box</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a utility for perform variable type checking.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:check_types</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include mixin classes for creating objects that require configuration data, translator objects, or component objects.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:mixins</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include the ability to load extensions dynamically.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:load_extensions</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a YAML based configuration.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:load_config</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a database object for storing information regarding source files.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:class_database</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a tool for reading software quality assurance requirement information from test specifications.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:get_requirement</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include the ability to build a file tree from markdown files.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:get_pages</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a test that ensures that all python files within the tests directories have an associated test specification.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/integrity:spec</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a tree node object with arbitrary property creation.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:base</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include tree object suitable for abstract syntax trees.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:tokens</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include tree object suitable for HTML.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:html</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include tree object suitable for latex.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:latex</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include tree object suitable for file structures.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:page</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include the ability to build a MOOSE application syntax tree.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:syntax</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li></ul><pre class="moose-pre"><code class="language-text">[Tests]
</code></pre><ul><li>Unit</li><li>MooseDocs shall include a TokenComponent and RenderComponent objects for creating extensions.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/base:components</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include lexer object capable of recursively processing text.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/base:lexer</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include reader object for reading files.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/base:reader</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall have a storage structure for holding ordered items by name.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:storage</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include utility for parsing key, value pairs from a string.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:parse_settings</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include special exceptions for use during tokenization and rendering.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:exceptions</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a utility for creating text boxes suitable for terminal output.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:box</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a utility for perform variable type checking.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:check_types</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include mixin classes for creating objects that require configuration data, translator objects, or component objects.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:mixins</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include the ability to load extensions dynamically.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:load_extensions</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a YAML based configuration.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:load_config</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a database object for storing information regarding source files.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:class_database</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a tool for reading software quality assurance requirement information from test specifications.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:get_requirement</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include the ability to build a file tree from markdown files.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/common:get_pages</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a test that ensures that all python files within the tests directories have an associated test specification.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/integrity:spec</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>The documentation system shall include basic support for a markdown-like syntax.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit:core</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include a tree node object with arbitrary property creation.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:base</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include tree object suitable for abstract syntax trees.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:tokens</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include tree object suitable for HTML.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:html</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include tree object suitable for latex.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:latex</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include tree object suitable for file structures.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:page</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li><li>MooseDocs shall include the ability to build a MOOSE application syntax tree.<p>Specification: <a class="modal-trigger" href="#XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX">unit/tree:syntax</a></p><p class="moose-sqa-items">Issue(s): <a href="https://github.com/idaholab/moose/issues/6699">#6699</a></p></li></ul><pre class="moose-pre"><code class="language-text">[Tests]
issues = '#6699'
design = 'MooseDocs/index.md'
[components]
@@ -2537,6 +2537,15 @@
requirement = &quot;MooseDocs shall include a test that ensures that all python files within the tests directories have an associated test specification.&quot;
[../]
[]
</code></pre><pre class="moose-pre"><code class="language-text">[Tests]
design = 'MooseDocs/index.md'
issues = '#6699'
[core]
type = PythonUnitTest
input = test_core.py
requirement = &quot;The documentation system shall include basic support for a markdown-like syntax.&quot;
[]
[]
</code></pre><pre class="moose-pre"><code class="language-text">[Tests]
issues = '#6699'
design = 'MooseDocs/index.md'
@@ -4,4 +4,4 @@ \chapter{\label{package-extension}Package Extension}\href{http://www.mooseframew
/path/to/gcc-8.3.0
\end{verbatim}

\par This is a sentence with gcc-8.3.0 .
\par This is a sentence with gcc-8.3.0 .

0 comments on commit d508c4e

Please sign in to comment.
You can’t perform that action at this time.