Skip to content
This repository was archived by the owner on Jun 23, 2022. It is now read-only.

Commit 66586c7

Browse files
committed
Refactor testing
1 parent e23de03 commit 66586c7

File tree

1 file changed

+81
-58
lines changed

1 file changed

+81
-58
lines changed

tests/test_rst_tables.py

+81-58
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
1+
""" Run tests with
2+
3+
pytest tests
4+
"""
5+
6+
import re
7+
18
# Mock out the vim library
9+
from os.path import join as pjoin, dirname, abspath
210
import sys
3-
sys.path = ['tests/mocks'] + sys.path
11+
ROOT = abspath(pjoin(dirname(__file__), '..'))
12+
MOD_NAME = 'rst_tables'
13+
VIM_PATH = pjoin(ROOT, 'ftplugin', MOD_NAME + '.vim')
14+
sys.path.insert(0, pjoin(ROOT, 'tests', 'mocks'))
15+
416
import vim
517
import mock
618

7-
vimvar = {}
819

20+
VIMVAR = {'expand("<sfile>")': VIM_PATH,
21+
'&encoding': 'utf-8'}
922

1023
def fake_eval(x):
11-
global vimvar
12-
return vimvar[x]
24+
global VIMVAR
25+
return VIMVAR[x]
1326

1427
vim.eval = fake_eval
1528
vim.current = mock.Mock()
16-
vimvar['foo'] = 'bar'
29+
30+
# Read Python code in rst_tables, make into a module.
31+
# https://stackoverflow.com/questions/5362771/how-to-load-a-module-from-code-in-a-string
32+
from types import ModuleType
33+
with open(VIM_PATH) as fobj:
34+
content = fobj.read()
35+
python_code = re.search(r'^Python << endpython(.*)^endpython',
36+
content, flags=re.M | re.S).groups()[0]
37+
mod = ModuleType(MOD_NAME)
38+
sys.modules[MOD_NAME] = mod
39+
exec(python_code, mod.__dict__)
1740

1841
# Begin normal module loading
1942
import os
@@ -48,44 +71,44 @@ def load_fixture_in_vim(self, name):
4871

4972
def testGetBounds(self):
5073
self.load_fixture_in_vim('default')
51-
self.assertEquals((3, 6), get_table_bounds())
74+
self.assertEqual((3, 6, ''), get_table_bounds())
5275

5376
def testGetBoundsOnBeginOfFile(self):
5477
self.load_fixture_in_vim('default')
5578
vim.current.window.cursor = (1, 0)
56-
self.assertEquals((1, 1), get_table_bounds())
79+
self.assertEqual((1, 1, ''), get_table_bounds())
5780

5881
def testGetBoundsOnEndOfFile(self):
5982
self.load_fixture_in_vim('default')
6083
vim.current.window.cursor = (8, 0)
61-
self.assertEquals((8, 9), get_table_bounds())
84+
self.assertEqual((8, 9, ''), get_table_bounds())
6285

6386
def testJoinSimpleRows(self):
6487
input_rows = [['x', 'y', 'z'], ['foo', 'bar']]
6588
expected = ['x\nfoo', 'y\nbar', 'z']
66-
self.assertEquals(expected, join_rows(input_rows))
89+
self.assertEqual(expected, join_rows(input_rows))
6790

6891
input_rows.append(['apple', '', 'pear'])
6992
expected = ['x foo apple', 'y bar', 'z pear']
70-
self.assertEquals(expected, join_rows(input_rows, sep=' '))
93+
self.assertEqual(expected, join_rows(input_rows, sep=' '))
7194

7295
def testPartitionRawLines(self):
73-
self.assertEquals([], partition_raw_lines([]))
74-
self.assertEquals([['']], partition_raw_lines(['']))
75-
self.assertEquals(
96+
self.assertEqual([], partition_raw_lines([]))
97+
self.assertEqual([['']], partition_raw_lines(['']))
98+
self.assertEqual(
7699
[['foo'], ['bar']],
77100
partition_raw_lines(['foo', 'bar']))
78-
self.assertEquals(
101+
self.assertEqual(
79102
[['foo'], ['bar']],
80103
partition_raw_lines(['foo', '+----+', 'bar']))
81-
self.assertEquals(
104+
self.assertEqual(
82105
[['foo', 'bar'], ['baz']],
83106
partition_raw_lines(['+-----+', 'foo', 'bar', '----', 'baz']))
84107

85108
def testParseSimpleTable(self):
86-
self.assertEquals([['x y z']], parse_table(['x y z']))
87-
self.assertEquals([['x', 'y z']], parse_table(['x y z']))
88-
self.assertEquals([['x', 'y', 'z']], parse_table(['x y z']))
109+
self.assertEqual([['x y z']], parse_table(['x y z']))
110+
self.assertEqual([['x', 'y z']], parse_table(['x y z']))
111+
self.assertEqual([['x', 'y', 'z']], parse_table(['x y z']))
89112

90113
def testParseTable(self):
91114
self.load_fixture_in_vim('default')
@@ -95,27 +118,27 @@ def testParseTable(self):
95118
['Bar', 'Even very very long lines like these are fine, as long as you do not put in line endings here.'],
96119
['Qux', 'This is the last line.'],
97120
]
98-
self.assertEquals(expected, parse_table(vim.current.buffer[2:6]))
121+
self.assertEqual(expected, parse_table(vim.current.buffer[2:6]))
99122

100123
def testParseTableUnifiesColumns(self):
101124
input = ['x y', 'a b c', 'only one']
102125
expected = [['x', 'y', ''], ['a', 'b', 'c'], ['only one', '', '']]
103-
self.assertEquals(expected, parse_table(input))
126+
self.assertEqual(expected, parse_table(input))
104127

105128
def testUnifyTables(self):
106129
input = [[' x ', ' y'], ['xxx', ' yyyy ', 'zz']]
107130
expected = [[' x ', ' y', ''], ['xxx', ' yyyy ', 'zz']]
108-
self.assertEquals(expected, unify_table(input))
131+
self.assertEqual(expected, unify_table(input))
109132

110133
def testUnifyTablesRemovesEmptyColumns(self):
111134
input = [['x', '', 'y'], ['xxx', '', 'yyyy', 'zz', ' ']]
112135
expected = [['x', 'y', ''], ['xxx', 'yyyy', 'zz']]
113-
self.assertEquals(expected, unify_table(input))
136+
self.assertEqual(expected, unify_table(input))
114137

115138
def testParseDealsWithSpacesAtLineEnd(self):
116139
input = ['x y ', 'a b ', 'only one']
117140
expected = [['x', 'y'], ['a', 'b'], ['only one', '']]
118-
self.assertEquals(expected, parse_table(input))
141+
self.assertEqual(expected, parse_table(input))
119142

120143
def testParseValidTable(self):
121144
input = ['+-----+----+',
@@ -124,7 +147,7 @@ def testParseValidTable(self):
124147
'| x | y |',
125148
'+-----+----+']
126149
expect = [['Foo', 'Mu'], ['x', 'y']]
127-
self.assertEquals(expect, parse_table(input))
150+
self.assertEqual(expect, parse_table(input))
128151

129152
def testParseCorruptedTable(self):
130153
input = ['+---+---------+',
@@ -135,7 +158,7 @@ def testParseCorruptedTable(self):
135158
'+-----+----+']
136159
expect = [['Foo', 'Mu'],
137160
['x\nblah', 'This became somewhat larger\nA new line']]
138-
self.assertEquals(expect, parse_table(input))
161+
self.assertEqual(expect, parse_table(input))
139162

140163
input = ['+---+---------+',
141164
'| Foo | Mu |',
@@ -145,7 +168,7 @@ def testParseCorruptedTable(self):
145168
'+-----+----+']
146169
expect = [['Foo', 'Mu'],
147170
['x\nblah', 'This became somewhat larger\nA new line']]
148-
self.assertEquals(expect, parse_table(input))
171+
self.assertEqual(expect, parse_table(input))
149172

150173
def testParseMultiLineFields(self):
151174
input = """\
@@ -158,18 +181,18 @@ def testParseMultiLineFields(self):
158181
+-----+---------------------+""".split('\n')
159182
expect = [['Foo', 'Bar'],
160183
['x', 'This is a long line\nthat is spread out\nover multiple lines']]
161-
self.assertEquals(expect, parse_table(input))
184+
self.assertEqual(expect, parse_table(input))
162185

163186
def testSplitRowIntoLines(self):
164187
input = ['Foo', 'Bar']
165188
expect = [['Foo', 'Bar']]
166-
self.assertEquals(expect, split_row_into_lines(input))
189+
self.assertEqual(expect, split_row_into_lines(input))
167190
input = ['One\nTwo\nThree', 'Only one']
168191
expect = [['One', 'Only one'], ['Two', ''], ['Three', '']]
169-
self.assertEquals(expect, split_row_into_lines(input))
192+
self.assertEqual(expect, split_row_into_lines(input))
170193
input = ['One\n\n\nThree', 'Foo\nBar']
171194
expect = [['One', 'Foo'], ['', 'Bar'], ['', ''], ['Three', '']]
172-
self.assertEquals(expect, split_row_into_lines(input))
195+
self.assertEqual(expect, split_row_into_lines(input))
173196

174197
def testDrawMultiLineFields(self):
175198
input = [['Foo', 'Bar'],
@@ -182,23 +205,23 @@ def testDrawMultiLineFields(self):
182205
| | that is spread out |
183206
| | over multiple lines |
184207
+-----+---------------------+""".split('\n')
185-
self.assertEquals(expect, draw_table(input))
208+
self.assertEqual(expect, draw_table('', input))
186209

187210
def testTableLine(self):
188-
self.assertEquals('', table_line([], True))
189-
self.assertEquals('++', table_line([0], True))
190-
self.assertEquals('+++', table_line([0,0], True))
191-
self.assertEquals('++-+', table_line([0,1]))
192-
self.assertEquals('+===+', table_line([3], True))
193-
self.assertEquals('+===+====+', table_line([3,4], True))
194-
self.assertEquals('+------------------+---+--------------------+',
211+
self.assertEqual('', table_line([], True))
212+
self.assertEqual('++', table_line([0], True))
213+
self.assertEqual('+++', table_line([0,0], True))
214+
self.assertEqual('++-+', table_line([0,1]))
215+
self.assertEqual('+===+', table_line([3], True))
216+
self.assertEqual('+===+====+', table_line([3,4], True))
217+
self.assertEqual('+------------------+---+--------------------+',
195218
table_line([18,3,20]))
196219

197220
def testGetColumnWidths(self):
198-
self.assertEquals([], get_column_widths([[]]))
199-
self.assertEquals([0], get_column_widths([['']]))
200-
self.assertEquals([1,2,3], get_column_widths([['x','yy','zzz']]))
201-
self.assertEquals([3,3,3],
221+
self.assertEqual([], get_column_widths([[]]))
222+
self.assertEqual([0], get_column_widths([['']]))
223+
self.assertEqual([1,2,3], get_column_widths([['x','yy','zzz']]))
224+
self.assertEqual([3,3,3],
202225
get_column_widths(
203226
[
204227
['x','y','zzz'],
@@ -207,15 +230,15 @@ def testGetColumnWidths(self):
207230
]))
208231

209232
def testGetColumnWidthsForMultiLineFields(self):
210-
self.assertEquals([3,6],
233+
self.assertEqual([3,6],
211234
get_column_widths([['Foo\nBar\nQux',
212235
'This\nis\nreally\nneat!']]))
213236

214237
def testGetColumnWidthsFromBorderSpec(self):
215238
input = ['+----+-----+--+-------+',
216239
'| xx | xxx | | xxxxx |',
217240
'+====+=====+==+=======+']
218-
self.assertEquals([2, 3, 0, 5],
241+
self.assertEqual([2, 3, 0, 5],
219242
get_column_widths_from_border_spec(input))
220243

221244
def testPadFields(self):
@@ -228,38 +251,38 @@ def testPadFields(self):
228251
[' Crisps ', ' Snacks ', ' Even more yummy, I tell you! ']]
229252
widths = get_column_widths(table)
230253
for input, expect in zip(table, expected_padding):
231-
self.assertEquals(expect, pad_fields(input, widths))
254+
self.assertEqual(expect, pad_fields(input, widths))
232255

233256
def testReflowRowContentsWithEnoughWidth(self):
234257
input = ['Foo\nbar', 'This line\nis spread\nout over\nfour lines.']
235258
expect = ['Foo bar', 'This line is spread out over four lines.']
236-
self.assertEquals(expect, reflow_row_contents(input, [99,99]))
259+
self.assertEqual(expect, reflow_row_contents(input, [99,99]))
237260

238261
def testReflowRowContentsWithWrapping(self):
239262
input = ['Foo\nbar', 'This line\nis spread\nout over\nfour lines.']
240263
expect = ['Foo bar', 'This line is spread\nout over four lines.']
241-
self.assertEquals(expect, reflow_row_contents(input, [10,20]))
264+
self.assertEqual(expect, reflow_row_contents(input, [10,20]))
242265

243266
input = ['Foo\nbar', 'This line\nis spread\nout over\nfour lines.']
244267
expect = ['Foo bar', 'This\nline\nis\nspread\nout\nover\nfour\nlines.']
245-
self.assertEquals(expect, reflow_row_contents(input, [10,6]))
268+
self.assertEqual(expect, reflow_row_contents(input, [10,6]))
246269

247270
def testReflowRowContentsWithoutRoom(self):
248-
#self.assertEquals(expect, reflow_row_contents(input))
271+
#self.assertEqual(expect, reflow_row_contents(input))
249272
pass
250273

251274
def testDrawTable(self):
252-
self.assertEquals([], draw_table([]))
253-
self.assertEquals(['+--+', '| |', '+==+'], draw_table([['']]))
254-
self.assertEquals(['+-----+', '| Foo |', '+=====+'],
255-
draw_table([['Foo']]))
256-
self.assertEquals(
275+
self.assertEqual([], draw_table('', []))
276+
self.assertEqual(['+--+', '| |', '+==+'], draw_table('', [['']]))
277+
self.assertEqual(['+-----+', '| Foo |', '+=====+'],
278+
draw_table('', [['Foo']]))
279+
self.assertEqual(
257280
['+-----+----+',
258281
'| Foo | Mu |',
259282
'+=====+====+',
260283
'| x | y |',
261284
'+-----+----+'],
262-
draw_table([['Foo', 'Mu'], ['x', 'y']]))
285+
draw_table('', [['Foo', 'Mu'], ['x', 'y']]))
263286

264287

265288
def testCreateTable(self):
@@ -281,7 +304,7 @@ def testCreateTable(self):
281304
a line ending.
282305
""".split('\n')
283306
reformat_table()
284-
self.assertEquals(expect, vim.current.buffer)
307+
self.assertEqual(expect, vim.current.buffer)
285308

286309
def testCreateComplexTable(self):
287310
raw_lines = self.read_fixture('multiline-cells')
@@ -303,7 +326,7 @@ def testCreateComplexTable(self):
303326
| | habitasse platea dictumst. Phasellus pretium iaculis. |
304327
+----------------+---------------------------------------------------------------+
305328
""".rstrip().split('\n')
306-
self.assertEquals(expect, draw_table(parse_table(raw_lines)))
329+
self.assertEqual(expect, draw_table('', parse_table(raw_lines)))
307330

308331
def testReflowTable(self):
309332
self.load_fixture_in_vim('reflow')
@@ -329,5 +352,5 @@ def testReflowTable(self):
329352
a line ending.
330353
""".split('\n')
331354
reflow_table()
332-
self.assertEquals(expect, vim.current.buffer)
355+
self.assertEqual(expect, vim.current.buffer)
333356

0 commit comments

Comments
 (0)