Class: TC_testCommand

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/bbcloud/vendor/gli/test/tc_command.rb

Defined Under Namespace

Classes: FakeStdOut

Instance Method Summary collapse

Instance Method Details

#setupObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 25

def setup
  GLI.reset
  GLI.program_desc 'A super awesome program'
  GLI.desc 'Some Global Option'
  GLI.switch :g
  GLI.switch :blah
  GLI.long_desc 'This is a very long description for a flag'
  GLI.flag [:y,:yes]
  @pre_called = false
  @post_called = false
  @error_called = false
  GLI.pre { |g,c,o,a| @pre_called = true }
  GLI.post { |g,c,o,a| @post_called = true }
  GLI.on_error { |g,c,o,a| @error_called = true }
  @glob = nil
  @verbose = nil
  @glob_verbose = nil
  @configure = nil
  @args = nil
  GLI.desc 'Some Basic Command that potentially has a really really really really really really really long description and stuff, but you know, who cares?'
  GLI.long_desc 'This is the long description: "Some Basic Command that potentially has a really really really really really really really long description and stuff, but you know, who cares?"'
  GLI.arg_name 'first_file second_file'
  GLI.command [:basic,:bs] do |c|
    c.desc 'be verbose'
    c.switch :v
    c.desc 'configure something or other, in some way that requires a lot of verbose text and whatnot'
    c.default_value 'crud'
    c.flag [:c,:configure]
    c.action do |global_options,options,arguments|
      @glob = global_options[:g] ? 'true' : 'false'
      @verbose = options[:v] ? 'true' : 'false'
      @glob_verbose = global_options[:v] ? 'true' : 'false'
      @configure = options[:c]
      @args = arguments
    end
  end
  @fake_stdout = FakeStdOut.new
  @fake_stderr = FakeStdOut.new
  DefaultHelpCommand.output_device=@fake_stdout
  DefaultHelpCommand.skips_pre=true
  DefaultHelpCommand.skips_post=true
  GLI.error_device=@fake_stderr
end

#tear_downObject



69
70
71
72
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 69

def tear_down
  FileUtils.rm_f "cruddo.rdoc"
  DefaultHelpCommand.output_device=$stdout
end

#test_basic_commandObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 92

def test_basic_command
  args_args = [%w(-g basic -v -c foo bar baz quux), %w(-g basic -v --configure=foo bar baz quux)]
  args_args.each do |args|
    GLI.run(args)
    assert_equal('true',@glob)
    assert_equal('true',@verbose)
    assert_equal('false',@glob_verbose)
    assert_equal('foo',@configure)
    assert_equal(%w(bar baz quux),@args)
    assert(@pre_called,"Pre block should have been called")
    assert(@post_called,"Post block should have been called")
    assert(!@error_called,"Error block should not have been called")
  end
end

#test_command_createObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 295

def test_command_create
  GLI.desc 'single symbol'
  GLI.command :single do |c|; end
  command = GLI.commands[:single]
  assert_equal :single, command.name
  assert_equal nil, command.aliases

  description = 'implicit array'
  GLI.desc description
  GLI.command :foo, :bar do |c|; end
  command = GLI.commands[:foo]
  assert_equal :foo, command.name
  assert_equal [:bar], command.aliases

  description = 'explicit array'
  GLI.desc description
  GLI.command [:baz, :blah] do |c|; end
  command = GLI.commands[:baz]
  assert_equal :baz, command.name
  assert_equal [:blah], command.aliases
end

#test_command_no_globalsObject



142
143
144
145
146
147
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 142

def test_command_no_globals
  args = %w(basic -c foo bar baz quux)
  GLI.run(args)
  assert_equal('foo',@configure)
  assert_equal(%w(bar baz quux),@args)
end

#test_command_skips_preObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 107

def test_command_skips_pre
  GLI.skips_pre
  GLI.skips_post

  skips_pre_called = false
  runs_pre_called = false

  GLI.command [:skipspre] do |c| 
    c.action do |g,o,a|
      skips_pre_called = true
    end
  end

  # Making sure skips_pre doesn't leak to other commands
  GLI.command [:runspre] do |c|
    c.action do |g,o,a|
      runs_pre_called = true
    end
  end

  GLI.run(['skipspre'])

  assert(skips_pre_called,"'skipspre' should have been called")
  assert(!@pre_called,"Pre block should not have been called")
  assert(!@post_called,"Post block should not have been called")
  assert(!@error_called,"Error block should not have been called")

  GLI.run(['runspre'])

  assert(runs_pre_called,"'runspre' should have been called")
  assert(@pre_called,"Pre block should not have been called")
  assert(@post_called,"Post block SHOULD have been called")
  assert(!@error_called,"Error block should not have been called")
end

#test_command_sortObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 79

def test_command_sort
  commands = [Command.new(:foo,"foo")]
  commands << Command.new(:bar,"bar")
  commands << Command.new(:zazz,"zazz")
  commands << Command.new(:zaz,"zaz")

  sorted = commands.sort
  assert_equal :bar,sorted[0].name
  assert_equal :foo,sorted[1].name
  assert_equal :zaz,sorted[2].name
  assert_equal :zazz,sorted[3].name
end

#test_defaults_get_setObject



149
150
151
152
153
154
155
156
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 149

def test_defaults_get_set
  args = %w(basic bar baz quux)
  GLI.run(args)
  assert_equal('false',@glob)
  assert_equal('false',@verbose)
  assert_equal('crud',@configure)
  assert_equal(%w(bar baz quux),@args)
end

#test_helpObject



193
194
195
196
197
198
199
200
201
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 193

def test_help
  args = %w(help)
  GLI.run(args)
  ['\[global options\]','\[command options\]','Global Options:','A super awesome program'].each do |opt|
    assert_contained(@fake_stdout,/#{opt}/)
  end
  assert(!@pre_called,"Expected pre block NOT to have been called")
  assert(!@post_called,"Expected post block NOT to have been called")
end

#test_help_completionObject



263
264
265
266
267
268
269
270
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 263

def test_help_completion
  GLI.command :foo, :bar do |c|; end
  GLI.command :ls, :list do |c|; end
  args = %w(help -c)
  GLI.run(args)
  assert_equal 7,@fake_stdout.strings.size
  assert_equal ['bar','basic','bs','foo','help','list','ls'],@fake_stdout.strings
end

#test_help_completion_partialObject



272
273
274
275
276
277
278
279
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 272

def test_help_completion_partial
  GLI.command :foo, :bar do |c|; end
  GLI.command :ls, :list do |c|; end
  args = %w(help -c b)
  GLI.run(args)
  assert_equal 3,@fake_stdout.strings.size
  assert_equal ['bar','basic','bs'],@fake_stdout.strings
end

#test_help_no_commandObject



288
289
290
291
292
293
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 288

def test_help_no_command
  GLI.program_name 'cruddo'
  args = %w(help foo)
  GLI.run(args)
  assert_equal('cruddo',GLI.program_name)
end

#test_help_no_global_optionsObject



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 224

def test_help_no_global_options
  GLI.reset
  GLI.desc 'Basic Command'
  GLI.command [:basic,:bs] do |c|
    c.action do |global_options,options,arguments|
    end
  end
  @fake_stdout = FakeStdOut.new
  DefaultHelpCommand.output_device=@fake_stdout

  GLI.run(%w(help))
  assert_not_contained(@fake_stdout,/\[global options\]/)
end

#test_help_one_commandObject



238
239
240
241
242
243
244
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 238

def test_help_one_command
  args = %w(help basic)
  GLI.run(args)
  ['Command Options:','\[command options\]'].each do |opt|
    assert_contained(@fake_stdout,/#{opt}/)
  end
end

#test_help_with_config_file_shows_config_valueObject



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 203

def test_help_with_config_file_shows_config_value
  config_file = Tempfile.new('gli_config')
  config = {
    :blah => true,
    :y => "foo",
  }
  File.open(config_file.path,'w') { |file| YAML.dump(config,file) }
  GLI.config_file(config_file.path)
  GLI.run(%w(help))
  assert_contained(@fake_stdout,/\(default: foo\)/)
end

#test_help_with_pre_calledObject



215
216
217
218
219
220
221
222
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 215

def test_help_with_pre_called
  args = %w(help)
  GLI::DefaultHelpCommand.skips_pre=false
  GLI::DefaultHelpCommand.skips_post=false
  GLI.run(args)
  assert(@pre_called,"Expected pre block to have been called")
  assert(@post_called,"Expected pre block to have been called")
end

#test_namesObject



74
75
76
77
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 74

def test_names
  command = Command.new([:ls,:list,:'list-them-all'],"List")
  assert_equal "ls, list-them-all, list",command.names
end

#test_no_argumentsObject



158
159
160
161
162
163
164
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 158

def test_no_arguments
  args = %w(basic -v)
  GLI.run(args)
  assert_equal('true',@verbose)
  assert_equal('crud',@configure)
  assert_equal([],@args)
end

#test_rdocObject



281
282
283
284
285
286
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 281

def test_rdoc
  GLI.program_name 'cruddo'
  args = %w(rdoc)
  GLI.run(args)
  assert File.exists?("cruddo.rdoc")
end

#test_unknown_argumentObject



184
185
186
187
188
189
190
191
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 184

def test_unknown_argument
  args = %w(basic --quux)
  GLI.run(args)
  assert(!@post_called)
  assert(@error_called)
  assert_contained(@fake_stderr,/ help basic/)
  assert_contained(@fake_stderr,/list of command options/)
end

#test_unknown_commandObject



166
167
168
169
170
171
172
173
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 166

def test_unknown_command
  args = %w(blah)
  GLI.run(args)
  assert(!@post_called)
  assert(@error_called)
  assert_contained(@fake_stderr,/ help/)
  assert_contained(@fake_stderr,/list of commands/)
end

#test_unknown_global_optionObject



175
176
177
178
179
180
181
182
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 175

def test_unknown_global_option
  args = %w(--quux basic)
  GLI.run(args)
  assert(!@post_called)
  assert(@error_called)
  assert_contained(@fake_stderr,/ help/)
  assert_contained(@fake_stderr,/list of global options/)
end

#test_versionObject



246
247
248
249
250
251
252
253
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 246

def test_version
  GLI.command :foo, :bar do |c|; end
  GLI.command :ls, :list do |c|; end
  GLI.version '1.3.4'
  args = %w(help)
  GLI.run(args)
  assert_not_nil @fake_stdout.strings.find{ |x| x =~ /^Version: 1.3.4/ }
end

#test_version_not_specifiedObject



255
256
257
258
259
260
261
# File 'lib/bbcloud/vendor/gli/test/tc_command.rb', line 255

def test_version_not_specified
  GLI.command :foo, :bar do |c|; end
  GLI.command :ls, :list do |c|; end
  args = %w(help)
  GLI.run(args)
  assert_nil @fake_stdout.strings.find{ |x| x =~ /^Version: 1.3.4/ }
end