Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_parse_keyword(self):
"""Make sure we can parse keywords."""
for word, node_type in [
('Returns', NodeType.RETURNS),
('Args', NodeType.ARGUMENTS),
('Arguments', NodeType.ARGUMENTS),
('Yields', NodeType.YIELDS),
('Raises', NodeType.RAISES)
]:
node = parse_keyword(Peaker(lex(word)))
self.assertEqual(
node.node_type,
node_type
)
self.assertEqual(
node.value,
word,
)
def test_take_while(self):
peaker = Peaker((x for x in 'name 1234'))
name = ''.join(peaker.take_while(str.isalpha))
self.assertEqual(name, 'name')
self.assertTrue(peaker.has_next())
spaces = ''.join(peaker.take_while(str.isspace))
self.assertEqual(spaces, ' ')
self.assertTrue(peaker.has_next())
numbers = ''.join(peaker.take_while(str.isdecimal))
self.assertEqual(numbers, '1234')
self.assertFalse(peaker.has_next())
def test_parse_line_with_type(self):
"""Make sure we can parse a line when it starts with a type."""
node = parse_line_with_type(Peaker(lex(
' int: the square of something.\n'
)))
self.assertEqual(
node.node_type,
NodeType.LINE,
)
child_types = [x.node_type for x in node.walk()]
self.assertEqual(
child_types,
[
NodeType.INDENT,
NodeType.WORD,
NodeType.TYPE,
NodeType.WORD,
NodeType.WORD,
NodeType.WORD,
def test_parse_word(self):
"""Make sure we can parse a word."""
node = parse_word(Peaker(lex('joHwI\'')))
self.assertEqual(
node.node_type,
NodeType.WORD,
)
self.assertEqual(
node.value,
'joHwI\'',
)
def test_parse_line_without_indent(self):
"""Make sure lines don't need to have indents."""
node = parse_line(Peaker(lex('word word\n')))
self.assertEqual(
node.node_type,
NodeType.LINE,
)
child_types = [x.node_type for x in node.walk()]
self.assertEqual(
child_types,
[NodeType.WORD, NodeType.WORD, NodeType.LINE],
)
def test_empty_stream_says_it_has_none(self):
peaker = Peaker((x for x in ''))
self.assertFalse(peaker.has_next())
def test_peaker_can_have_n_lookahead(self):
peaker = Peaker((x for x in 'abcd'), lookahead=3)
self.assertEqual(peaker.peak(lookahead=1), 'a')
self.assertEqual(peaker.peak(lookahead=2), 'b')
self.assertEqual(peaker.peak(lookahead=3), 'c')
def test_parse_compound(self):
"""Make sure we can parse a compound section."""
node = parse_compound_section(Peaker(lex('\n'.join([
'Args:',
' x: X.',
' y: Y1.',
' Y2.',
' z (int, optional): Z.',
'\n'
])), lookahead=3))
self.assertEqual(node.node_type, NodeType.SECTION)
body = node.children[1]
self.assertEqual(
body.node_type,
NodeType.SECTION_COMPOUND_BODY,
)
self.assertEqual(
len(body.children),
3,
def test_parse_primitive_type(self):
"""Make sure we can parse a primitive type like int or str."""
node = parse_type(Peaker(lex('(int)')))
self.assertEqual(
node.node_type,
NodeType.TYPE,
)
self.assertEqual(
node.children[1].value,
'int',
)
children = list()
# Get the first node, which may be a type description.
if with_type and _is(TokenType.WORD, peaker.peak()):
next_value = peaker.next()
if next_value.value.startswith('(') and next_value.value.endswith(')'):
first_node = parse_type(
Peaker((x for x in [next_value]))
)
elif _is(TokenType.COLON, peaker.peak()):
first_node = parse_type(
Peaker((x for x in [next_value, peaker.next()]))
)
else:
first_node = parse_word(
Peaker((x for x in [next_value]))
)
children.append(first_node)
# Get the remaining nodes in the line, up to the newline.
while peaker.has_next() and not peaker.peak().token_type == TokenType.NEWLINE:
next_child = peaker.peak()
assert next_child is not None
if _is(TokenType.WORD, next_child) and next_child.value in KEYWORDS:
children.append(parse_keyword(peaker))
elif _is(TokenType.WORD, next_child):
children.append(parse_word(peaker))
elif _is(TokenType.INDENT, next_child):
children.append(parse_indent(peaker))
elif _is(TokenType.COLON, next_child):
children.append(parse_colon(peaker))
elif _is(TokenType.LPAREN, next_child):