hammer_cli 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -2
  3. data/bin/hammer +6 -0
  4. data/config/cli_config.template.yml +3 -0
  5. data/doc/creating_commands.md +19 -0
  6. data/doc/i18n.md +15 -0
  7. data/doc/installation.md +1 -1
  8. data/doc/release_notes.md +61 -0
  9. data/lib/hammer_cli/abstract.rb +10 -37
  10. data/lib/hammer_cli/apipie/command.rb +10 -6
  11. data/lib/hammer_cli/apipie/option_builder.rb +26 -13
  12. data/lib/hammer_cli/apipie/options.rb +12 -10
  13. data/lib/hammer_cli/completer.rb +2 -2
  14. data/lib/hammer_cli/exception_handler.rb +5 -4
  15. data/lib/hammer_cli/exceptions.rb +1 -0
  16. data/lib/hammer_cli/i18n.rb +41 -0
  17. data/lib/hammer_cli/main.rb +2 -2
  18. data/lib/hammer_cli/options/normalizers.rb +31 -0
  19. data/lib/hammer_cli/output/adapter/abstract.rb +5 -1
  20. data/lib/hammer_cli/output/adapter/csv.rb +140 -12
  21. data/lib/hammer_cli/shell.rb +6 -6
  22. data/lib/hammer_cli/subcommand.rb +82 -0
  23. data/lib/hammer_cli/utils.rb +13 -0
  24. data/lib/hammer_cli/version.rb +1 -1
  25. data/locale/Makefile +11 -10
  26. data/locale/README.md +18 -0
  27. data/locale/en/LC_MESSAGES/hammer-cli.mo +0 -0
  28. data/locale/en/hammer-cli.po +227 -0
  29. data/locale/en_GB/LC_MESSAGES/hammer-cli.mo +0 -0
  30. data/locale/en_GB/hammer-cli.po +274 -0
  31. data/locale/es/LC_MESSAGES/hammer-cli.mo +0 -0
  32. data/locale/es/hammer-cli.po +275 -0
  33. data/locale/fr/LC_MESSAGES/hammer-cli.mo +0 -0
  34. data/locale/fr/hammer-cli.po +276 -0
  35. data/locale/hammer-cli.pot +158 -93
  36. data/test/reports/TEST-Fields-ContainerField-display-.xml +7 -0
  37. data/test/reports/TEST-Fields-ContainerField-display-blank-is-allowed.xml +15 -0
  38. data/test/reports/TEST-Fields-ContainerField-display-blank-is-not-allowed.xml +15 -0
  39. data/test/reports/TEST-Fields-ContainerField.xml +7 -0
  40. data/test/reports/TEST-Fields-Field-display-.xml +7 -0
  41. data/test/reports/TEST-Fields-Field-display-blank-is-allowed.xml +11 -0
  42. data/test/reports/TEST-Fields-Field-display-blank-is-not-allowed.xml +11 -0
  43. data/test/reports/TEST-Fields-Field-hide-blank-.xml +11 -0
  44. data/test/reports/TEST-Fields-Field-parameters.xml +9 -0
  45. data/test/reports/TEST-Fields-Field.xml +13 -0
  46. data/test/reports/TEST-HammerCLI-AbstractCommand-build-options.xml +15 -0
  47. data/test/reports/TEST-HammerCLI-AbstractCommand-exception-handler.xml +13 -0
  48. data/test/reports/TEST-HammerCLI-AbstractCommand-logging.xml +21 -0
  49. data/test/reports/TEST-HammerCLI-AbstractCommand-option-builder.xml +11 -0
  50. data/test/reports/TEST-HammerCLI-AbstractCommand-options.xml +11 -0
  51. data/test/reports/TEST-HammerCLI-AbstractCommand-output.xml +19 -0
  52. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-remove-subcommand.xml +11 -0
  53. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-subcommand-.xml +13 -0
  54. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-subcommand.xml +11 -0
  55. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior.xml +7 -0
  56. data/test/reports/TEST-HammerCLI-AbstractCommand.xml +11 -0
  57. data/test/reports/TEST-HammerCLI-Apipie-Command-options.xml +11 -0
  58. data/test/reports/TEST-HammerCLI-Apipie-Command-resource-defined.xml +9 -0
  59. data/test/reports/TEST-HammerCLI-Apipie-Command-setting-resources.xml +19 -0
  60. data/test/reports/TEST-HammerCLI-Apipie-Command.xml +9 -0
  61. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-aliasing-resources.xml +13 -0
  62. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-filtering-options.xml +15 -0
  63. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-required-options.xml +11 -0
  64. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-setting-correct-normalizers.xml +9 -0
  65. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-with-hash-params.xml +11 -0
  66. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-with-one-simple-param.xml +15 -0
  67. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder.xml +7 -0
  68. data/test/reports/TEST-HammerCLI-Completer-command-completion.xml +29 -0
  69. data/test/reports/TEST-HammerCLI-Completer-option-value-completion.xml +17 -0
  70. data/test/reports/TEST-HammerCLI-Completer-subcommand-completion.xml +19 -0
  71. data/test/reports/TEST-HammerCLI-Completer.xml +7 -0
  72. data/test/reports/TEST-HammerCLI-CompleterLine-line-complete.xml +25 -0
  73. data/test/reports/TEST-HammerCLI-CompleterLine-splitting-words.xml +29 -0
  74. data/test/reports/TEST-HammerCLI-CompleterLine.xml +7 -0
  75. data/test/reports/TEST-HammerCLI-CompleterWord-complete-.xml +23 -0
  76. data/test/reports/TEST-HammerCLI-CompleterWord-quote.xml +15 -0
  77. data/test/reports/TEST-HammerCLI-CompleterWord-quoted-.xml +13 -0
  78. data/test/reports/TEST-HammerCLI-CompleterWord.xml +7 -0
  79. data/test/reports/TEST-HammerCLI-Connection.xml +21 -0
  80. data/test/reports/TEST-HammerCLI-ExceptionHandler.xml +21 -0
  81. data/test/reports/TEST-HammerCLI-I18n.xml +11 -0
  82. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-password.xml +11 -0
  83. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-username.xml +11 -0
  84. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-verbose.xml +9 -0
  85. data/test/reports/TEST-HammerCLI-MainCommand-loading-context.xml +7 -0
  86. data/test/reports/TEST-HammerCLI-MainCommand.xml +7 -0
  87. data/test/reports/TEST-HammerCLI-Modules-find-by-name.xml +13 -0
  88. data/test/reports/TEST-HammerCLI-Modules-load-a-module-module-not-found.xml +13 -0
  89. data/test/reports/TEST-HammerCLI-Modules-load-a-module-module-runtime-exception.xml +13 -0
  90. data/test/reports/TEST-HammerCLI-Modules-load-a-module-success.xml +15 -0
  91. data/test/reports/TEST-HammerCLI-Modules-load-a-module.xml +7 -0
  92. data/test/reports/TEST-HammerCLI-Modules-load-all-modules.xml +9 -0
  93. data/test/reports/TEST-HammerCLI-Modules-names.xml +13 -0
  94. data/test/reports/TEST-HammerCLI-Modules.xml +7 -0
  95. data/test/reports/TEST-HammerCLI-OptionBuilderContainer.0.xml +7 -0
  96. data/test/reports/TEST-HammerCLI-OptionBuilderContainer.xml +11 -0
  97. data/test/reports/TEST-HammerCLI-Options-Normalizers-abstract.xml +9 -0
  98. data/test/reports/TEST-HammerCLI-Options-Normalizers-bool.xml +31 -0
  99. data/test/reports/TEST-HammerCLI-Options-Normalizers-datetime.xml +17 -0
  100. data/test/reports/TEST-HammerCLI-Options-Normalizers-enum.xml +15 -0
  101. data/test/reports/TEST-HammerCLI-Options-Normalizers-enumlist.xml +21 -0
  102. data/test/reports/TEST-HammerCLI-Options-Normalizers-json-input.xml +15 -0
  103. data/test/reports/TEST-HammerCLI-Options-Normalizers-key-value-list.xml +17 -0
  104. data/test/reports/TEST-HammerCLI-Options-Normalizers-list.xml +15 -0
  105. data/test/reports/TEST-HammerCLI-Options-Normalizers.xml +7 -0
  106. data/test/reports/TEST-HammerCLI-Options-OptionDefinition-context.xml +9 -0
  107. data/test/reports/TEST-HammerCLI-Options-OptionDefinition-formatters.xml +11 -0
  108. data/test/reports/TEST-HammerCLI-Options-OptionDefinition.xml +7 -0
  109. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-error-messages.xml +15 -0
  110. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-messages.xml +11 -0
  111. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-test-data-for-field.xml +15 -0
  112. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract.xml +17 -0
  113. data/test/reports/TEST-HammerCLI-Output-Adapter-Base-print-collection-show-ids.xml +9 -0
  114. data/test/reports/TEST-HammerCLI-Output-Adapter-Base-print-collection.xml +27 -0
  115. data/test/reports/TEST-HammerCLI-Output-Adapter-Base.xml +7 -0
  116. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-formatters.xml +11 -0
  117. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-fields-with-collections.xml +13 -0
  118. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-fields-with-containers.xml +11 -0
  119. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-ids.xml +11 -0
  120. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection.xml +11 -0
  121. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-message.xml +11 -0
  122. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues.xml +7 -0
  123. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-column-width.xml +15 -0
  124. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-formatters.xml +11 -0
  125. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-handle-ids.xml +11 -0
  126. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-sort-columns.xml +9 -0
  127. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection.xml +11 -0
  128. data/test/reports/TEST-HammerCLI-Output-Adapter-Table.xml +7 -0
  129. data/test/reports/TEST-HammerCLI-Output-Definition-empty-.xml +11 -0
  130. data/test/reports/TEST-HammerCLI-Output-Definition.xml +11 -0
  131. data/test/reports/TEST-HammerCLI-Output-Dsl-collection.xml +13 -0
  132. data/test/reports/TEST-HammerCLI-Output-Dsl-custom-fields.xml +11 -0
  133. data/test/reports/TEST-HammerCLI-Output-Dsl-fields.xml +15 -0
  134. data/test/reports/TEST-HammerCLI-Output-Dsl-label.xml +13 -0
  135. data/test/reports/TEST-HammerCLI-Output-Dsl-path-definition.xml +13 -0
  136. data/test/reports/TEST-HammerCLI-Output-Dsl.xml +9 -0
  137. data/test/reports/TEST-HammerCLI-Output-FieldFilter.xml +13 -0
  138. data/test/reports/TEST-HammerCLI-Output-Formatters-BooleanFormatter.xml +11 -0
  139. data/test/reports/TEST-HammerCLI-Output-Formatters-ColorFormatter.xml +9 -0
  140. data/test/reports/TEST-HammerCLI-Output-Formatters-DateFormatter.xml +11 -0
  141. data/test/reports/TEST-HammerCLI-Output-Formatters-FieldFormatter.xml +13 -0
  142. data/test/reports/TEST-HammerCLI-Output-Formatters-FormatterContainer.xml +13 -0
  143. data/test/reports/TEST-HammerCLI-Output-Formatters-FormatterLibrary.xml +11 -0
  144. data/test/reports/TEST-HammerCLI-Output-Formatters-KeyValueFormatter.xml +13 -0
  145. data/test/reports/TEST-HammerCLI-Output-Formatters-ListFormatter.xml +13 -0
  146. data/test/reports/TEST-HammerCLI-Output-Formatters-LongTextFormatter.xml +13 -0
  147. data/test/reports/TEST-HammerCLI-Output-Output-adapters.xml +17 -0
  148. data/test/reports/TEST-HammerCLI-Output-Output-data.xml +15 -0
  149. data/test/reports/TEST-HammerCLI-Output-Output-formatters.xml +9 -0
  150. data/test/reports/TEST-HammerCLI-Output-Output-messages.xml +19 -0
  151. data/test/reports/TEST-HammerCLI-Output-Output.xml +7 -0
  152. data/test/reports/TEST-HammerCLI-Output-RecordCollection.xml +13 -0
  153. data/test/reports/TEST-HammerCLI-Settings-load-from-paths.xml +15 -0
  154. data/test/reports/TEST-HammerCLI-Settings.xml +25 -0
  155. data/test/reports/TEST-HammerCLI-ShellHistory-loading-old-history.xml +11 -0
  156. data/test/reports/TEST-HammerCLI-ShellHistory-saving-history.xml +15 -0
  157. data/test/reports/TEST-HammerCLI-ShellHistory.xml +7 -0
  158. data/test/reports/TEST-MiniTest-Spec.xml +7 -0
  159. data/test/reports/TEST-String-camelize.xml +11 -0
  160. data/test/reports/TEST-String-formatting.xml +17 -0
  161. data/test/reports/TEST-String-indent.xml +11 -0
  162. data/test/reports/TEST-String-interactive-.xml +13 -0
  163. data/test/reports/TEST-String.xml +7 -0
  164. data/test/reports/TEST-constraints-HammerCLI-Validator-AllConstraint-exist-.xml +13 -0
  165. data/test/reports/TEST-constraints-HammerCLI-Validator-AllConstraint.xml +7 -0
  166. data/test/reports/TEST-constraints-HammerCLI-Validator-AnyConstraint-exist-.xml +13 -0
  167. data/test/reports/TEST-constraints-HammerCLI-Validator-AnyConstraint.xml +7 -0
  168. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-exist-.xml +9 -0
  169. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-rejected.xml +13 -0
  170. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-required.xml +13 -0
  171. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint.xml +7 -0
  172. data/test/reports/TEST-constraints.xml +7 -0
  173. data/test/unit/abstract_test.rb +15 -11
  174. data/test/unit/apipie/command_test.rb +29 -1
  175. data/test/unit/apipie/option_builder_test.rb +22 -0
  176. data/test/unit/fixtures/apipie/documented.json +44 -0
  177. data/test/unit/options/normalizers_test.rb +35 -0
  178. data/test/unit/output/adapter/csv_test.rb +104 -4
  179. data/test/unit/utils_test.rb +58 -0
  180. metadata +292 -6
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <testsuite name="constraints::HammerCLI::Validator::BaseConstraint::rejected" tests="3" time="0.001188178" failures="0" errors="0" skipped="0" assertions="3">
3
+ <testcase name="test_0003_should return nil when exist? returns true" time="0.000339906" assertions="1">
4
+ </testcase>
5
+ <testcase name="test_0002_should raise exception with a message" time="0.000330188" assertions="1">
6
+ </testcase>
7
+ <testcase name="test_0001_should raise exception when exist? returns true" time="0.000327038" assertions="1">
8
+ </testcase>
9
+ <system-out>
10
+ </system-out>
11
+ <system-err>
12
+ </system-err>
13
+ </testsuite>
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <testsuite name="constraints::HammerCLI::Validator::BaseConstraint::required" tests="3" time="0.001221194" failures="0" errors="0" skipped="0" assertions="2">
3
+ <testcase name="test_0003_should return nil when exist? returns true" time="0.000335162" assertions="1">
4
+ </testcase>
5
+ <testcase name="test_0001_should raise exception when exist? returns true" time="0.000327945" assertions="1">
6
+ </testcase>
7
+ <testcase name="test_0002_should raise exception with a message" time="0.000337518" assertions="0">
8
+ </testcase>
9
+ <system-out>
10
+ </system-out>
11
+ <system-err>
12
+ </system-err>
13
+ </testsuite>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <testsuite name="constraints::HammerCLI::Validator::BaseConstraint" tests="0" time="0.000163178" failures="0" errors="0" skipped="0" assertions="0">
3
+ <system-out>
4
+ </system-out>
5
+ <system-err>
6
+ </system-err>
7
+ </testsuite>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <testsuite name="constraints" tests="0" time="0.000161677" failures="0" errors="0" skipped="0" assertions="0">
3
+ <system-out>
4
+ </system-out>
5
+ <system-err>
6
+ </system-err>
7
+ </testsuite>
@@ -277,16 +277,6 @@ describe HammerCLI::AbstractCommand do
277
277
  HammerCLI::AbstractCommand.option_builder.builders.empty?.must_equal true
278
278
  end
279
279
 
280
- it "can be initialized with custom builder" do
281
- cmd = Class.new(HammerCLI::AbstractCommand) do
282
- def self.custom_option_builders
283
- [:builder_1, :builder_2]
284
- end
285
- end
286
-
287
- cmd.option_builder.builders.must_equal [:builder_1, :builder_2]
288
- end
289
-
290
280
  end
291
281
 
292
282
  describe "build options" do
@@ -304,7 +294,7 @@ describe HammerCLI::AbstractCommand do
304
294
 
305
295
  class TestBuilderCmd < HammerCLI::AbstractCommand
306
296
 
307
- def self.option_builder
297
+ def self.create_option_builder
308
298
  TestOptionBuilder.new
309
299
  end
310
300
 
@@ -327,6 +317,20 @@ describe HammerCLI::AbstractCommand do
327
317
  TestBuilderCmd.recognised_options.map(&:description).flatten.sort.must_equal ["original_test", "test2"].sort
328
318
  end
329
319
 
320
+ it "passes params to the builders" do
321
+ @params = {:param1 => 1, :param2 => :value}
322
+ TestBuilderCmd.option_builder.expects(:build).with(@params).returns([])
323
+ TestBuilderCmd.build_options(@params)
324
+ end
325
+
326
+ it "accepts block" do
327
+ @params = {:param1 => 1, :param2 => :value}
328
+ TestBuilderCmd.option_builder.expects(:build).with(@params).returns([])
329
+ TestBuilderCmd.build_options do |params|
330
+ @params
331
+ end
332
+ end
333
+
330
334
  end
331
335
 
332
336
  it "should inherit command_name" do
@@ -13,6 +13,19 @@ describe HammerCLI::Apipie::Command do
13
13
  action :show
14
14
  end
15
15
 
16
+ class OptionCommand < TestCommand
17
+ resource :architectures, :create
18
+
19
+ def option_name
20
+ 'test_name'
21
+ end
22
+
23
+ def option_operatingsystem_ids
24
+ nil
25
+ end
26
+
27
+ end
28
+
16
29
  class CommandA < TestCommand
17
30
  resource :architectures, :index
18
31
 
@@ -103,5 +116,20 @@ describe HammerCLI::Apipie::Command do
103
116
 
104
117
  end
105
118
 
106
- end
119
+ context "options" do
107
120
 
121
+ it "should collect method options from given options" do
122
+ cmd_opt = OptionCommand.new("", ctx)
123
+ params = cmd_opt.class.resource.action(:create).params
124
+ cmd_opt.method_options_for_params(params, {'option_name' => 'name'}).must_equal({"architecture" => {"name" => "name"}})
125
+ end
126
+
127
+ it "should collect method options from methods" do
128
+ cmd_opt = OptionCommand.new("", ctx)
129
+ params = cmd_opt.class.resource.action(:create).params
130
+ cmd_opt.method_options_for_params(params, {}).must_equal({"architecture"=>{"name"=>"test_name"}})
131
+ end
132
+
133
+ end
134
+
135
+ end
@@ -106,5 +106,27 @@ describe HammerCLI::Apipie::OptionBuilder do
106
106
  end
107
107
 
108
108
  end
109
+
110
+ context "aliasing resources" do
111
+ let(:action) {api.resource(:documented).action(:action_with_ids)}
112
+ let(:builder_options) { {:resource_mapping => {:organization => 'company', 'compute_resource' => :compute_provider}} }
113
+
114
+ it "renames options" do
115
+ # builder_options[:resource_mapping] = {:organization => 'company', 'compute_resource' => :compute_provider}
116
+ options.map(&:long_switch).sort.must_equal ["--company-id", "--company-ids", "--compute-provider-id", "--name"]
117
+ end
118
+
119
+ it "renames option types" do
120
+ # builder_options[:resource_mapping] = {:organization => 'company', 'compute_resource' => :compute_provider}
121
+ options.map(&:type).sort.must_equal ["COMPANY_ID", "COMPANY_IDS", "COMPUTE_PROVIDER_ID", "NAME"]
122
+ end
123
+
124
+ it "keeps option accessor the same" do
125
+ # builder_options[:resource_mapping] = {:organization => 'company', 'compute_resource' => :compute_provider}
126
+ options.map(&:attribute_name).sort.must_equal HammerCLI.option_accessor_name("compute_resource_id", "name", "organization_id", "organization_ids")
127
+ end
128
+
129
+ end
130
+
109
131
  end
110
132
 
@@ -26,6 +26,50 @@
26
26
  ],
27
27
  "full_description": ""
28
28
  },
29
+ {
30
+ "name": "action_with_ids",
31
+ "examples": [],
32
+ "errors": [],
33
+ "params": [
34
+ {
35
+ "name": "name",
36
+ "allow_null": false,
37
+ "full_name": "name",
38
+ "validator": "Must be String",
39
+ "expected_type": "string",
40
+ "description": "",
41
+ "required": false
42
+ },
43
+ {
44
+ "name": "organization_id",
45
+ "allow_null": false,
46
+ "full_name": "organization_id",
47
+ "validator": "Must be number",
48
+ "expected_type": "number",
49
+ "description": "",
50
+ "required": false
51
+ },
52
+ {
53
+ "name": "organization_ids",
54
+ "allow_null": false,
55
+ "full_name": "organization_ids",
56
+ "validator": "Array of numbers",
57
+ "expected_type": "Array",
58
+ "description": "",
59
+ "required": false
60
+ },
61
+ {
62
+ "name": "compute_resource_id",
63
+ "allow_null": false,
64
+ "full_name": "compute_resource_id",
65
+ "validator": "Must be number",
66
+ "expected_type": "number",
67
+ "description": "",
68
+ "required": false
69
+ }
70
+ ],
71
+ "full_description": ""
72
+ },
29
73
  {
30
74
  "name": "create",
31
75
  "examples": [],
@@ -171,6 +171,41 @@ describe HammerCLI::Options::Normalizers do
171
171
 
172
172
  end
173
173
 
174
+ describe 'enumlist' do
175
+
176
+ let (:formatter) { HammerCLI::Options::Normalizers::EnumList.new ['1', '2', 'a', 'b'] }
177
+
178
+ it "should return array of values when the values are allowed" do
179
+ formatter.format("a,b,1").must_equal(['a', 'b', '1'])
180
+ end
181
+
182
+ it "should raise argument error when any of the values isn't in the list" do
183
+ proc { formatter.format("c,d") }.must_raise ArgumentError
184
+ proc { formatter.format('1,x') }.must_raise ArgumentError
185
+ end
186
+
187
+ it "should remove duplicate values" do
188
+ formatter.format("a,a,a,a,a").must_equal ['a']
189
+ end
190
+
191
+ it "should not change order of the values" do
192
+ formatter.format("a,b,2,1").must_equal ['a', 'b', '2', '1']
193
+ end
194
+
195
+ it "should return empty array on nil" do
196
+ formatter.format(nil).must_equal []
197
+ end
198
+
199
+ it "should return empty array on empty string" do
200
+ formatter.format("").must_equal []
201
+ end
202
+
203
+ it "should list allowed values in description" do
204
+ formatter.description.must_equal("Any combination (comma separated list) of ''1', '2', 'a', 'b''")
205
+ end
206
+
207
+ end
208
+
174
209
  describe 'datetime' do
175
210
 
176
211
  let(:formatter) { HammerCLI::Options::Normalizers::DateTime.new }
@@ -16,13 +16,16 @@ describe HammerCLI::Output::Adapter::CSValues do
16
16
  :started_at => "2000"
17
17
  }]}
18
18
 
19
-
20
19
  it "should print column name" do
21
- proc { adapter.print_collection(fields, data) }.must_output(/.*Name,Started At.*/, "")
20
+ out, err = capture_io { adapter.print_collection(fields, data) }
21
+ out.must_match /.*Name,Started At.*/
22
+ err.must_match //
22
23
  end
23
24
 
24
25
  it "should print field value" do
25
- proc { adapter.print_collection(fields, data) }.must_output(/.*John Doe.*/, "")
26
+ out, err = capture_io { adapter.print_collection(fields, data) }
27
+ out.must_match /.*John Doe.*/
28
+ err.must_match //
26
29
  end
27
30
 
28
31
  context "handle ids" do
@@ -30,11 +33,15 @@ describe HammerCLI::Output::Adapter::CSValues do
30
33
  let(:fields) {
31
34
  [field_name, field_id]
32
35
  }
36
+ let(:data) { HammerCLI::Output::RecordCollection.new [{
37
+ :name => "John Doe",
38
+ :some_id => "2000"
39
+ }]}
33
40
 
34
41
  it "should ommit column of type Id by default" do
35
42
  out, err = capture_io { adapter.print_collection(fields, data) }
36
43
  out.wont_match(/.*Id.*/)
37
- out.wont_match(/.*John Doe,.*/)
44
+ out.wont_match(/.*2000,.*/)
38
45
  end
39
46
 
40
47
  it "should print column of type Id when --show-ids is set" do
@@ -44,6 +51,99 @@ describe HammerCLI::Output::Adapter::CSValues do
44
51
  end
45
52
  end
46
53
 
54
+ context "handle fields with containers" do
55
+ let(:demographics) {
56
+ Fields::Label.new(:path => [], :label => "Demographics") do
57
+ from :demographics do
58
+ field :age, "Age"
59
+ field :gender, "Gender"
60
+ label _("Biometrics") do
61
+ from :biometrics do
62
+ field :weight, "Weight"
63
+ field :height, "Height"
64
+ end
65
+ end
66
+ end
67
+ end
68
+ }
69
+ let(:fields) {
70
+ [field_name, field_started_at, demographics]
71
+ }
72
+ let(:data) { HammerCLI::Output::RecordCollection.new [{
73
+ :name => "John Doe",
74
+ :started_at => "2000",
75
+ :demographics=> { :age => '22', :gender => 'm',
76
+ :biometrics => { :weight => '123', :height => '155' } }
77
+ }]}
78
+
79
+ it "should print column names" do
80
+ out, err = capture_io { adapter.print_collection(fields, data) }
81
+ out.must_match /.*Demographics::Age,Demographics::Gender,Biometrics::Weight,Biometrics::Height*/
82
+ err.must_match //
83
+ end
84
+
85
+ it "should print data" do
86
+ out, err = capture_io { adapter.print_collection(fields, data) }
87
+ out.must_match /.*2000,22,m,123,155*/
88
+ err.must_match //
89
+ end
90
+ end
91
+
92
+ context "handle fields with collections" do
93
+ let(:items) {
94
+ Fields::Collection.new(:path => [:items], :label => "Items") do
95
+ from :item do
96
+ field :name, "Item Name"
97
+ field :quantity, "Item Quantity"
98
+ end
99
+ end
100
+ }
101
+ let(:fields) {
102
+ [field_name, field_started_at, items]
103
+ }
104
+ let(:data) { HammerCLI::Output::RecordCollection.new [
105
+ {
106
+ :name => "John Doe",
107
+ :started_at => "2000",
108
+ :items => [{:item => { :name => 'hammer', :quantity => '100'}}]
109
+ },
110
+ {
111
+ :name => "Jane Roe",
112
+ :started_at => "2001",
113
+ :items => [{:item => { :name => 'cleaver', :quantity => '1'}}, {:item => { :name => 'sledge', :quantity => '50'}}]
114
+ }
115
+ ]}
116
+
117
+ it "should print collection column name" do
118
+ out, err = capture_io { adapter.print_collection(fields, data) }
119
+
120
+ lines = out.split("\n")
121
+ lines[0].must_equal 'Name,Started At,Items::Item Name::1,Items::Item Quantity::1,Items::Item Name::2,Items::Item Quantity::2'
122
+
123
+ err.must_match //
124
+ end
125
+
126
+ it "should print collection data" do
127
+ out, err = capture_io { adapter.print_collection(fields, data) }
128
+ lines = out.split("\n")
129
+
130
+ lines[1].must_equal 'John Doe,2000,hammer,100,"",""'
131
+ lines[2].must_equal 'Jane Roe,2001,cleaver,1,sledge,50'
132
+
133
+ err.must_match //
134
+ end
135
+
136
+ it "should handle empty collection" do
137
+ out, err = capture_io { adapter.print_collection(fields, []) }
138
+ lines = out.split("\n")
139
+
140
+ lines[0].must_equal 'Name,Started At,Items'
141
+
142
+ err.must_match //
143
+ end
144
+
145
+ end
146
+
47
147
  context "formatters" do
48
148
  it "should apply formatters" do
49
149
  class DotFormatter < HammerCLI::Output::Formatters::FieldFormatter
@@ -1,6 +1,13 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
 
4
+ module Constant
5
+ module Test
6
+ class X
7
+ end
8
+ end
9
+ end
10
+
4
11
  describe String do
5
12
 
6
13
  context "formatting" do
@@ -56,6 +63,35 @@ describe String do
56
63
 
57
64
  end
58
65
 
66
+ describe "constantize" do
67
+
68
+ it "raises NameError for empty string" do
69
+ proc {
70
+ "".constantize
71
+ }.must_raise NameError
72
+ end
73
+
74
+ it "raises NameError for unknown constant" do
75
+ proc {
76
+ "UnknownClass".constantize
77
+ }.must_raise NameError
78
+ end
79
+
80
+ it "returns correct constant" do
81
+ "Object".constantize.must_equal Object
82
+ end
83
+
84
+ it "returns correct namespaced constant" do
85
+ "Constant::Test::X".constantize.must_equal Constant::Test::X
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+
92
+
93
+ describe HammerCLI do
94
+
59
95
  describe "interactive?" do
60
96
 
61
97
  before :each do
@@ -89,5 +125,27 @@ describe String do
89
125
  end
90
126
  end
91
127
 
128
+
129
+ describe "constant_path" do
130
+
131
+ it "returns empty array for empty string" do
132
+ HammerCLI.constant_path("").must_equal []
133
+ end
134
+
135
+ it "raises NameError for unknown constant" do
136
+ proc {
137
+ HammerCLI.constant_path("UnknownClass")
138
+ }.must_raise NameError
139
+ end
140
+
141
+ it "returns single constant" do
142
+ HammerCLI.constant_path("Object").must_equal [Object]
143
+ end
144
+
145
+ it "returns correct path for namespaced constant" do
146
+ HammerCLI.constant_path("Constant::Test::X").must_equal [Constant, Constant::Test, Constant::Test::X]
147
+ end
148
+ end
149
+
92
150
  end
93
151