rubikon 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@
6
6
  require 'test_helper'
7
7
  require 'testapps'
8
8
 
9
- class ApplicationTests < Test::Unit::TestCase
9
+ class TestApplication < Test::Unit::TestCase
10
10
 
11
11
  context 'A Rubikon application\'s class' do
12
12
 
@@ -32,6 +32,7 @@ class ApplicationTests < Test::Unit::TestCase
32
32
  @app = TestApp
33
33
  @ostream = StringIO.new
34
34
  @app.set :ostream, @ostream
35
+ @app.set :raise_errors, true
35
36
  end
36
37
 
37
38
  should 'exit gracefully' do
@@ -45,7 +46,6 @@ class ApplicationTests < Test::Unit::TestCase
45
46
  @ostream.rewind
46
47
  assert_equal "Error:\n", @ostream.gets
47
48
  assert_equal " Unknown command: unknown\n", @ostream.gets
48
- @app.set :raise_errors, true
49
49
  end
50
50
 
51
51
  should 'run its default command without arguments' do
@@ -76,9 +76,10 @@ class ApplicationTests < Test::Unit::TestCase
76
76
  assert_equal 'input: ', @ostream.gets
77
77
  end
78
78
 
79
- should "not break output while displaying a throbber or progress bar" do
79
+ should 'not break output while displaying a throbber or progress bar' do
80
80
  @app.run(%w{throbber})
81
- assert_equal " \b-\b\\\b|\b/\bdon't\nbreak\n", @ostream.string
81
+ assert_match (/ \x08(?:(?:-|\\|\/|\|)\x08){4,}don't\nbreak\n/), @ostream.string
82
+
82
83
  @ostream.rewind
83
84
 
84
85
  @app.run(%w{progressbar})
@@ -108,17 +109,54 @@ class ApplicationTests < Test::Unit::TestCase
108
109
  $VERBOSE = false
109
110
  end
110
111
 
112
+ should 'have working global parameters' do
113
+ assert_equal 'flag', @app.run(%w{globalopt --gflag})
114
+ assert_equal 'flag', @app.run(%w{globalopt --gf1})
115
+ assert_equal 'flag', @app.run(%w{globalopt --gf2})
116
+ assert_equal 'test', @app.run(%w{globalopt --gopt test})
117
+ assert_equal 'test', @app.run(%w{globalopt --go1 test})
118
+ assert_equal 'test', @app.run(%w{globalopt --go2 test})
119
+ end
120
+
111
121
  should 'have a working help command' do
112
122
  @app.run(%w{help})
113
- assert_match /Usage: [^ ]* \[--debug\|-d\] \[--gopt\|--go \.\.\.\] \[--verbose\|-v\] command \[args\]\n\nCommands:\n help Display this help screen\n input \n object_id \n parameters \n progressbar \n throbber \n/, @ostream.string
123
+ assert_match /Usage: [^ ]* \[--debug\|-d\] \[--gflag\|--gf1\|--gf2\] \[--gopt\|--go1\|--go2 \.\.\.\] \[--verbose\|-v\] command \[args\]\n\nCommands:\n arguments \n globalopt \n help Display this help screen\n input \n object_id \n parameters \n progressbar \n sandbox \n throbber \n/, @ostream.string
114
124
  end
115
125
 
116
126
  should 'have a working DSL for command parameters' do
117
127
  params = @app.run(%w{parameters}).values.uniq.sort { |a,b| a.name.to_s <=> b.name.to_s }
118
128
  assert_equal :flag, params[0].name
119
129
  assert_equal [:f], params[0].aliases
130
+ assert !params[0].active?
120
131
  assert_equal :option, params[1].name
121
132
  assert_equal [:o], params[1].aliases
133
+ assert !params[1].active?
134
+ end
135
+
136
+ should 'allow simplified access to arguments' do
137
+ result = @app.run(%w{arguments cmd_test --arg opt_test})
138
+ assert_equal %w{opt_test opt_test cmd_test}, result
139
+ end
140
+
141
+ should 'be protected by a sandbox' do
142
+ %w{init parse_arguments run}.each do |method|
143
+ assert_raise NoMethodError do
144
+ @app.run(['sandbox', method])
145
+ end
146
+ end
147
+ end
148
+
149
+ should 'know its file system location' do
150
+ dir = File.expand_path(File.dirname(__FILE__))
151
+ assert_equal dir + '/testapps.rb', @app.base_file
152
+ assert_equal dir, @app.path
153
+ end
154
+
155
+ should 'have working hooks' do
156
+ TestAppWithHooks.set :ostream, @ostream
157
+ TestAppWithHooks.run(%w{execute})
158
+
159
+ assert_equal "pre init\npost init\npre execute\nexecute\npost execute\n", @ostream.string
122
160
  end
123
161
 
124
162
  end
@@ -3,27 +3,14 @@
3
3
  #
4
4
  # Copyright (c) 2009-2010, Sebastian Staudt
5
5
 
6
- require 'test_helper'
6
+ require 'test_parameter'
7
7
 
8
- class DummyApp < Application::Base
8
+ class TestCommand < Test::Unit::TestCase
9
9
 
10
- set :autorun, false
11
-
12
- attr_accessor :external_command_run
13
-
14
- end
15
-
16
- class CommandTests < Test::Unit::TestCase
10
+ include TestParameter
17
11
 
18
12
  context 'A Rubikon command' do
19
13
 
20
- setup do
21
- @app = DummyApp.instance
22
- @app.instance_eval do
23
- @path = File.dirname(__FILE__)
24
- end
25
- end
26
-
27
14
  should 'be a Parameter' do
28
15
  assert Command.included_modules.include?(Parameter)
29
16
  assert Command.new(@app, :command){}.is_a?(Parameter)
@@ -53,16 +40,20 @@ class CommandTests < Test::Unit::TestCase
53
40
  end
54
41
 
55
42
  should 'correctly parse given parameters' do
56
- command = Command.new @app, :command do end
57
- option = Option.new(:test, 1)
58
- command << option
59
- flag = Flag.new(:t)
60
- command << flag
43
+ command = Command.new @app, :command, [:cmd_arg] do end
44
+ option = Option.new(@app, :test, [:opt_arg])
45
+ command.add_param option
46
+ flag = Flag.new(@app, :t)
47
+ command.add_param flag
61
48
  command.run(*%w{--test arg -t test})
62
49
  assert option.active?
63
50
  assert flag.active?
64
51
  assert_equal %w{test}, command.arguments
52
+ assert_equal 'test', command[0]
53
+ assert_equal 'test', command.cmd_arg
65
54
  assert_equal %w{arg}, command.parameters[:test].args
55
+ assert_equal 'arg', command.test[0]
56
+ assert_equal 'arg', command.test.opt_arg
66
57
 
67
58
  assert_raise UnknownParameterError do
68
59
  command.run(*%w{--unknown})
@@ -71,11 +62,12 @@ class CommandTests < Test::Unit::TestCase
71
62
 
72
63
  should 'allow parameter aliases' do
73
64
  command = Command.new @app, :command do end
74
- flag1 = Flag.new(:test)
75
- command << flag1
76
- flag2 = Flag.new(:test2)
77
- command << flag2
78
- command << { :t => :test, :t2 => :test2 }
65
+ command.add_param({ :t => :test })
66
+ flag1 = Flag.new(@app, :test)
67
+ command.add_param flag1
68
+ flag2 = Flag.new(@app, :test2)
69
+ command.add_param flag2
70
+ command.add_param({ :t2 => :test2 })
79
71
  command.run(*%w{-t --t2})
80
72
  assert flag1.active?
81
73
  assert flag2.active?
@@ -91,10 +83,9 @@ class CommandTests < Test::Unit::TestCase
91
83
  end
92
84
 
93
85
  should 'run external code if no block is given' do
94
- @app.external_command_run = false
95
86
  command = Command.new @app, :external_command
96
87
  command.run
97
- assert @app.external_command_run
88
+ assert @app.sandbox.instance_variable_get(:@external_command_run)
98
89
  end
99
90
 
100
91
  end
@@ -3,20 +3,22 @@
3
3
  #
4
4
  # Copyright (c) 2010, Sebastian Staudt
5
5
 
6
- require 'test_helper'
6
+ require 'test_parameter'
7
7
 
8
- class FlagTests < Test::Unit::TestCase
8
+ class TestFlag < Test::Unit::TestCase
9
+
10
+ include TestParameter
9
11
 
10
12
  context 'A Rubikon flag' do
11
13
 
12
14
  should 'be a Parameter' do
13
15
  assert Flag.included_modules.include?(Parameter)
14
- assert Flag.new(:test).is_a?(Parameter)
16
+ assert Flag.new(@app, :test).is_a?(Parameter)
15
17
  end
16
18
 
17
19
  should 'call its code block if it is activated' do
18
20
  block_run = false
19
- flag = Flag.new :flag do
21
+ flag = Flag.new @app, :flag do
20
22
  block_run = true
21
23
  end
22
24
  flag.active!
@@ -25,7 +27,7 @@ class FlagTests < Test::Unit::TestCase
25
27
  end
26
28
 
27
29
  should 'not allow any arguments' do
28
- flag = Flag.new :test
30
+ flag = Flag.new @app, :test
29
31
  assert_raise ExtraArgumentError do
30
32
  flag << 'argument'
31
33
  end
@@ -0,0 +1,99 @@
1
+ # This code is free software; you can redistribute it and/or modify it under
2
+ # the terms of the new BSD License.
3
+ #
4
+ # Copyright (c) 2010, Sebastian Staudt
5
+
6
+ require 'test_helper'
7
+ require 'test_parameter'
8
+
9
+ class HasArg
10
+ include HasArguments
11
+
12
+ attr_reader :arg_names
13
+
14
+ def initialize(arg_count)
15
+ super(DummyApp.instance, 'dummy', arg_count)
16
+ end
17
+ end
18
+
19
+ class TestHasArguments < Test::Unit::TestCase
20
+
21
+ context 'A parameter with arguments' do
22
+
23
+ should 'allow a Numeric as argument count' do
24
+ @has_arg = HasArg.new(1)
25
+ assert_equal 1..1, @has_arg.arg_count
26
+ assert_nil @has_arg.arg_names
27
+ end
28
+
29
+ should 'allow a Range as argument count' do
30
+ @has_arg = HasArg.new(1..3)
31
+ assert_equal 1..3, @has_arg.arg_count
32
+ assert_nil @has_arg.arg_names
33
+ end
34
+
35
+ should 'allow an Array as argument count' do
36
+ @has_arg = HasArg.new([2, 5, 6])
37
+ assert_equal 2..6, @has_arg.arg_count
38
+ assert_nil @has_arg.arg_names
39
+ end
40
+
41
+ should 'allow a Symbol Array as argument names' do
42
+ @has_arg = HasArg.new([:arg1, :arg2, :arg3])
43
+ assert_equal 3..3, @has_arg.arg_count
44
+ assert_equal [:arg1, :arg2, :arg3], @has_arg.arg_names
45
+ end
46
+
47
+ should 'only have required arguments if argument count is > 0' do
48
+ @has_arg = HasArg.new(2)
49
+ assert !@has_arg.args_full?
50
+ assert @has_arg.more_args?
51
+ @has_arg << 'argument'
52
+ assert_equal %w{argument}, @has_arg.args
53
+ assert_raise MissingArgumentError do
54
+ @has_arg.check_args
55
+ end
56
+ @has_arg << 'argument'
57
+ assert @has_arg.args_full?
58
+ assert !@has_arg.more_args?
59
+ assert_equal %w{argument argument}, @has_arg.args
60
+ assert_raise ExtraArgumentError do
61
+ @has_arg << 'argument'
62
+ end
63
+ assert_equal %w{argument argument}, @has_arg.args
64
+ end
65
+
66
+ should 'have required and optional arguments if argument count is < 0' do
67
+ @has_arg = HasArg.new(-1)
68
+ assert !@has_arg.args_full?
69
+ assert @has_arg.more_args?
70
+ assert_raise MissingArgumentError do
71
+ @has_arg.check_args
72
+ end
73
+ @has_arg << 'argument'
74
+ assert @has_arg.args_full?
75
+ assert @has_arg.more_args?
76
+ assert_equal %w{argument}, @has_arg.args
77
+ end
78
+
79
+ should 'only have optional arguments if argument count is 0' do
80
+ @has_arg = HasArg.new(0)
81
+ assert @has_arg.args_full?
82
+ assert @has_arg.more_args?
83
+ @has_arg << 'argument'
84
+ assert_equal %w{argument}, @has_arg.args
85
+ end
86
+
87
+ should 'provide named arguments' do
88
+ @has_arg = HasArg.new([:named])
89
+ @has_arg << 'argument'
90
+ assert_equal 'argument', @has_arg[:named]
91
+ assert_equal 'argument', @has_arg.named
92
+ assert_raise NoMethodError do
93
+ @has_arg.not_named
94
+ end
95
+ end
96
+
97
+ end
98
+
99
+ end
data/test/test_helper.rb CHANGED
@@ -10,6 +10,7 @@ include Rubikon
10
10
 
11
11
  require 'rubygems'
12
12
  require 'shoulda'
13
+ require 'stringio'
13
14
 
14
15
  unless RUBY_VERSION[0..2] == '1.9'
15
16
  begin require 'redgreen'; rescue LoadError; end
@@ -0,0 +1,32 @@
1
+ # This code is free software; you can redistribute it and/or modify it under
2
+ # the terms of the new BSD License.
3
+ #
4
+ # Copyright (c) 2010, Sebastian Staudt
5
+
6
+ require 'test_parameter'
7
+
8
+ class TestOption < Test::Unit::TestCase
9
+
10
+ include TestParameter
11
+
12
+ context 'A Rubikon option' do
13
+
14
+ should 'be a Parameter with arguments' do
15
+ assert Option.included_modules.include?(Parameter)
16
+ assert Option.included_modules.include?(HasArguments)
17
+ assert Option.new(@app, :test).is_a?(Parameter)
18
+ end
19
+
20
+ should 'call its code block if it is activated' do
21
+ block_run = false
22
+ option = Option.new @app, :test do
23
+ block_run = true
24
+ end
25
+ option.active!
26
+ assert option.active?
27
+ assert block_run
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,21 @@
1
+ # This code is free software; you can redistribute it and/or modify it under
2
+ # the terms of the new BSD License.
3
+ #
4
+ # Copyright (c) 2010, Sebastian Staudt
5
+
6
+ require 'test_helper'
7
+ require 'testapps'
8
+
9
+ module TestParameter
10
+
11
+ def setup
12
+ @app = DummyApp.instance
13
+ sandbox = nil
14
+ @app.instance_eval do
15
+ @path = File.dirname(__FILE__)
16
+ sandbox = @sandbox
17
+ end
18
+ @sandbox = sandbox
19
+ end
20
+
21
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  require 'test_helper'
7
7
 
8
- class ProgressBarTests < Test::Unit::TestCase
8
+ class TestProgressBar < Test::Unit::TestCase
9
9
 
10
10
  context 'A progress bar' do
11
11
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  require 'test_helper'
7
7
 
8
- class ThrobberTests < Test::Unit::TestCase
8
+ class TestThrobber < Test::Unit::TestCase
9
9
 
10
10
  context 'A throbber' do
11
11
 
@@ -14,13 +14,7 @@ class ThrobberTests < Test::Unit::TestCase
14
14
  end
15
15
 
16
16
  should 'have default throbber strings' do
17
- unless RUBY_VERSION[0..2] == '1.9'
18
- consts = %w{SPINNER}
19
- else
20
- consts = [:SPINNER, :MUTEX_FOR_THREAD_EXCLUSIVE]
21
- end
22
- assert_equal consts, Throbber.constants
23
- assert_equal '-\|/', Throbber.const_get(:SPINNER)
17
+ assert_equal '-\|/', Throbber::SPINNER
24
18
  end
25
19
 
26
20
  should 'work correctly' do
@@ -35,7 +29,7 @@ class ThrobberTests < Test::Unit::TestCase
35
29
  thread.join
36
30
  throbber.join
37
31
 
38
- spinner = Throbber.const_get(:SPINNER)
32
+ spinner = Throbber::SPINNER
39
33
  check_throbber = ' '
40
34
  ((finished_at - started_at) / 0.25).floor.times do |char_index|
41
35
  check_throbber << "\b"
data/test/testapps.rb CHANGED
@@ -3,21 +3,46 @@
3
3
  #
4
4
  # Copyright (c) 2009-2010, Sebastian Staudt
5
5
 
6
+ class DummyApp < Application::Base
7
+
8
+ set :autorun, false
9
+
10
+ attr_accessor :external_command_run
11
+
12
+ end
13
+
6
14
  class TestApp < Application::Base
7
15
 
8
16
  set :autorun, false
9
17
  set :name, 'Rubikon test application'
10
18
  set :raise_errors, true
11
19
 
12
- global_option :go => :gopt
20
+ global_flag :gf1 => :gflag
21
+ global_flag :gflag do
22
+ @global = 'flag'
23
+ end
24
+ global_flag :gf2 => :gflag
25
+
26
+ global_option :go1 => :gopt
13
27
  global_option :gopt, 1 do
14
28
  @global = args[0]
15
29
  end
30
+ global_option :go2 => :gopt
16
31
 
17
32
  default do
18
33
  'default command'
19
34
  end
20
35
 
36
+ option :arg, [:opt_arg] do
37
+ @result = []
38
+ @result << opt_arg
39
+ end
40
+ command :arguments, [:cmd_arg] do
41
+ @result << arg.opt_arg
42
+ @result << cmd_arg
43
+ @result
44
+ end
45
+
21
46
  command :input do
22
47
  input 'input'
23
48
  end
@@ -44,6 +69,14 @@ class TestApp < Application::Base
44
69
  end
45
70
  end
46
71
 
72
+ command :sandbox, 1 do
73
+ send(args[0].to_sym)
74
+ end
75
+
76
+ command :globalopt do
77
+ @global
78
+ end
79
+
47
80
  command :throbber do
48
81
  throbber do
49
82
  sleep 0.5
@@ -62,3 +95,29 @@ class TestAppWithoutDefault < Application::Base
62
95
  set :raise_errors, true
63
96
 
64
97
  end
98
+
99
+ class TestAppWithHooks < Application::Base
100
+
101
+ set :autorun, false
102
+
103
+ pre_init do
104
+ puts 'pre init'
105
+ end
106
+
107
+ post_init do
108
+ puts 'post init'
109
+ end
110
+
111
+ pre_execute do
112
+ puts 'pre execute'
113
+ end
114
+
115
+ post_execute do
116
+ puts 'post execute'
117
+ end
118
+
119
+ command :execute do
120
+ puts 'execute'
121
+ end
122
+
123
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubikon
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sebastian Staudt
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-26 00:00:00 +02:00
18
+ date: 2010-10-16 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -33,7 +33,7 @@ dependencies:
33
33
  type: :development
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
- name: yard
36
+ name: shoulda
37
37
  prerelease: false
38
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
39
  none: false
@@ -46,6 +46,20 @@ dependencies:
46
46
  version: "0"
47
47
  type: :development
48
48
  version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: yard
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
49
63
  description: A simple to use, yet powerful Ruby framework for building console-based applications.
50
64
  email: koraktor@gmail.com
51
65
  executables: []
@@ -59,28 +73,34 @@ files:
59
73
  - LICENSE
60
74
  - README.md
61
75
  - Rakefile
76
+ - lib/core_ext/object.rb
62
77
  - lib/core_ext/string.rb
63
78
  - lib/rubikon.rb
64
79
  - lib/rubikon/application/base.rb
65
80
  - lib/rubikon/application/class_methods.rb
66
81
  - lib/rubikon/application/dsl_methods.rb
67
82
  - lib/rubikon/application/instance_methods.rb
83
+ - lib/rubikon/application/sandbox.rb
68
84
  - lib/rubikon/command.rb
69
85
  - lib/rubikon/exceptions.rb
70
86
  - lib/rubikon/flag.rb
87
+ - lib/rubikon/has_arguments.rb
71
88
  - lib/rubikon/option.rb
72
89
  - lib/rubikon/parameter.rb
73
90
  - lib/rubikon/progress_bar.rb
74
91
  - lib/rubikon/throbber.rb
75
- - test/application_tests.rb
76
- - test/command_tests.rb
92
+ - samples/helloworld/hello_world.rb
77
93
  - test/commands/external_command.rb
78
- - test/flag_tests.rb
79
- - test/option_tests.rb
80
- - test/progress_bar_tests.rb
94
+ - test/test_application.rb
95
+ - test/test_command.rb
96
+ - test/test_flag.rb
97
+ - test/test_has_arguments.rb
81
98
  - test/test_helper.rb
99
+ - test/test_option.rb
100
+ - test/test_parameter.rb
101
+ - test/test_progress_bar.rb
102
+ - test/test_throbber.rb
82
103
  - test/testapps.rb
83
- - test/throbber_tests.rb
84
104
  has_rdoc: false
85
105
  homepage: http://koraktor.github.com/rubikon
86
106
  licenses: []
@@ -116,12 +136,14 @@ signing_key:
116
136
  specification_version: 3
117
137
  summary: Rubikon - A Ruby console app framework
118
138
  test_files:
119
- - test/application_tests.rb
120
- - test/command_tests.rb
121
139
  - test/commands/external_command.rb
122
- - test/flag_tests.rb
123
- - test/option_tests.rb
124
- - test/progress_bar_tests.rb
140
+ - test/test_application.rb
141
+ - test/test_command.rb
142
+ - test/test_flag.rb
143
+ - test/test_has_arguments.rb
125
144
  - test/test_helper.rb
145
+ - test/test_option.rb
146
+ - test/test_parameter.rb
147
+ - test/test_progress_bar.rb
148
+ - test/test_throbber.rb
126
149
  - test/testapps.rb
127
- - test/throbber_tests.rb