Added test cases.

This commit is contained in:
kennytm 2017-02-18 23:47:52 +08:00
parent c90cbe8642
commit 8e98db48c4
No known key found for this signature in database
GPG key ID: FEF6C8051D0E013C
6 changed files with 47 additions and 9 deletions

View file

@ -45,7 +45,7 @@ def display_text(cell):
def display_len(s): def display_len(s):
return sum(DISPLAY_WIDTH[unicodedata.east_asian_width(c)] for c in s) return sum(DISPLAY_WIDTH[unicodedata.east_asian_width(c)] for c in s)
def main(odf_path): def main(odf_path, out_file):
ods = ezodf.opendoc(odf_path) ods = ezodf.opendoc(odf_path)
for sheet in ods.sheets: for sheet in ods.sheets:
@ -53,7 +53,7 @@ def main(odf_path):
if not any(column_widths): if not any(column_widths):
continue continue
print('##', sheet.name) print('##', sheet.name, file=out_file)
printed_header = False printed_header = False
for row in sheet.rows(): for row in sheet.rows():
@ -61,22 +61,22 @@ def main(odf_path):
if not any(contents): if not any(contents):
continue continue
print('|', end='') print('|', end='', file=out_file)
for m, content in enumerate(contents): for m, content in enumerate(contents):
column_width = column_widths[m] column_width = column_widths[m]
if not column_width: if not column_width:
continue continue
disp_len = column_width + len(content) - display_len(content) disp_len = column_width + len(content) - display_len(content)
print(' {0:<{1}}'.format(content, disp_len), end=' |') print(' {0:<{1}}'.format(content, disp_len), end=' |', file=out_file)
print() print(file=out_file)
if not printed_header: if not printed_header:
printed_header = True printed_header = True
print('|', end='') print('|', end='', file=out_file)
for w in column_widths: for w in column_widths:
if w: if w:
print(':', '-' * (w+1), '|', sep='', end='') print(':', '-' * (w+1), '|', sep='', end='', file=out_file)
print() print(file=out_file)
if __name__ == '__main__': if __name__ == '__main__':
main(sys.argv[1]) main(sys.argv[1], sys.stdout)

18
test.py Normal file
View file

@ -0,0 +1,18 @@
import ods2md
import unittest
import os.path
import io
class Test(unittest.TestCase):
def perform_test(self, base):
output = io.StringIO()
ods2md.main(os.path.join('test', base + '.ods'), output)
with open(os.path.join('test', base + '.md'), 'r') as f:
self.assertMultiLineEqual(f.read(), output.getvalue())
def test_sample(self):
self.perform_test('sample')
def test_padded(self):
self.perform_test('padded')

8
test/padded.md Normal file
View file

@ -0,0 +1,8 @@
## Sheet1
| X | Y |
|:----|:-----------|
| 0.1 | 1999-12-31 |
| 0.2 | 2000-01-01 |
| | ! |
| 0.4 | |
| 0.7 | ? |

BIN
test/padded.ods Normal file

Binary file not shown.

12
test/sample.md Normal file
View file

@ -0,0 +1,12 @@
## Sheet1
| 版本 | 英文代號 | 中譯 |
|:-------------|:-----------------|:-------------------|
| Ubuntu 17.04 | Zesty Zapus | 熱情的北美草原跳鼠 |
| Ubuntu 16.10 | Yakkety Yak | 喋喋不休的氂牛 |
| Ubuntu 16.04 | Xenial Xerus | 好客的非洲地松鼠 |
| Ubuntu 15.10 | Wily Werewolf | 老謀深算的狼人 |
| Ubuntu 15.04 | Vivid Vervet | 活潑的長尾黑顎猴 |
| Ubuntu 14.10 | Utopic Unicorn | 烏托邦的獨角獸 |
| Ubuntu 14.04 | Trusty Tahr | 可靠的塔爾羊 |
| Ubuntu 13.10 | Saucy Salamander | 活潑的蠑螈 |
| Ubuntu 13.04 | Raring Ringtail | 卯足了勁的環尾貓熊 |

BIN
test/sample.ods Normal file

Binary file not shown.