diff options
Diffstat (limited to 'docs/sphinx/hieroglyph/test')
-rw-r--r-- | docs/sphinx/hieroglyph/test/__init__.py | 2 | ||||
-rw-r--r-- | docs/sphinx/hieroglyph/test/test_comments.py | 586 | ||||
-rw-r--r-- | docs/sphinx/hieroglyph/test/test_hierglyph.py | 264 | ||||
-rw-r--r-- | docs/sphinx/hieroglyph/test/test_nodes.py | 386 |
4 files changed, 1238 insertions, 0 deletions
diff --git a/docs/sphinx/hieroglyph/test/__init__.py b/docs/sphinx/hieroglyph/test/__init__.py new file mode 100644 index 000000000..fd249423f --- /dev/null +++ b/docs/sphinx/hieroglyph/test/__init__.py @@ -0,0 +1,2 @@ +__author__ = 'rjs' +
\ No newline at end of file diff --git a/docs/sphinx/hieroglyph/test/test_comments.py b/docs/sphinx/hieroglyph/test/test_comments.py new file mode 100644 index 000000000..d1a1453ee --- /dev/null +++ b/docs/sphinx/hieroglyph/test/test_comments.py @@ -0,0 +1,586 @@ +import unittest + +from hieroglyph.hieroglyph import parse_hieroglyph_text +from hieroglyph.errors import HieroglyphError + +class CommentTests(unittest.TestCase): + + def test_comment1(self): + source = """Fetches rows from a Bigtable. + This is a continuation of the opening paragraph. + + Retrieves rows pertaining to the given keys from the Table instance + represented by big_table. Silly things may happen if + other_silly_variable is not None. + + Args: + big_table: An open Bigtable Table instance. + keys: A sequence of strings representing the key of each table row + to fetch. + other_silly_variable (str): Another optional variable, that has a much + longer name than the other args, and which does nothing. + + Returns: + A dict mapping keys to the corresponding table row data + fetched. Each row is represented as a tuple of strings. For + example: + + {'Serak': ('Rigel VII', 'Preparer'), + 'Zim': ('Irk', 'Invader'), + 'Lrrr': ('Omicron Persei 8', 'Emperor')} + + If a key from the keys argument is missing from the dictionary, + then that row was not found in the table. + + Raises: + IOError: An error occurred accessing the bigtable.Table object. + """ + + expected = """ Fetches rows from a Bigtable. + This is a continuation of the opening paragraph. + + Retrieves rows pertaining to the given keys from the Table instance + represented by big_table. Silly things may happen if + other_silly_variable is not None. + + :param big_table: An open Bigtable Table instance. + + :param keys: A sequence of strings representing the key of each table row + to fetch. + + :param other_silly_variable: Another optional variable, that has a much + longer name than the other args, and which does nothing. + + :type other_silly_variable: str + + :returns: A dict mapping keys to the corresponding table row data + fetched. Each row is represented as a tuple of strings. For + example: + + {'Serak': ('Rigel VII', 'Preparer'), + 'Zim': ('Irk', 'Invader'), + 'Lrrr': ('Omicron Persei 8', 'Emperor')} + + If a key from the keys argument is missing from the dictionary, + then that row was not found in the table. + + :raises: + IOError - An error occurred accessing the bigtable.Table object. + """ + source_lines = source.splitlines() + actual_lines = parse_hieroglyph_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_comment2(self): + source = """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. + + Args: + 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. + """ + + 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. + """ + source_lines = source.splitlines() + actual_lines = parse_hieroglyph_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_comment3(self): + source = """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. + + Args: + 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. + """ + + 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. + """ + source_lines = source.splitlines() + actual_lines = parse_hieroglyph_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_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_hieroglyph_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_hieroglyph_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_hieroglyph_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_comment7(self): + source = """Projects each element of a sequence to an intermediate new sequence, + flattens the resulting sequences into one sequence and optionally + transforms the flattened sequence using a selector function. + + Note: This method uses deferred execution. + + Args: + collection_selector: A unary function mapping each element of the + source iterable into an intermediate sequence. The single + argument of the collection_selector is the value of an element + from the source sequence. The return value should be an + iterable derived from that element value. The default + collection_selector, which is the identity function, assumes + that each element of the source sequence is itself iterable. + + result_selector: An optional unary function mapping the elements in + the flattened intermediate sequence to corresponding elements + of the result sequence. The single argument of the + result_selector is the value of an element from the flattened + intermediate sequence. The return value should be the + corresponding value in the result sequence. The default + result_selector is the identity function. + + Returns: + A Queryable over a generated sequence whose elements are the result + of applying the one-to-many collection_selector to each element of + the source sequence, concatenating the results into an intermediate + sequence, and then mapping each of those elements through the + result_selector into the result sequence. + + Raises: + ValueError: If this Queryable has been closed. + TypeError: If either collection_selector or result_selector are not + callable. + """ + + expected = """ Projects each element of a sequence to an intermediate new sequence, + flattens the resulting sequences into one sequence and optionally + transforms the flattened sequence using a selector function. + + .. note:: + + This method uses deferred execution. + + :param collection_selector: A unary function mapping each element of the + source iterable into an intermediate sequence. The single + argument of the collection_selector is the value of an element + from the source sequence. The return value should be an + iterable derived from that element value. The default + collection_selector, which is the identity function, assumes + that each element of the source sequence is itself iterable. + + :param result_selector: An optional unary function mapping the elements in + the flattened intermediate sequence to corresponding elements + of the result sequence. The single argument of the + result_selector is the value of an element from the flattened + intermediate sequence. The return value should be the + corresponding value in the result sequence. The default + result_selector is the identity function. + + :returns: A Queryable over a generated sequence whose elements are the result + of applying the one-to-many collection_selector to each element of + the source sequence, concatenating the results into an intermediate + sequence, and then mapping each of those elements through the + result_selector into the result sequence. + + :raises: + * ValueError - If this Queryable has been closed. + + * TypeError - If either collection_selector or result_selector are not + callable. + """ + source_lines = source.splitlines() + actual_lines = parse_hieroglyph_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_comment8(self): + source = """A convenience factory for creating Records. + + Args: + **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. + """ + + 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. + +""" + source_lines = source.splitlines() + actual_lines = parse_hieroglyph_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(HieroglyphError, lambda: parse_hieroglyph_text(source_lines)) + + def test_comment10(self): + source = """ + Execute the command described by concatenating the string function arguments + with the p4 -s global scripting flag and return the results in a dictionary. + + For example, to run the command:: + + p4 -s fstat -T depotFile foo.h + + call:: + + p4('fstat', '-T', 'depotFile', 'foo.h') + + Args: + args: The arguments to the p4 command as a list of objects which will + be converted to strings. + + Returns: + A dictionary of lists where each key in the dictionary is the field name + from the command output, and each value is a list of output lines in + order. + + Raises: + PerforceError: If the command could not be run or if the command + reported an error. + """ + + expected = """ + Execute the command described by concatenating the string function arguments + with the p4 -s global scripting flag and return the results in a dictionary. + + For example, to run the command:: + + p4 -s fstat -T depotFile foo.h + + call:: + + p4('fstat', '-T', 'depotFile', 'foo.h') + + :param args: The arguments to the p4 command as a list of objects which will + be converted to strings. + + :returns: A dictionary of lists where each key in the dictionary is the field name + from the command output, and each value is a list of output lines in + order. + + :raises: + PerforceError - If the command could not be run or if the command + reported an error. + +""" + + source_lines = source.splitlines() + actual_lines = parse_hieroglyph_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_comment11(self): + source = """Projects each element of a sequence to an intermediate new sequence, + flattens the resulting sequences into one sequence and optionally + transforms the flattened sequence using a selector function. + + Warning: This method may explode at short notice. + + Args: + collection_selector: A unary function mapping each element of the + source iterable into an intermediate sequence. The single + argument of the collection_selector is the value of an element + from the source sequence. The return value should be an + iterable derived from that element value. The default + collection_selector, which is the identity function, assumes + that each element of the source sequence is itself iterable. + + result_selector: An optional unary function mapping the elements in + the flattened intermediate sequence to corresponding elements + of the result sequence. The single argument of the + result_selector is the value of an element from the flattened + intermediate sequence. The return value should be the + corresponding value in the result sequence. The default + result_selector is the identity function. + + Returns: + A Queryable over a generated sequence whose elements are the result + of applying the one-to-many collection_selector to each element of + the source sequence, concatenating the results into an intermediate + sequence, and then mapping each of those elements through the + result_selector into the result sequence. + + Raises: + ValueError: If this Queryable has been closed. + TypeError: If either collection_selector or result_selector are not + callable. + """ + + expected = """ Projects each element of a sequence to an intermediate new sequence, + flattens the resulting sequences into one sequence and optionally + transforms the flattened sequence using a selector function. + + .. warning:: + + This method may explode at short notice. + + :param collection_selector: A unary function mapping each element of the + source iterable into an intermediate sequence. The single + argument of the collection_selector is the value of an element + from the source sequence. The return value should be an + iterable derived from that element value. The default + collection_selector, which is the identity function, assumes + that each element of the source sequence is itself iterable. + + :param result_selector: An optional unary function mapping the elements in + the flattened intermediate sequence to corresponding elements + of the result sequence. The single argument of the + result_selector is the value of an element from the flattened + intermediate sequence. The return value should be the + corresponding value in the result sequence. The default + result_selector is the identity function. + + :returns: A Queryable over a generated sequence whose elements are the result + of applying the one-to-many collection_selector to each element of + the source sequence, concatenating the results into an intermediate + sequence, and then mapping each of those elements through the + result_selector into the result sequence. + + :raises: + * ValueError - If this Queryable has been closed. + + * TypeError - If either collection_selector or result_selector are not + callable. + """ + source_lines = source.splitlines() + actual_lines = parse_hieroglyph_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_comment12(self): + source = """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. + + Args: + 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: + This is not a proper exception description + """ + + source_lines = source.splitlines() + self.assertRaises(HieroglyphError, lambda: parse_hieroglyph_text(source_lines)) + diff --git a/docs/sphinx/hieroglyph/test/test_hierglyph.py b/docs/sphinx/hieroglyph/test/test_hierglyph.py new file mode 100644 index 000000000..42947cb0c --- /dev/null +++ b/docs/sphinx/hieroglyph/test/test_hierglyph.py @@ -0,0 +1,264 @@ +import unittest
+from hieroglyph.hieroglyph import first_paragraph_indent, gather_lines, unindent
+
+__author__ = 'Robert Smallshire'
+
+class UnindentTests(unittest.TestCase):
+
+ def test_zero_lines(self):
+ source = []
+ expected = []
+ actual = unindent(source)
+ self.assertEqual(actual, expected)
+
+ def test_one_zero_indent_line(self):
+ source = ["First line"]
+ expected = [(0, "First line")]
+ actual = unindent(source)
+ self.assertEqual(actual, expected)
+
+ def test_two_zero_indent_lines(self):
+ source = ["First line",
+ "Second line"]
+ expected = [(0, "First line"),
+ (0, "Second line")]
+ actual = unindent(source)
+ self.assertEqual(actual, expected)
+
+ def test_two_indented_lines(self):
+ source = [" First line",
+ " Second line"]
+ expected = [(4, "First line"),
+ (6, "Second line")]
+ actual = unindent(source)
+ self.assertEqual(actual, expected)
+
+ def test_whitespace_line(self):
+ source = [" "]
+ expected = [(4, "")]
+ actual = unindent(source)
+ self.assertEqual(actual, expected)
+
+ def test_tab_line(self):
+ source = ["\tHello"]
+ expected = [(1, "Hello")]
+ actual = unindent(source)
+ self.assertEqual(actual, expected)
+
+
+class FirstParagraphIndentTests(unittest.TestCase):
+
+ def test_zero_lines(self):
+ source = []
+ expected = []
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+ def test_single_line_non_indented_comment(self):
+ source = [(0, "A single line comment")]
+ expected = [(0, "A single line comment")]
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+ def test_single_line_indented_comment(self):
+ source = [(4, "A single line comment")]
+ expected = [(4, "A single line comment")]
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+ def test_double_line_non_indented_comment(self):
+ source = [(0, "The first line"),
+ (0, "The second line")]
+ expected = [(0, "The first line"),
+ (0, "The second line")]
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+ def test_double_line_indented_comment(self):
+ source = [(4, "The first line"),
+ (4, "The second line")]
+ expected = [(4, "The first line"),
+ (4, "The second line")]
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+ def test_first_line_indent(self):
+ source = [(4, "The first line"),
+ (0, "The second line")]
+ expected = [(4, "The first line"),
+ (0, "The second line")]
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+ def test_first_line_non_indent(self):
+ source = [(0, "The first line"),
+ (4, "The second line")]
+ expected = [(4, "The first line"),
+ (4, "The second line")]
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+ def test_increasing_indent(self):
+ source = [(0, "The first line"),
+ (4, "The second line"),
+ (8, "The third line")]
+ expected = [(4, "The first line"),
+ (4, "The second line"),
+ (8, "The third line")]
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+ def test_separate_paragraphs(self):
+ source = [(0, "This is the first paragraph"),
+ (0, ""),
+ (4, "This is the second paragraph")]
+ expected = [(0, "This is the first paragraph"),
+ (0, ""),
+ (4, "This is the second paragraph")]
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+ def test_separate_paragraphs_indented(self):
+ source = [(4, "This is the first paragraph"),
+ (4, ""),
+ (8, "This is the second paragraph")]
+ expected = [(4, "This is the first paragraph"),
+ (4, ""),
+ (8, "This is the second paragraph")]
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+ def test_separated_lines_first_line_non_indented(self):
+ source = [(0, "The first line"),
+ (0, ""),
+ (4, "The third line")]
+ expected = [(0, "The first line"),
+ (0, ""),
+ (4, "The third line")]
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+ def test_separated_lines_first_line_indented(self):
+ source = [(4, "The first line"),
+ (4, ""),
+ (4, "The third line")]
+ expected = [(4, "The first line"),
+ (4, ""),
+ (4, "The third line")]
+ actual = first_paragraph_indent(source)
+ self.assertEqual(actual, expected)
+
+class GatherLinesTests(unittest.TestCase):
+
+ def test_empty(self):
+ source = []
+ expected = []
+ actual = gather_lines(source)
+ self.assertEqual(actual, expected)
+
+ def test_one_liner(self):
+ source = [(0, 'One liner')]
+ expected = [(0, ['One liner'])]
+ actual = gather_lines(source)
+ self.assertEqual(actual, expected)
+
+ def test_two_liner(self):
+ source = [(0, 'First line'),
+ (0, 'Second line')]
+ expected = [(0, ['First line',
+ 'Second line'])]
+ actual = gather_lines(source)
+ self.assertEqual(actual, expected)
+
+ def test_separated_lines(self):
+ source = [(0, 'First line'),
+ (0, ''),
+ (0, 'Third line')]
+ expected = [(0, ['First line',
+ '']),
+ (0, ['Third line'])]
+ actual = gather_lines(source)
+ self.assertEqual(actual, expected)
+
+ def test_separated_multi_lines(self):
+ source = [(0, 'First line'),
+ (0, 'Second line'),
+ (0, ''),
+ (0, 'Fourth line'),
+ (0, 'Fifth line')]
+ expected = [(0, ['First line',
+ 'Second line',
+ '']),
+ (0, ['Fourth line',
+ 'Fifth line'])]
+ actual = gather_lines(source)
+ self.assertEqual(actual, expected)
+
+
+ def test_indented_lines(self):
+ source = [(0, 'First line'),
+ (4, 'Second line')]
+ expected = [(0, ['First line']),
+ (4, ['Second line'])]
+ actual = gather_lines(source)
+ self.assertEqual(actual, expected)
+
+ def test_dedented_lines(self):
+ source = [(4, 'First line'),
+ (0, 'Second line')]
+ expected = [(4, ['First line']),
+ (0, ['Second line'])]
+ actual = gather_lines(source)
+ self.assertEqual(actual, expected)
+
+ def test_indented_multi_lines(self):
+ source = [(0, 'First line'),
+ (0, 'Second line'),
+ (4, 'Third line'),
+ (4, 'Fourth line')]
+ expected = [(0, ['First line',
+ 'Second line']),
+ (4, ['Third line',
+ 'Fourth line'])]
+ actual = gather_lines(source)
+ self.assertEqual(actual, expected)
+
+ def test_dedented_multi_lines(self):
+ source = [(4, 'First line'),
+ (4, 'Second line'),
+ (0, 'Third line'),
+ (0, 'Fourth line')]
+ expected = [(4, ['First line',
+ 'Second line']),
+ (0, ['Third line',
+ 'Fourth line'])]
+ actual = gather_lines(source)
+ self.assertEqual(actual, expected)
+
+ def test_indented_separated_multi_lines(self):
+ source = [(0, 'First line'),
+ (0, 'Second line'),
+ (0, ''),
+ (4, 'Fourth line'),
+ (4, 'Fifth line')]
+ expected = [(0, ['First line',
+ 'Second line',
+ '']),
+ (4, ['Fourth line',
+ 'Fifth line'])]
+ actual = gather_lines(source)
+ self.assertEqual(actual, expected)
+
+ def test_dedented_separated_multi_lines(self):
+ source = [(4, 'First line'),
+ (4, 'Second line'),
+ (4, ''),
+ (0, 'Fourth line'),
+ (0, 'Fifth line')]
+ expected = [(4, ['First line',
+ 'Second line',
+ '']),
+ (0, ['Fourth line',
+ 'Fifth line'])]
+ actual = gather_lines(source)
+ self.assertEqual(actual, expected)
diff --git a/docs/sphinx/hieroglyph/test/test_nodes.py b/docs/sphinx/hieroglyph/test/test_nodes.py new file mode 100644 index 000000000..4cc17b477 --- /dev/null +++ b/docs/sphinx/hieroglyph/test/test_nodes.py @@ -0,0 +1,386 @@ +import unittest
+from hieroglyph.nodes import Node, Arg, Raises, Except, Returns, Warning, Note
+
+__author__ = 'Robert Smallshire'
+
+class NodeTests(unittest.TestCase):
+
+ def test_create_default_node(self):
+ node = Node()
+ self.assertEqual(node.indent, 0)
+ self.assertEqual(node.lines, [])
+ self.assertIsNone(node.parent)
+
+ def test_create_with_indent(self):
+ node = Node(indent=4)
+ self.assertEqual(node.indent, 4)
+ self.assertEqual(node.lines, [])
+ self.assertIsNone(node.parent)
+
+ def test_create_with_lines(self):
+ node = Node(lines= ['First', 'Second', 'Third'])
+ self.assertEqual(node.indent, 0)
+ self.assertEqual(node.lines, ['First', 'Second', 'Third'])
+ self.assertIsNone(node.parent)
+
+ def test_repr(self):
+ node = Node(5, ['One', 'Two', 'Three'])
+ actual = repr(node)
+ expected = "Node(5, ['One', 'Two', 'Three'], children=[])"
+ self.assertEqual(expected, actual)
+
+ def test_add_one_child(self):
+ node = Node()
+ child = Node(parent=node)
+ node.add_child(child)
+ self.assertIs(node.children[0], child)
+
+ def test_add_two_children(self):
+ node = Node()
+ child0 = Node(parent=node)
+ child1 = Node(parent=node)
+ node.add_child(child0)
+ node.add_child(child1)
+ self.assertIs(node.children[0], child0)
+ self.assertIs(node.children[1], child1)
+
+ def test_render_rst_empty(self):
+ node = Node()
+ rst = node.render_rst()
+ self.assertEqual(len(rst), 0)
+
+ def test_render_rst_indent(self):
+ node = Node(indent=4)
+ rst = node.render_rst()
+ self.assertEqual(len(rst), 0)
+
+ def test_render_rst_lines(self):
+ node = Node(lines= ['First',
+ 'Second',
+ 'Third'])
+ rst = node.render_rst()
+ self.assertEqual(rst, ['First',
+ 'Second',
+ 'Third'])
+
+ def test_render_rst_indented_lines(self):
+ node = Node(indent=3, lines= ['First',
+ 'Second',
+ 'Third'])
+ rst = node.render_rst()
+ self.assertEqual(rst, [' First',
+ ' Second',
+ ' Third'])
+
+ def test_render_rst_with_child(self):
+ node = Node(indent=4, lines=["Parent"])
+ child = Node(indent=8, lines=["Child"], parent=node)
+ node.add_child(child)
+ rst = node.render_rst()
+ self.assertEqual(rst, [' Parent',
+ ' Child'])
+
+ def test_render_rst_with_children(self):
+ node = Node(indent=4, lines=["Parent"])
+ child_a = Node(indent=8, lines=["ChildA"], parent=node)
+ node.add_child(child_a)
+ child_b = Node(indent=6, lines=["ChildB"], parent=node)
+ node.add_child(child_b)
+ rst = node.render_rst()
+ self.assertEqual(rst, [' Parent',
+ ' ChildA',
+ ' ChildB'])
+
+
+class ArgTests(unittest.TestCase):
+
+ def test_create(self):
+ node = Arg(5, 10, 'foo')
+ self.assertEqual(node.indent, 5)
+ self.assertEqual(node.child_indent, 10)
+ self.assertEqual(node.name, 'foo')
+ self.assertEqual(node.lines, [])
+ self.assertIsNone(node.parent)
+
+ def test_set_type(self):
+ node = Arg(5, 10, 'foo')
+ node.type = 'str'
+ self.assertEqual(node.type, 'str')
+
+ def test_add_one_child(self):
+ node = Arg(5, 10, 'foo')
+ child = Node(parent=node)
+ node.add_child(child)
+ self.assertIs(node.children[0], child)
+
+ def test_add_two_children(self):
+ node = Arg(5, 10, 'foo')
+ child0 = Node(parent=node)
+ child1 = Node(parent=node)
+ node.add_child(child0)
+ node.add_child(child1)
+ self.assertIs(node.children[0], child0)
+ self.assertIs(node.children[1], child1)
+
+ def test_repr(self):
+ node = Arg(5, 10, 'foo')
+ actual = repr(node)
+ expected = "Arg('foo', None, children=[])"
+ self.assertEqual(expected, actual)
+
+ def test_render_rst_empty(self):
+ node = Arg(5, 10, 'bar')
+ rst = node.render_rst()
+ self.assertEqual(rst, [' :param bar: ',
+ ''])
+
+ def test_render_rst_with_child(self):
+ node = Arg(5, 10, 'bar')
+ child = Node(indent=10, lines=["Description"], parent=node)
+ node.add_child(child)
+ rst = node.render_rst()
+ self.assertEqual(rst, [' :param bar: Description',
+ ''])
+
+ def test_render_rst_with_children(self):
+ node = Arg(5, 10, 'bar')
+ child_a = Node(indent=10, lines=["ChildA"], parent=node)
+ node.add_child(child_a)
+ child_b = Node(indent=10, lines=["ChildB"], parent=node)
+ node.add_child(child_b)
+ rst = node.render_rst()
+ self.assertEqual(rst, [' :param bar: ChildA',
+ ' ChildB',
+ ''])
+
+ def test_render_rst_with_type(self):
+ node = Arg(5, 10, 'bar')
+ node.type = 'str'
+ rst = node.render_rst()
+ self.assertEqual(rst, [' :param bar: ',
+ ' :type bar: str',
+ ''])
+
+
+class RaisesTests(unittest.TestCase):
+
+ def test_create_default_node(self):
+ node = Raises()
+ self.assertEqual(node.indent, 0)
+ self.assertEqual(node.lines, [])
+ self.assertIsNone(node.parent)
+
+ def test_create_with_indent(self):
+ node = Raises(indent=4)
+ self.assertEqual(node.indent, 4)
+ self.assertEqual(node.lines, [])
+ self.assertIsNone(node.parent)
+
+ def test_repr(self):
+ node = Raises(5)
+ actual = repr(node)
+ expected = "Raises(5, children=[])"
+ self.assertEqual(expected, actual)
+
+ def test_add_one_child(self):
+ node = Raises()
+ child = Node(parent=node)
+ node.add_child(child)
+ self.assertIs(node.children[0], child)
+
+ def test_add_two_children(self):
+ node = Raises()
+ child0 = Node(parent=node)
+ child1 = Node(parent=node)
+ node.add_child(child0)
+ node.add_child(child1)
+ self.assertIs(node.children[0], child0)
+ self.assertIs(node.children[1], child1)
+
+ def test_render_rst_empty(self):
+ node = Raises()
+ rst = node.render_rst()
+ self.assertEqual(rst, [':raises:',
+ ''])
+
+ def test_render_rst_indent(self):
+ node = Raises(indent=5)
+ rst = node.render_rst()
+ self.assertEqual(rst, [' :raises:',
+ ''])
+
+ def test_render_rst_with_child(self):
+ node = Raises(5)
+ child = Node(indent=10, lines=["Description"], parent=node)
+ node.add_child(child)
+ rst = node.render_rst()
+ self.assertEqual(rst, [' :raises:',
+ ' Description',
+ ''])
+
+ def test_render_rst_with_children(self):
+ node = Raises(5)
+ child_a = Node(indent=10, lines=["ChildA"], parent=node)
+ node.add_child(child_a)
+ child_b = Node(indent=10, lines=["ChildB"], parent=node)
+ node.add_child(child_b)
+ rst = node.render_rst()
+ self.assertEqual(rst, [' :raises:',
+ ' ChildA',
+ ' ChildB',
+ ''])
+
+
+class ExceptTests(unittest.TestCase):
+
+ def test_create(self):
+ node = Except(5, 'FooError')
+ self.assertEqual(node.indent, 5)
+ self.assertEqual(node.type, 'FooError')
+ self.assertEqual(node.lines, [])
+ self.assertIsNone(node.parent)
+
+ def test_add_one_child(self):
+ node = Except(5, 'FooError')
+ child = Node(parent=node)
+ node.add_child(child)
+ self.assertIs(node.children[0], child)
+
+ def test_add_two_children(self):
+ node = Except(5, 'FooError')
+ child0 = Node(parent=node)
+ child1 = Node(parent=node)
+ node.add_child(child0)
+ node.add_child(child1)
+ self.assertIs(node.children[0], child0)
+ self.assertIs(node.children[1], child1)
+
+ def test_repr(self):
+ node = Except(5,'FooError')
+ actual = repr(node)
+ expected = "Except('FooError', children=[])"
+ self.assertEqual(expected, actual)
+
+ def test_render_rst_empty(self):
+ node = Except(5, 'FooError')
+ rst = node.render_rst()
+ self.assertEqual(rst, [' * FooError - ',
+ ''])
+
+ def test_render_rst_indent(self):
+ node = Except(5, 'FooError')
+ rst = node.render_rst()
+ self.assertEqual(rst, [' * FooError - ',
+ ''])
+
+ def test_render_rst_with_child(self):
+ node = Except(5, 'FooError')
+ child = Node(indent=10, lines=["Description"], parent=node)
+ node.add_child(child)
+ rst = node.render_rst()
+ self.assertEqual(rst, [' * FooError - Description',
+ ''])
+
+ def test_render_rst_with_children(self):
+ node = Except(5, 'FooError')
+ child_a = Node(indent=10, lines=["ChildA"], parent=node)
+ node.add_child(child_a)
+ child_b = Node(indent=10, lines=["ChildB"], parent=node)
+ node.add_child(child_b)
+ rst = node.render_rst()
+ self.assertEqual(rst, [' * FooError - ChildA',
+ ' ChildB',
+ ''])
+
+class ReturnsTests(unittest.TestCase):
+
+ def test_create(self):
+ node = Returns(5)
+ self.assertEqual(node.indent, 5)
+ self.assertEqual(node.lines, [])
+ self.assertIsNone(node.parent)
+
+ def test_add_one_child(self):
+ node = Returns(5)
+ child = Node(parent=node)
+ node.add_child(child)
+ self.assertIs(node.children[0], child)
+
+ def test_add_two_children(self):
+ node = Returns(5)
+ child0 = Node(parent=node)
+ child1 = Node(parent=node)
+ node.add_child(child0)
+ node.add_child(child1)
+ self.assertIs(node.children[0], child0)
+ self.assertIs(node.children[1], child1)
+
+ def test_repr(self):
+ node = Returns(5)
+ actual = repr(node)
+ expected = "Returns(5, children=[])"
+ self.assertEqual(expected, actual)
+
+ # TODO test_render_rst
+
+class WarningTests(unittest.TestCase):
+
+ def test_create(self):
+ node = Warning(5)
+ self.assertEqual(node.indent, 5)
+ self.assertEqual(node.lines, [])
+ self.assertIsNone(node.parent)
+
+ def test_add_one_child(self):
+ node = Warning(5)
+ child = Node(parent=node)
+ node.add_child(child)
+ self.assertIs(node.children[0], child)
+
+ def test_add_two_children(self):
+ node = Warning(5)
+ child0 = Node(parent=node)
+ child1 = Node(parent=node)
+ node.add_child(child0)
+ node.add_child(child1)
+ self.assertIs(node.children[0], child0)
+ self.assertIs(node.children[1], child1)
+
+ def test_repr(self):
+ node = Warning(5)
+ actual = repr(node)
+ expected = "Warning(5, children=[])"
+ self.assertEqual(expected, actual)
+
+ # TODO test_render_rst
+
+class NoteTests(unittest.TestCase):
+
+ def test_create(self):
+ node = Note(5)
+ self.assertEqual(node.indent, 5)
+ self.assertEqual(node.lines, [])
+ self.assertIsNone(node.parent)
+
+ def test_add_one_child(self):
+ node = Note(5)
+ child = Node(parent=node)
+ node.add_child(child)
+ self.assertIs(node.children[0], child)
+
+ def test_add_two_children(self):
+ node = Note(5)
+ child0 = Node(parent=node)
+ child1 = Node(parent=node)
+ node.add_child(child0)
+ node.add_child(child1)
+ self.assertIs(node.children[0], child0)
+ self.assertIs(node.children[1], child1)
+
+ def test_repr(self):
+ node = Note(5)
+ actual = repr(node)
+ expected = "Note(5, children=[])"
+ self.assertEqual(expected, actual)
+
+ # TODO test_render_rst
|