Skip to content

Commit

Permalink
Fixed #111 : Covering xml:lang on /text
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Sep 23, 2017
1 parent db9c094 commit c861ccb
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.1.5 - 2017-09-23

- ( Added a fix for #111 ) Refactored language test unit to cover all cases of guidelines
- Started refactoring a little some unit tests, with example generations.

## 1.1.4 - 2017-09-19

- Updated RNG checks for compatibility with _JAVA_OPTIONS environment variable on Travis
Expand Down
38 changes: 18 additions & 20 deletions HookTest/capitains_units/cts.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,27 +585,25 @@ def language(self):
""" Tests to make sure an xml:lang element is on the correct node
"""
if self.scheme == "epidoc":
try:
self.lang = self.xml.xpath('/tei:TEI/tei:text/tei:body/tei:div[@type="edition" or @type="translation" or @type="commentary"]',
namespaces=TESTUnit.NS)[0].get('{http://www.w3.org/XML/1998/namespace}lang')
except:
self.lang = ''
if self.lang == '' or self.lang is None:
self.lang = 'UNK'
yield False
else:
yield True
urns_holding_node = self.xml.xpath(
"//tei:text/tei:body/tei:div"
"[@type='edition' or @type='translation' or @type='commentary']"
"[starts-with(@n, 'urn:cts:')]",
namespaces=TESTUnit.NS
)
elif self.scheme == "tei":
try:
self.lang = self.xml.xpath('/tei:TEI/tei:text/tei:body',
namespaces=TESTUnit.NS)[0].get('{http://www.w3.org/XML/1998/namespace}lang')
except:
self.lang = ''
if self.lang == '' or self.lang is None:
self.lang = 'UNK'
yield False
else:
yield True
urns_holding_node = self.xml.xpath("//tei:text/tei:body[starts-with(@n, 'urn:cts:')]", namespaces=TESTUnit.NS) + \
self.xml.xpath("//tei:text[starts-with(@xml:base, 'urn:cts:')]", namespaces=TESTUnit.NS)

try:
self.lang = urns_holding_node[0].get('{http://www.w3.org/XML/1998/namespace}lang')
except:
self.lang = ''
if self.lang == '' or self.lang is None:
self.lang = 'UNK'
yield False
else:
yield True

def test(self, scheme, inventory=None):
""" Test a file with various checks
Expand Down
Empty file added tests/test_cts/__init__.py
Empty file.
Empty file.
80 changes: 80 additions & 0 deletions tests/test_cts/mocks/xmllang_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from MyCapytain.common.utils import xmlparser
from lxml.etree import tostring

TEMPLATES = """<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
<encodingDesc>
<refsDecl n="CTS">
<cRefPattern n="chapter" matchPattern="(.+).(.+)" replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div[@type='edition']/tei:div[@n='$1']/tei:div[@n='$2'])"/>
<cRefPattern n="book" matchPattern="(.+)" replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div[@type='edition']/tei:div[@n='$1'])"/>
</refsDecl>
</encodingDesc>
</teiHeader>
<text><body><div></div></body></text>
</TEI>"""
URN = "urn:cts:latinLit:phi1294.phi002.perseus-lat2"
LANG = "lat"

# The following variable is a list of element with error that they should throw and status (XML_OBJECT, STATUS, ERROR)
XMLLANG_DOCUMENTS = [
("tei", "<text>", "<text xml:base='{urn}' xml:lang='{lang}'>", True, "TEI works with urn and xml lang on @base/text"),
("tei", "<text>", "<text n='{urn}' xml:lang='{lang}'>", False, "TEI fails with urn and xml lang on @n/text"),
("tei", "<text>", "<text xml:base='{urn}'>", False, "TEI fails with urn without xml lang on @base/text"),
("tei", "<text>", "<text n='{urn}'>", False, "TEI fails with urn without xml lang on @n/text"),

("epidoc", "<text>", "<text xml:base='{urn}' xml:lang='{lang}'>", False, "Epidoc fails with urn and xml lang on @base/text"),
("epidoc", "<text>", "<text n='{urn}' xml:lang='{lang}'>", False, "Epidoc fails with urn and xml lang on @n/text"),
("epidoc", "<text>", "<text xml:base='{urn}'>", False, "Epidoc fails with urn without xml lang on @base/text"),
("epidoc", "<text>", "<text n='{urn}'>", False, "Epidoc fails with urn without xml lang on @n/text"),

("tei", "<body>", "<body xml:base='{urn}' xml:lang='{lang}'>", False, "TEI fails with urn and xml lang on @base/body"),
("tei", "<body>", "<body n='{urn}' xml:lang='{lang}'>", True, "TEI works with urn and xml lang on @n/body"),
("tei", "<body>", "<body xml:base='{urn}'>", False, "TEI fails with urn without xml lang on @base/body"),
("tei", "<body>", "<body n='{urn}'>", False, "TEI fails with urn without xml lang on @n/body"),

("epidoc", "<body>", "<body xml:base='{urn}' xml:lang='{lang}'>", False, "Epidoc fails with urn and xml lang on @base/body"),
("epidoc", "<body>", "<body n='{urn}' xml:lang='{lang}'>", False, "Epidoc fails with urn and xml lang on @n/body"),
("epidoc", "<body>", "<body xml:base='{urn}'>", False, "Epidoc fails with urn without xml lang on @base/body"),
("epidoc", "<body>", "<body n='{urn}'>", False, "Epidoc fails with urn without xml lang on @n/body"),

]
# Epidocs Tests
XMLLANG_DOCUMENTS += [
(scheme, source, replacement.replace("{epidoc}", type_epidoc), boolean, message.replace("{epidoc}", type_epidoc))
for scheme, source, replacement, boolean, message in
[
("tei", "<div>", "<div type='{epidoc}' n='{urn}' xml:lang='{lang}'>", False,
"TEI fails with urn and xml lang on @n/div-{epidoc}"),

("tei", "<div>", "<div type='{epidoc}' xml:base='{urn}' xml:lang='{lang}'>", False,
"TEI fails with urn and xml lang on @xml:base/div-{epidoc}"),

("tei", "<div>", "<div type='{epidoc}' xml:base='{urn}' xml:lang='{lang}'>", False,
"TEI fails with urn and without xml lang on @n/div-{epidoc}"),

("tei", "<div>", "<div type='{epidoc}' n='{urn}' xml:lang='{lang}'>", False,
"TEI fails with urn and without xml lang on @xml:base/div-{epidoc}"),

("epidoc", "<div>", "<div type='{epidoc}' n='{urn}' xml:lang='{lang}'>", True,
"Epidoc works with urn and xml lang on @n/div-{epidoc}"),

("epidoc", "<div>", "<div type='{epidoc}' xml:base='{urn}' xml:lang='{lang}'>", False,
"Epidoc fails with urn and xml lang on @xml:base/div-{epidoc}"),

("epidoc", "<div>", "<div type='{epidoc}' xml:base='{urn}'>", False,
"Epidoc fails with urn and without xml lang on @n/div-{epidoc}"),

("epidoc", "<div>", "<div type='{epidoc}' n='{urn}'>", False,
"Epidoc fails with urn and without xml lang on @xml:base/div-{epidoc}")
]
for type_epidoc in ["edition", "translation", "commentary"]
]
XMLLANG_DOCUMENTS = [
(
scheme,
tostring(xmlparser(TEMPLATES.replace(source, replacement).format(urn=URN, lang=LANG)), encoding=str),
boolean,
msg + " ("+replacement.format(urn=URN, lang=LANG)+")"
)
for scheme, source, replacement, boolean, msg in XMLLANG_DOCUMENTS
]
41 changes: 41 additions & 0 deletions tests/test_cts/test_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
import HookTest.capitains_units.cts
import HookTest.units
import tests.test_cts.mocks.xmllang_data as CAPITAINS_MOCKS
from lxml import etree

class TestCTS(unittest.TestCase):
""" Test the UnitTest for __cts__
"""
pass


class TestText(unittest.TestCase):
""" Test the UnitTests for Text
"""

def setUp(self):
self.backup = [x for x in HookTest.capitains_units.cts.CTSText_TestUnit.tests]

def tearDown(self):
HookTest.capitains_units.cts.CTSText_TestUnit.tests = self.backup

def unit_from_string(self, xml_string, scheme):
""" Generates a test unit based on xml_string and scheme
:param xml_string: String representation of the XML Document
:param scheme: Scheme to apply
:return: HookTest.capitains_units.cts.CTSText_TestUnit
"""
node = HookTest.capitains_units.cts.CTSText_TestUnit("/false/path")
node.scheme = scheme
node.xml = etree.fromstring(xml_string, HookTest.units.TESTUnit.PARSER)
return node

def test_xml_lang(self):
""" Ensure the XML:LANG test checks against the guideline
"""
for scheme, xml, status, message in CAPITAINS_MOCKS.XMLLANG_DOCUMENTS:
unit = self.unit_from_string(xml, scheme)
results = [result for result in unit.language()]
self.assertEqual(results[0], status, message)

0 comments on commit c861ccb

Please sign in to comment.