def test_coincident_ends(self): """We shouldn't emit empty tags even when coincidently-ending tags don't start together.""" # These Regions aren't in startpoint order. That makes tags_from_test() # instantiate them in a funny order, which makes them sort in the wrong # order, which is realistic. tags = sorted(tags_from_text('d _______\n' 'c _________\n' 'b ___________\n' 'a ____________\n' 'e ___________\n'), key=nesting_order) eq_( spaced_tags(balanced_tags(tags)), '<L>\n' '<a>\n' ' <b>\n' ' <c>\n' ' <e>\n' ' <d>\n' ' </d>\n' ' </e>\n' ' </c>\n' ' </b>\n' ' </a>\n' ' <e>\n' ' </e>\n' ' </L>')
def test_multiline_comment(self): """Multi-line spans should close at the end of one line and reopen at the beginning of the next.""" c = Region('c') c2 = Region('c') l = LINE tags = [(0, True, c), (79, False, c), (80, False, l), (80, True, c2), (151, False, l), (222, False, l), (284, False, c2), (285, False, l), (286, False, l)] text = u"""/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ """ lines = split_content_lines(text) offsets = build_offset_map(lines) actual_lines = [ html_line(text_line.rstrip('\r\n'), e, offset) for text_line, e, offset in zip( lines, tags_per_line(balanced_tags(tags)), offsets) ] expected_lines = [ '<span class="c">/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */</span>', '<span class="c">/* This Source Code Form is subject to the terms of the Mozilla Public</span>', '<span class="c"> * License, v. 2.0. If a copy of the MPL was not distributed with this</span>', '<span class="c"> * file, You can obtain one at http://mozilla.org/MPL/2.0/. */</span>', '' ] eq_(actual_lines, expected_lines)
def test_coincident_ends(self): """We shouldn't emit empty tags even when coincidently-ending tags don't start together.""" # These Regions aren't in startpoint order. That makes tags_from_test() # instantiate them in a funny order, which makes them sort in the wrong # order, which is realistic. tags = sorted(tags_from_text('d _______\n' 'c _________\n' 'b ___________\n' 'a ____________\n' 'e ___________\n'), key=nesting_order) eq_(spaced_tags(balanced_tags(tags)), '<L>\n' '<a>\n' ' <b>\n' ' <c>\n' ' <e>\n' ' <d>\n' ' </d>\n' ' </e>\n' ' </c>\n' ' </b>\n' ' </a>\n' ' <e>\n' ' </e>\n' ' </L>')
def test_coincident(self): """We shouldn't emit pointless empty tags when tempted to.""" tags = sorted(tags_from_text('a _____\n' 'b _____\n' 'c _____\n'), key=nesting_order) eq_(spaced_tags(balanced_tags(tags)), '<L>\n' '<a>\n' '<b>\n' '<c>\n' ' </c>\n' ' </b>\n' ' </a>\n' ' </L>')
def test_coincident(self): """We shouldn't emit pointless empty tags when tempted to.""" tags = sorted(tags_from_text('a _____\n' 'b _____\n' 'c _____\n'), key=nesting_order) eq_( spaced_tags(balanced_tags(tags)), '<L>\n' '<a>\n' '<b>\n' '<c>\n' ' </c>\n' ' </b>\n' ' </a>\n' ' </L>')
def test_horrors(self): """Try a fairly horrific scenario:: A _______________ (0, 7) B _________ (2, 6) C ____________ (5, 9) D _______ (8, 11) E __ (10, 11) 0 2 5 6 7 8 9 A contains B. B closes while C's still going on. D and E end at the same time. There's even a Region in there. """ a = RefWithoutData('a') b = Region('b') c = RefWithoutData('c') d = RefWithoutData('d') e = RefWithoutData('e') tags = [(0, True, a), (2, True, b), (5, True, c), (6, False, b), (7, False, a), (8, True, d), (9, False, c), (10, True, e), (11, False, e), (11, False, d)] eq_( spaced_tags(balanced_tags(tags)), '<L>\n' '<a>\n' ' <b>\n' ' <c>\n' ' </c>\n' ' </b>\n' ' <c>\n' ' </c>\n' ' </a>\n' ' <c>\n' ' <d>\n' ' </d>\n' ' </c>\n' ' <d>\n' ' <e>\n' ' </e>\n' ' </d>\n' ' </L>')
def test_horrors(self): """Try a fairly horrific scenario:: A _______________ (0, 7) B _________ (2, 6) C ____________ (5, 9) D _______ (8, 11) E __ (10, 11) 0 2 5 6 7 8 9 A contains B. B closes while C's still going on. D and E end at the same time. There's even a Region in there. """ a = RefWithoutData('a') b = Region('b') c = RefWithoutData('c') d = RefWithoutData('d') e = RefWithoutData('e') tags = [(0, True, a), (2, True, b), (5, True, c), (6, False, b), (7, False, a), (8, True, d), (9, False, c), (10, True, e), (11, False, e), (11, False, d)] eq_(spaced_tags(balanced_tags(tags)), '<L>\n' '<a>\n' ' <b>\n' ' <c>\n' ' </c>\n' ' </b>\n' ' <c>\n' ' </c>\n' ' </a>\n' ' <c>\n' ' <d>\n' ' </d>\n' ' </c>\n' ' <d>\n' ' <e>\n' ' </e>\n' ' </d>\n' ' </L>')
def test_multiline_comment(self): """Multi-line spans should close at the end of one line and reopen at the beginning of the next.""" c = Region('c') c2 = Region('c') l = LINE tags = [(0, True, c), (79, False, c), (80, False, l), (80, True, c2), (151, False, l), (222, False, l), (284, False, c2), (285, False, l), (286, False, l)] text = u"""/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ """ lines = text.splitlines(True) offsets = cumulative_sum(map(len, lines)) actual_lines = [html_line(text_line.rstrip('\r\n'), e, offset) for text_line, e, offset in zip(lines, tags_per_line(balanced_tags(tags)), offsets)] expected_lines = ['<span class="c">/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */</span>', '<span class="c">/* This Source Code Form is subject to the terms of the Mozilla Public</span>', '<span class="c"> * License, v. 2.0. If a copy of the MPL was not distributed with this</span>', '<span class="c"> * file, You can obtain one at http://mozilla.org/MPL/2.0/. */</span>', ''] eq_(actual_lines, expected_lines)
def test_empty(self): """Some files are empty. Make sure they work.""" eq_(list(balanced_tags([])), [])