devtools/testlib.py
changeset 6980 8e59c2cdcc99
parent 6967 07d889e3f35d
parent 6979 a8fbcf9b6572
child 7040 9b1f9bc74f5d
equal deleted inserted replaced
6975:75a232b2e477 6980:8e59c2cdcc99
   662         self.assertEqual(len(self.open_sessions), nbsessions)
   662         self.assertEqual(len(self.open_sessions), nbsessions)
   663         clear_cache(req, 'get_authorization')
   663         clear_cache(req, 'get_authorization')
   664 
   664 
   665     # content validation #######################################################
   665     # content validation #######################################################
   666 
   666 
   667     def assertDocTestFile(self, testfile):
       
   668         # doctest returns tuple (failure_count, test_count)
       
   669         result = self.shell().process_script(testfile)
       
   670         if result[0] and result[1]:
       
   671             raise self.failureException("doctest file '%s' failed"
       
   672                                         % testfile)
       
   673 
       
   674     # validators are used to validate (XML, DTD, whatever) view's content
   667     # validators are used to validate (XML, DTD, whatever) view's content
   675     # validators availables are :
   668     # validators availables are :
   676     #  DTDValidator : validates XML + declared DTD
   669     #  DTDValidator : validates XML + declared DTD
   677     #  SaxOnlyValidator : guarantees XML is well formed
   670     #  SaxOnlyValidator : guarantees XML is well formed
   678     #  None : do not try to validate anything
   671     #  None : do not try to validate anything
   753             except:
   746             except:
   754                 msg = '[%s in %s] undisplayable exception' % (klass, view.__regid__)
   747                 msg = '[%s in %s] undisplayable exception' % (klass, view.__regid__)
   755             raise AssertionError, msg, tcbk
   748             raise AssertionError, msg, tcbk
   756         return self._check_html(output, view, template)
   749         return self._check_html(output, view, template)
   757 
   750 
       
   751     def get_validator(self, view=None, content_type=None, output=None):
       
   752         if view is not None:
       
   753             try:
       
   754                 return self.vid_validators[view.__regid__]()
       
   755             except KeyError:
       
   756                 if content_type is None:
       
   757                     content_type = view.content_type
       
   758         if content_type is None:
       
   759             content_type = 'text/html'
       
   760         if content_type in ('text/html', 'application/xhtml+xml'):
       
   761             if output and output.startswith('<?xml'):
       
   762                 default_validator = htmlparser.DTDValidator
       
   763             else:
       
   764                 default_validator = htmlparser.HTMLValidator
       
   765         else:
       
   766             default_validator = None
       
   767         validatorclass = self.content_type_validators.get(content_type,
       
   768                                                           default_validator)
       
   769         if validatorclass is None:
       
   770             return
       
   771         return validatorclass()
       
   772 
   758     @nocoverage
   773     @nocoverage
   759     def _check_html(self, output, view, template='main-template'):
   774     def _check_html(self, output, view, template='main-template'):
   760         """raises an exception if the HTML is invalid"""
   775         """raises an exception if the HTML is invalid"""
   761         output = output.strip()
   776         output = output.strip()
   762         try:
   777         validator = self.get_validator(view, output=output)
   763             validatorclass = self.vid_validators[view.__regid__]
   778         if validator is None:
   764         except KeyError:
       
   765             if view.content_type in ('text/html', 'application/xhtml+xml'):
       
   766                 if output.startswith('<?xml'):
       
   767                     default_validator = htmlparser.DTDValidator
       
   768                 else:
       
   769                     default_validator = htmlparser.HTMLValidator
       
   770             else:
       
   771                 default_validator = None
       
   772             validatorclass = self.content_type_validators.get(view.content_type,
       
   773                                                               default_validator)
       
   774         if validatorclass is None:
       
   775             return
   779             return
   776         validator = validatorclass()
       
   777         if isinstance(validator, htmlparser.DTDValidator):
   780         if isinstance(validator, htmlparser.DTDValidator):
   778             # XXX remove <canvas> used in progress widget, unknown in html dtd
   781             # XXX remove <canvas> used in progress widget, unknown in html dtd
   779             output = re.sub('<canvas.*?></canvas>', '', output)
   782             output = re.sub('<canvas.*?></canvas>', '', output)
   780         return self.assertWellFormed(validator, output.strip(), context= view.__regid__)
   783         return self.assertWellFormed(validator, output.strip(), context= view.__regid__)
   781 
   784 
   813                     content = u'\n'.join(line_template % (idx + 1, line)
   816                     content = u'\n'.join(line_template % (idx + 1, line)
   814                                          for idx, line in enumerate(content)
   817                                          for idx, line in enumerate(content)
   815                                          if line_context_filter(idx+1, position))
   818                                          if line_context_filter(idx+1, position))
   816                     msg += u'\nfor content:\n%s' % content
   819                     msg += u'\nfor content:\n%s' % content
   817             raise AssertionError, msg, tcbk
   820             raise AssertionError, msg, tcbk
       
   821 
       
   822     def assertDocTestFile(self, testfile):
       
   823         # doctest returns tuple (failure_count, test_count)
       
   824         result = self.shell().process_script(testfile)
       
   825         if result[0] and result[1]:
       
   826             raise self.failureException("doctest file '%s' failed"
       
   827                                         % testfile)
   818 
   828 
   819     # deprecated ###############################################################
   829     # deprecated ###############################################################
   820 
   830 
   821     @deprecated('[3.8] use self.execute(...).get_entity(0, 0)')
   831     @deprecated('[3.8] use self.execute(...).get_entity(0, 0)')
   822     def entity(self, rql, args=None, eidkey=None, req=None):
   832     def entity(self, rql, args=None, eidkey=None, req=None):