From 1232ba15527ad4ed1151febc80353a46be2c91d7 Mon Sep 17 00:00:00 2001 From: Franco Masotti Date: Sat, 9 Mar 2019 12:19:48 +0100 Subject: [PATCH] Fixed unit tests. Fixed a base case. --- md_toc/api.py | 27 +++++++++------- tests/tests.py | 85 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 76 insertions(+), 36 deletions(-) diff --git a/md_toc/api.py b/md_toc/api.py index d9c0841..ddaf012 100644 --- a/md_toc/api.py +++ b/md_toc/api.py @@ -254,8 +254,9 @@ def toc_renders_as_list(header_type_curr=1, indentation_array[header_type_curr] = True # 2. Reset next cells to false as a detection mechanism. - indentation_array[(header_type_curr + - 1):md_parser['github']['header']['max_levels']] = False + indentation_array[( + header_type_curr + + 1):md_parser['github']['header']['max_levels']] = False # 3. Check for previous false cells. If there is a "hole" in the array # it means that the TOC will have extra indentation space, thus not @@ -312,15 +313,17 @@ def compute_toc_line_indentation_spaces(header_type_curr=1, if (parser == 'github' or parser == 'cmark' or parser == 'gitlab' or parser == 'commonmarker'): + # TODO Explain that list indentation with this parser is always based on + # the previous state. if list_marker_top == list(): - for i in range (0,md_parser['github']['header']['max_levels']): + for i in range(0, md_parser['github']['header']['max_levels']): if ordered: list_marker_top.append('0.') else: list_marker_top.append(list_marker) - if header_type_prev == 0 or header_type_prev == 1: + if header_type_prev == 0: # Base case for the first toc line or for a previous h1. no_of_indentation_spaces_curr = 0 elif header_type_curr == header_type_prev: @@ -337,16 +340,19 @@ def compute_toc_line_indentation_spaces(header_type_curr=1, list_marker_prev = str(list_marker_top[header_type_curr - 1]) if header_type_curr > header_type_prev: # More indentation. - no_of_indentation_spaces_curr = (no_of_indentation_spaces_prev + - len(list_marker_prev) + len(' ')) + no_of_indentation_spaces_curr = ( + no_of_indentation_spaces_prev + len(list_marker_prev) + + len(' ')) elif header_type_curr < header_type_prev: # Less indentation. - no_of_indentation_spaces_curr = (no_of_indentation_spaces_prev - - len(list_marker_prev) - len(' ')) + no_of_indentation_spaces_curr = ( + no_of_indentation_spaces_prev - len(list_marker_prev) - + len(' ')) # Reset older nested list indices.. - for i in range(header_type_curr - 1 + 1, md_parser['github']['header']['max_levels']): - # Reset with the appropriate marker. + for i in range(header_type_curr - 1 + 1, + md_parser['github']['header']['max_levels']): + # Reset with the appropriate marker. FIXME. list_marker_top[i] = None if ordered: @@ -354,7 +360,6 @@ def compute_toc_line_indentation_spaces(header_type_curr=1, else: list_marker_top[header_type_curr - 1] = list_marker - # TODO: how does redcarpet deal with this? elif parser == 'redcarpet': no_of_indentation_spaces_curr = 4 * (header_type_curr - 1) diff --git a/tests/tests.py b/tests/tests.py index 34959a9..17cdaf7 100755 --- a/tests/tests.py +++ b/tests/tests.py @@ -25,13 +25,24 @@ import unittest # Some static generic variables. + +# Lines. LINE = 'This is a static line' LINE_EMPTY = '' +LINE_ESCAPE = '\\' +LINE_NEWLINE = '\n' +LINE_CARRIAGE_RETURN = '\r' +LINE_SQUARE_BRACKET_OPEN = '[' +LINE_SQUARE_BRACKET_CLOSE = ']' + +# Spaces. S1 = 1 * ' ' S2 = 2 * ' ' S3 = 3 * ' ' S4 = 4 * ' ' S10 = 10 * ' ' + +# ATX headers. H1 = 1 * '#' H2 = 2 * '#' H3 = 3 * '#' @@ -39,13 +50,17 @@ H5 = 5 * '#' H6 = 6 * '#' H7 = 7 * '#' + +# Lists. LIST_INDENTATION = 4 * ' ' UNORDERED_LIST_SYMBOL = '-' -LINE_ESCAPE = '\\' -LINE_NEWLINE = '\n' -LINE_CARRIAGE_RETURN = '\r' -LINE_SQUARE_BRACKET_OPEN = '[' -LINE_SQUARE_BRACKET_CLOSE = ']' + +# Header types. +GENERIC_HEADER_TYPE_PREV = 2 +GENERIC_HEADER_TYPE_CURR = 6 +BASE_CASE_HEADER_TYPE_PREV = 0 + +GENERIC_NUMBER_OF_INDENTATION_SPACES = 128 # github test lines. GITHUB_LINE_FOO = 'foo' @@ -117,8 +132,8 @@ def test_compute_toc_line_indentation_spaces(self): # First TOC line. self.assertEqual( api.compute_toc_line_indentation_spaces( - header_type_curr=4, - header_type_prev=0, + header_type_curr=GENERIC_HEADER_TYPE_CURR, + header_type_prev=BASE_CASE_HEADER_TYPE_PREV, parser='github', ordered=False, no_of_indentation_spaces_prev=0), 0) @@ -126,38 +141,58 @@ def test_compute_toc_line_indentation_spaces(self): # A generic TOC line after h1. self.assertEqual( api.compute_toc_line_indentation_spaces( - header_type_curr=4, + header_type_curr=GENERIC_HEADER_TYPE_CURR, header_type_prev=1, parser='github', ordered=False, - no_of_indentation_spaces_prev=0), 0) + no_of_indentation_spaces_prev=0), + len(UNORDERED_LIST_SYMBOL) + len(S1)) # First TOC line with the incorrect number of indentation spaces. self.assertEqual( api.compute_toc_line_indentation_spaces( - header_type_curr=4, - header_type_prev=0, + header_type_curr=GENERIC_HEADER_TYPE_CURR, + header_type_prev=BASE_CASE_HEADER_TYPE_PREV, parser='github', ordered=False, - no_of_indentation_spaces_prev=2), 0) + no_of_indentation_spaces_prev= + GENERIC_NUMBER_OF_INDENTATION_SPACES), 0) - # A generic TOC line with the incorrect number of indentation spaces. -# self.assertEqual( -# api.compute_toc_line_indentation_spaces( -# header_type_curr=2, -# header_type_prev=2, -# parser='github', -# ordered=False, -# no_of_indentation_spaces_prev=999), 2) + # Another base case. + # Note that no_of_indentation_spaces_prev might not contain the correct value. + self.assertEqual( + api.compute_toc_line_indentation_spaces( + header_type_curr=GENERIC_HEADER_TYPE_CURR, + header_type_prev=GENERIC_HEADER_TYPE_CURR, + parser='github', + ordered=False, + no_of_indentation_spaces_prev= + GENERIC_NUMBER_OF_INDENTATION_SPACES), + GENERIC_NUMBER_OF_INDENTATION_SPACES) + + # Generic case more indentation. + self.assertEqual( + api.compute_toc_line_indentation_spaces( + header_type_curr=GENERIC_HEADER_TYPE_CURR, + header_type_prev=GENERIC_HEADER_TYPE_PREV, + parser='github', + ordered=False, + no_of_indentation_spaces_prev= + GENERIC_NUMBER_OF_INDENTATION_SPACES), + GENERIC_NUMBER_OF_INDENTATION_SPACES + len(UNORDERED_LIST_SYMBOL) + + len(S1)) - # Generic case. + # Generic case less indentation. self.assertEqual( api.compute_toc_line_indentation_spaces( - header_type_curr=3, - header_type_prev=2, + header_type_curr=GENERIC_HEADER_TYPE_PREV, + header_type_prev=GENERIC_HEADER_TYPE_CURR, parser='github', ordered=False, - no_of_indentation_spaces_prev=2), 4) + no_of_indentation_spaces_prev= + GENERIC_NUMBER_OF_INDENTATION_SPACES), + GENERIC_NUMBER_OF_INDENTATION_SPACES - len(UNORDERED_LIST_SYMBOL) - + len(S1)) # Ordered TOC. # Generic case. More Indentation. @@ -172,7 +207,7 @@ def test_compute_toc_line_indentation_spaces(self): list_marker='.', ordered=True, index=1002, - list_marker_top=['998.','999.','1001.','0.','0.','0.'], + list_marker_top=['998.', '999.', '1001.', '0.', '0.', '0.'], no_of_indentation_spaces_prev=10), 5) # redcarpet.