def test_comment6(self): source_lines = [u('A convenience factory for creating Records.'), u(''), u('Args:'), u(' **kwargs: Each keyword argument will be used to initialise an'), u(' attribute with the same name as the argument and the given'), u(' value.'), u(''), u('Returns:'), u(' A Record which has a named attribute for each of the keyword arguments.'), u('')] expected = """A convenience factory for creating Records. :param \*\*kwargs: Each keyword argument will be used to initialise an attribute with the same name as the argument and the given value. :returns: A Record which has a named attribute for each of the keyword arguments. """ actual_lines = parse_cartouche_text(source_lines) expected_lines = expected.splitlines() self.assertEqual(len(actual_lines), len(expected_lines)) for actual_line, result_line in zip(actual_lines, expected_lines): if len(actual_line.strip()) == 0: self.assertTrue(len(result_line.strip()) == 0) else: self.assertEqual(actual_line, result_line)
def test_comment6(self): source_lines = [ u('A convenience factory for creating Records.'), u(''), u('Args:'), u(' **kwargs: Each keyword argument will be used to initialise an' ), u(' attribute with the same name as the argument and the given' ), u(' value.'), u(''), u('Returns:'), u(' A Record which has a named attribute for each of the keyword arguments.' ), u('') ] expected = """A convenience factory for creating Records. :param \*\*kwargs: Each keyword argument will be used to initialise an attribute with the same name as the argument and the given value. :returns: A Record which has a named attribute for each of the keyword arguments. """ actual_lines = parse_cartouche_text(source_lines) expected_lines = expected.splitlines() self.assertEqual(len(actual_lines), len(expected_lines)) for actual_line, result_line in zip(actual_lines, expected_lines): if len(actual_line.strip()) == 0: self.assertTrue(len(result_line.strip()) == 0) else: self.assertEqual(actual_line, result_line)
def test_comment5(self): source_lines = [ u('An empty Queryable.'), u(''), u('Note: The same empty instance will be returned each time.'), u(''), u('Returns: A Queryable over an empty sequence.'), u('') ] expected = """An empty Queryable. .. note:: The same empty instance will be returned each time. :returns: A Queryable over an empty sequence. """ actual_lines = parse_cartouche_text(source_lines) expected_lines = expected.splitlines() self.assertEqual(len(actual_lines), len(expected_lines)) for actual_line, result_line in zip(actual_lines, expected_lines): if len(actual_line.strip()) == 0: self.assertTrue(len(result_line.strip()) == 0) else: self.assertEqual(actual_line, result_line)
def test_comment5(self): source_lines = [u('An empty Queryable.'), u(''), u('Note: The same empty instance will be returned each time.'), u(''), u('Returns: A Queryable over an empty sequence.'), u('')] expected = """An empty Queryable. .. note:: The same empty instance will be returned each time. :returns: A Queryable over an empty sequence. """ actual_lines = parse_cartouche_text(source_lines) expected_lines = expected.splitlines() self.assertEqual(len(actual_lines), len(expected_lines)) for actual_line, result_line in zip(actual_lines, expected_lines): if len(actual_line.strip()) == 0: self.assertTrue(len(result_line.strip()) == 0) else: self.assertEqual(actual_line, result_line)
# -*- coding: utf-8 -*- from __future__ import print_function from contextlib import contextmanager import re from cartouche._portability import u from .errors import CartoucheError from .nodes import (Node, Raises, Except, Note, Warning, Returns, Arg, Yields, ensure_terminal_blank) OPTIONAL_BULLET_PATTERN = u(r'(?:[\*\+\-\•\‣\⁃]\s+)?') ARGS_PATTERN = u(r'(\*{0,2}\w+)(\s+\(([\.\w]+)\))?\s*:\s*(.*)') RAISES_PATTERN = u(r'([\w\.]+)\s*:\s*(.*)') ARGS_REGEX = re.compile(ARGS_PATTERN) RAISES_REGEX = re.compile(RAISES_PATTERN) class CartoucheSyntaxError(CartoucheError): pass def parse_cartouche_text(lines): '''Parse text in cartouche format and return a reStructuredText equivalent Args: lines: A sequence of strings representing the lines of a single docstring as read from the source by Sphinx. This string should be in a format that can be parsed by cartouche. Returns:
def test_comment9(self): source_lines = [ u('Parse a single line of a tree to determine depth and node.'), u(''), u('Args:'), u(' This line is missing an argument name.'), u(' '), u('Returns:'), u(' A 2-tuple containing the tree 0 based tree depth as the first' ), u(' element and the node description as the second element.'), u(''), u('Raises:'), u(' ValueError: If line does not have the expected form.'), u('') ] self.assertRaises(CartoucheError, lambda: parse_cartouche_text(source_lines))
def test_comment4(self): source_lines = [ u('Determine if all elements in the source sequence satisfy a condition.' ), u(''), u('All of the source sequence will be consumed.'), u(''), u('Note: This method uses immediate execution.'), u(''), u('Args:'), u(' predicate: An optional single argument function used to test each' ), u(' elements. If omitted, the bool() function is used resulting in' ), u(' the elements being tested directly.'), u(''), u('Returns:'), u(' True if all elements in the sequence meet the predicate condition,' ), u(' otherwise False.'), u(''), u('Raises:'), u(' ValueError: If the Queryable is closed()'), u(' TypeError: If predicate is not callable.'), u('') ] expected = """Determine if all elements in the source sequence satisfy a condition. All of the source sequence will be consumed. .. note:: This method uses immediate execution. :param predicate: An optional single argument function used to test each elements. If omitted, the bool() function is used resulting in the elements being tested directly. :returns: True if all elements in the sequence meet the predicate condition, otherwise False. :raises: * ValueError - If the Queryable is closed() * TypeError - If predicate is not callable. """ actual_lines = parse_cartouche_text(source_lines) expected_lines = expected.splitlines() self.assertEqual(len(actual_lines), len(expected_lines)) for actual_line, result_line in zip(actual_lines, expected_lines): if len(actual_line.strip()) == 0: self.assertTrue(len(result_line.strip()) == 0) else: self.assertEqual(actual_line, result_line)
def test_comment9(self): source_lines = [u('Parse a single line of a tree to determine depth and node.'), u(''), u('Args:'), u(' This line is missing an argument name.'), u(' '), u('Returns:'), u(' A 2-tuple containing the tree 0 based tree depth as the first'), u(' element and the node description as the second element.'), u(''), u('Raises:'), u(' ValueError: If line does not have the expected form.'), u('')] self.assertRaises(CartoucheError, lambda: parse_cartouche_text(source_lines))
def test_comment4(self): source_lines = [u('Determine if all elements in the source sequence satisfy a condition.'), u(''), u('All of the source sequence will be consumed.'), u(''), u('Note: This method uses immediate execution.'), u(''), u('Args:'), u(' predicate: An optional single argument function used to test each'), u(' elements. If omitted, the bool() function is used resulting in'), u(' the elements being tested directly.'), u(''), u('Returns:'), u(' True if all elements in the sequence meet the predicate condition,'), u(' otherwise False.'), u(''), u('Raises:'), u(' ValueError: If the Queryable is closed()'), u(' TypeError: If predicate is not callable.'), u('')] expected = """Determine if all elements in the source sequence satisfy a condition. All of the source sequence will be consumed. .. note:: This method uses immediate execution. :param predicate: An optional single argument function used to test each elements. If omitted, the bool() function is used resulting in the elements being tested directly. :returns: True if all elements in the sequence meet the predicate condition, otherwise False. :raises: * ValueError - If the Queryable is closed() * TypeError - If predicate is not callable. """ actual_lines = parse_cartouche_text(source_lines) expected_lines = expected.splitlines() self.assertEqual(len(actual_lines), len(expected_lines)) for actual_line, result_line in zip(actual_lines, expected_lines): if len(actual_line.strip()) == 0: self.assertTrue(len(result_line.strip()) == 0) else: self.assertEqual(actual_line, result_line)
def test_issue3(self): lines = [u('Parse a single line of a tree to determine depth and node.'), u(''), u('Args:'), u(' A single line string from a SCons dependency tree.'), u(' '), u('Returns:'), u(' A 2-tuple containing the tree 0 based tree depth as the first'), u(' element and the node description as the second element.'), u(''), u('Raises:'), u(' ValueError: If line does not have the expected form.'), u('')] self.assertRaises(CartoucheSyntaxError, lambda: parse_cartouche_text(lines))
def test_issue3(self): lines = [ u('Parse a single line of a tree to determine depth and node.'), u(''), u('Args:'), u(' A single line string from a SCons dependency tree.'), u(' '), u('Returns:'), u(' A 2-tuple containing the tree 0 based tree depth as the first' ), u(' element and the node description as the second element.'), u(''), u('Raises:'), u(' ValueError: If line does not have the expected form.'), u('') ] self.assertRaises(CartoucheSyntaxError, lambda: parse_cartouche_text(lines))