1
+ """ Run tests with
2
+
3
+ pytest tests
4
+ """
5
+
6
+ import re
7
+
1
8
# Mock out the vim library
9
+ from os .path import join as pjoin , dirname , abspath
2
10
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
+
4
16
import vim
5
17
import mock
6
18
7
- vimvar = {}
8
19
20
+ VIMVAR = {'expand("<sfile>")' : VIM_PATH ,
21
+ '&encoding' : 'utf-8' }
9
22
10
23
def fake_eval (x ):
11
- global vimvar
12
- return vimvar [x ]
24
+ global VIMVAR
25
+ return VIMVAR [x ]
13
26
14
27
vim .eval = fake_eval
15
28
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__ )
17
40
18
41
# Begin normal module loading
19
42
import os
@@ -48,44 +71,44 @@ def load_fixture_in_vim(self, name):
48
71
49
72
def testGetBounds (self ):
50
73
self .load_fixture_in_vim ('default' )
51
- self .assertEquals ((3 , 6 ), get_table_bounds ())
74
+ self .assertEqual ((3 , 6 , '' ), get_table_bounds ())
52
75
53
76
def testGetBoundsOnBeginOfFile (self ):
54
77
self .load_fixture_in_vim ('default' )
55
78
vim .current .window .cursor = (1 , 0 )
56
- self .assertEquals ((1 , 1 ), get_table_bounds ())
79
+ self .assertEqual ((1 , 1 , '' ), get_table_bounds ())
57
80
58
81
def testGetBoundsOnEndOfFile (self ):
59
82
self .load_fixture_in_vim ('default' )
60
83
vim .current .window .cursor = (8 , 0 )
61
- self .assertEquals ((8 , 9 ), get_table_bounds ())
84
+ self .assertEqual ((8 , 9 , '' ), get_table_bounds ())
62
85
63
86
def testJoinSimpleRows (self ):
64
87
input_rows = [['x' , 'y' , 'z' ], ['foo' , 'bar' ]]
65
88
expected = ['x\n foo' , 'y\n bar' , 'z' ]
66
- self .assertEquals (expected , join_rows (input_rows ))
89
+ self .assertEqual (expected , join_rows (input_rows ))
67
90
68
91
input_rows .append (['apple' , '' , 'pear' ])
69
92
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 = ' ' ))
71
94
72
95
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 (
76
99
[['foo' ], ['bar' ]],
77
100
partition_raw_lines (['foo' , 'bar' ]))
78
- self .assertEquals (
101
+ self .assertEqual (
79
102
[['foo' ], ['bar' ]],
80
103
partition_raw_lines (['foo' , '+----+' , 'bar' ]))
81
- self .assertEquals (
104
+ self .assertEqual (
82
105
[['foo' , 'bar' ], ['baz' ]],
83
106
partition_raw_lines (['+-----+' , 'foo' , 'bar' , '----' , 'baz' ]))
84
107
85
108
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' ]))
89
112
90
113
def testParseTable (self ):
91
114
self .load_fixture_in_vim ('default' )
@@ -95,27 +118,27 @@ def testParseTable(self):
95
118
['Bar' , 'Even very very long lines like these are fine, as long as you do not put in line endings here.' ],
96
119
['Qux' , 'This is the last line.' ],
97
120
]
98
- self .assertEquals (expected , parse_table (vim .current .buffer [2 :6 ]))
121
+ self .assertEqual (expected , parse_table (vim .current .buffer [2 :6 ]))
99
122
100
123
def testParseTableUnifiesColumns (self ):
101
124
input = ['x y' , 'a b c' , 'only one' ]
102
125
expected = [['x' , 'y' , '' ], ['a' , 'b' , 'c' ], ['only one' , '' , '' ]]
103
- self .assertEquals (expected , parse_table (input ))
126
+ self .assertEqual (expected , parse_table (input ))
104
127
105
128
def testUnifyTables (self ):
106
129
input = [[' x ' , ' y' ], ['xxx' , ' yyyy ' , 'zz' ]]
107
130
expected = [[' x ' , ' y' , '' ], ['xxx' , ' yyyy ' , 'zz' ]]
108
- self .assertEquals (expected , unify_table (input ))
131
+ self .assertEqual (expected , unify_table (input ))
109
132
110
133
def testUnifyTablesRemovesEmptyColumns (self ):
111
134
input = [['x' , '' , 'y' ], ['xxx' , '' , 'yyyy' , 'zz' , ' ' ]]
112
135
expected = [['x' , 'y' , '' ], ['xxx' , 'yyyy' , 'zz' ]]
113
- self .assertEquals (expected , unify_table (input ))
136
+ self .assertEqual (expected , unify_table (input ))
114
137
115
138
def testParseDealsWithSpacesAtLineEnd (self ):
116
139
input = ['x y ' , 'a b ' , 'only one' ]
117
140
expected = [['x' , 'y' ], ['a' , 'b' ], ['only one' , '' ]]
118
- self .assertEquals (expected , parse_table (input ))
141
+ self .assertEqual (expected , parse_table (input ))
119
142
120
143
def testParseValidTable (self ):
121
144
input = ['+-----+----+' ,
@@ -124,7 +147,7 @@ def testParseValidTable(self):
124
147
'| x | y |' ,
125
148
'+-----+----+' ]
126
149
expect = [['Foo' , 'Mu' ], ['x' , 'y' ]]
127
- self .assertEquals (expect , parse_table (input ))
150
+ self .assertEqual (expect , parse_table (input ))
128
151
129
152
def testParseCorruptedTable (self ):
130
153
input = ['+---+---------+' ,
@@ -135,7 +158,7 @@ def testParseCorruptedTable(self):
135
158
'+-----+----+' ]
136
159
expect = [['Foo' , 'Mu' ],
137
160
['x\n blah' , 'This became somewhat larger\n A new line' ]]
138
- self .assertEquals (expect , parse_table (input ))
161
+ self .assertEqual (expect , parse_table (input ))
139
162
140
163
input = ['+---+---------+' ,
141
164
'| Foo | Mu |' ,
@@ -145,7 +168,7 @@ def testParseCorruptedTable(self):
145
168
'+-----+----+' ]
146
169
expect = [['Foo' , 'Mu' ],
147
170
['x\n blah' , 'This became somewhat larger\n A new line' ]]
148
- self .assertEquals (expect , parse_table (input ))
171
+ self .assertEqual (expect , parse_table (input ))
149
172
150
173
def testParseMultiLineFields (self ):
151
174
input = """\
@@ -158,18 +181,18 @@ def testParseMultiLineFields(self):
158
181
+-----+---------------------+""" .split ('\n ' )
159
182
expect = [['Foo' , 'Bar' ],
160
183
['x' , 'This is a long line\n that is spread out\n over multiple lines' ]]
161
- self .assertEquals (expect , parse_table (input ))
184
+ self .assertEqual (expect , parse_table (input ))
162
185
163
186
def testSplitRowIntoLines (self ):
164
187
input = ['Foo' , 'Bar' ]
165
188
expect = [['Foo' , 'Bar' ]]
166
- self .assertEquals (expect , split_row_into_lines (input ))
189
+ self .assertEqual (expect , split_row_into_lines (input ))
167
190
input = ['One\n Two\n Three' , 'Only one' ]
168
191
expect = [['One' , 'Only one' ], ['Two' , '' ], ['Three' , '' ]]
169
- self .assertEquals (expect , split_row_into_lines (input ))
192
+ self .assertEqual (expect , split_row_into_lines (input ))
170
193
input = ['One\n \n \n Three' , 'Foo\n Bar' ]
171
194
expect = [['One' , 'Foo' ], ['' , 'Bar' ], ['' , '' ], ['Three' , '' ]]
172
- self .assertEquals (expect , split_row_into_lines (input ))
195
+ self .assertEqual (expect , split_row_into_lines (input ))
173
196
174
197
def testDrawMultiLineFields (self ):
175
198
input = [['Foo' , 'Bar' ],
@@ -182,23 +205,23 @@ def testDrawMultiLineFields(self):
182
205
| | that is spread out |
183
206
| | over multiple lines |
184
207
+-----+---------------------+""" .split ('\n ' )
185
- self .assertEquals (expect , draw_table (input ))
208
+ self .assertEqual (expect , draw_table ('' , input ))
186
209
187
210
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 ('+------------------+---+--------------------+' ,
195
218
table_line ([18 ,3 ,20 ]))
196
219
197
220
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 ],
202
225
get_column_widths (
203
226
[
204
227
['x' ,'y' ,'zzz' ],
@@ -207,15 +230,15 @@ def testGetColumnWidths(self):
207
230
]))
208
231
209
232
def testGetColumnWidthsForMultiLineFields (self ):
210
- self .assertEquals ([3 ,6 ],
233
+ self .assertEqual ([3 ,6 ],
211
234
get_column_widths ([['Foo\n Bar\n Qux' ,
212
235
'This\n is\n really\n neat!' ]]))
213
236
214
237
def testGetColumnWidthsFromBorderSpec (self ):
215
238
input = ['+----+-----+--+-------+' ,
216
239
'| xx | xxx | | xxxxx |' ,
217
240
'+====+=====+==+=======+' ]
218
- self .assertEquals ([2 , 3 , 0 , 5 ],
241
+ self .assertEqual ([2 , 3 , 0 , 5 ],
219
242
get_column_widths_from_border_spec (input ))
220
243
221
244
def testPadFields (self ):
@@ -228,38 +251,38 @@ def testPadFields(self):
228
251
[' Crisps ' , ' Snacks ' , ' Even more yummy, I tell you! ' ]]
229
252
widths = get_column_widths (table )
230
253
for input , expect in zip (table , expected_padding ):
231
- self .assertEquals (expect , pad_fields (input , widths ))
254
+ self .assertEqual (expect , pad_fields (input , widths ))
232
255
233
256
def testReflowRowContentsWithEnoughWidth (self ):
234
257
input = ['Foo\n bar' , 'This line\n is spread\n out over\n four lines.' ]
235
258
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 ]))
237
260
238
261
def testReflowRowContentsWithWrapping (self ):
239
262
input = ['Foo\n bar' , 'This line\n is spread\n out over\n four lines.' ]
240
263
expect = ['Foo bar' , 'This line is spread\n out over four lines.' ]
241
- self .assertEquals (expect , reflow_row_contents (input , [10 ,20 ]))
264
+ self .assertEqual (expect , reflow_row_contents (input , [10 ,20 ]))
242
265
243
266
input = ['Foo\n bar' , 'This line\n is spread\n out over\n four lines.' ]
244
267
expect = ['Foo bar' , 'This\n line\n is\n spread\n out\n over\n four\n lines.' ]
245
- self .assertEquals (expect , reflow_row_contents (input , [10 ,6 ]))
268
+ self .assertEqual (expect , reflow_row_contents (input , [10 ,6 ]))
246
269
247
270
def testReflowRowContentsWithoutRoom (self ):
248
- #self.assertEquals (expect, reflow_row_contents(input))
271
+ #self.assertEqual (expect, reflow_row_contents(input))
249
272
pass
250
273
251
274
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 (
257
280
['+-----+----+' ,
258
281
'| Foo | Mu |' ,
259
282
'+=====+====+' ,
260
283
'| x | y |' ,
261
284
'+-----+----+' ],
262
- draw_table ([['Foo' , 'Mu' ], ['x' , 'y' ]]))
285
+ draw_table ('' , [['Foo' , 'Mu' ], ['x' , 'y' ]]))
263
286
264
287
265
288
def testCreateTable (self ):
@@ -281,7 +304,7 @@ def testCreateTable(self):
281
304
a line ending.
282
305
""" .split ('\n ' )
283
306
reformat_table ()
284
- self .assertEquals (expect , vim .current .buffer )
307
+ self .assertEqual (expect , vim .current .buffer )
285
308
286
309
def testCreateComplexTable (self ):
287
310
raw_lines = self .read_fixture ('multiline-cells' )
@@ -303,7 +326,7 @@ def testCreateComplexTable(self):
303
326
| | habitasse platea dictumst. Phasellus pretium iaculis. |
304
327
+----------------+---------------------------------------------------------------+
305
328
""" .rstrip ().split ('\n ' )
306
- self .assertEquals (expect , draw_table (parse_table (raw_lines )))
329
+ self .assertEqual (expect , draw_table ('' , parse_table (raw_lines )))
307
330
308
331
def testReflowTable (self ):
309
332
self .load_fixture_in_vim ('reflow' )
@@ -329,5 +352,5 @@ def testReflowTable(self):
329
352
a line ending.
330
353
""" .split ('\n ' )
331
354
reflow_table ()
332
- self .assertEquals (expect , vim .current .buffer )
355
+ self .assertEqual (expect , vim .current .buffer )
333
356
0 commit comments