rubikon 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,102 @@
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) 2009-2010, Sebastian Staudt
5
+
6
+ require 'test_helper'
7
+
8
+ class DummyApp < Application::Base
9
+
10
+ set :autorun, false
11
+
12
+ attr_accessor :external_command_run
13
+
14
+ end
15
+
16
+ class CommandTests < Test::Unit::TestCase
17
+
18
+ context 'A Rubikon command' do
19
+
20
+ setup do
21
+ @app = DummyApp.instance
22
+ @app.instance_eval do
23
+ @path = File.dirname(__FILE__)
24
+ end
25
+ end
26
+
27
+ should 'be a Parameter' do
28
+ assert Command.included_modules.include?(Parameter)
29
+ assert Command.new(@app, :command){}.is_a?(Parameter)
30
+ end
31
+
32
+ should 'raise an exception when no appliation is given' do
33
+ assert_raise ArgumentError do
34
+ Command.new nil, :command
35
+ end
36
+ end
37
+
38
+ should 'raise an exception when no code block is given' do
39
+ assert_raise BlockMissingError do
40
+ Command.new @app, :command
41
+ end
42
+ end
43
+
44
+ should 'not raise an exception when created with correct options' do
45
+ description = 'This is a command'
46
+ name = :command
47
+ assert_nothing_raised do
48
+ command = Command.new @app, name do end
49
+ command.description = description
50
+ assert_equal name, command.name
51
+ assert_equal description, command.description
52
+ end
53
+ end
54
+
55
+ 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
61
+ command.run(*%w{--test arg -t test})
62
+ assert option.active?
63
+ assert flag.active?
64
+ assert_equal %w{test}, command.arguments
65
+ assert_equal %w{arg}, command.parameters[:test].args
66
+
67
+ assert_raise UnknownParameterError do
68
+ command.run(*%w{--unknown})
69
+ end
70
+ end
71
+
72
+ should 'allow parameter aliases' do
73
+ 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 }
79
+ command.run(*%w{-t --t2})
80
+ assert flag1.active?
81
+ assert flag2.active?
82
+ end
83
+
84
+ should 'run the code supplied inside its block' do
85
+ block_run = false
86
+ command = Command.new @app, :command do
87
+ block_run = true
88
+ end
89
+ command.run
90
+ assert block_run
91
+ end
92
+
93
+ should 'run external code if no block is given' do
94
+ @app.external_command_run = false
95
+ command = Command.new @app, :external_command
96
+ command.run
97
+ assert @app.external_command_run
98
+ end
99
+
100
+ end
101
+
102
+ end
@@ -0,0 +1 @@
1
+ @external_command_run = true
@@ -0,0 +1,40 @@
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
+
8
+ class FlagTests < Test::Unit::TestCase
9
+
10
+ context 'A Rubikon flag' do
11
+
12
+ should 'be a Parameter' do
13
+ assert Flag.included_modules.include?(Parameter)
14
+ assert Flag.new(:test).is_a?(Parameter)
15
+ end
16
+
17
+ should 'call its code block if it is activated' do
18
+ block_run = false
19
+ flag = Flag.new :flag do
20
+ block_run = true
21
+ end
22
+ flag.active!
23
+ assert flag.active?
24
+ assert block_run
25
+ end
26
+
27
+ should 'not allow any arguments' do
28
+ flag = Flag.new :test
29
+ assert_raise ExtraArgumentError do
30
+ flag << 'argument'
31
+ end
32
+
33
+ assert flag.args_full?
34
+ assert !flag.more_args?
35
+ assert !flag.respond_to?(:args)
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,75 @@
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
+
8
+ class FlagTests < Test::Unit::TestCase
9
+
10
+ context 'A Rubikon option' do
11
+
12
+ should 'be a Parameter' do
13
+ assert Option.included_modules.include?(Parameter)
14
+ assert Option.new(:test).is_a?(Parameter)
15
+ end
16
+
17
+ should 'call its code block if it is activated' do
18
+ block_run = false
19
+ option = Option.new :test do
20
+ block_run = true
21
+ end
22
+ option.active!
23
+ assert option.active?
24
+ assert block_run
25
+ end
26
+
27
+ should 'have arguments' do
28
+ option = Option.new :test
29
+ assert option.respond_to?(:arg_count)
30
+ assert option.respond_to?(:args)
31
+ end
32
+
33
+ should 'only have required arguments if argument count is > 0' do
34
+ option = Option.new :test, 2
35
+ assert !option.args_full?
36
+ assert option.more_args?
37
+ option << 'argument'
38
+ assert_equal %w{argument}, option.args
39
+ assert_raise MissingArgumentError do
40
+ option.check_args
41
+ end
42
+ option << 'argument'
43
+ assert option.args_full?
44
+ assert !option.more_args?
45
+ assert_equal %w{argument argument}, option.args
46
+ assert_raise ExtraArgumentError do
47
+ option << 'argument'
48
+ end
49
+ assert_equal %w{argument argument}, option.args
50
+ end
51
+
52
+ should 'have required and optional arguments if argument count is < 0' do
53
+ option = Option.new :test, -1
54
+ assert !option.args_full?
55
+ assert option.more_args?
56
+ assert_raise MissingArgumentError do
57
+ option.check_args
58
+ end
59
+ option << 'argument'
60
+ assert option.args_full?
61
+ assert option.more_args?
62
+ assert_equal %w{argument}, option.args
63
+ end
64
+
65
+ should 'only have optional arguments if argument count is 0' do
66
+ option = Option.new :test, 0
67
+ assert option.args_full?
68
+ assert option.more_args?
69
+ option << 'argument'
70
+ assert_equal %w{argument}, option.args
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -1,7 +1,7 @@
1
- # This code is free software; you can redistribute it and/or modify it under the
2
- # terms of the new BSD License.
1
+ # This code is free software; you can redistribute it and/or modify it under
2
+ # the terms of the new BSD License.
3
3
  #
4
- # Copyright (c) 2009, Sebastian Staudt
4
+ # Copyright (c) 2009-2010, Sebastian Staudt
5
5
 
6
6
  require 'test_helper'
7
7
 
@@ -19,6 +19,16 @@ class ProgressBarTests < Test::Unit::TestCase
19
19
  assert_equal 0, @bar.instance_variable_get(:@value)
20
20
  end
21
21
 
22
+ should 'have an easily changeable maximum' do
23
+ @bar = ProgressBar.new(10)
24
+ assert_equal 2, @bar.instance_variable_get(:@factor)
25
+ assert_equal 10, @bar.instance_variable_get(:@maximum)
26
+ assert_equal $stdout, @bar.instance_variable_get(:@ostream)
27
+ assert_equal 0, @bar.instance_variable_get(:@progress)
28
+ assert_equal '#', @bar.instance_variable_get(:@progress_char)
29
+ assert_equal 0, @bar.instance_variable_get(:@value)
30
+ end
31
+
22
32
  should 'be customizable' do
23
33
  options = {
24
34
  :char => '+',
data/test/test_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
- # This code is free software; you can redistribute it and/or modify it under the
2
- # terms of the new BSD License.
1
+ # This code is free software; you can redistribute it and/or modify it under
2
+ # the terms of the new BSD License.
3
3
  #
4
- # Copyright (c) 2009, Sebastian Staudt
4
+ # Copyright (c) 2009-2010, Sebastian Staudt
5
5
 
6
6
  $: << File.join(File.dirname(__FILE__), '..', 'lib')
7
7
  $: << File.dirname(__FILE__)
data/test/testapps.rb ADDED
@@ -0,0 +1,64 @@
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) 2009-2010, Sebastian Staudt
5
+
6
+ class TestApp < Application::Base
7
+
8
+ set :autorun, false
9
+ set :name, 'Rubikon test application'
10
+ set :raise_errors, true
11
+
12
+ global_option :go => :gopt
13
+ global_option :gopt, 1 do
14
+ @global = args[0]
15
+ end
16
+
17
+ default do
18
+ 'default command'
19
+ end
20
+
21
+ command :input do
22
+ input 'input'
23
+ end
24
+
25
+ command :alias_before => :object_id
26
+
27
+ command :object_id do
28
+ object_id
29
+ end
30
+
31
+ command :alias_after => :object_id
32
+
33
+ flag :flag
34
+ flag :f => :flag
35
+ option :option, 1
36
+ option :o => :option
37
+ command :parameters do
38
+ parameters
39
+ end
40
+
41
+ command :progressbar do
42
+ progress_bar(:maximum => 4) do |progress|
43
+ 4.times { progress.+; puts 'test' }
44
+ end
45
+ end
46
+
47
+ command :throbber do
48
+ throbber do
49
+ sleep 0.5
50
+ puts 'don\'t'
51
+ sleep 0.5
52
+ puts 'break'
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ class TestAppWithoutDefault < Application::Base
59
+
60
+ set :autorun, false
61
+ set :help_as_default, false
62
+ set :raise_errors, true
63
+
64
+ end
@@ -1,7 +1,7 @@
1
- # This code is free software; you can redistribute it and/or modify it under the
2
- # terms of the new BSD License.
1
+ # This code is free software; you can redistribute it and/or modify it under
2
+ # the terms of the new BSD License.
3
3
  #
4
- # Copyright (c) 2009, Sebastian Staudt
4
+ # Copyright (c) 2009-2010, Sebastian Staudt
5
5
 
6
6
  require 'test_helper'
7
7
 
@@ -23,6 +23,28 @@ class ThrobberTests < Test::Unit::TestCase
23
23
  assert_equal '-\|/', Throbber.const_get(:SPINNER)
24
24
  end
25
25
 
26
+ should 'work correctly' do
27
+ ostream = StringIO.new
28
+ started_at = Time.now
29
+ finished_at = nil
30
+ thread = Thread.new do
31
+ sleep 1
32
+ finished_at = Time.now
33
+ end
34
+ throbber = Throbber.new(ostream, thread)
35
+ thread.join
36
+ throbber.join
37
+
38
+ spinner = Throbber.const_get(:SPINNER)
39
+ check_throbber = ' '
40
+ ((finished_at - started_at) / 0.25).floor.times do |char_index|
41
+ check_throbber << "\b"
42
+ check_throbber << spinner[char_index % 4]
43
+ end
44
+ check_throbber << "\b"
45
+ assert_equal check_throbber, ostream.string
46
+ end
47
+
26
48
  end
27
49
 
28
50
  end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubikon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Sebastian Staudt
@@ -9,10 +15,37 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-11-09 00:00:00 +01:00
18
+ date: 2010-09-26 00:00:00 +02:00
13
19
  default_executable:
14
- dependencies: []
15
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: jeweler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: yard
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
16
49
  description: A simple to use, yet powerful Ruby framework for building console-based applications.
17
50
  email: koraktor@gmail.com
18
51
  executables: []
@@ -26,57 +59,69 @@ files:
26
59
  - LICENSE
27
60
  - README.md
28
61
  - Rakefile
29
- - VERSION.yml
62
+ - lib/core_ext/string.rb
30
63
  - lib/rubikon.rb
31
- - lib/rubikon/action.rb
32
64
  - lib/rubikon/application/base.rb
33
65
  - lib/rubikon/application/class_methods.rb
66
+ - lib/rubikon/application/dsl_methods.rb
34
67
  - lib/rubikon/application/instance_methods.rb
68
+ - lib/rubikon/command.rb
35
69
  - lib/rubikon/exceptions.rb
70
+ - lib/rubikon/flag.rb
71
+ - lib/rubikon/option.rb
72
+ - lib/rubikon/parameter.rb
36
73
  - lib/rubikon/progress_bar.rb
37
74
  - lib/rubikon/throbber.rb
38
- - test/action_tests.rb
39
75
  - test/application_tests.rb
76
+ - test/command_tests.rb
77
+ - test/commands/external_command.rb
78
+ - test/flag_tests.rb
79
+ - test/option_tests.rb
40
80
  - test/progress_bar_tests.rb
41
81
  - test/test_helper.rb
42
- - test/testapp.rb
82
+ - test/testapps.rb
43
83
  - test/throbber_tests.rb
44
- has_rdoc: true
84
+ has_rdoc: false
45
85
  homepage: http://koraktor.github.com/rubikon
46
86
  licenses: []
47
87
 
48
88
  post_install_message:
49
89
  rdoc_options:
50
- - --all
51
- - --inline-source
52
- - --line-numbers
53
- - --charset=utf-8
54
- - --webcvs=http://github.com/koraktor/rubikon/blob/master/%s
90
+ - --charset=UTF-8
55
91
  require_paths:
56
92
  - lib
57
93
  required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
58
95
  requirements:
59
96
  - - ">="
60
97
  - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
61
101
  version: "0"
62
- version:
63
102
  required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
64
104
  requirements:
65
105
  - - ">="
66
106
  - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
67
110
  version: "0"
68
- version:
69
111
  requirements: []
70
112
 
71
113
  rubyforge_project: rubikon
72
- rubygems_version: 1.3.5
114
+ rubygems_version: 1.3.7
73
115
  signing_key:
74
116
  specification_version: 3
75
117
  summary: Rubikon - A Ruby console app framework
76
118
  test_files:
77
- - test/action_tests.rb
78
119
  - test/application_tests.rb
120
+ - test/command_tests.rb
121
+ - test/commands/external_command.rb
122
+ - test/flag_tests.rb
123
+ - test/option_tests.rb
79
124
  - test/progress_bar_tests.rb
80
- - test/testapp.rb
81
125
  - test/test_helper.rb
126
+ - test/testapps.rb
82
127
  - test/throbber_tests.rb
data/VERSION.yml DELETED
@@ -1,4 +0,0 @@
1
- ---
2
- :major: 0
3
- :minor: 2
4
- :patch: 1
@@ -1,74 +0,0 @@
1
- # This code is free software; you can redistribute it and/or modify it under the
2
- # terms of the new BSD License.
3
- #
4
- # Copyright (c) 2009, Sebastian Staudt
5
-
6
- module Rubikon
7
-
8
- # Instances of the Action class are used to define the real code that should
9
- # be executed when running the application.
10
- class Action
11
-
12
- attr_reader :block, :description, :name, :param_type
13
-
14
- # Create a new Action using the given name, options and code block.
15
- #
16
- # +name+:: The name of this Action, used in Application options
17
- # +options+:: A Hash of options that define more details about the Action
18
- # +block+:: The code block which should be executed by this Action
19
- #
20
- # Options:
21
- # +description+:: A description of the action. This isn't used at the
22
- # moment.
23
- # +param_type+:: A single Class or a Array of classes that represent the
24
- # type(s) of argument(s) this action expects
25
- def initialize(options = {}, &block)
26
- raise BlockMissingError unless block_given?
27
-
28
- @description = options[:description] || ''
29
- @param_type = options[:param_type] || Object
30
-
31
- @block = block
32
- @arg_count = block.arity
33
- end
34
-
35
- # Run this action's code block
36
- #
37
- # +args+:: The argument which should be relayed to the block of this Action
38
- def run(*args)
39
- raise MissingArgumentError unless check_argument_count(args.size)
40
- raise Rubikon::ArgumentTypeError unless check_argument_types(args)
41
- @block[*args]
42
- end
43
-
44
- private
45
-
46
- # Checks if the number of arguments given fits the number of arguments of
47
- # this Action
48
- #
49
- # +count+:: The number of arguments
50
- def check_argument_count(count)
51
- !((@arg_count >= 0 && count < @arg_count) || (@arg_count < 0 && count < -@arg_count - 1))
52
- end
53
-
54
- # Checks the types of the given arguments using the Class or Array of
55
- # classes given in the +:param_type+ option of this action.
56
- #
57
- # +args+:: The arguments which should be checked
58
- def check_argument_types(args)
59
- if @param_type.is_a? Array
60
- args.each_index do |arg_index|
61
- return false unless args[arg_index].is_a? @param_type[arg_index]
62
- end
63
- else
64
- args.each do |arg|
65
- return false unless arg.is_a? @param_type
66
- end
67
- end
68
-
69
- true
70
- end
71
-
72
- end
73
-
74
- end
data/test/action_tests.rb DELETED
@@ -1,36 +0,0 @@
1
- # This code is free software; you can redistribute it and/or modify it under the
2
- # terms of the new BSD License.
3
- #
4
- # Copyright (c) 2009, Sebastian Staudt
5
-
6
- require 'test_helper'
7
-
8
- class ActionTests < Test::Unit::TestCase
9
-
10
- context 'A Rubikon action' do
11
-
12
- should 'throw an exception when no code block is given' do
13
- assert_raise BlockMissingError do
14
- Action.new
15
- end
16
- assert_raise BlockMissingError do
17
- options = {}
18
- Action.new options
19
- end
20
- end
21
-
22
- should 'not raise an exception when created without options' do
23
- action_options = {
24
- :description => 'this is an action',
25
- :param_type => String
26
- }
27
- assert_nothing_raised do
28
- action = Action.new action_options do end
29
- assert_equal action_options[:description], action.description
30
- assert_equal action_options[:param_type], action.param_type
31
- end
32
- end
33
-
34
- end
35
-
36
- end
data/test/testapp.rb DELETED
@@ -1,64 +0,0 @@
1
- # This code is free software; you can redistribute it and/or modify it under the
2
- # terms of the new BSD License.
3
- #
4
- # Copyright (c) 2009, Sebastian Staudt
5
-
6
- class RubikonTestApp < Application::Base
7
-
8
- set :autorun, false
9
- set :name, 'Rubikon test application'
10
- set :raise_errors, true
11
-
12
- default do
13
- 'default action'
14
- end
15
-
16
- action 'input' do
17
- input 'input'
18
- end
19
-
20
- action_alias :alias_before, :object_id
21
-
22
- action 'object_id' do
23
- object_id
24
- end
25
-
26
- action_alias :alias_after, :object_id
27
-
28
- action 'noarg' do
29
- 'noarg action'
30
- end
31
-
32
- action 'realnoarg' do ||
33
- end
34
-
35
- action 'noarg2' do
36
- end
37
-
38
- action 'number_string', :param_type => [Numeric, String] do |s,n|
39
- end
40
-
41
- action 'output', :param_type => String do |s|
42
- puts s
43
- put s
44
- putc s[0]
45
- end
46
-
47
- action 'required' do |what|
48
- "required argument was #{what}"
49
- end
50
-
51
- action 'throbber' do |*output|
52
- throbber do
53
- if output[0]
54
- sleep 0.5
55
- puts 'don\'t'
56
- sleep 0.5
57
- puts 'break'
58
- else
59
- sleep 1
60
- end
61
- end
62
- end
63
-
64
- end