mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
add tests
This commit is contained in:
@ -90,6 +90,20 @@ def _lstrip(string: str) -> str:
|
|||||||
return '\n'.join(new_lstrip_list)
|
return '\n'.join(new_lstrip_list)
|
||||||
|
|
||||||
|
|
||||||
|
def _rstrip(string: str) -> str:
|
||||||
|
"""find the rightmost non-whitespace character and rstrip to that index"""
|
||||||
|
rstrip_list = [x for x in string.splitlines() if not len(x.strip()) == 0]
|
||||||
|
end_points = (len(x.rstrip()) for x in rstrip_list)
|
||||||
|
max_point = max(end_points)
|
||||||
|
new_rstrip_list = (x[:max_point] for x in rstrip_list)
|
||||||
|
return '\n'.join(new_rstrip_list)
|
||||||
|
|
||||||
|
|
||||||
|
def _strip(string: str) -> str:
|
||||||
|
string = _lstrip(string)
|
||||||
|
string = _rstrip(string)
|
||||||
|
return string
|
||||||
|
|
||||||
def _table_sniff(string: str) -> str:
|
def _table_sniff(string: str) -> str:
|
||||||
"""Find the table-type via heuristics"""
|
"""Find the table-type via heuristics"""
|
||||||
# pretty tables
|
# pretty tables
|
||||||
@ -326,7 +340,7 @@ def parse(
|
|||||||
|
|
||||||
if jc.utils.has_data(data):
|
if jc.utils.has_data(data):
|
||||||
data = _remove_ansi(data)
|
data = _remove_ansi(data)
|
||||||
data = _lstrip(data)
|
data = _strip(data)
|
||||||
table_type = _table_sniff(data)
|
table_type = _table_sniff(data)
|
||||||
|
|
||||||
if table_type == 'pretty':
|
if table_type == 'pretty':
|
||||||
|
222
tests/test_asciitable_m.py
Normal file
222
tests/test_asciitable_m.py
Normal file
@ -0,0 +1,222 @@
|
|||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
import jc.parsers.asciitable_m
|
||||||
|
|
||||||
|
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
|
||||||
|
class MyTests(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_asciitable_m_nodata(self):
|
||||||
|
"""
|
||||||
|
Test 'asciitable_m' with no data
|
||||||
|
"""
|
||||||
|
self.assertEqual(jc.parsers.asciitable_m.parse('', quiet=True), [])
|
||||||
|
|
||||||
|
def test_asciitable_m_pure_ascii(self):
|
||||||
|
"""
|
||||||
|
Test 'asciitable_m' with a pure ASCII table
|
||||||
|
"""
|
||||||
|
input = '''
|
||||||
|
+========+========+========+========+========+========+========+
|
||||||
|
| type | tota | used | fr ee | shar | buff | avai |
|
||||||
|
| | l | | | ed | _cac | labl |
|
||||||
|
| | | | | | he | e |
|
||||||
|
+========+========+========+========+========+========+========+
|
||||||
|
| Mem | 3861 | 2228 | 3364 | 1183 | 2743 | 3389 |
|
||||||
|
| | 332 | 20 | 176 | 2 | 36 | 588 |
|
||||||
|
+--------+--------+--------+--------+--------+--------+--------+
|
||||||
|
| | | | | | | |
|
||||||
|
| | | | | test 2 | | |
|
||||||
|
+--------+--------+--------+--------+--------+--------+--------+
|
||||||
|
| last | last | last | ab cde | | | final |
|
||||||
|
+========+========+========+========+========+========+========+
|
||||||
|
'''
|
||||||
|
expected = [
|
||||||
|
{
|
||||||
|
"type": "Mem",
|
||||||
|
"tota_l": "3861\n332",
|
||||||
|
"used": "2228\n20",
|
||||||
|
"fr_ee": "3364\n176",
|
||||||
|
"shar_ed": "1183\n2",
|
||||||
|
"buff_cac_he": "2743\n36",
|
||||||
|
"avai_labl_e": "3389\n588"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "",
|
||||||
|
"tota_l": "",
|
||||||
|
"used": "",
|
||||||
|
"fr_ee": "",
|
||||||
|
"shar_ed": "test 2",
|
||||||
|
"buff_cac_he": "",
|
||||||
|
"avai_labl_e": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "last",
|
||||||
|
"tota_l": "last",
|
||||||
|
"used": "last",
|
||||||
|
"fr_ee": "ab cde",
|
||||||
|
"shar_ed": "",
|
||||||
|
"buff_cac_he": "",
|
||||||
|
"avai_labl_e": "final"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(jc.parsers.asciitable_m.parse(input, quiet=True), expected)
|
||||||
|
|
||||||
|
def test_asciitable_m_unicode(self):
|
||||||
|
"""
|
||||||
|
Test 'asciitable_m' with a unicode table
|
||||||
|
"""
|
||||||
|
input = '''
|
||||||
|
╒════════╤════════╤════════╤════════╤════════╤════════╤════════╕
|
||||||
|
│ type │ tota │ used │ fr ee │ shar │ buff │ avai │
|
||||||
|
│ │ l │ │ │ ed │ _cac │ labl │
|
||||||
|
│ │ │ │ │ │ he │ e │
|
||||||
|
╞════════╪════════╪════════╪════════╪════════╪════════╪════════╡
|
||||||
|
│ Mem │ 3861 │ 2228 │ 3364 │ 1183 │ 2743 │ 3389 │
|
||||||
|
│ │ 332 │ 20 │ 176 │ 2 │ 36 │ 588 │
|
||||||
|
├────────┼────────┼────────┼────────┼────────┼────────┼────────┤
|
||||||
|
│ Swap │ 2097 │ 0 │ 2097 │ │ │ │
|
||||||
|
│ │ 148 │ │ 148 │ │ │ │
|
||||||
|
│ │ │ │ kb │ │ │ │
|
||||||
|
├────────┼────────┼────────┼────────┼────────┼────────┼────────┤
|
||||||
|
│ last │ last │ last │ ab cde │ │ │ final │
|
||||||
|
╘════════╧════════╧════════╧════════╧════════╧════════╧════════╛
|
||||||
|
'''
|
||||||
|
expected = [
|
||||||
|
{
|
||||||
|
"type": "Mem",
|
||||||
|
"tota_l": "3861\n332",
|
||||||
|
"used": "2228\n20",
|
||||||
|
"fr_ee": "3364\n176",
|
||||||
|
"shar_ed": "1183\n2",
|
||||||
|
"buff_cac_he": "2743\n36",
|
||||||
|
"avai_labl_e": "3389\n588"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Swap",
|
||||||
|
"tota_l": "2097\n148",
|
||||||
|
"used": "0",
|
||||||
|
"fr_ee": "2097\n148\nkb",
|
||||||
|
"shar_ed": "",
|
||||||
|
"buff_cac_he": "",
|
||||||
|
"avai_labl_e": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "last",
|
||||||
|
"tota_l": "last",
|
||||||
|
"used": "last",
|
||||||
|
"fr_ee": "ab cde",
|
||||||
|
"shar_ed": "",
|
||||||
|
"buff_cac_he": "",
|
||||||
|
"avai_labl_e": "final"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(jc.parsers.asciitable_m.parse(input, quiet=True), expected)
|
||||||
|
|
||||||
|
def test_asciitable_m_pure_ascii_extra_spaces(self):
|
||||||
|
"""
|
||||||
|
Test 'asciitable_m' with a pure ASCII table that has heading and
|
||||||
|
trailing spaces and newlines.
|
||||||
|
"""
|
||||||
|
input = '''
|
||||||
|
|
||||||
|
|
||||||
|
+========+========+========+========+========+========+========+
|
||||||
|
| type | tota | used | fr ee | shar | buff | avai
|
||||||
|
| | l | | | ed | _cac | labl
|
||||||
|
| | | | | | he | e |
|
||||||
|
+========+========+========+========+========+========+========+
|
||||||
|
| Mem | 3861 | 2228 | 3364 | 1183 | 2743 | 3389 |
|
||||||
|
| | 332 | 20 | 176 | 2 | 36 | 588 |
|
||||||
|
+--------+--------+--------+--------+--------+--------+--------+
|
||||||
|
| | | | | | | |
|
||||||
|
| | | | | test 2 | | |
|
||||||
|
+--------+--------+--------+--------+--------+--------+--------+
|
||||||
|
| last | last | last | ab cde | | | final
|
||||||
|
+========+========+========+========+========+========+========+
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
expected = [
|
||||||
|
{
|
||||||
|
"type": "Mem",
|
||||||
|
"tota_l": "3861\n332",
|
||||||
|
"used": "2228\n20",
|
||||||
|
"fr_ee": "3364\n176",
|
||||||
|
"shar_ed": "1183\n2",
|
||||||
|
"buff_cac_he": "2743\n36",
|
||||||
|
"avai_labl_e": "3389\n588"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "",
|
||||||
|
"tota_l": "",
|
||||||
|
"used": "",
|
||||||
|
"fr_ee": "",
|
||||||
|
"shar_ed": "test 2",
|
||||||
|
"buff_cac_he": "",
|
||||||
|
"avai_labl_e": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "last",
|
||||||
|
"tota_l": "last",
|
||||||
|
"used": "last",
|
||||||
|
"fr_ee": "ab cde",
|
||||||
|
"shar_ed": "",
|
||||||
|
"buff_cac_he": "",
|
||||||
|
"avai_labl_e": "final"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(jc.parsers.asciitable_m.parse(input, quiet=True), expected)
|
||||||
|
|
||||||
|
def test_asciitable_m_unicode_extra_spaces(self):
|
||||||
|
"""
|
||||||
|
Test 'asciitable_m' with a pure ASCII table that has heading and
|
||||||
|
trailing spaces and newlines.
|
||||||
|
"""
|
||||||
|
input = '''
|
||||||
|
|
||||||
|
|
||||||
|
╒════════╤════════╤════════╤════════╤════════╤════════╤════════╕
|
||||||
|
type │ tota │ used │ free │ shar │ buff │ avai
|
||||||
|
│ l │ │ │ ed │ _cac │ labl
|
||||||
|
│ │ │ │ │ he │ e
|
||||||
|
╞════════╪════════╪════════╪════════╪════════╪════════╪════════╡
|
||||||
|
Mem │ 3861 │ 2228 │ 3364 │ 1183 │ 2743 │ 3389
|
||||||
|
│ 332 │ 20 │ 176 │ 2 │ 36 │ 588
|
||||||
|
├────────┼────────┼────────┼────────┼────────┼────────┼────────┤
|
||||||
|
Swap │ 2097 │ 0 │ 2097 │ │ │
|
||||||
|
│ 148 │ │ 148 │ │ │
|
||||||
|
╘════════╧════════╧════════╧════════╧════════╧════════╧════════╛
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
expected = [
|
||||||
|
{
|
||||||
|
"type": "Mem",
|
||||||
|
"tota_l": "3861\n332",
|
||||||
|
"used": "2228\n20",
|
||||||
|
"free": "3364\n176",
|
||||||
|
"shar_ed": "1183\n2",
|
||||||
|
"buff_cac_he": "2743\n36",
|
||||||
|
"avai_labl_e": "3389\n588"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Swap",
|
||||||
|
"tota_l": "2097\n148",
|
||||||
|
"used": "0",
|
||||||
|
"free": "2097\n148",
|
||||||
|
"shar_ed": "",
|
||||||
|
"buff_cac_he": "",
|
||||||
|
"avai_labl_e": ""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(jc.parsers.asciitable_m.parse(input, quiet=True), expected)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
Reference in New Issue
Block a user