11import pytest
22
33from code42cli import PRODUCT_NAME
4- from code42cli .util import does_user_agree , find_format_width
4+ from code42cli .util import (
5+ does_user_agree ,
6+ find_format_width ,
7+ format_string_list_to_columns ,
8+ _PADDING_SIZE ,
9+ )
510
611TEST_HEADER = {u"key1" : u"Column 1" , u"key2" : u"Column 10" , u"key3" : u"Column 100" }
712
@@ -22,9 +27,20 @@ def context_without_assume_yes(mocker, cli_state):
2227 return mocker .patch ("code42cli.util.get_current_context" , return_value = ctx )
2328
2429
30+ @pytest .fixture
31+ def echo_output (mocker ):
32+ return mocker .patch ("code42cli.util.echo" )
33+
34+
2535_NAMESPACE = "{}.util" .format (PRODUCT_NAME )
2636
2737
38+ def get_expected_row_width (max_col_len , max_width ):
39+ col_size = max_col_len + _PADDING_SIZE
40+ num_cols = int (max_width / col_size ) or 1
41+ return col_size * num_cols
42+
43+
2844def test_does_user_agree_when_user_says_y_returns_true (mocker , context_without_assume_yes ):
2945 mocker .patch ("builtins.input" , return_value = "y" )
3046 assert does_user_agree ("Test Prompt" )
@@ -76,3 +92,54 @@ def test_find_format_width_filters_keys_not_present_in_header():
7692 result , _ = find_format_width (report , header_with_subset_keys )
7793 for item in result :
7894 assert u"key2" not in item .keys ()
95+
96+
97+ def test_format_string_list_to_columns_when_given_no_string_list_does_not_echo (echo_output ):
98+ format_string_list_to_columns ([], None )
99+ format_string_list_to_columns (None , None )
100+ assert not echo_output .call_count
101+
102+
103+ def test_format_string_list_to_columns_when_not_given_max_uses_shell_size (mocker , echo_output ):
104+ terminal_size = mocker .patch ("code42cli.util.shutil.get_terminal_size" )
105+ max_width = 30
106+ terminal_size .return_value = (max_width , None ) # Cols, Rows
107+
108+ columns = ["col1" , "col2" ]
109+ format_string_list_to_columns (columns )
110+
111+ printed_row = echo_output .call_args_list [0 ][0 ][0 ]
112+ assert len (printed_row ) == get_expected_row_width (4 , max_width )
113+ assert printed_row == "col1 col2 "
114+
115+
116+ def test_format_string_list_to_columns_when_given_small_max_width_prints_one_column_per_row (echo_output ):
117+ max_width = 5
118+
119+ columns = ["col1" , "col2" ]
120+ format_string_list_to_columns (columns , max_width )
121+
122+ expected_row_width = get_expected_row_width (4 , max_width )
123+ printed_row = echo_output .call_args_list [0 ][0 ][0 ]
124+ assert len (printed_row ) == expected_row_width
125+ assert printed_row == "col1 "
126+
127+ printed_row = echo_output .call_args_list [1 ][0 ][0 ]
128+ assert len (printed_row ) == expected_row_width
129+ assert printed_row == "col2 "
130+
131+
132+ def test_format_string_list_to_columns_uses_width_of_longest_string (echo_output ):
133+ max_width = 5
134+
135+ columns = ["col1" , "col2_that_is_really_long" ]
136+ format_string_list_to_columns (columns , max_width )
137+
138+ expected_row_width = get_expected_row_width (len ("col2_that_is_really_long" ), max_width )
139+ printed_row = echo_output .call_args_list [0 ][0 ][0 ]
140+ assert len (printed_row ) == expected_row_width
141+ assert printed_row == "col1 "
142+
143+ printed_row = echo_output .call_args_list [1 ][0 ][0 ]
144+ assert len (printed_row ) == expected_row_width
145+ assert printed_row == "col2_that_is_really_long "
0 commit comments