From 17c3c2f029a0a47eb02d681567619d471b99949c Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 22 Mar 2022 15:55:59 -0700 Subject: [PATCH] add bold bar seperator and ANSI code tests --- jc/parsers/asciitable.py | 3 ++- jc/parsers/asciitable_m.py | 2 +- tests/test_asciitable.py | 31 +++++++++++++++++++++++++++++++ tests/test_asciitable_m.py | 24 ++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/jc/parsers/asciitable.py b/jc/parsers/asciitable.py index 17624fb0..10a1986f 100644 --- a/jc/parsers/asciitable.py +++ b/jc/parsers/asciitable.py @@ -208,7 +208,7 @@ def _normalize_rows(table: str) -> List[str]: continue # data row - remove column separators - line = line.replace('│', ' ').replace('|', ' ') + line = line.replace('│', ' ').replace('|', ' ').replace('┃', ' ') result.append(line) result[0] = _snake_case(result[0]) @@ -244,6 +244,7 @@ def parse( if jc.utils.has_data(data): data = _remove_ansi(data) + print(data) data = _strip(data) data_list = _normalize_rows(data) raw_output = _parse_pretty(data_list) diff --git a/jc/parsers/asciitable_m.py b/jc/parsers/asciitable_m.py index a8eb2ddc..edb95845 100644 --- a/jc/parsers/asciitable_m.py +++ b/jc/parsers/asciitable_m.py @@ -204,7 +204,7 @@ def _snake_case(line: str) -> str: def _fixup_separators(line: str) -> str: """Normalize separators, and remove first and last separators""" # normalize separator - line = line.replace('│', '|') + line = line.replace('│', '|').replace('┃', '|') # remove first separator if it is the first char in the line if line[0] == '|': diff --git a/tests/test_asciitable.py b/tests/test_asciitable.py index e3f4de95..a8cdc14f 100644 --- a/tests/test_asciitable.py +++ b/tests/test_asciitable.py @@ -270,6 +270,37 @@ class MyTests(unittest.TestCase): self.assertEqual(jc.parsers.asciitable.parse(input, quiet=True), expected) + def test_asciitable_pretty_ansi(self): + """ + Test 'asciitable' with a pretty table with ANSI codes + """ + input = '''┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓ \n ┃\x1b[1m \x1b[0m\x1b[1mReleased \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1mTitle \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m Box Office\x1b[0m\x1b[1m \x1b[0m┃ \n ┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━┩ \n │\x1b[36m \x1b[0m\x1b[36mDec 20, 2019\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mStar Wars: The Rise of Skywalker \x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m $952,110,690\x1b[0m\x1b[32m \x1b[0m│ \n │\x1b[36m \x1b[0m\x1b[36mMay 25, 2018\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mSolo: A Star Wars Story \x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m $393,151,347\x1b[0m\x1b[32m \x1b[0m│ \n │\x1b[36m \x1b[0m\x1b[36mDec 15, 2017\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mStar Wars Ep. V111: The Last Jedi\x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m$1,332,539,889\x1b[0m\x1b[32m \x1b[0m│ \n │\x1b[36m \x1b[0m\x1b[36mDec 16, 2016\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mRogue One: A Star Wars Story \x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m$1,332,439,889\x1b[0m\x1b[32m \x1b[0m│ \n └──────────────┴───────────────────────────────────┴────────────────┘ \n''' + + expected = [ + { + "released": "Dec 20, 2019", + "title": "Star Wars: The Rise of Skywalker", + "box_office": "$952,110,690" + }, + { + "released": "May 25, 2018", + "title": "Solo: A Star Wars Story", + "box_office": "$393,151,347" + }, + { + "released": "Dec 15, 2017", + "title": "Star Wars Ep. V111: The Last Jedi", + "box_office": "$1,332,539,889" + }, + { + "released": "Dec 16, 2016", + "title": "Rogue One: A Star Wars Story", + "box_office": "$1,332,439,889" + } + ] + + self.assertEqual(jc.parsers.asciitable.parse(input, quiet=True), expected) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_asciitable_m.py b/tests/test_asciitable_m.py index 22c57f0a..d2c6f483 100644 --- a/tests/test_asciitable_m.py +++ b/tests/test_asciitable_m.py @@ -218,6 +218,30 @@ class MyTests(unittest.TestCase): self.assertEqual(jc.parsers.asciitable_m.parse(input, quiet=True), expected) + def test_asciitable_m_pretty_ansi(self): + """ + Test 'asciitable-m' with a pretty table with ANSI codes + """ + input = ''' +┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓ +┃\x1b[1m \x1b[0m\x1b[1mReleased \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1mTitle \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m Box Office\x1b[0m\x1b[1m \x1b[0m┃ +┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━┩ +│\x1b[36m \x1b[0m\x1b[36mDec 20, 2019\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mStar Wars: The Rise of Skywalker \x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m $952,110,690\x1b[0m\x1b[32m \x1b[0m│ +│\x1b[36m \x1b[0m\x1b[36mMay 25, 2018\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mSolo: A Star Wars Story \x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m $393,151,347\x1b[0m\x1b[32m \x1b[0m│ +│\x1b[36m \x1b[0m\x1b[36mDec 15, 2017\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mStar Wars Ep. V111: The Last Jedi\x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m$1,332,539,889\x1b[0m\x1b[32m \x1b[0m│ +│\x1b[36m \x1b[0m\x1b[36mDec 16, 2016\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mRogue One: A Star Wars Story \x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m$1,332,439,889\x1b[0m\x1b[32m \x1b[0m│ +└──────────────┴───────────────────────────────────┴────────────────┘ +''' + expected = [ + { + "released": "Dec 20, 2019\nMay 25, 2018\nDec 15, 2017\nDec 16, 2016", + "title": "Star Wars: The Rise of Skywalker\nSolo: A Star Wars Story\nStar Wars Ep. V111: The Last Jedi\nRogue One: A Star Wars Story", + "box_office": "$952,110,690\n$393,151,347\n$1,332,539,889\n$1,332,439,889" + } + ] + + self.assertEqual(jc.parsers.asciitable_m.parse(input, quiet=True), expected) + def test_asciitable_m_markdown(self): """ Test 'asciitable_m' with a markdown table. Should raise a ParseError