Skip to content

Commit

Permalink
Fixed unit tests. Fixed a base case.
Browse files Browse the repository at this point in the history
  • Loading branch information
Franco Masotti committed Mar 9, 2019
1 parent 2e6af20 commit 1232ba1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 36 deletions.
27 changes: 16 additions & 11 deletions md_toc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -337,24 +340,26 @@ 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:
list_marker_top[header_type_curr - 1] = str(index) + list_marker
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)
Expand Down
85 changes: 60 additions & 25 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,42 @@
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 * '#'
H4 = 4 * '#'
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'
Expand Down Expand Up @@ -117,47 +132,67 @@ 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)

# 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.
Expand All @@ -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.
Expand Down

0 comments on commit 1232ba1

Please sign in to comment.