aboutsummaryrefslogtreecommitdiffstats
path: root/jython-tosca-parser/src/main/resources/Lib/site-packages/cliff-2.4.0-py2.7.egg/cliff/tests/test_formatters_table.py
blob: 7e8cf97a1f734cf232335b80192941298c2a345a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/usr/bin/env python
#
#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.

import mock
from six import StringIO
import os
import argparse

from cliff.formatters import table
from cliff.tests import test_columns


class args(object):
    def __init__(self, max_width=0, print_empty=False):
        if max_width > 0:
            self.max_width = max_width
        else:
            # Envvar is only taken into account iff CLI parameter not given
            self.max_width = int(os.environ.get('CLIFF_MAX_TERM_WIDTH', 0))
        self.print_empty = print_empty


def _table_tester_helper(tags, data, extra_args=None):
    """Get table output as a string, formatted according to
    CLI arguments, environment variables and terminal size

    tags - tuple of strings for data tags (column headers or fields)
    data - tuple of strings for single data row
         - list of tuples of strings for multiple rows of data
    extra_args - an instance of class args
               - a list of strings for CLI arguments
    """
    sf = table.TableFormatter()

    if extra_args is None:
        # Default to no CLI arguments
        parsed_args = args()
    elif type(extra_args) == args:
        # Use the given CLI arguments
        parsed_args = extra_args
    else:
        # Parse arguments as if passed on the command-line
        parser = argparse.ArgumentParser(description='Testing...')
        sf.add_argument_group(parser)
        parsed_args = parser.parse_args(extra_args)

    output = StringIO()
    emitter = sf.emit_list if type(data) is list else sf.emit_one
    emitter(tags, data, output, parsed_args)
    return output.getvalue()


@mock.patch('cliff.utils.terminal_width')
def test_table_formatter(tw):
    tw.return_value = 80
    c = ('a', 'b', 'c', 'd')
    d = ('A', 'B', 'C', 'test\rcarriage\r\nreturn')
    expected = '''\
+-------+---------------+
| Field | Value         |
+-------+---------------+
| a     | A             |
| b     | B             |
| c     | C             |
| d     | test carriage |
|       | return        |
+-------+---------------+
'''
    assert expected == _table_tester_helper(c, d)


# Multi-line output when width is restricted to 42 columns
expected_ml_val = '''\
+-------+--------------------------------+
| Field | Value                          |
+-------+--------------------------------+
| a     | A                              |
| b     | B                              |
| c     | C                              |
| d     | dddddddddddddddddddddddddddddd |
|       | dddddddddddddddddddddddddddddd |
|       | ddddddddddddddddd              |
+-------+--------------------------------+
'''

# Multi-line output when width is restricted to 80 columns
expected_ml_80_val = '''\
+-------+----------------------------------------------------------------------+
| Field | Value                                                                |
+-------+----------------------------------------------------------------------+
| a     | A                                                                    |
| b     | B                                                                    |
| c     | C                                                                    |
| d     | dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd |
|       | ddddddddd                                                            |
+-------+----------------------------------------------------------------------+
'''  # noqa

# Single-line output, for when no line length restriction apply
expected_sl_val = '''\
+-------+-------------------------------------------------------------------------------+
| Field | Value                                                                         |
+-------+-------------------------------------------------------------------------------+
| a     | A                                                                             |
| b     | B                                                                             |
| c     | C                                                                             |
| d     | ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd |
+-------+-------------------------------------------------------------------------------+
'''  # noqa


@mock.patch('cliff.utils.terminal_width')
def test_table_formatter_no_cli_param(tw):
    tw.return_value = 80
    c = ('a', 'b', 'c', 'd')
    d = ('A', 'B', 'C', 'd' * 77)
    assert expected_ml_80_val == _table_tester_helper(c, d, extra_args=args())


@mock.patch('cliff.utils.terminal_width')
def test_table_formatter_cli_param(tw):
    tw.return_value = 80
    c = ('a', 'b', 'c', 'd')
    d = ('A', 'B', 'C', 'd' * 77)
    assert (expected_ml_val ==
            _table_tester_helper(c, d, extra_args=['--max-width', '42']))


@mock.patch('cliff.utils.terminal_width')
def test_table_formatter_no_cli_param_unlimited_tw(tw):
    tw.return_value = 0
    c = ('a', 'b', 'c', 'd')
    d = ('A', 'B', 'C', 'd' * 77)
    # output should not be wrapped to multiple lines
    assert expected_sl_val == _table_tester_helper(c, d, extra_args=args())


@mock.patch('cliff.utils.terminal_width')
def test_table_formatter_cli_param_unlimited_tw(tw):
    tw.return_value = 0
    c = ('a', 'b', 'c', 'd')
    d = ('A', 'B', 'C', 'd' * 77)
    assert (expected_ml_val ==
            _table_tester_helper(c, d, extra_args=['--max-width', '42']))


@mock.patch('cliff.utils.terminal_width')
@mock.patch.dict(os.environ, {'CLIFF_MAX_TERM_WIDTH': '666'})
def test_table_formatter_cli_param_envvar_big(tw):
    tw.return_value = 80
    c = ('a', 'b', 'c', 'd')
    d = ('A', 'B', 'C', 'd' * 77)
    assert (expected_ml_val ==
            _table_tester_helper(c, d, extra_args=['--max-width', '42']))


@mock.patch('cliff.utils.terminal_width')
@mock.patch.dict(os.environ, {'CLIFF_MAX_TERM_WIDTH': '23'})
def test_table_formatter_cli_param_envvar_tiny(tw):
    tw.return_value = 80
    c = ('a', 'b', 'c', 'd')
    d = ('A', 'B', 'C', 'd' * 77)
    assert (expected_ml_val ==
            _table_tester_helper(c, d, extra_args=['--max-width', '42']))


@mock.patch('cliff.utils.terminal_width')
def test_table_formatter_max_width(tw):
    tw.return_value = 80
    c = ('field_name', 'a_really_long_field_name')
    d = ('the value', 'a value significantly longer than the field')
    expected = '''\
+--------------------------+---------------------------------------------+
| Field                    | Value                                       |
+--------------------------+---------------------------------------------+
| field_name               | the value                                   |
| a_really_long_field_name | a value significantly longer than the field |
+--------------------------+---------------------------------------------+
'''
    assert expected == _table_tester_helper(c, d)

    # resize value column
    tw.return_value = 70
    expected = '''\
+--------------------------+-----------------------------------------+
| Field                    | Value                                   |
+--------------------------+-----------------------------------------+
| field_name               | the value                               |
| a_really_long_field_name | a value significantly longer than the   |
|                          | field                                   |
+--------------------------+-----------------------------------------+
'''
    assert expected == _table_tester_helper(c, d)

    # resize both columns
    tw.return_value = 50
    expected = '''\
+-----------------------+------------------------+
| Field                 | Value                  |
+-----------------------+------------------------+
| field_name            | the value              |
| a_really_long_field_n | a value significantly  |
| ame                   | longer than the field  |
+-----------------------+------------------------+
'''
    assert expected == _table_tester_helper(c, d)

    # resize all columns limited by min_width=16
    tw.return_value = 10
    expected = '''\
+------------------+------------------+
| Field            | Value            |
+------------------+------------------+
| field_name       | the value        |
| a_really_long_fi | a value          |
| eld_name         | significantly    |
|                  | longer than the  |
|                  | field            |
+------------------+------------------+
'''
    assert expected == _table_tester_helper(c, d)


@mock.patch('cliff.utils.terminal_width')
def test_table_list_formatter(tw):
    tw.return_value = 80
    c = ('a', 'b', 'c')
    d1 = ('A', 'B', 'C')
    d2 = ('D', 'E', 'test\rcarriage\r\nreturn')
    data = [d1, d2]
    expected = '''\
+---+---+---------------+
| a | b | c             |
+---+---+---------------+
| A | B | C             |
| D | E | test carriage |
|   |   | return        |
+---+---+---------------+
'''
    assert expected == _table_tester_helper(c, data)


@mock.patch('cliff.utils.terminal_width')
def test_table_formatter_formattable_column(tw):
    tw.return_value = 0
    c = ('a', 'b', 'c', 'd')
    d = ('A', 'B', 'C', test_columns.FauxColumn(['the', 'value']))
    expected = '''\
+-------+---------------------------------------------+
| Field | Value                                       |
+-------+---------------------------------------------+
| a     | A                                           |
| b     | B                                           |
| c     | C                                           |
| d     | I made this string myself: ['the', 'value'] |
+-------+---------------------------------------------+
'''
    assert expected == _table_tester_helper(c, d)


_col_names = ('one', 'two', 'three')
_col_data = [(
    'one one one one one',
    'two two two two',
    'three three')]

_expected_mv = {
    80: '''\
+---------------------+-----------------+-------------+
| one                 | two             | three       |
+---------------------+-----------------+-------------+
| one one one one one | two two two two | three three |
+---------------------+-----------------+-------------+
''',

    50: '''\
+----------------+-----------------+-------------+
| one            | two             | three       |
+----------------+-----------------+-------------+
| one one one    | two two two two | three three |
| one one        |                 |             |
+----------------+-----------------+-------------+
''',

    47: '''\
+---------------+---------------+-------------+
| one           | two           | three       |
+---------------+---------------+-------------+
| one one one   | two two two   | three three |
| one one       | two           |             |
+---------------+---------------+-------------+
''',

    45: '''\
+--------------+--------------+-------------+
| one          | two          | three       |
+--------------+--------------+-------------+
| one one one  | two two two  | three three |
| one one      | two          |             |
+--------------+--------------+-------------+
''',

    40: '''\
+------------+------------+------------+
| one        | two        | three      |
+------------+------------+------------+
| one one    | two two    | three      |
| one one    | two two    | three      |
| one        |            |            |
+------------+------------+------------+
''',

    10: '''\
+----------+----------+----------+
| one      | two      | three    |
+----------+----------+----------+
| one one  | two two  | three    |
| one one  | two two  | three    |
| one      |          |          |
+----------+----------+----------+
''',
}


@mock.patch('cliff.utils.terminal_width')
def test_table_list_formatter_formattable_column(tw):
    tw.return_value = 80
    c = ('a', 'b', 'c')
    d1 = ('A', 'B', test_columns.FauxColumn(['the', 'value']))
    data = [d1]
    expected = '''\
+---+---+---------------------------------------------+
| a | b | c                                           |
+---+---+---------------------------------------------+
| A | B | I made this string myself: ['the', 'value'] |
+---+---+---------------------------------------------+
'''
    assert expected == _table_tester_helper(c, data)


@mock.patch('cliff.utils.terminal_width')
def test_table_list_formatter_max_width(tw):
    # no resize
    l = tw.return_value = 80
    assert _expected_mv[l] == _table_tester_helper(_col_names, _col_data)

    # resize 1 column
    l = tw.return_value = 50
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[l] == actual
    assert len(actual.splitlines()[0]) == l

    # resize 2 columns
    l = tw.return_value = 45
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[l] == actual
    assert len(actual.splitlines()[0]) == l

    # resize all columns
    l = tw.return_value = 40
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[l] == actual
    assert len(actual.splitlines()[0]) == l

    # resize all columns limited by min_width=8
    l = tw.return_value = 10
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[l] == actual
    # 3 columns each 8 wide, plus table spacing and borders
    expected_width = 11 * 3 + 1
    assert len(actual.splitlines()[0]) == expected_width


# Force a wide terminal by overriding its width with envvar
@mock.patch('cliff.utils.terminal_width')
@mock.patch.dict(os.environ, {'CLIFF_MAX_TERM_WIDTH': '666'})
def test_table_list_formatter_max_width_and_envvar_max(tw):
    # no resize
    tw.return_value = 80
    assert _expected_mv[80] == _table_tester_helper(_col_names, _col_data)

    # resize 1 column
    tw.return_value = 50
    assert _expected_mv[80] == _table_tester_helper(_col_names, _col_data)

    # resize 2 columns
    tw.return_value = 45
    assert _expected_mv[80] == _table_tester_helper(_col_names, _col_data)

    # resize all columns
    tw.return_value = 40
    assert _expected_mv[80] == _table_tester_helper(_col_names, _col_data)

    # resize all columns limited by min_width=8
    tw.return_value = 10
    assert _expected_mv[80] == _table_tester_helper(_col_names, _col_data)


# Force a narrow terminal by overriding its width with envvar
@mock.patch('cliff.utils.terminal_width')
@mock.patch.dict(os.environ, {'CLIFF_MAX_TERM_WIDTH': '47'})
def test_table_list_formatter_max_width_and_envvar_mid(tw):
    # no resize
    tw.return_value = 80
    assert _expected_mv[47] == _table_tester_helper(_col_names, _col_data)

    # resize 1 column
    tw.return_value = 50
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[47] == actual
    assert len(actual.splitlines()[0]) == 47

    # resize 2 columns
    tw.return_value = 45
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[47] == actual
    assert len(actual.splitlines()[0]) == 47

    # resize all columns
    tw.return_value = 40
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[47] == actual
    assert len(actual.splitlines()[0]) == 47

    # resize all columns limited by min_width=8
    tw.return_value = 10
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[47] == actual
    assert len(actual.splitlines()[0]) == 47


@mock.patch.dict(os.environ, {'CLIFF_MAX_TERM_WIDTH': '80'})
def test_table_list_formatter_env_maxwidth_noresize():
    # no resize
    assert _expected_mv[80] == _table_tester_helper(_col_names, _col_data)


@mock.patch.dict(os.environ, {'CLIFF_MAX_TERM_WIDTH': '50'})
def test_table_list_formatter_env_maxwidth_resize_one():
    # resize 1 column
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[50] == actual
    assert len(actual.splitlines()[0]) == 50


@mock.patch.dict(os.environ, {'CLIFF_MAX_TERM_WIDTH': '45'})
def test_table_list_formatter_env_maxwidth_resize_two():
    # resize 2 columns
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[45] == actual
    assert len(actual.splitlines()[0]) == 45


@mock.patch.dict(os.environ, {'CLIFF_MAX_TERM_WIDTH': '40'})
def test_table_list_formatter_env_maxwidth_resize_all():
    # resize all columns
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[40] == actual
    assert len(actual.splitlines()[0]) == 40


@mock.patch.dict(os.environ, {'CLIFF_MAX_TERM_WIDTH': '8'})
def test_table_list_formatter_env_maxwidth_resize_all_tiny():
    # resize all columns limited by min_width=8
    actual = _table_tester_helper(_col_names, _col_data)
    assert _expected_mv[10] == actual
    # 3 columns each 8 wide, plus table spacing and borders
    expected_width = 11 * 3 + 1
    assert len(actual.splitlines()[0]) == expected_width


@mock.patch.dict(os.environ, {'CLIFF_MAX_TERM_WIDTH': '42'})
def test_table_list_formatter_env_maxwidth_args_big():
    assert _expected_mv[80] == _table_tester_helper(_col_names, _col_data,
                                                    extra_args=args(666))


@mock.patch.dict(os.environ, {'CLIFF_MAX_TERM_WIDTH': '42'})
def test_table_list_formatter_env_maxwidth_args_tiny():
    assert _expected_mv[40] == _table_tester_helper(_col_names, _col_data,
                                                    extra_args=args(40))


@mock.patch('cliff.utils.terminal_width')
def test_table_list_formatter_empty(tw):
    tw.return_value = 80
    c = ('a', 'b', 'c')
    data = []
    expected = '\n'
    assert expected == _table_tester_helper(c, data)


@mock.patch('cliff.utils.terminal_width')
def test_table_list_formatter_empty_table(tw):
    tw.return_value = 80
    c = ('a', 'b', 'c')
    data = []
    expected = '''\
+---+---+---+
| a | b | c |
+---+---+---+
+---+---+---+
'''
    assert expected == _table_tester_helper(c, data,
                                            extra_args=['--print-empty'])


def test_field_widths():
    tf = table.TableFormatter
    assert {
        'a': 1,
        'b': 2,
        'c': 3,
        'd': 10
    } == tf._field_widths(
        ('a', 'b', 'c', 'd'),
        '+---+----+-----+------------+')


def test_field_widths_zero():
    tf = table.TableFormatter
    assert {
        'a': 0,
        'b': 0,
        'c': 0
    } == tf._field_widths(
        ('a', 'b', 'c'),
        '+--+-++')


def test_width_info():
    tf = table.TableFormatter
    assert (49, 4) == (tf._width_info(80, 10))
    assert (76, 76) == (tf._width_info(80, 1))
    assert (79, 0) == (tf._width_info(80, 0))
    assert (0, 0) == (tf._width_info(0, 80))