def test_command_formatter(self): formatter = CommandFormatter() for prettify in [True, False]: formatter.prettify = prettify self.assertIsNone(formatter.format(None)) self.assertIsNone(formatter.format('')) self.assertEqual(formatter.format('Some text'), 'Some text') if not prettify: self.assertEqual(formatter.format(formatter), DictSerializer.dump(formatter, typed=False)) table_headers = ['First', 'Second', 'Third'] table_values = [ ['value 1', 'value 2', 'value 3'], ['value 1', 'value 2', 'value 3'], ['value 1', 'value 2', 'value 3'], ] tabular_result = CommandResult.to_tabular(table_headers, table_values) tabular_repr = formatter.format(tabular_result) tabular_data_repr = formatter.format(tabular_result.from_tabular()) self.assertIsNotNone(tabular_repr) self.assertIsNotNone(tabular_data_repr) self.assertNotEqual(tabular_data_repr, tabular_repr)
def __init__(self, client=None, roots=None, formatters=None, main_parser=None, main_parser_options=None): self.client = client self.roots = roots or CommandStorage.roots self.parser = None self.shared_parser = None self.subparsers = None self.formatters = [] self.main_parser = main_parser if main_parser_options: self.main_options = set({ v.get('dest') or k for k, v in list(main_parser_options.items()) }) else: self.main_options = set() if self.client: self.register_client(self.client) if formatters: for formatter in formatters: self.add_formatter(formatter) else: self.add_formatter(CommandJSONFormatter()) self.add_formatter(CommandFormatter(prettify=False)) # default
def test_command_formatter_table_format(self): formatter = CommandFormatter() table_headers = ['First', 'Second', 'Third'] table_values = [ ['value 1', 'value 2', 'value 3'], ['value 1', 'value 2', 'value 3'], ['value 1', 'value 2', 'value 3'], ] tabular_result = CommandResult.to_tabular(table_headers, table_values) tabular_repr = formatter.format(tabular_result) tabular_data_repr = formatter.format(tabular_result.from_tabular()) self.assertIsNotNone(tabular_repr) self.assertIsNotNone(tabular_data_repr) self.assertNotEqual(tabular_data_repr, tabular_repr)
def test_namespace(self): ns_dict = dict() fmt = CommandFormatter() fmt_json = CommandJSONFormatter() self.assertFalse(fmt.supports(ns_dict)) self.assertFalse(fmt_json.supports(ns_dict)) ns_dict = dict(json=False) self.assertFalse(fmt.supports(ns_dict)) self.assertFalse(fmt_json.supports(ns_dict)) ns_dict = dict(json=True) self.assertFalse(fmt.supports(ns_dict)) self.assertTrue(fmt_json.supports(ns_dict)) fmt_json.clear_argument(ns_dict) self.assertNotIn('json', ns_dict)
def test_command_formatter_pretify(self): formatter = CommandFormatter(prettify=True) pretty_dict_msg = \ 'None: null\n' \ 'bool: true\n' \ 'float: 1.234\n' \ 'int: 5\n' \ 'string: is coding worth it?\n' self.assertIsNone(formatter.format(None)) self.assertIsNone(formatter.format('')) self.assertEqual(self.text, formatter.format(self.text)) self.assertEqual(pretty_dict_msg, formatter.format(self.dictionary))