2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
from tempfile import NamedTemporaryFile as ntf
|
|
|
|
|
|
|
|
# -- Test Data
|
2016-02-16 19:11:37 +00:00
|
|
|
import example
|
2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
# -- SUT
|
|
|
|
from tldp.typeguesser import guess
|
2016-02-13 18:54:38 +00:00
|
|
|
from tldp.doctypes.common import SignatureChecker
|
2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2016-02-16 19:11:37 +00:00
|
|
|
for ex in example.sources:
|
2016-02-13 19:26:00 +00:00
|
|
|
if isinstance(ex.type, SignatureChecker):
|
|
|
|
dt = genericGuessTest(ex.content, ex['ext'])
|
|
|
|
self.assertEqual(ex.type, dt)
|
2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
def testDetectionByExtension(self):
|
2016-02-16 19:11:37 +00:00
|
|
|
for ex in example.sources:
|
2016-02-13 19:26:00 +00:00
|
|
|
if not isinstance(ex.type, SignatureChecker):
|
|
|
|
dt = genericGuessTest(ex.content, ex.ext)
|
|
|
|
self.assertEqual(ex.type, dt)
|
2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
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):
|
2016-02-16 19:11:37 +00:00
|
|
|
a = example.ex_docbook4xml.content
|
|
|
|
b = example.ex_docbook5xml.content
|
|
|
|
four, fourdt = a + b, example.ex_docbook4xml.type
|
2016-02-13 07:48:48 +00:00
|
|
|
dt = genericGuessTest(four, '.xml')
|
|
|
|
self.assertIs(dt, fourdt)
|
2016-02-16 19:11:37 +00:00
|
|
|
five, fivedt = b + a, example.ex_docbook5xml.type
|
2016-02-13 07:48:48 +00:00
|
|
|
dt = genericGuessTest(five, '.xml')
|
|
|
|
self.assertIs(dt, fivedt)
|
|
|
|
|
|
|
|
#
|
|
|
|
# -- end of file
|