caty 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ require 'rubygems'
2
+ require 'bacon'
3
+ require 'facon'
4
+ require 'caty'
5
+
6
+ describe 'An Option' do
7
+
8
+ it 'should return a default value' do
9
+ args = %w{bar be cue sauce}
10
+
11
+ %w{boolean string integer}.each do |type|
12
+ o = Caty::Option.new('beer', type.to_sym)
13
+ o.grep!(args).should.be.nil
14
+ args.length.should.be.equal 4
15
+ end
16
+
17
+ [true, 'coke', 42].each do |default|
18
+ o = Caty::Option.new('beer', default)
19
+ o.grep!(args).should.be.equal default
20
+ args.length.should.be.equal 4
21
+ end
22
+ end
23
+
24
+ it 'should complain on bad default value type' do
25
+ lambda {
26
+ Caty::Option.new('beer', Object.new)
27
+ }.should.raise ArgumentError
28
+ end
29
+
30
+ it 'should grep boolean values' do
31
+ %w{-beer=true -beer -beer=1}.each do |option|
32
+ args = [option]
33
+ o = Caty::Option.new('beer', :boolean)
34
+ o.grep!(args).should.be.true
35
+ args.should.be.empty
36
+ end
37
+
38
+ %w{-beer=false -beer=0}.each do |option|
39
+ args = [option]
40
+ o = Caty::Option.new('beer', :boolean)
41
+ o.grep!(args).should.be.false
42
+ args.should.be.empty
43
+ end
44
+
45
+ lambda {
46
+ o = Caty::Option.new('beer', :boolean)
47
+ o.grep!(%w{-beer=shallow})
48
+ }.should.raise Caty::OptionArgumentError
49
+ end
50
+
51
+ it 'should grep string values' do
52
+ args = %w{-beer=fine}
53
+ o = Caty::Option.new('beer', :string)
54
+ o.grep!(args).should.be.equal 'fine'
55
+ args.should.be.empty
56
+
57
+ lambda {
58
+ o = Caty::Option.new('beer', :string)
59
+ o.grep!(%w{-beer})
60
+ }.should.raise Caty::OptionArgumentError
61
+ end
62
+
63
+ it 'should grep integer values' do
64
+ args = %w{-beer=42}
65
+ o = Caty::Option.new('beer', :integer)
66
+ o.grep!(args).should.be.equal 42
67
+ args.should.be.empty
68
+
69
+ args = %w{-beer=-23}
70
+ o = Caty::Option.new('beer', :integer)
71
+ o.grep!(args).should.be.equal -23
72
+ args.should.be.empty
73
+
74
+ lambda {
75
+ o = Caty::Option.new('beer', :integer)
76
+ o.grep!(%w{-beer})
77
+ }.should.raise Caty::OptionArgumentError
78
+ end
79
+
80
+ end
81
+
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'bacon'
3
+ require 'facon'
4
+ require 'caty'
5
+
6
+ describe 'An OptionArray' do
7
+
8
+ it 'should grep' do
9
+ a = Caty::OptionArray.new
10
+ mock1 = mock('beer', :name => 'beer')
11
+ mock2 = mock('rootbeer', :name => 'rootbeer')
12
+
13
+ mock1.should.receive(:grep!).with([]).and_return(42)
14
+ mock2.should.receive(:grep!).with([]).and_return(23)
15
+
16
+ a << mock1
17
+ a << mock2
18
+
19
+ opts = a.grep!([])
20
+ opts.beer.should.equal 42
21
+ opts.rootbeer.should.equal 23
22
+ end
23
+
24
+ end
25
+
@@ -0,0 +1,72 @@
1
+ require 'bacon'
2
+ require 'caty'
3
+
4
+ class TestCaty < Caty
5
+
6
+ global_options do
7
+ boolean 'booleanoption', false
8
+ string 'stringoption', 'uiae'
9
+ integer 'integeroption', 42
10
+ end
11
+
12
+ default :task1
13
+
14
+ def task1
15
+ end
16
+
17
+ map 'mappedtask' => :task2
18
+ task_options 'stringoption' => :string,
19
+ 'integeroption' => :integer,
20
+ 'booleanoption' => :boolean
21
+ def task2
22
+ end
23
+
24
+ class << self
25
+ attr_accessor :tasks, :global_options, :default
26
+ end
27
+
28
+ end
29
+
30
+ describe 'Caty' do
31
+
32
+ it 'should have tasks and mappings' do
33
+ tasks = TestCaty.tasks
34
+ tasks.length.should.be.equal 3
35
+
36
+ tasks[:task1].should.not.be.nil
37
+ tasks[:task1].name.should.be.equal :task1
38
+
39
+ tasks[:task2].should.not.be.nil
40
+ tasks[:task2].name.should.be.equal :task2
41
+
42
+ tasks[:mappedtask].should.not.be.nil
43
+ tasks[:mappedtask].target.should.be.equal :task2
44
+
45
+ TestCaty.default.should.be.equal :task1
46
+ end
47
+
48
+ it 'should have global options' do
49
+ gopts = TestCaty.global_options
50
+ gopts.length.should.be.equal 3
51
+ gopts.sort_by(&:name)
52
+
53
+ gopts[0].name.should.be.equal 'booleanoption'
54
+ gopts[1].name.should.be.equal 'stringoption'
55
+ gopts[2].name.should.be.equal 'integeroption'
56
+ end
57
+
58
+ it 'should have task options' do
59
+ opts = TestCaty.tasks[:task2].instance_variable_get(:@options)
60
+ opts.should.not.be.nil
61
+ opts.length.should.be.equal 3
62
+ opts.sort! do |a,b|
63
+ a.name.to_s <=> b.name.to_s
64
+ end
65
+
66
+ opts[0].name.should.be.equal :booleanoption
67
+ opts[1].name.should.be.equal :integeroption
68
+ opts[2].name.should.be.equal :stringoption
69
+ end
70
+
71
+ end
72
+
data/test/test_task.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'bacon'
3
+ require 'facon'
4
+ require 'caty'
5
+
6
+ describe 'A Task' do
7
+
8
+ it 'should call its associated method and pass the arguments' do
9
+ context = mock('context')
10
+ meth = mock('method')
11
+ meth.should.receive(:call).with('beer')
12
+ meth.should.receive(:bind).with(context).and_return(meth)
13
+
14
+ t = Caty::Task.new('brew_beer', meth, mock('options', :grep! => nil))
15
+ t.parse!(%w{beer})
16
+ t.execute(context)
17
+ end
18
+
19
+ it 'should let its options grep' do
20
+ options = mock('options')
21
+ options.should.receive(:grep!).with(%w{beer}).and_return(options)
22
+
23
+ t = Caty::Task.new('brew_beer', nil, options)
24
+ t.parse!(%w{beer}).should.equal options
25
+ end
26
+
27
+ end
28
+
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: caty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fabian Streitel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-08 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ohash
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Caty is a command line parser that maps arguments to instance methods
26
+ email: karottenreibe@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE.txt
33
+ - README.markdown
34
+ files:
35
+ - HISTORY.markdown
36
+ - LICENSE.txt
37
+ - README.markdown
38
+ - Rakefile
39
+ - VERSION
40
+ - caty.gemspec
41
+ - lib/caty.rb
42
+ - lib/caty/converters.rb
43
+ - lib/caty/errors.rb
44
+ - lib/caty/global_option.rb
45
+ - lib/caty/has_description.rb
46
+ - lib/caty/help_system.rb
47
+ - lib/caty/helpers.rb
48
+ - lib/caty/indirection.rb
49
+ - lib/caty/option.rb
50
+ - lib/caty/option_array.rb
51
+ - lib/caty/option_constructor.rb
52
+ - lib/caty/task.rb
53
+ - lib/caty/task_hash.rb
54
+ - test/kitty.rb
55
+ - test/test_caty.rb
56
+ - test/test_option.rb
57
+ - test/test_option_array.rb
58
+ - test/test_structure.rb
59
+ - test/test_task.rb
60
+ has_rdoc: true
61
+ homepage: http://github.com/karottenreibe/caty
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project: k-gems
84
+ rubygems_version: 1.3.4
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Caty is a command line parser that maps arguments to instance methods
88
+ test_files:
89
+ - test/kitty.rb
90
+ - test/test_caty.rb
91
+ - test/test_option.rb
92
+ - test/test_option_array.rb
93
+ - test/test_structure.rb
94
+ - test/test_task.rb