initial test suite framework and first tests

This commit is contained in:
Martin A. Brown 2016-02-12 23:48:48 -08:00
parent 236ae3cbd4
commit 39ca112e40
4 changed files with 281 additions and 0 deletions

121
tests/examples.py Normal file
View File

@ -0,0 +1,121 @@
from __future__ import absolute_import, division, print_function
import tldp.doctypes
ex_linuxdoc = {
'ext': '.sgml',
'type': tldp.doctypes.linuxdoc.Linuxdoc,
'content': '''<!doctype linuxdoc system>
<article>
<title>B
<author>A
<date>2016-02-11
<abstract> abstract </abstract>
<toc>
<sect>Introduction
<p>
<sect>Stuff.
<p>
<sect>More-stuff.
<p>
</article>''',
}
ex_docbooksgml = {
'ext': '.sgml',
'type': tldp.doctypes.docbooksgml.DocbookSGML,
'content': '''<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook V4.1//EN">
<article>
<articleinfo>
<title>T</title>
<author>
<firstname>A</firstname> <surname>B</surname>
<affiliation>
<address><email>devnull@example.org</email></address>
</affiliation>
</author>
<pubdate>2016-02-11</pubdate>
<abstract><para>abstract</para> </abstract>
<revhistory>
<revision>
<revnumber>1.0</revnumber>
<date>2016-02-11</date>
<authorinitials>AB</authorinitials>
<revremark>Initial release.</revremark>
</revision>
</revhistory>
</articleinfo>
<sect1 id="intro"><title>Introduction</title>
<para>Text</para>
<sect2 id="copyright"><title>More stuff</title>
<para>Text</para>
</sect2>
</sect1>
</article>'''
}
ex_docbook4xml = {
'ext': '.xml',
'type': tldp.doctypes.docbook4xml.Docbook4XML,
'content': '''<?xml version="1.0" standalone="no"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<article>
<articleinfo>
<title>T</title>
<author><firstname>A</firstname><surname>B</surname></author>
<authorinitials>AB</authorinitials>
<revhistory> <revision>
<revnumber>v0.0</revnumber>
<date>2016-02-11</date>
<authorinitials>AB</authorinitials>
<revremark> Initial release. </revremark>
</revision> </revhistory>
<abstract> <para> abstract </para> </abstract>
</articleinfo>
<sect1 id="intro">
<title>Intro</title>
<para>Text</para>
<sect2>
<title>Intro</title>
<para>Text</para>
</sect2>
</sect1>
</article>'''
}
ex_docbook5xml = {
'ext': '.xml',
'type': tldp.doctypes.docbook5xml.Docbook5XML,
'content': '''<?xml version="1.0" encoding="utf-8"?>
<article xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="en">
<title>Sample article</title>
<para>This is a ridiculously terse article.</para>
</article>'''
}
ex_rst = {
'ext': '.rst',
'type': tldp.doctypes.rst.RestructuredText,
'content': '''Empty.''',
}
ex_text = {
'ext': '.txt',
'type': tldp.doctypes.text.Text,
'content': '''Empty.''',
}
ex_markdown = {
'ext': '.md',
'type': tldp.doctypes.markdown.Markdown,
'content': '''Empty.''',
}
# -- end of file

48
tests/test_sources.py Normal file
View File

@ -0,0 +1,48 @@
from __future__ import absolute_import, division, print_function
import os
import unittest
from tempfile import NamedTemporaryFile as ntf
from tempfile import mkdtemp, mkstemp
import shutil
# -- Test Data
from examples import *
# -- SUT
from tldp.sources import Sources, SourceDocument
class TestSources(unittest.TestCase):
def setUp(self):
self.tempdir = mkdtemp(prefix='tldp-sources-test-')
def tearDown(self):
shutil.rmtree(self.tempdir)
class TestInvalidSources(TestSources):
def test_validateDirs_onebad(self):
invalid0 = os.path.join(self.tempdir, 'unique', 'rabbit')
with self.assertRaises(OSError) as ecm:
Sources([invalid0])
e = ecm.exception
self.assertTrue('unique/rabbit' in e.message)
def test_validateDirs_multibad(self):
invalid0 = os.path.join(self.tempdir, 'unique', 'rabbit')
invalid1 = os.path.join(self.tempdir, 'affable', 'elephant')
with self.assertRaises(OSError) as ecm:
Sources([invalid0, invalid1])
e = ecm.exception
self.assertTrue('affable/elephant' in e.message)
def testEmptyDir(self):
s = Sources([self.tempdir])
self.assertEquals(0, len(s.docs))
#
# -- end of file

64
tests/test_typeguesser.py Normal file
View File

@ -0,0 +1,64 @@
from __future__ import absolute_import, division, print_function
import os
import unittest
from tempfile import NamedTemporaryFile as ntf
# -- Test Data
from examples import *
# -- SUT
from tldp.typeguesser import guess
def genericGuessTest(content, ext):
f = ntf(prefix='tldp-guesser-test-', suffix=ext, delete=False)
f.write(content)
f.flush()
dt = guess(f.name)
f.close()
os.unlink(f.name)
return dt
class TestDoctypes(unittest.TestCase):
def testDetectionBySignature(self):
for example in (ex_linuxdoc, ex_docbooksgml, ex_docbook4xml,
ex_docbook5xml):
dt = genericGuessTest(example['content'], example['ext'])
self.assertEqual(example['type'], dt)
def testDetectionByExtension(self):
for example in (ex_rst, ex_markdown, ex_text):
dt = genericGuessTest(example['content'], example['ext'])
self.assertEqual(example['type'], dt)
def testDetectionBogusExtension(self):
dt = genericGuessTest('franks-cheese-shop', '.wmix')
self.assertIsNone(dt)
def testDetectionMissingExtension(self):
dt = genericGuessTest('franks-cheese-shop', '')
self.assertIsNone(dt)
def testGuessNumber(self):
self.assertIsNone(guess(7))
def testGuessBadXML(self):
dt = genericGuessTest('<valid class="bogus">XML</valid>', '.xml')
self.assertIsNone(dt)
def testGuessTooManyMatches(self):
a = ex_docbook4xml['content']
b = ex_docbook5xml['content']
four, fourdt = a + b, ex_docbook4xml['type']
dt = genericGuessTest(four, '.xml')
self.assertIs(dt, fourdt)
five, fivedt = b + a, ex_docbook5xml['type']
dt = genericGuessTest(five, '.xml')
self.assertIs(dt, fivedt)
#
# -- end of file

48
tests/test_utils.py Normal file
View File

@ -0,0 +1,48 @@
from __future__ import absolute_import, division, print_function
import os
import unittest
from tempfile import NamedTemporaryFile as ntf
# -- SUT
from tldp.utils import makefh, which
class Test_which(unittest.TestCase):
def test_good_which_python(self):
python = which('python')
self.assertIsInstance(python, str)
self.assertTrue(os.path.isfile(python))
qualified_python = which(python)
self.assertEqual(python, qualified_python)
def test_bad_silly_name(self):
silly = which('silliest-executable-name-which-may-yet-be-possible')
self.assertIsNone(silly)
def test_fq_executable(self):
f = ntf(prefix='tldp-which-test', delete=False)
f.close()
notfound = which(f.name)
self.assertIsNone(notfound)
os.chmod(f.name, 0755)
found = which(f.name)
self.assertEqual(f.name, found)
os.unlink(f.name)
class Test_makefh(unittest.TestCase):
def test_makefh(self):
f = ntf(prefix='tldp-makefh-openfile-test-', delete=False)
# fprime = makefh(f.file)
# self.assertIs(f, fprime)
# del fprime
f.close()
fprime = makefh(f.name)
self.assertIs(f.name, fprime.name)
os.unlink(f.name)
#
# -- end of file