hammer_cli 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/hammer +3 -1
- data/config/cli_config.template.yml +3 -0
- data/doc/creating_commands.md +1 -1
- data/doc/installation.md +3 -2
- data/doc/release_notes.md +17 -0
- data/doc/writing_a_plugin.md +1 -1
- data/lib/hammer_cli/abstract.rb +46 -9
- data/lib/hammer_cli/apipie/command.rb +10 -1
- data/lib/hammer_cli/apipie/option_builder.rb +29 -11
- data/lib/hammer_cli/apipie/option_definition.rb +18 -0
- data/lib/hammer_cli/apipie/resource.rb +4 -22
- data/lib/hammer_cli/exception_handler.rb +18 -6
- data/lib/hammer_cli/exceptions.rb +1 -0
- data/lib/hammer_cli/i18n.rb +4 -0
- data/lib/hammer_cli/main.rb +2 -0
- data/lib/hammer_cli/modules.rb +30 -2
- data/lib/hammer_cli/options/matcher.rb +48 -0
- data/lib/hammer_cli/output/adapter.rb +3 -1
- data/lib/hammer_cli/output/adapter/csv.rb +5 -2
- data/lib/hammer_cli/output/adapter/json.rb +17 -0
- data/lib/hammer_cli/output/adapter/table.rb +11 -17
- data/lib/hammer_cli/output/adapter/tree_structure.rb +74 -0
- data/lib/hammer_cli/output/adapter/wrapper_formatter.rb +20 -0
- data/lib/hammer_cli/output/adapter/yaml.rb +16 -0
- data/lib/hammer_cli/shell.rb +3 -8
- data/lib/hammer_cli/utils.rb +10 -0
- data/lib/hammer_cli/version.rb +1 -1
- data/locale/de/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/de/hammer-cli.po +296 -0
- data/locale/en/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/en/hammer-cli.po +1 -1
- data/locale/en_GB/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/en_GB/hammer-cli.po +133 -133
- data/locale/es/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/es/hammer-cli.po +148 -147
- data/locale/fr/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/fr/hammer-cli.po +142 -141
- data/locale/hammer-cli.pot +39 -32
- data/locale/it/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/it/hammer-cli.po +293 -0
- data/locale/ja/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/ja/hammer-cli.po +291 -0
- data/locale/ko/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/ko/hammer-cli.po +292 -0
- data/locale/pt_BR/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/pt_BR/hammer-cli.po +294 -0
- data/locale/ru/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/ru/hammer-cli.po +293 -0
- data/locale/zh_CN/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/zh_CN/hammer-cli.po +292 -0
- data/locale/zh_TW/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/zh_TW/hammer-cli.po +292 -0
- data/test/unit/apipie/command_test.rb +22 -0
- data/test/unit/apipie/option_builder_test.rb +21 -7
- data/test/unit/apipie/option_definition_test.rb +26 -0
- data/test/unit/fixtures/apipie/documented.json +26 -0
- data/test/unit/modules_test.rb +13 -2
- data/test/unit/options/matcher_test.rb +61 -0
- data/test/unit/output/adapter/json_test.rb +214 -0
- data/test/unit/output/adapter/table_test.rb +66 -0
- data/test/unit/output/adapter/yaml_test.rb +213 -0
- data/test/unit/utils_test.rb +17 -0
- metadata +249 -205
data/test/unit/modules_test.rb
CHANGED
@@ -28,7 +28,7 @@ describe HammerCLI::Modules do
|
|
28
28
|
|
29
29
|
describe "names" do
|
30
30
|
it "must return list of modules" do
|
31
|
-
HammerCLI::Modules.names.
|
31
|
+
HammerCLI::Modules.names.must_equal ["hammer_cli_jerry", "hammer_cli_tom"]
|
32
32
|
end
|
33
33
|
|
34
34
|
it "must return empty array by default" do
|
@@ -42,8 +42,9 @@ describe HammerCLI::Modules do
|
|
42
42
|
:tom => {},
|
43
43
|
:modules => ['hammer_cli_tom', 'hammer_cli_jerry'],
|
44
44
|
})
|
45
|
-
HammerCLI::Modules.names.
|
45
|
+
HammerCLI::Modules.names.must_equal ["hammer_cli_jerry", "hammer_cli_tom"]
|
46
46
|
end
|
47
|
+
|
47
48
|
end
|
48
49
|
|
49
50
|
describe "find by name" do
|
@@ -68,6 +69,16 @@ describe HammerCLI::Modules do
|
|
68
69
|
HammerCLI::Modules.load_all
|
69
70
|
end
|
70
71
|
|
72
|
+
it "must detect dependency on disabled module" do
|
73
|
+
HammerCLI::Settings.clear
|
74
|
+
HammerCLI::Settings.load({
|
75
|
+
:tom => { :enable_module => false },
|
76
|
+
:jerry => { :enable_module => true },
|
77
|
+
})
|
78
|
+
HammerCLI::Modules.stubs(:load).returns(nil)
|
79
|
+
HammerCLI::Modules.stubs(:loaded_modules).returns(['hammer_cli_jerry', 'hammer_cli_tom'])
|
80
|
+
proc { HammerCLI::Modules.load_all }.must_raise HammerCLI::ModuleDisabledButRequired
|
81
|
+
end
|
71
82
|
end
|
72
83
|
|
73
84
|
describe "load a module" do
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../test_helper')
|
2
|
+
|
3
|
+
|
4
|
+
describe HammerCLI::Options::Matcher do
|
5
|
+
|
6
|
+
let(:option_a1) { Clamp::Option::Definition.new("--opt_a1", "OPT_A", "some option") }
|
7
|
+
let(:option_a2) { Clamp::Option::Definition.new("--opt_a2", "OPT_A", "some option") }
|
8
|
+
let(:option_a3) { Clamp::Option::Definition.new("--opt_a3", "OPT_A", "some option") }
|
9
|
+
let(:option_b1) { Clamp::Option::Definition.new("--opt_b1", "OPT_B", "some option") }
|
10
|
+
|
11
|
+
it "tests value" do
|
12
|
+
matcher = HammerCLI::Options::Matcher.new(
|
13
|
+
:long_switch => '--opt_a1'
|
14
|
+
)
|
15
|
+
matcher.matches?(option_a1).must_equal true
|
16
|
+
matcher.matches?(option_a2).must_equal false
|
17
|
+
end
|
18
|
+
|
19
|
+
it "tests regex" do
|
20
|
+
matcher = HammerCLI::Options::Matcher.new(
|
21
|
+
:long_switch => /--opt_a.*/
|
22
|
+
)
|
23
|
+
matcher.matches?(option_a1).must_equal true
|
24
|
+
matcher.matches?(option_a2).must_equal true
|
25
|
+
matcher.matches?(option_b1).must_equal false
|
26
|
+
end
|
27
|
+
|
28
|
+
it "tests multiple conditions" do
|
29
|
+
matcher = HammerCLI::Options::Matcher.new(
|
30
|
+
:long_switch => /--opt_.1/,
|
31
|
+
:type => 'OPT_A'
|
32
|
+
)
|
33
|
+
matcher.matches?(option_a1).must_equal true
|
34
|
+
matcher.matches?(option_a2).must_equal false
|
35
|
+
matcher.matches?(option_a3).must_equal false
|
36
|
+
matcher.matches?(option_b1).must_equal false
|
37
|
+
end
|
38
|
+
|
39
|
+
it "tests multiple values or regexes" do
|
40
|
+
matcher = HammerCLI::Options::Matcher.new(
|
41
|
+
:long_switch => [/--opt_.1/, "--opt_a3"]
|
42
|
+
)
|
43
|
+
matcher.matches?(option_a1).must_equal true
|
44
|
+
matcher.matches?(option_a2).must_equal false
|
45
|
+
matcher.matches?(option_a3).must_equal true
|
46
|
+
matcher.matches?(option_b1).must_equal true
|
47
|
+
end
|
48
|
+
|
49
|
+
it "tests nil for unknown methods" do
|
50
|
+
matcher = HammerCLI::Options::Matcher.new(
|
51
|
+
:unknown_method => "some value"
|
52
|
+
)
|
53
|
+
matcher.matches?(option_a1).must_equal false
|
54
|
+
|
55
|
+
matcher = HammerCLI::Options::Matcher.new(
|
56
|
+
:unknown_method => nil
|
57
|
+
)
|
58
|
+
matcher.matches?(option_a1).must_equal true
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,214 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../../test_helper')
|
2
|
+
|
3
|
+
describe HammerCLI::Output::Adapter::Json do
|
4
|
+
|
5
|
+
let(:context) {{}}
|
6
|
+
let(:adapter) { HammerCLI::Output::Adapter::Json.new(context, HammerCLI::Output::Output.formatters) }
|
7
|
+
|
8
|
+
context "print_collection" do
|
9
|
+
|
10
|
+
let(:id) { Fields::Id.new(:path => [:id], :label => "Id") }
|
11
|
+
let(:name) { Fields::Field.new(:path => [:name], :label => "Name") }
|
12
|
+
let(:unlabeled) { Fields::Field.new(:path => [:name]) }
|
13
|
+
let(:surname) { Fields::Field.new(:path => [:surname], :label => "Surname") }
|
14
|
+
let(:address_city) { Fields::Field.new(:path => [:address, :city], :label => "City") }
|
15
|
+
let(:city) { Fields::Field.new(:path => [:city], :label => "City") }
|
16
|
+
let(:label_address) { Fields::Label.new(:path => [:address], :label => "Address") }
|
17
|
+
let(:num_contacts) { Fields::Collection.new(:path => [:contacts], :label => "Contacts") }
|
18
|
+
let(:num_one_contact) { Fields::Collection.new(:path => [:one_contact], :label => "Contacts") }
|
19
|
+
let(:contacts) { Fields::Collection.new(:path => [:contacts], :label => "Contacts", :numbered => false) }
|
20
|
+
let(:one_contact) { Fields::Collection.new(:path => [:one_contact], :label => "Contacts", :numbered => false) }
|
21
|
+
let(:desc) { Fields::Field.new(:path => [:desc], :label => "Description") }
|
22
|
+
let(:contact) { Fields::Field.new(:path => [:contact], :label => "Contact") }
|
23
|
+
let(:params) { Fields::KeyValueList.new(:path => [:params], :label => "Parameters") }
|
24
|
+
let(:params_collection) { Fields::Collection.new(:path => [:params], :label => "Parameters") }
|
25
|
+
let(:param) { Fields::KeyValue.new(:path => nil, :label => nil) }
|
26
|
+
let(:blank) { Fields::Field.new(:path => [:blank], :label => "Blank", :hide_blank => true) }
|
27
|
+
|
28
|
+
let(:data) { HammerCLI::Output::RecordCollection.new [{
|
29
|
+
:id => 112,
|
30
|
+
:name => "John",
|
31
|
+
:surname => "Doe",
|
32
|
+
:address => {
|
33
|
+
:city => "New York"
|
34
|
+
},
|
35
|
+
:contacts => [
|
36
|
+
{
|
37
|
+
:desc => 'personal email',
|
38
|
+
:contact => 'john.doe@doughnut.com'
|
39
|
+
},
|
40
|
+
{
|
41
|
+
:desc => 'telephone',
|
42
|
+
:contact => '123456789'
|
43
|
+
}
|
44
|
+
],
|
45
|
+
:one_contact => [
|
46
|
+
{
|
47
|
+
:desc => 'personal email',
|
48
|
+
:contact => 'john.doe@doughnut.com'
|
49
|
+
}
|
50
|
+
],
|
51
|
+
:params => [
|
52
|
+
{
|
53
|
+
:name => 'weight',
|
54
|
+
:value => '83'
|
55
|
+
},
|
56
|
+
{
|
57
|
+
:name => 'size',
|
58
|
+
:value => '32'
|
59
|
+
}
|
60
|
+
]
|
61
|
+
}]}
|
62
|
+
|
63
|
+
it "should print one field" do
|
64
|
+
fields = [name]
|
65
|
+
expected_output = JSON.pretty_generate([{ 'Name' => 'John' }]) + "\n"
|
66
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should field with nested data" do
|
70
|
+
fields = [address_city]
|
71
|
+
expected_output = JSON.pretty_generate([{ 'City' => 'New York' }]) + "\n"
|
72
|
+
|
73
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should print labeled fields" do
|
77
|
+
label_address.output_definition.append [city]
|
78
|
+
fields = [label_address]
|
79
|
+
hash = [{
|
80
|
+
'Address' => {
|
81
|
+
'City' => 'New York'
|
82
|
+
}
|
83
|
+
}]
|
84
|
+
expected_output = JSON.pretty_generate(hash) + "\n"
|
85
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should print collection" do
|
89
|
+
num_contacts.output_definition.append [desc, contact]
|
90
|
+
fields = [num_contacts]
|
91
|
+
hash = [{
|
92
|
+
'Contacts' => {
|
93
|
+
1 => {
|
94
|
+
'Description' => 'personal email',
|
95
|
+
'Contact' => 'john.doe@doughnut.com'
|
96
|
+
},
|
97
|
+
2 => {
|
98
|
+
'Description' => 'telephone',
|
99
|
+
'Contact' => '123456789'
|
100
|
+
}
|
101
|
+
}
|
102
|
+
}]
|
103
|
+
|
104
|
+
expected_output = JSON.pretty_generate(hash) + "\n"
|
105
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should print collection with one element" do
|
109
|
+
num_one_contact.output_definition.append [desc, contact]
|
110
|
+
fields = [num_one_contact]
|
111
|
+
hash = [{
|
112
|
+
'Contacts' => {
|
113
|
+
1 => {
|
114
|
+
'Description' => 'personal email',
|
115
|
+
'Contact' => 'john.doe@doughnut.com'
|
116
|
+
}
|
117
|
+
}
|
118
|
+
}]
|
119
|
+
|
120
|
+
expected_output = JSON.pretty_generate(hash) + "\n"
|
121
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should print unnumbered collection" do
|
125
|
+
contacts.output_definition.append [desc, contact]
|
126
|
+
fields = [contacts]
|
127
|
+
hash = [{
|
128
|
+
'Contacts' => [
|
129
|
+
{
|
130
|
+
'Description' => 'personal email',
|
131
|
+
'Contact' => 'john.doe@doughnut.com'
|
132
|
+
},
|
133
|
+
{
|
134
|
+
'Description' => 'telephone',
|
135
|
+
'Contact' => '123456789'
|
136
|
+
}]
|
137
|
+
}]
|
138
|
+
|
139
|
+
expected_output = JSON.pretty_generate(hash) + "\n"
|
140
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should print unnumbered collection with one element" do
|
144
|
+
one_contact.output_definition.append [desc, contact]
|
145
|
+
fields = [one_contact]
|
146
|
+
hash = [{
|
147
|
+
'Contacts' => [
|
148
|
+
{
|
149
|
+
'Description' => 'personal email',
|
150
|
+
'Contact' => 'john.doe@doughnut.com'
|
151
|
+
}]
|
152
|
+
}]
|
153
|
+
|
154
|
+
expected_output = JSON.pretty_generate(hash) + "\n"
|
155
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
it "hides ids by default" do
|
160
|
+
fields = [id, name]
|
161
|
+
hash = [{'Name' => 'John'}]
|
162
|
+
expected_output = JSON.pretty_generate(hash) + "\n"
|
163
|
+
|
164
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "skips blank values" do
|
168
|
+
fields = [name, blank]
|
169
|
+
hash = [{'Name' => 'John'}]
|
170
|
+
expected_output = JSON.pretty_generate(hash) + "\n"
|
171
|
+
|
172
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should print key -> value" do
|
176
|
+
params_collection.output_definition.append [param]
|
177
|
+
fields = [params_collection]
|
178
|
+
|
179
|
+
hash = [{
|
180
|
+
'Parameters' => {
|
181
|
+
1 => {
|
182
|
+
:name => 'weight',
|
183
|
+
:value => '83'
|
184
|
+
},
|
185
|
+
2 => {
|
186
|
+
:name => 'size',
|
187
|
+
:value => '32'
|
188
|
+
}
|
189
|
+
}
|
190
|
+
}]
|
191
|
+
expected_output = JSON.pretty_generate(hash) + "\n"
|
192
|
+
|
193
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
194
|
+
end
|
195
|
+
|
196
|
+
context "show ids" do
|
197
|
+
|
198
|
+
let(:context) { {:show_ids => true} }
|
199
|
+
|
200
|
+
it "shows ids if it's required in the context" do
|
201
|
+
fields = [id, name]
|
202
|
+
hash = [{
|
203
|
+
'Id' => 112,
|
204
|
+
'Name' => 'John'
|
205
|
+
}]
|
206
|
+
expected_output = JSON.pretty_generate(hash) + "\n"
|
207
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
@@ -23,6 +23,8 @@ describe HammerCLI::Output::Adapter::Table do
|
|
23
23
|
:long => "SomeVeryLongString"
|
24
24
|
}]}
|
25
25
|
|
26
|
+
let(:empty_data) { HammerCLI::Output::RecordCollection.new [] }
|
27
|
+
|
26
28
|
it "should print column name " do
|
27
29
|
proc { adapter.print_collection(fields, data) }.must_output(/.*NAME.*/, "")
|
28
30
|
end
|
@@ -42,11 +44,32 @@ describe HammerCLI::Output::Adapter::Table do
|
|
42
44
|
out.wont_match(/.*ID.*/)
|
43
45
|
end
|
44
46
|
|
47
|
+
it "should ommit column of type Id by default but no data" do
|
48
|
+
expected_output = [
|
49
|
+
"----",
|
50
|
+
"NAME",
|
51
|
+
"----",
|
52
|
+
""
|
53
|
+
].join("\n")
|
54
|
+
proc { adapter.print_collection(fields, empty_data) }.must_output(expected_output)
|
55
|
+
end
|
56
|
+
|
45
57
|
it "should print column of type Id when --show-ids is set" do
|
46
58
|
adapter = HammerCLI::Output::Adapter::Table.new( { :show_ids => true } )
|
47
59
|
out, err = capture_io { adapter.print_collection(fields, data) }
|
48
60
|
out.must_match(/.*ID.*/)
|
49
61
|
end
|
62
|
+
|
63
|
+
it "should print column of type ID when --show-ids is set but no data" do
|
64
|
+
expected_output = [
|
65
|
+
"-----|---",
|
66
|
+
"NAME | ID",
|
67
|
+
"-----|---",
|
68
|
+
"",
|
69
|
+
].join("\n")
|
70
|
+
adapter = HammerCLI::Output::Adapter::Table.new( { :show_ids => true } )
|
71
|
+
proc { adapter.print_collection(fields, empty_data) }.must_output(expected_output)
|
72
|
+
end
|
50
73
|
end
|
51
74
|
|
52
75
|
context "column width" do
|
@@ -83,6 +106,19 @@ describe HammerCLI::Output::Adapter::Table do
|
|
83
106
|
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
84
107
|
end
|
85
108
|
|
109
|
+
it "sets width to the longest column name when no data" do
|
110
|
+
first_field = Fields::Field.new(:path => [:long], :label => "VeryLongTableHeaderName")
|
111
|
+
fields = [first_field, field_lastname]
|
112
|
+
|
113
|
+
expected_output = [
|
114
|
+
"------------------------|---------",
|
115
|
+
"VERYLONGTABLEHEADERNAME | LASTNAME",
|
116
|
+
"------------------------|---------",
|
117
|
+
""
|
118
|
+
].join("\n")
|
119
|
+
proc { adapter.print_collection(fields, empty_data) }.must_output(expected_output)
|
120
|
+
end
|
121
|
+
|
86
122
|
it "sets certain width" do
|
87
123
|
first_field = Fields::Field.new(:path => [:long], :label => "Long", :width => 25)
|
88
124
|
fields = [first_field, field_lastname]
|
@@ -99,6 +135,21 @@ describe HammerCLI::Output::Adapter::Table do
|
|
99
135
|
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
100
136
|
end
|
101
137
|
|
138
|
+
it "sets certain width when no data" do
|
139
|
+
first_field = Fields::Field.new(:path => [:long], :label => "Long", :width => 25)
|
140
|
+
fields = [first_field, field_lastname]
|
141
|
+
|
142
|
+
expected_output = [
|
143
|
+
"--------------------------|---------",
|
144
|
+
"LONG | LASTNAME",
|
145
|
+
"--------------------------|---------",
|
146
|
+
""
|
147
|
+
].join("\n")
|
148
|
+
|
149
|
+
proc { adapter.print_collection(fields, empty_data) }.must_output(expected_output)
|
150
|
+
end
|
151
|
+
|
152
|
+
|
102
153
|
it "gives preference to width over maximal width" do
|
103
154
|
first_field = Fields::Field.new(:path => [:long], :label => "Long", :width => 25, :max_width => 10)
|
104
155
|
fields = [first_field, field_lastname]
|
@@ -115,6 +166,21 @@ describe HammerCLI::Output::Adapter::Table do
|
|
115
166
|
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
116
167
|
end
|
117
168
|
|
169
|
+
it "gives preference to width over maximal width when no data" do
|
170
|
+
first_field = Fields::Field.new(:path => [:long], :label => "Long", :width => 25, :max_width => 10)
|
171
|
+
fields = [first_field, field_lastname]
|
172
|
+
|
173
|
+
expected_output = [
|
174
|
+
"--------------------------|---------",
|
175
|
+
"LONG | LASTNAME",
|
176
|
+
"--------------------------|---------",
|
177
|
+
""
|
178
|
+
].join("\n")
|
179
|
+
|
180
|
+
proc { adapter.print_collection(fields, empty_data) }.must_output(expected_output)
|
181
|
+
end
|
182
|
+
|
183
|
+
|
118
184
|
end
|
119
185
|
|
120
186
|
context "formatters" do
|
@@ -0,0 +1,213 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../../test_helper')
|
2
|
+
require 'yaml'
|
3
|
+
describe HammerCLI::Output::Adapter::Yaml do
|
4
|
+
|
5
|
+
let(:context) {{}}
|
6
|
+
let(:adapter) { HammerCLI::Output::Adapter::Yaml.new(context, HammerCLI::Output::Output.formatters) }
|
7
|
+
|
8
|
+
context "print_collection" do
|
9
|
+
|
10
|
+
let(:id) { Fields::Id.new(:path => [:id], :label => "Id") }
|
11
|
+
let(:name) { Fields::Field.new(:path => [:name], :label => "Name") }
|
12
|
+
let(:unlabeled) { Fields::Field.new(:path => [:name]) }
|
13
|
+
let(:surname) { Fields::Field.new(:path => [:surname], :label => "Surname") }
|
14
|
+
let(:address_city) { Fields::Field.new(:path => [:address, :city], :label => "City") }
|
15
|
+
let(:city) { Fields::Field.new(:path => [:city], :label => "City") }
|
16
|
+
let(:label_address) { Fields::Label.new(:path => [:address], :label => "Address") }
|
17
|
+
let(:num_contacts) { Fields::Collection.new(:path => [:contacts], :label => "Contacts") }
|
18
|
+
let(:num_one_contact) { Fields::Collection.new(:path => [:one_contact], :label => "Contacts") }
|
19
|
+
let(:contacts) { Fields::Collection.new(:path => [:contacts], :label => "Contacts", :numbered => false) }
|
20
|
+
let(:one_contact) { Fields::Collection.new(:path => [:one_contact], :label => "Contacts", :numbered => false) }
|
21
|
+
let(:desc) { Fields::Field.new(:path => [:desc], :label => "Description") }
|
22
|
+
let(:contact) { Fields::Field.new(:path => [:contact], :label => "Contact") }
|
23
|
+
let(:params) { Fields::KeyValueList.new(:path => [:params], :label => "Parameters") }
|
24
|
+
let(:params_collection) { Fields::Collection.new(:path => [:params], :label => "Parameters") }
|
25
|
+
let(:param) { Fields::KeyValue.new(:path => nil, :label => nil) }
|
26
|
+
let(:blank) { Fields::Field.new(:path => [:blank], :label => "Blank", :hide_blank => true) }
|
27
|
+
|
28
|
+
let(:data) { HammerCLI::Output::RecordCollection.new [{
|
29
|
+
:id => 112,
|
30
|
+
:name => "John",
|
31
|
+
:surname => "Doe",
|
32
|
+
:address => {
|
33
|
+
:city => "New York"
|
34
|
+
},
|
35
|
+
:contacts => [
|
36
|
+
{
|
37
|
+
:desc => 'personal email',
|
38
|
+
:contact => 'john.doe@doughnut.com'
|
39
|
+
},
|
40
|
+
{
|
41
|
+
:desc => 'telephone',
|
42
|
+
:contact => '123456789'
|
43
|
+
}
|
44
|
+
],
|
45
|
+
:one_contact => [
|
46
|
+
{
|
47
|
+
:desc => 'personal email',
|
48
|
+
:contact => 'john.doe@doughnut.com'
|
49
|
+
}
|
50
|
+
],
|
51
|
+
:params => [
|
52
|
+
{
|
53
|
+
:name => 'weight',
|
54
|
+
:value => '83'
|
55
|
+
},
|
56
|
+
{
|
57
|
+
:name => 'size',
|
58
|
+
:value => '32'
|
59
|
+
}
|
60
|
+
]
|
61
|
+
}]}
|
62
|
+
|
63
|
+
it "should print one field" do
|
64
|
+
fields = [name]
|
65
|
+
expected_output = YAML.dump([{ 'Name' => 'John' }])
|
66
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should field with nested data" do
|
70
|
+
fields = [address_city]
|
71
|
+
expected_output = YAML.dump([{ 'City' => 'New York' }])
|
72
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should print labeled fields" do
|
76
|
+
label_address.output_definition.append [city]
|
77
|
+
fields = [label_address]
|
78
|
+
hash = [{
|
79
|
+
'Address' => {
|
80
|
+
'City' => 'New York'
|
81
|
+
}
|
82
|
+
}]
|
83
|
+
expected_output = YAML.dump(hash)
|
84
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should print collection" do
|
88
|
+
num_contacts.output_definition.append [desc, contact]
|
89
|
+
fields = [num_contacts]
|
90
|
+
hash = [{
|
91
|
+
'Contacts' => {
|
92
|
+
1 => {
|
93
|
+
'Description' => 'personal email',
|
94
|
+
'Contact' => 'john.doe@doughnut.com'
|
95
|
+
},
|
96
|
+
2 => {
|
97
|
+
'Description' => 'telephone',
|
98
|
+
'Contact' => '123456789'
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}]
|
102
|
+
|
103
|
+
expected_output = YAML.dump(hash)
|
104
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should print collection with one element" do
|
108
|
+
num_one_contact.output_definition.append [desc, contact]
|
109
|
+
fields = [num_one_contact]
|
110
|
+
hash = [{
|
111
|
+
'Contacts' => {
|
112
|
+
1 => {
|
113
|
+
'Description' => 'personal email',
|
114
|
+
'Contact' => 'john.doe@doughnut.com'
|
115
|
+
}
|
116
|
+
}
|
117
|
+
}]
|
118
|
+
|
119
|
+
expected_output = YAML.dump(hash)
|
120
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should print unnumbered collection" do
|
124
|
+
contacts.output_definition.append [desc, contact]
|
125
|
+
fields = [contacts]
|
126
|
+
hash = [{
|
127
|
+
'Contacts' => [
|
128
|
+
{
|
129
|
+
'Description' => 'personal email',
|
130
|
+
'Contact' => 'john.doe@doughnut.com'
|
131
|
+
},
|
132
|
+
{
|
133
|
+
'Description' => 'telephone',
|
134
|
+
'Contact' => '123456789'
|
135
|
+
}]
|
136
|
+
}]
|
137
|
+
|
138
|
+
expected_output = YAML.dump(hash)
|
139
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should print unnumbered collection with one element" do
|
143
|
+
one_contact.output_definition.append [desc, contact]
|
144
|
+
fields = [one_contact]
|
145
|
+
hash = [{
|
146
|
+
'Contacts' => [
|
147
|
+
{
|
148
|
+
'Description' => 'personal email',
|
149
|
+
'Contact' => 'john.doe@doughnut.com'
|
150
|
+
}]
|
151
|
+
}]
|
152
|
+
|
153
|
+
expected_output = YAML.dump(hash)
|
154
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
it "hides ids by default" do
|
159
|
+
fields = [id, name]
|
160
|
+
hash = [{'Name' => 'John'}]
|
161
|
+
expected_output = YAML.dump(hash)
|
162
|
+
|
163
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
164
|
+
end
|
165
|
+
|
166
|
+
it "skips blank values" do
|
167
|
+
fields = [name, blank]
|
168
|
+
hash = [{'Name' => 'John'}]
|
169
|
+
expected_output = YAML.dump(hash)
|
170
|
+
|
171
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should print key -> value" do
|
175
|
+
params_collection.output_definition.append [param]
|
176
|
+
fields = [params_collection]
|
177
|
+
|
178
|
+
hash = [{
|
179
|
+
'Parameters' => {
|
180
|
+
1 => {
|
181
|
+
:name => 'weight',
|
182
|
+
:value => '83'
|
183
|
+
},
|
184
|
+
2 => {
|
185
|
+
:name => 'size',
|
186
|
+
:value => '32'
|
187
|
+
}
|
188
|
+
}
|
189
|
+
}]
|
190
|
+
expected_output = YAML.dump(hash)
|
191
|
+
|
192
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
193
|
+
end
|
194
|
+
|
195
|
+
context "show ids" do
|
196
|
+
|
197
|
+
let(:context) { {:show_ids => true} }
|
198
|
+
|
199
|
+
it "shows ids if it's required in the context" do
|
200
|
+
fields = [id, name]
|
201
|
+
hash = [{
|
202
|
+
'Id' => 112,
|
203
|
+
'Name' => 'John'
|
204
|
+
}]
|
205
|
+
expected_output = YAML.dump(hash)
|
206
|
+
proc { adapter.print_collection(fields, data) }.must_output(expected_output)
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|