def chapter(self, name): """ Get a chapter from the document, use the same error as 'should'ing if it fails """ fname = name.lower().strip() if fname in self._chapters: return self._chapters[fname] else: raise DocException("No Chapter Found", missing=name)
def section(self, value): """ Get a subsection of a section """ fvalue = value.lower().strip() if fvalue in self._named_subsections: return self._named_subsections[fvalue] else: raise DocException("Subsection not found", missing=value)
def cite(self, citation): """ Check for a citation in the section's citation set """ citation_set = self._ref.get_citations() if citation in citation_set: return self else: raise DocException('No Citation Found: {}'.format(citation))
def words(self): """ Test against a given word count """ word_count = self._ref.get_word_count() if self.state['comp'](word_count, self.state['compVal']): return self else: raise DocException('Not Enough Words: {} / {}'.format( word_count, self.state['compVal']))
def _subsections_len(self): """ Test against a given subsection count """ num_sections = len(self._ref.subsections) if self.state['comp'](num_sections, self.state['compVal']): return self else: err_msg = 'Not Enough Subsections: {} / {}' raise DocException( err_msg.format(num_sections, self.state['compVal']))
def citations(self): """ Test against a given citation count """ cite_count = len(self._ref.get_citations()) if self.state['comp'](cite_count, self.state['compVal']): return self else: err_msg = 'Not Enough Citations: {} / {}' raise DocException( err_msg.format(cite_count, self.state['compVal']))
def sentences(self): """ Test against a given sentence count """ sentence_count = self._ref.get_sentence_count() if self.state['comp'](sentence_count, self.state['compVal']): return self else: err_msg = 'Not Enough Sentences: {} / {}' raise DocException( err_msg.format(sentence_count, self.state['compVal']))
def paragraphs(self): """ Test against a given paragraph count """ paragraph_count = self._ref.get_paragraph_count() if self.state['comp'](paragraph_count, self.state['compVal']): return self else: err_msg = "Not Enough Paragraphs: {} / {}" raise DocException( err_msg.format(paragraph_count, self.state['compVal']))
def __call__(self): """ The main call to run tests """ passed = 0 failed = 0 tests = [x for x in dir(self) if DocTestRunner.TEST_NAME in x and \ DocTestRunner.FAIL not in x] fail_tests = [x for x in dir(self) if DocTestRunner.FAIL in x] total = len(tests)+len(fail_tests) logging.info('====================') logging.info("Running Tests:") logging.info('====================') for test in tests: try: result = getattr(self, test)() if result is not None and result is False: raise DocException('Test returned False') except DocException as err: logging.warning('------------------------------') logging.warning("{}{} {} : {} {} {} {} {}".format(DocTestRunner.TAB, DocTestRunner.TAB, DocTestRunner.CROSS, test, '\n', DocTestRunner.TAB, DocTestRunner.TAB, repr(err))) logging.warning('------------------------------') failed += 1 else: logging.info("{} {} : {}".format(DocTestRunner.TAB, DocTestRunner.TICK, test)) passed += 1 logging.info('====================') logging.info('Running Fail Tests:') logging.info('====================') for test in fail_tests: try: result = getattr(self, test)() if result is None or result is False: logging.info('------------------------------') logging.info('{}{} {} : {}'.format(DocTestRunner.TAB, DocTestRunner.TAB, DocTestRunner.CROSS, test)) logging.info('------------------------------') failed += 1 except DocException as err: passed += 1 logging.info("{} {} : {}".format(DocTestRunner.TAB, DocTestRunner.TICK, test)) log_str = "\n\nFinished. {}/{} Passed. {}/{} Failed." logging.info(log_str.format(passed, total, failed, total))
def _subsections(self, vals): """ Boolean check for correct names, or corrent number, of subsections """ if isinstance(vals, list) and all([self._ref.section(x) for x in vals]): return self elif isinstance(vals, int) and len( self._ref._ordered_subsections) == vals: return self raise DocException('Subsection mismatch: {} / {}'.format( vals, len(self._ref._ordered_subsections)))
def pages(self): """ Test against a given page count """ base_wordcount = self._ref.get_word_count() compare_to = SizedShould.WordsInAPage * self.state['compVal'] if self.state['comp'](base_wordcount, compare_to): return self else: err_msg = 'Not Enough words to fulfill page count: {}/({} * {})' raise DocException( err_msg.format(base_wordcount, self.state['compVal'], SizedShould.WordsInAPage))
def regex(self, reg): """ Test a selected Doc/Sec/SubSec/Para/Sentence for a regex """ #apply it to the text found = False if self._ref.is_document(): for x in self.ref._chapters.values(): found = found or x.regex(reg) #apply it to the subsections if self._ref.is_section(): found = found or self._ref.regex(reg) if not found: raise DocException('Regex not found') return self
def precede(self, name): """ Set state for a final test """ #go up to parent section, ensure the ref section is before the input name parent = self._ref.get_parent() #Not that elegant, but avoids recursive imports: if parent.is_document(): raise Exception('Attempting to specify order of chapter') ordered_titles = [x._title for x in parent._ordered_subsections] try: index_1 = ordered_titles.index(self._ref._title) except ValueError as err: raise DocException('Section can not be found', missing=self._ref._title) try: index_2 = ordered_titles.index(name.lower().strip()) except ValueError as err: raise DocException('Section can not be found', missing=name) if index_1 < index_2: return self err_msg = "Bad Precedence order: {}({}) <-> {}({})" raise DocException( err_msg.format(self._ref._title, index_1, name, index_2))
def tag(self, tag): """ Test a selected Document/Section/Subsection/Paragraph/Sentence for a tag """ if not self._ref.has_tag(tag): raise DocException("Tag not found", missing=tag) return self
def mention(self, reference): """ Check for a specific string in the specified section """ if self._ref.mentions(reference): return self else: raise DocException("No mention found", missing=reference)