2 # -*- coding: utf-8 -*-
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
20 from cliff.formatters import commaseparated
21 from cliff.tests import test_columns
24 def test_commaseparated_list_formatter():
25 sf = commaseparated.CSVLister()
30 expected = 'a,b,c\nA,B,C\nD,E,F\n'
31 output = six.StringIO()
32 parsed_args = mock.Mock()
33 parsed_args.quote_mode = 'none'
34 sf.emit_list(c, data, output, parsed_args)
35 actual = output.getvalue()
36 assert expected == actual
39 def test_commaseparated_list_formatter_quoted():
40 sf = commaseparated.CSVLister()
45 expected = '"a","b","c"\n"A","B","C"\n"D","E","F"\n'
46 output = six.StringIO()
47 # Parse arguments as if passed on the command-line
48 parser = argparse.ArgumentParser(description='Testing...')
49 sf.add_argument_group(parser)
50 parsed_args = parser.parse_args(['--quote', 'all'])
51 sf.emit_list(c, data, output, parsed_args)
52 actual = output.getvalue()
53 assert expected == actual
56 def test_commaseparated_list_formatter_formattable_column():
57 sf = commaseparated.CSVLister()
59 d1 = ('A', 'B', test_columns.FauxColumn(['the', 'value']))
61 expected = 'a,b,c\nA,B,[\'the\'\\, \'value\']\n'
62 output = six.StringIO()
63 parsed_args = mock.Mock()
64 parsed_args.quote_mode = 'none'
65 sf.emit_list(c, data, output, parsed_args)
66 actual = output.getvalue()
67 assert expected == actual
70 def test_commaseparated_list_formatter_unicode():
71 sf = commaseparated.CSVLister()
72 c = (u'a', u'b', u'c')
73 d1 = (u'A', u'B', u'C')
75 d2 = (u'D', u'E', happy)
77 expected = u'a,b,c\nA,B,C\nD,E,%s\n' % happy
78 output = six.StringIO()
79 parsed_args = mock.Mock()
80 parsed_args.quote_mode = 'none'
81 sf.emit_list(c, data, output, parsed_args)
82 actual = output.getvalue()
84 actual = actual.decode('utf-8')
85 assert expected == actual