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
18 from cliff.formatters import json_format
19 from cliff.tests import test_columns
24 def test_json_format_one():
25 sf = json_format.JSONFormatter()
26 c = ('a', 'b', 'c', 'd')
27 d = ('A', 'B', 'C', '"escape me"')
35 sf.add_argument_group(args)
38 output = six.StringIO()
39 sf.emit_one(c, d, output, args)
40 value = output.getvalue()
41 print(len(value.splitlines()))
42 assert 1 == len(value.splitlines())
43 actual = json.loads(value)
44 assert expected == actual
47 output = six.StringIO()
48 sf.emit_one(c, d, output, args)
49 value = output.getvalue()
50 assert 6 == len(value.splitlines())
51 actual = json.loads(value)
52 assert expected == actual
55 def test_json_format_formattablecolumn_one():
56 sf = json_format.JSONFormatter()
57 c = ('a', 'b', 'c', 'd')
58 d = ('A', 'B', 'C', test_columns.FauxColumn(['the', 'value']))
63 'd': ['the', 'value'],
66 sf.add_argument_group(args)
69 output = six.StringIO()
70 sf.emit_one(c, d, output, args)
71 value = output.getvalue()
72 print(len(value.splitlines()))
73 assert 1 == len(value.splitlines())
74 actual = json.loads(value)
75 assert expected == actual
78 def test_json_format_list():
79 sf = json_format.JSONFormatter()
87 {'a': 'A1', 'b': 'B1', 'c': 'C1'},
88 {'a': 'A2', 'b': 'B2', 'c': 'C2'},
89 {'a': 'A3', 'b': 'B3', 'c': 'C3'}
92 sf.add_argument_group(args)
95 output = six.StringIO()
96 sf.emit_list(c, d, output, args)
97 value = output.getvalue()
98 assert 1 == len(value.splitlines())
99 actual = json.loads(value)
100 assert expected == actual
102 args.noindent = False
103 output = six.StringIO()
104 sf.emit_list(c, d, output, args)
105 value = output.getvalue()
106 assert 17 == len(value.splitlines())
107 actual = json.loads(value)
108 assert expected == actual
111 def test_json_format_formattablecolumn_list():
112 sf = json_format.JSONFormatter()
115 ('A1', 'B1', test_columns.FauxColumn(['the', 'value'])),
118 {'a': 'A1', 'b': 'B1', 'c': ['the', 'value']},
121 sf.add_argument_group(args)
124 output = six.StringIO()
125 sf.emit_list(c, d, output, args)
126 value = output.getvalue()
127 assert 1 == len(value.splitlines())
128 actual = json.loads(value)
129 assert expected == actual