hammer_cli 0.15.1 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/doc/commands_modification.md +82 -0
- data/doc/creating_commands.md +78 -38
- data/doc/developer_docs.md +2 -0
- data/doc/help_modification.md +119 -0
- data/doc/i18n.md +2 -2
- data/doc/installation_rpm.md +3 -3
- data/doc/release_notes.md +9 -1
- data/lib/hammer_cli.rb +0 -1
- data/lib/hammer_cli/abstract.rb +44 -13
- data/lib/hammer_cli/exception_handler.rb +1 -1
- data/lib/hammer_cli/help/definition.rb +55 -0
- data/lib/hammer_cli/help/definition/abstract_item.rb +38 -0
- data/lib/hammer_cli/help/definition/list.rb +53 -0
- data/lib/hammer_cli/help/definition/section.rb +36 -0
- data/lib/hammer_cli/help/definition/text.rb +21 -0
- data/lib/hammer_cli/help/text_builder.rb +26 -49
- data/lib/hammer_cli/i18n.rb +0 -1
- data/lib/hammer_cli/options/option_collector.rb +12 -9
- data/lib/hammer_cli/options/option_processor.rb +17 -0
- data/lib/hammer_cli/options/processor_list.rb +37 -0
- data/lib/hammer_cli/options/sources/base.rb +17 -0
- data/lib/hammer_cli/options/sources/command_line.rb +3 -1
- data/lib/hammer_cli/options/sources/saved_defaults.rb +3 -1
- data/lib/hammer_cli/options/validators/base.rb +20 -0
- data/lib/hammer_cli/options/validators/dsl.rb +160 -0
- data/lib/hammer_cli/options/validators/dsl_block_validator.rb +19 -0
- data/lib/hammer_cli/output/adapter/csv.rb +11 -11
- data/lib/hammer_cli/output/adapter/tree_structure.rb +5 -3
- data/lib/hammer_cli/output/definition.rb +42 -4
- data/lib/hammer_cli/output/dsl.rb +4 -2
- data/lib/hammer_cli/output/fields.rb +9 -2
- data/lib/hammer_cli/testing/messages.rb +6 -6
- data/lib/hammer_cli/utils.rb +15 -0
- data/lib/hammer_cli/version.rb +1 -1
- data/locale/ca/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/de/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/en/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/en_GB/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/es/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/fr/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/it/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/ja/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/ko/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/pt_BR/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/ru/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/zh_CN/LC_MESSAGES/hammer-cli.mo +0 -0
- data/locale/zh_TW/LC_MESSAGES/hammer-cli.mo +0 -0
- data/test/unit/abstract_test.rb +70 -5
- data/test/unit/exception_handler_test.rb +1 -1
- data/test/unit/help/definition/abstract_item_test.rb +33 -0
- data/test/unit/help/definition/list_test.rb +17 -0
- data/test/unit/help/definition/section_test.rb +25 -0
- data/test/unit/help/definition/text_test.rb +11 -0
- data/test/unit/help/definition_test.rb +236 -0
- data/test/unit/help/text_builder_test.rb +170 -1
- data/test/unit/options/option_collector_test.rb +18 -9
- data/test/unit/options/processor_list_test.rb +70 -0
- data/test/unit/{validator_test.rb → options/validators/dsl_test.rb} +57 -93
- data/test/unit/output/adapter/abstract_test.rb +3 -0
- data/test/unit/output/adapter/base_test.rb +5 -0
- data/test/unit/output/adapter/csv_test.rb +3 -0
- data/test/unit/output/adapter/json_test.rb +12 -0
- data/test/unit/output/adapter/table_test.rb +5 -0
- data/test/unit/output/adapter/yaml_test.rb +11 -0
- data/test/unit/output/definition_test.rb +221 -1
- data/test/unit/utils_test.rb +23 -0
- metadata +31 -5
- data/lib/hammer_cli/validator.rb +0 -172
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/test/unit/abstract_test.rb
CHANGED
@@ -425,14 +425,79 @@ describe HammerCLI::AbstractCommand do
|
|
425
425
|
CmdOD2.output_definition.fields.length.must_equal 1
|
426
426
|
end
|
427
427
|
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
validate_options do; end
|
428
|
+
describe 'validate_options' do
|
429
|
+
it "adds a validation block" do
|
430
|
+
cmd = Class.new(HammerCLI::AbstractCommand)
|
431
|
+
cmd.validate_options do; end
|
432
|
+
|
433
|
+
mode, target, validator = cmd.validation_blocks[0]
|
434
|
+
|
435
|
+
assert_equal :append, mode
|
436
|
+
assert_nil target
|
437
|
+
assert validator.is_a?(HammerCLI::Options::Validators::DSLBlockValidator)
|
438
|
+
end
|
439
|
+
|
440
|
+
it "allows for multiple validation blocks" do
|
441
|
+
cmd = Class.new(HammerCLI::AbstractCommand)
|
442
|
+
cmd.validate_options do; end
|
443
|
+
cmd.validate_options do; end
|
444
|
+
|
445
|
+
assert_equal 2, cmd.validation_blocks.length
|
432
446
|
end
|
433
447
|
|
434
|
-
|
448
|
+
it "allows relative inserts of custom validators" do
|
449
|
+
custom_validator = mock('Validator')
|
450
|
+
|
451
|
+
cmd = Class.new(HammerCLI::AbstractCommand)
|
452
|
+
cmd.validate_options(:before, 'DefaultInputs', validator: custom_validator)
|
453
|
+
|
454
|
+
mode, target, validator = cmd.validation_blocks[0]
|
455
|
+
|
456
|
+
assert_equal :before, mode
|
457
|
+
assert_equal 'DefaultInputs', target
|
458
|
+
assert validator, custom_validator
|
459
|
+
end
|
435
460
|
end
|
436
461
|
|
462
|
+
describe 'add_validators' do
|
463
|
+
it 'is called upon the option_collector initialization' do
|
464
|
+
cmd = Class.new(HammerCLI::AbstractCommand)
|
465
|
+
|
466
|
+
cmd_instance = cmd.new('', {})
|
467
|
+
cmd_instance.expects(:add_validators).with do |sources|
|
468
|
+
sources.map(&:name) == ['DefaultInputs']
|
469
|
+
end
|
470
|
+
cmd_instance.send(:option_collector)
|
471
|
+
end
|
472
|
+
|
473
|
+
it 'inserts validators' do
|
474
|
+
validator1 = stub('Validator1', :name => 'Validator1')
|
475
|
+
validator2 = stub('Validator2', :name => 'Validator2')
|
476
|
+
sources = HammerCLI::Options::ProcessorList.new([
|
477
|
+
stub('Source1', :name => 'Source1'),
|
478
|
+
stub('Source2', :name => 'Source2')
|
479
|
+
])
|
480
|
+
|
481
|
+
cmd = Class.new(HammerCLI::AbstractCommand)
|
482
|
+
cmd.validate_options(:after, 'Source1', validator: validator1)
|
483
|
+
cmd.validate_options(validator: validator2)
|
484
|
+
|
485
|
+
cmd_instance = cmd.new('', {})
|
486
|
+
result = cmd_instance.send(:add_validators, sources)
|
487
|
+
assert_equal ['Source1', 'Validator1', 'Source2', 'Validator2'], result.map(&:name)
|
488
|
+
end
|
489
|
+
|
490
|
+
it 'allows for creating inheritable validators' do
|
491
|
+
validator1 = stub('Validator1', :name => 'Validator1')
|
492
|
+
cmd = Class.new(HammerCLI::AbstractCommand)
|
493
|
+
cmd.send(:define_method, :add_validators) { |sources| [ validator1 ] }
|
494
|
+
|
495
|
+
sub_cmd = Class.new(cmd)
|
496
|
+
|
497
|
+
cmd_instance = sub_cmd.new('', {})
|
498
|
+
result = cmd_instance.send(:option_collector).option_processor
|
499
|
+
assert_equal ['Validator1'], result.map(&:name)
|
500
|
+
end
|
501
|
+
end
|
437
502
|
end
|
438
503
|
|
@@ -30,7 +30,7 @@ describe HammerCLI::ExceptionHandler do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should handle help request" do
|
33
|
-
output.expects(:print_message).with(cmd.help,
|
33
|
+
output.expects(:print_message).with(cmd.help, {}, verbosity: HammerCLI::V_QUIET)
|
34
34
|
handler.handle_exception(Clamp::HelpWanted.new(cmd), :heading => heading)
|
35
35
|
|
36
36
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../../test_helper')
|
2
|
+
|
3
|
+
describe HammerCLI::Help::AbstractItem do
|
4
|
+
describe '#indent' do
|
5
|
+
let(:first_item) { HammerCLI::Help::Text.new('Lorem ipsum') }
|
6
|
+
let(:second_item) { HammerCLI::Help::Text.new(' Dolor sit amet') }
|
7
|
+
let(:sub_definition) { HammerCLI::Help::Definition.new([first_item, second_item]) }
|
8
|
+
|
9
|
+
it 'indents text' do
|
10
|
+
section = HammerCLI::Help::Section.new('Heading', sub_definition)
|
11
|
+
expected_result = [
|
12
|
+
'Heading:',
|
13
|
+
' Lorem ipsum',
|
14
|
+
'',
|
15
|
+
' Dolor sit amet',
|
16
|
+
''
|
17
|
+
].join("\n")
|
18
|
+
section.build_string.must_equal(expected_result)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'indents text with custom padding' do
|
22
|
+
section = HammerCLI::Help::Section.new('Heading', sub_definition, indentation: '**')
|
23
|
+
expected_result = [
|
24
|
+
'Heading:',
|
25
|
+
'**Lorem ipsum',
|
26
|
+
'**',
|
27
|
+
'** Dolor sit amet',
|
28
|
+
''
|
29
|
+
].join("\n")
|
30
|
+
section.build_string.must_equal(expected_result)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../../test_helper')
|
2
|
+
|
3
|
+
describe HammerCLI::Help::List do
|
4
|
+
describe '#build_string' do
|
5
|
+
let(:first_item) { [:first, 'This is first line'] }
|
6
|
+
let(:second_item) { [:second, 'This is second line'] }
|
7
|
+
let(:list) { HammerCLI::Help::List.new([first_item, second_item]) }
|
8
|
+
|
9
|
+
it 'builds string' do
|
10
|
+
list.build_string.must_equal [
|
11
|
+
'first This is first line',
|
12
|
+
'second This is second line',
|
13
|
+
''
|
14
|
+
].join("\n")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../../test_helper')
|
2
|
+
|
3
|
+
describe HammerCLI::Help::Section do
|
4
|
+
describe '#build_string' do
|
5
|
+
let(:section) { HammerCLI::Help::Section.new('section') }
|
6
|
+
let(:first_text) { HammerCLI::Help::Text.new('first') }
|
7
|
+
let(:second_text) { HammerCLI::Help::Text.new('second') }
|
8
|
+
|
9
|
+
it 'builds string without definition' do
|
10
|
+
section.build_string.must_equal "section:\n\n"
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'builds string with definition' do
|
14
|
+
section.definition = HammerCLI::Help::Definition.new([first_text, second_text])
|
15
|
+
expected_output = [
|
16
|
+
'section:',
|
17
|
+
' first',
|
18
|
+
'',
|
19
|
+
' second',
|
20
|
+
''
|
21
|
+
].join("\n")
|
22
|
+
section.build_string.must_equal expected_output
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../../test_helper')
|
2
|
+
|
3
|
+
describe HammerCLI::Help::Text do
|
4
|
+
describe '#build_string' do
|
5
|
+
let(:text) { HammerCLI::Help::Text.new('text') }
|
6
|
+
|
7
|
+
it 'builds string' do
|
8
|
+
text.build_string.must_equal 'text'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,236 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../test_helper')
|
2
|
+
|
3
|
+
describe HammerCLI::Help::Definition do
|
4
|
+
let(:definition) { HammerCLI::Help::Definition.new }
|
5
|
+
let(:old_text_item) { HammerCLI::Help::Text.new('Lorem ipsum', id: :old) }
|
6
|
+
let(:new_text_item) { HammerCLI::Help::Text.new('Dolor sit amet', id: :new) }
|
7
|
+
|
8
|
+
describe '#build_string' do
|
9
|
+
describe 'text' do
|
10
|
+
it 'prints text via definition' do
|
11
|
+
definition << HammerCLI::Help::Text.new('Lorem ipsum')
|
12
|
+
definition.build_string.must_equal "Lorem ipsum\n"
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'prints multiple blocks with spaces via definition' do
|
16
|
+
definition << HammerCLI::Help::Text.new('Lorem ipsum')
|
17
|
+
definition << HammerCLI::Help::Text.new('Dolor sit amet')
|
18
|
+
definition.build_string.must_equal [
|
19
|
+
'Lorem ipsum',
|
20
|
+
'',
|
21
|
+
'Dolor sit amet',
|
22
|
+
''
|
23
|
+
].join("\n")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'section' do
|
28
|
+
it 'prints section heading via definition' do
|
29
|
+
definition << HammerCLI::Help::Section.new('Heading')
|
30
|
+
definition.build_string.must_equal "Heading:\n\n"
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'indents section content via definition' do
|
34
|
+
sub_definition = HammerCLI::Help::Definition.new
|
35
|
+
sub_definition << HammerCLI::Help::Text.new('Lorem ipsum')
|
36
|
+
sub_definition << HammerCLI::Help::Text.new('Dolor sit amet')
|
37
|
+
definition << HammerCLI::Help::Section.new('Heading', sub_definition)
|
38
|
+
definition.build_string.must_equal [
|
39
|
+
'Heading:',
|
40
|
+
' Lorem ipsum',
|
41
|
+
'',
|
42
|
+
' Dolor sit amet',
|
43
|
+
''
|
44
|
+
].join("\n")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'list' do
|
49
|
+
it 'prints empty list via definition' do
|
50
|
+
builder = HammerCLI::Help::TextBuilder.new
|
51
|
+
builder.list([])
|
52
|
+
builder.definition.build_string.must_equal ''
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'prints single column list' do
|
56
|
+
definition << HammerCLI::Help::List.new([
|
57
|
+
:a,
|
58
|
+
:bb,
|
59
|
+
:ccc
|
60
|
+
])
|
61
|
+
definition.build_string.must_equal [
|
62
|
+
'a',
|
63
|
+
'bb',
|
64
|
+
'ccc',
|
65
|
+
''
|
66
|
+
].join("\n")
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'prints two column list via definition' do
|
70
|
+
definition << HammerCLI::Help::List.new([
|
71
|
+
[:a, 'This is line A'],
|
72
|
+
[:bb, 'This is line B'],
|
73
|
+
[:ccc]
|
74
|
+
])
|
75
|
+
definition.build_string.must_equal [
|
76
|
+
'a This is line A',
|
77
|
+
'bb This is line B',
|
78
|
+
'ccc',
|
79
|
+
''
|
80
|
+
].join("\n")
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'handles multiple lines in the second column via definition' do
|
84
|
+
definition << HammerCLI::Help::List.new([
|
85
|
+
[:a, "This is line A\nThis is line A part two"],
|
86
|
+
[:bb, 'This is line B'],
|
87
|
+
[:ccc, 'This is line C']
|
88
|
+
])
|
89
|
+
definition.build_string.must_equal [
|
90
|
+
'a This is line A',
|
91
|
+
' This is line A part two',
|
92
|
+
'bb This is line B',
|
93
|
+
'ccc This is line C',
|
94
|
+
''
|
95
|
+
].join("\n")
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'can adjust indentation of the second column via definition' do
|
99
|
+
definition << HammerCLI::Help::List.new([
|
100
|
+
['a', 'This is line A'],
|
101
|
+
['This line B is too long for the first column', 'This is line B'],
|
102
|
+
['ccc', 'This is line C']
|
103
|
+
])
|
104
|
+
definition.build_string.must_equal [
|
105
|
+
'a This is line A',
|
106
|
+
'This line B is too long for the first column This is line B',
|
107
|
+
'ccc This is line C',
|
108
|
+
''
|
109
|
+
].join("\n")
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe '#find_item' do
|
115
|
+
it 'finds an item' do
|
116
|
+
definition << old_text_item
|
117
|
+
definition << new_text_item
|
118
|
+
definition.find_item(:new).must_equal new_text_item
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe '#insert_definition' do
|
123
|
+
let(:new_definition) { HammerCLI::Help::Definition.new([new_text_item, new_text_item]) }
|
124
|
+
let(:section_item) { HammerCLI::Help::Section.new('section', id: :section) }
|
125
|
+
|
126
|
+
describe 'before' do
|
127
|
+
it 'should insert new help item before the old one' do
|
128
|
+
definition << old_text_item
|
129
|
+
definition.insert_definition(:before, :old, new_text_item.definition)
|
130
|
+
definition.first.id.must_equal new_text_item.id
|
131
|
+
definition.count.must_equal 2
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'should insert multiple items before old item' do
|
135
|
+
definition << old_text_item
|
136
|
+
definition.insert_definition(:before, :old, new_definition)
|
137
|
+
definition.first.id.must_equal new_text_item.id
|
138
|
+
definition.count.must_equal 3
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'should work with labels' do
|
142
|
+
definition << section_item
|
143
|
+
definition.insert_definition(:before, 'section', new_definition)
|
144
|
+
definition.first.id.must_equal new_text_item.id
|
145
|
+
definition.count.must_equal 3
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe 'after' do
|
150
|
+
it 'should insert new help item after the old one' do
|
151
|
+
definition << old_text_item
|
152
|
+
definition.insert_definition(:after, :old, new_text_item.definition)
|
153
|
+
definition[0].id.must_equal old_text_item.id
|
154
|
+
definition[1].id.must_equal new_text_item.id
|
155
|
+
definition.count.must_equal 2
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'should insert multiple items after old item' do
|
159
|
+
definition << old_text_item
|
160
|
+
definition.insert_definition(:after, :old, new_definition)
|
161
|
+
definition[0].id.must_equal old_text_item.id
|
162
|
+
definition[1].id.must_equal new_text_item.id
|
163
|
+
definition[2].id.must_equal new_text_item.id
|
164
|
+
definition.count.must_equal 3
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'should work with labels' do
|
168
|
+
definition << section_item
|
169
|
+
definition.insert_definition(:after, 'section', new_text_item.definition)
|
170
|
+
definition[1].id.must_equal new_text_item.id
|
171
|
+
definition.count.must_equal 2
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
describe 'replace' do
|
176
|
+
it 'should replace the old help item with new one' do
|
177
|
+
definition << old_text_item
|
178
|
+
definition.insert_definition(:replace, :old, new_text_item.definition)
|
179
|
+
definition.first.id.must_equal new_text_item.id
|
180
|
+
definition.count.must_equal 1
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'should replace the old help item with new ones' do
|
184
|
+
definition << old_text_item
|
185
|
+
definition.insert_definition(:replace, :old, new_definition)
|
186
|
+
definition[0].id.must_equal new_text_item.id
|
187
|
+
definition[1].id.must_equal new_text_item.id
|
188
|
+
definition.count.must_equal 2
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'should work with labels' do
|
192
|
+
definition << section_item
|
193
|
+
definition.insert_definition(:replace, 'section', new_text_item.definition)
|
194
|
+
definition.first.id.must_equal new_text_item.id
|
195
|
+
definition.count.must_equal 1
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
describe '#at' do
|
201
|
+
it 'should return self if path is empty' do
|
202
|
+
definition << old_text_item
|
203
|
+
definition.at([]) do |h|
|
204
|
+
h.definition.first.id.must_equal old_text_item.id
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'should accept integer' do
|
209
|
+
definition << old_text_item
|
210
|
+
definition << new_text_item
|
211
|
+
definition.at(1) do |h|
|
212
|
+
h.definition.first.id.must_equal new_text_item.id
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'should return definition of specified help item with path' do
|
217
|
+
sub_definition = HammerCLI::Help::Definition.new
|
218
|
+
sub_definition << old_text_item
|
219
|
+
sub_definition << new_text_item
|
220
|
+
definition << HammerCLI::Help::Section.new('first', sub_definition, id: :first_section)
|
221
|
+
definition << HammerCLI::Help::Section.new('second', sub_definition, id: :second_section)
|
222
|
+
definition.at(:first_section).definition << HammerCLI::Help::Section.new('nested', sub_definition, id: :nested_section)
|
223
|
+
definition.at([:first_section, :nested_section]).definition.first.id.must_equal old_text_item.id
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'should work with labels' do
|
227
|
+
sub_definition = HammerCLI::Help::Definition.new
|
228
|
+
sub_definition << old_text_item
|
229
|
+
sub_definition << new_text_item
|
230
|
+
definition << HammerCLI::Help::Section.new('first', sub_definition)
|
231
|
+
definition << HammerCLI::Help::Section.new('second', sub_definition)
|
232
|
+
definition.at('first').definition << HammerCLI::Help::Section.new('nested', sub_definition)
|
233
|
+
definition.at(['first', 'nested']).definition.first.id.must_equal old_text_item.id
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
@@ -136,5 +136,174 @@ describe HammerCLI::Help::TextBuilder do
|
|
136
136
|
help.indent(text, '**').must_equal(expected_result)
|
137
137
|
end
|
138
138
|
end
|
139
|
-
end
|
140
139
|
|
140
|
+
describe '#find_item' do
|
141
|
+
it 'finds an item' do
|
142
|
+
help.text('Lorem ipsum', id: :lorem)
|
143
|
+
help.text('Dolor sit amet', id: :dolor)
|
144
|
+
help.find_item(:dolor).id.must_equal :dolor
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'finds a nested item' do
|
148
|
+
help.section('Heading') do |h|
|
149
|
+
h.text('Lorem ipsum', id: :lorem)
|
150
|
+
h.text('Dolor sit amet', id: :dolor)
|
151
|
+
end
|
152
|
+
help.at('Heading') do |h|
|
153
|
+
h.find_item(:dolor).id.must_equal :dolor
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe '#insert' do
|
159
|
+
describe 'before' do
|
160
|
+
it 'should insert new help item before the old one' do
|
161
|
+
help.text('old', id: :old)
|
162
|
+
help.insert(:before, :old) do |h|
|
163
|
+
h.text('new', id: :new)
|
164
|
+
end
|
165
|
+
help.definition.first.id.must_equal :new
|
166
|
+
help.definition.count.must_equal 2
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'should insert multiple items before old item' do
|
170
|
+
help.text('old', id: :old)
|
171
|
+
help.insert(:before, :old) do |h|
|
172
|
+
h.text('new', id: :new)
|
173
|
+
h.text('new2', id: :new2)
|
174
|
+
end
|
175
|
+
help.definition.first.id.must_equal :new
|
176
|
+
help.definition.count.must_equal 3
|
177
|
+
end
|
178
|
+
|
179
|
+
it 'should work with labels' do
|
180
|
+
help.section('section') do |h|
|
181
|
+
h.text('text in section')
|
182
|
+
end
|
183
|
+
help.insert(:before, 'section') do |h|
|
184
|
+
h.text('text before section', id: :before_section)
|
185
|
+
end
|
186
|
+
help.definition.first.id.must_equal :before_section
|
187
|
+
help.definition.count.must_equal 2
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
describe 'after' do
|
192
|
+
it 'should insert new help item after the old one' do
|
193
|
+
help.text('old', id: :old)
|
194
|
+
help.insert(:after, :old) do |h|
|
195
|
+
h.text('new', id: :new)
|
196
|
+
end
|
197
|
+
help.definition[0].id.must_equal :old
|
198
|
+
help.definition[1].id.must_equal :new
|
199
|
+
help.definition.count.must_equal 2
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'should insert multiple items after old item' do
|
203
|
+
help.text('old', id: :old)
|
204
|
+
help.insert(:after, :old) do |h|
|
205
|
+
h.text('new', id: :new)
|
206
|
+
h.text('new2', id: :new2)
|
207
|
+
end
|
208
|
+
help.definition[0].id.must_equal :old
|
209
|
+
help.definition[1].id.must_equal :new
|
210
|
+
help.definition[2].id.must_equal :new2
|
211
|
+
help.definition.count.must_equal 3
|
212
|
+
end
|
213
|
+
|
214
|
+
it 'should work with labels' do
|
215
|
+
help.section('section') do |h|
|
216
|
+
h.text('text in section')
|
217
|
+
end
|
218
|
+
help.insert(:after, 'section') do |h|
|
219
|
+
h.text('text before section', id: :after_section)
|
220
|
+
end
|
221
|
+
help.definition[1].id.must_equal :after_section
|
222
|
+
help.definition.count.must_equal 2
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
describe 'replace' do
|
227
|
+
it 'should replace the old help item with new one' do
|
228
|
+
help.text('old', id: :old)
|
229
|
+
help.insert(:replace, :old) do |h|
|
230
|
+
h.text('new', id: :new)
|
231
|
+
end
|
232
|
+
help.definition.first.id.must_equal :new
|
233
|
+
help.definition.count.must_equal 1
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'should replace the old help item with new ones' do
|
237
|
+
help.text('old', id: :old)
|
238
|
+
help.insert(:replace, :old) do |h|
|
239
|
+
h.text('new', id: :new)
|
240
|
+
h.text('new2', id: :new2)
|
241
|
+
end
|
242
|
+
help.definition[0].id.must_equal :new
|
243
|
+
help.definition[1].id.must_equal :new2
|
244
|
+
help.definition.count.must_equal 2
|
245
|
+
end
|
246
|
+
|
247
|
+
it 'should work with labels' do
|
248
|
+
help.section('section') do |h|
|
249
|
+
h.text('text in section')
|
250
|
+
end
|
251
|
+
help.insert(:replace, 'section') do |h|
|
252
|
+
h.text('text instead of section', id: :instead_of_section)
|
253
|
+
end
|
254
|
+
help.definition.first.id.must_equal :instead_of_section
|
255
|
+
help.definition.count.must_equal 1
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
describe '#at' do
|
261
|
+
it 'should return self if path is empty' do
|
262
|
+
help.text('foo', id: :foo)
|
263
|
+
help.at([]) do |h|
|
264
|
+
h.definition.first.id.must_equal :foo
|
265
|
+
end
|
266
|
+
help.at do |h|
|
267
|
+
h.definition.first.id.must_equal :foo
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
it 'should return definition of specified help item with path' do
|
272
|
+
help.section('first', id: :first_section) do |h|
|
273
|
+
h.text('first text in the first section', id: :first_text1)
|
274
|
+
h.text('second text in the first section', id: :secon_text1)
|
275
|
+
end
|
276
|
+
help.section('second', id: :second_section) do |h|
|
277
|
+
h.text('first text in the second section', id: :first_text2)
|
278
|
+
h.text('second text in the second section', id: :secon_text2)
|
279
|
+
end
|
280
|
+
help.at(:first_section) do |h|
|
281
|
+
h.section('nested section', id: :nested_section) do |h|
|
282
|
+
h.text('text in nested section', id: :nested_text)
|
283
|
+
end
|
284
|
+
end
|
285
|
+
help.at([:first_section, :nested_section]) do |h|
|
286
|
+
h.definition.first.id.must_equal :nested_text
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
it 'should work with labels' do
|
291
|
+
help.section('first') do |h|
|
292
|
+
h.text('first text in the first section', id: :first_text1)
|
293
|
+
h.text('second text in the first section', id: :secon_text1)
|
294
|
+
end
|
295
|
+
help.section('second') do |h|
|
296
|
+
h.text('first text in the second section', id: :first_text2)
|
297
|
+
h.text('second text in the second section', id: :secon_text2)
|
298
|
+
end
|
299
|
+
help.at('first') do |h|
|
300
|
+
h.section('nested section') do |h|
|
301
|
+
h.text('text in nested section', id: :nested_text)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
help.at(['first', 'nested section']) do |h|
|
305
|
+
h.definition.first.id.must_equal :nested_text
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|