#!/usr/bin/python
"""
$Id: structure.py,v 1.2 2006/06/12 10:11:45 dom Exp $

The TableTestCase implements the validator.testcase.TestCase
interface for the tests related to the following BPs:
* Use features of the markup language to indicate logical document structure.


License
-------
Copyright (c) 2006 World Wide Web Consortium, (Massachusetts
Institute of Technology, European Research Consortium for Informatics
and Mathematics, Keio University). All Rights Reserved. This work is
distributed under the W3C Software License [1] in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[1] http://www.w3.org/Consortium/Legal/copyright-software

"""


from  validator import testcase 

# Tests regarding the STRUCTURE BP
class StructureTestCase(testcase.XMLBasedTestCase):
    BpId = ["STRUCTURE"]

    def startDocument(self):
        self._currentHeadingLevel = 0
            
    def startElement(self, name, attrs):
        # let's check if the order of the headings is logical
        if name in ["h1","h2","h3","h4","h5","h6"]:
            location = testcase.LineColumnLocation(self.uri,self.getEncoding(),self.locator.getLineNumber(),self.locator.getColumnNumber())
            headingLevel = int(name[1])
            if headingLevel > self._currentHeadingLevel + 1:
                self.addObservation(testcase.Observation("SU2",location))
            self._currentHeadingLevel = headingLevel

    def endDocument(self):
        if not "SU2" in self._observations:
            self.addObservation(testcase.Observation("SU1",testcase.Location(self.uri)))



# -------------------------
# Unit tests for this module
import unittest

from validator.testcase import TestResults, Observation, Location, LineColumnLocation
class Tests(unittest.TestCase):
    def _test(self,inp,res):
        a = StructureTestCase()
        self.assertEqual(a.run(inp),res)
        
    def testWithLogicalHeadings(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/headings-1.xhtml"
        res = TestResults(
            [Observation("SU1",Location(inp))]
            )
        self._test(inp,res)

    def testWithIllogicalHeadings(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/headings-2.xhtml"
        res = TestResults(
            [Observation('SU2',LineColumnLocation(inp,'utf-8',11,0)),
             Observation('SU2',LineColumnLocation(inp,'utf-8',18,0) )])
        self._test(inp,res)



def _test():
    unittest.main()

if __name__ == '__main__':
    _test()



