def test_top_parse_sections_do_not_start_with_newlines_and_nonempty(self): for _ in range(MAX_REPS): tokens = random_tokens() sections = top_parse(tokens) for section in sections: self.assertTrue(section, '{}\n\n{}'.format(tokens, sections)) self.assertTrue(section[0].token_type != TokenType.NEWLINE)
def test_no_double_newlines_after_top_parse(self): for _ in range(MAX_REPS): tokens = random_tokens() parsed = top_parse(tokens) for section in parsed: self.assertFalse( self.has_double_newline(section), str(tokens), )
def test_top_parse_separates_by_indent_if_section_starts(self): """Make sure we an ignore indentations if between sections.""" docstring = '\n'.join([ 'A short summary.', ' ', 'Args:', ' x: y.', ' ', 'Returns:', ' Something.', ]) parsed = top_parse(condense(lex(docstring))) self.assertEqual(len(parsed), 3)
def test_top_parse_only_separates_by_indent_if_followed_by_newline(self): docstring = '\n'.join([ 'Short shorts.', '', ' Long Description.', ' ', 'Args:', ' x: y', ]) parsed = top_parse(condense(lex(docstring))) self.assertEqual(len(parsed), 3) self.assertTrue( parsed[1][0].token_type == TokenType.INDENT, 'Expected INDENT but was {}'.format(parsed[1][0].token_type, ))
def test_expected_amount_of_sections(self): docstring = '\n'.join([ 'foobar', '', 'Args:', ' foo: foobar', '', 'Returns:', ' bar', ]) tokens = condense(lex(docstring)) sections = top_parse(tokens) self.assertEqual( len(sections), 3, )
def test_top_parse_sections_le_nonnewline_tokens(self): r"""Make sure that aren't too many sections. We are attempting to guarantee that s <= t where s = the number of sections, t = |{ token_i \in string | token_i /= newline \/ ( token_i+1 /= newline /\ token_i-1 /= newline )}| """ for _ in range(MAX_REPS): tokens = random_tokens(exclude={TokenType.NEWLINE}) doubles_amount = randint(0, 10) for _ in range(doubles_amount): i = randint(0, len(tokens) - 1) tokens.insert( i, Token( value='\n', token_type=TokenType.NEWLINE, line_number=0, ), ) tokens.insert( i, Token( value='\n', token_type=TokenType.NEWLINE, line_number=0, ), ) parsed = top_parse(tokens) self.assertTrue( len(parsed) <= len(tokens) )
def test_leading_newlines_stripped(self): docstring = '\n boDqRvgBr # Returns' sections = top_parse(condense(lex(docstring))) self.assertTrue(sections[0][0].token_type != TokenType.NEWLINE)