3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
17 from cliff import lister
22 class FauxFormatter(object):
26 self.obj = weakref.proxy(self)
28 def emit_list(self, columns, data, stdout, args):
29 self.args.append((columns, data))
32 class ExerciseLister(lister.Lister):
34 def _load_formatter_plugins(self):
36 'test': FauxFormatter(),
39 def take_action(self, parsed_args):
42 [('a', 'A'), ('b', 'B')],
46 def test_formatter_args():
48 test_lister = ExerciseLister(app, [])
50 parsed_args = mock.Mock()
51 parsed_args.columns = ('Col1', 'Col2')
52 parsed_args.formatter = 'test'
54 test_lister.run(parsed_args)
55 f = test_lister._formatter_plugins['test']
56 assert len(f.args) == 1
58 assert args[0] == list(parsed_args.columns)
60 assert data == [['a', 'A'], ['b', 'B']]
63 def test_no_exist_column():
64 test_lister = ExerciseLister(mock.Mock(), [])
65 parsed_args = mock.Mock()
66 parsed_args.columns = ('no_exist_column',)
67 parsed_args.formatter = 'test'
68 with mock.patch.object(test_lister, 'take_action') as mock_take_action:
69 mock_take_action.return_value = (('Col1', 'Col2', 'Col3'), [])
71 test_lister.run(parsed_args)
75 assert False, 'Should have had an exception'