pablo 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ ---
2
+ :program: program
3
+ :version: version
4
+ :license: license
5
+ :desc: desc
6
+ :longdesc: longdesc
7
+ :usage: usage
8
+ :expand: true
9
+ :command_consumes_all: false
10
+ :capabilities:
11
+ - capa
12
+ - bili
13
+ - ties
14
+ :parsers:
15
+ - :command:
16
+ - :foo
17
+ - :foofoo
18
+ :consume_all: false
19
+ :longdesc: longdesc
20
+ :desc: desc
21
+ :usage: usage
22
+ - :option:
23
+ - :bar
24
+ :consume:
25
+ - one
26
+ - two
27
+ - :option:
28
+ - :nobar
29
+ :negates: :bar
30
+ - :token:
31
+ - :file?
32
+ - !ruby/regexp /goo/
33
+ :consume: 1
34
+ :default: moose
35
+ - :help_command: true
36
+ :desc: desc
37
+ :nothing: nothing
@@ -0,0 +1,13 @@
1
+ ---
2
+ :help: " test 0.1\n\n test [usage]\n\n Commands:\n foo \n help : Displays this message.\n bar \n\n Options:\n --bla \n --nobla\n\n Tokens:\n blubb : Nothing.\n"
3
+ :color_help: " $test 0.1$\n\n +test+ _[usage]_\n\n %Commands:%\n +foo +\n +help + : Displays this message.\n +bar +\n\n %Options:%\n +--bla +\n +--nobla+\n\n %Tokens:%\n +blubb + : Nothing.\n"
4
+ :command_help: " test 0.1\n\n help [<command>]\n\n Shows a simple help message for this program.\n If a command is given as well, a more detailed message about that\n particular command is shown (such as this one).\n"
5
+ :help_expand: "\n foobar\n\n"
6
+ :unknown: "Sorry, but I don't know 'luugu'.\nTry running 'help' without parameters to get a list of all the commands, options etc.\n"
7
+ :version: "test 0.1\n+woodle-doodle -foobar\n"
8
+ :missing_argument: "Missing argument for 'foo'.\n"
9
+ :missing_argument_2: "Missing argument 'bar' for 'foo'.\n"
10
+ :wrong_argument: "Wrong argument 'bar' for 'foo'.\n"
11
+ :wrong_argument_2: "Wrong argument 'bar' for 'foo'. Expected regular expression.\n"
12
+ :ambiguity_command: "Ambiguous argument 'foo'. Could be any of:\n * foobar\n * foogoo\n"
13
+ :colorize_text: "foo+bar+foo foo + bar + foo"
@@ -0,0 +1,47 @@
1
+ ###### DON'T PANIC License 1.1 ###########
2
+ #
3
+ # Don't panic, this piece of software is
4
+ # free, i.e. you can do with it whatever
5
+ # you like, including, but not limited to:
6
+ #
7
+ # * using it
8
+ # * copying it
9
+ # * (re)distributing it
10
+ # * burning/burying/shredding it
11
+ # * eating it
12
+ # * using it to obtain world domination
13
+ # * and ignoring it
14
+ #
15
+ # Under the sole condition that you
16
+ #
17
+ # * CONSIDER buying the author a strong
18
+ # brownian motion producer, say a nice
19
+ # hot cup of tea, should you ever meet
20
+ # him in person.
21
+ #
22
+ ##########################################
23
+
24
+ require 'test/unit'
25
+ require 'yaml'
26
+
27
+ class Test::Unit::TestCase
28
+
29
+ def fixtures
30
+ self.class.instance_variable_get(:@fixtures)
31
+ end
32
+
33
+ class << self
34
+ def fixture file, format = :yml
35
+ @fixtures ||= Hash.new
36
+
37
+ open(File.join(File.dirname(__FILE__), 'fixtures', file.to_s + '.' + format.to_s)) { |f|
38
+ @fixtures[file] =
39
+ case format
40
+ when :yml then YAML::load(f)
41
+ else f.read()
42
+ end
43
+ }
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,56 @@
1
+ ###### DON'T PANIC License 1.1 ###########
2
+ #
3
+ # Don't panic, this piece of software is
4
+ # free, i.e. you can do with it whatever
5
+ # you like, including, but not limited to:
6
+ #
7
+ # * using it
8
+ # * copying it
9
+ # * (re)distributing it
10
+ # * burning/burying/shredding it
11
+ # * eating it
12
+ # * using it to obtain world domination
13
+ # * and ignoring it
14
+ #
15
+ # Under the sole condition that you
16
+ #
17
+ # * CONSIDER buying the author a strong
18
+ # brownian motion producer, say a nice
19
+ # hot cup of tea, should you ever meet
20
+ # him in person.
21
+ #
22
+ ##########################################
23
+
24
+ require "test/unit"
25
+ require "pablo"
26
+
27
+ class TestArguments < Test::Unit::TestCase
28
+
29
+ def test_arguments_consume
30
+ p = Pablo.new
31
+
32
+ p.command :foo do |args, pablo|
33
+ assert_equal 1, args.length
34
+ args.consume 0
35
+ assert_equal 0, args.length
36
+ end
37
+
38
+ p.parse %w{foo bar}
39
+ end
40
+
41
+ def test_consume_all
42
+ p = Pablo.new
43
+
44
+ p.command :foo do |args, pablo|
45
+ assert !args.consumed?
46
+ assert_equal 2, args.length
47
+ args.consume_all
48
+ assert_equal 0, args.length
49
+ assert args.consumed?
50
+ end
51
+
52
+ p.parse %w{foo bar baz}
53
+ end
54
+
55
+ end
56
+
@@ -0,0 +1,62 @@
1
+ ###### DON'T PANIC License 1.1 ###########
2
+ #
3
+ # Don't panic, this piece of software is
4
+ # free, i.e. you can do with it whatever
5
+ # you like, including, but not limited to:
6
+ #
7
+ # * using it
8
+ # * copying it
9
+ # * (re)distributing it
10
+ # * burning/burying/shredding it
11
+ # * eating it
12
+ # * using it to obtain world domination
13
+ # * and ignoring it
14
+ #
15
+ # Under the sole condition that you
16
+ #
17
+ # * CONSIDER buying the author a strong
18
+ # brownian motion producer, say a nice
19
+ # hot cup of tea, should you ever meet
20
+ # him in person.
21
+ #
22
+ ##########################################
23
+
24
+ require "test/unit"
25
+ require "pablo"
26
+
27
+ class TestExpansion < Test::Unit::TestCase
28
+
29
+ def test_expansion
30
+ p = Pablo.new :expand => true, :command_consumes_all => false
31
+
32
+ p.option :barbar
33
+ p.option :bazbaz
34
+ p.option :bazbass
35
+ p.command :foofoo
36
+
37
+ p.parse %w{foo --bar --baz}
38
+
39
+ assert p.commands[:foofoo]
40
+ assert p.options[:barbar]
41
+ assert_nil p.options[:bazbaz]
42
+ assert_nil p.options[:bazbass]
43
+ end
44
+
45
+ def test_ambiguity
46
+ worked = false
47
+ p = Pablo.new :expand => true
48
+
49
+ p.option :foobar
50
+ p.option :foogoo
51
+
52
+ p.ambiguity do |arg,names|
53
+ worked = true
54
+ assert_equal 'foo', arg
55
+ assert_equal ['foobar', 'foogoo'], names
56
+ end
57
+
58
+ p.parse %w{foo}
59
+ end
60
+
61
+ end
62
+
@@ -0,0 +1,202 @@
1
+ ###### DON'T PANIC License 1.1 ###########
2
+ #
3
+ # Don't panic, this piece of software is
4
+ # free, i.e. you can do with it whatever
5
+ # you like, including, but not limited to:
6
+ #
7
+ # * using it
8
+ # * copying it
9
+ # * (re)distributing it
10
+ # * burning/burying/shredding it
11
+ # * eating it
12
+ # * using it to obtain world domination
13
+ # * and ignoring it
14
+ #
15
+ # Under the sole condition that you
16
+ #
17
+ # * CONSIDER buying the author a strong
18
+ # brownian motion producer, say a nice
19
+ # hot cup of tea, should you ever meet
20
+ # him in person.
21
+ #
22
+ ##########################################
23
+
24
+ require "test/unit"
25
+ require "pablo"
26
+ require File.join File.dirname(__FILE__), 'help_fixtures'
27
+
28
+ class TestOutput < Test::Unit::TestCase
29
+
30
+ fixture :output
31
+ fixture :license, :txt
32
+
33
+ def setup
34
+ $stdout = $stderr = @io = StringIO.new
35
+ end
36
+
37
+ def teardown
38
+ $stdout = STDOUT
39
+ $stderr = STDERR
40
+ end
41
+
42
+ def test_help
43
+ p = Pablo.new :program => 'test', :version => '0.1', :usage => '[usage]'
44
+ p.command :foo
45
+ p.help_command
46
+ p.command :bar
47
+ p.option :bla
48
+ p.option :nobla, :negates => :bla
49
+ p.desc("Nothing.")
50
+ p.token :blubb
51
+
52
+ p.parse ['help']
53
+ assert_equal fixtures[:output][:help], @io.string
54
+ end
55
+
56
+ def test_command_help
57
+ p = Pablo.new :program => 'test', :version => '0.1', :usage => '[usage]'
58
+ p.command :foo
59
+ p.help_command
60
+ p.command :bar
61
+
62
+ p.parse ['help', 'help']
63
+ assert_equal fixtures[:output][:command_help], @io.string
64
+ end
65
+
66
+ def test_unknown_help
67
+ p = Pablo.new :program => 'test', :version => '0.1', :usage => '[usage]'
68
+ p.command :foo
69
+ p.help_command
70
+ p.command :bar
71
+
72
+ p.parse ['help', 'luugu']
73
+ assert_equal fixtures[:output][:unknown], @io.string
74
+ end
75
+
76
+ def test_version
77
+ p = Pablo.new :program => 'test', :version => '0.1', :usage => '[usage]',
78
+ :capabilities => %w{+woodle-doodle -foobar}
79
+ p.version_command
80
+
81
+ p.parse ['version']
82
+ assert_equal fixtures[:output][:version], @io.string
83
+ end
84
+
85
+ def test_license
86
+ p = Pablo.new :license => 'test/fixtures/license.txt'
87
+ p.license_command
88
+
89
+ p.parse ['license']
90
+ assert_equal fixtures[:license], @io.string
91
+ end
92
+
93
+ def test_colorize
94
+ p = Pablo.new :program => 'test', :version => '0.1', :usage => '[usage]'
95
+ p.colorizing do |str, type|
96
+ case type
97
+ when :em then "+#{str}+"
98
+ when :h1 then "$#{str}$"
99
+ when :h2 then "%#{str}%"
100
+ when :usage then "_#{str}_"
101
+ else str
102
+ end
103
+ end
104
+
105
+ p.command :foo
106
+ p.help_command
107
+ p.command :bar
108
+ p.option :bla
109
+ p.option :nobla, :negates => :bla
110
+ p.desc("Nothing.")
111
+ p.token :blubb
112
+
113
+ p.parse ['help']
114
+ assert_equal fixtures[:output][:color_help], @io.string
115
+
116
+ t = p.colorize("foo$bar$foo foo $ bar $ foo", :text)
117
+ assert_equal fixtures[:output][:colorize_text], t
118
+ end
119
+
120
+ def test_errors
121
+ p = Pablo.new
122
+ p.command :foo do
123
+ p.missing_argument
124
+ end
125
+
126
+ p.parse %w{foo}
127
+ assert_equal fixtures[:output][:missing_argument], @io.string
128
+ setup()
129
+
130
+ p = Pablo.new
131
+ p.command :foo do
132
+ p.missing_argument('bar')
133
+ end
134
+
135
+ p.parse %w{foo}
136
+ assert_equal fixtures[:output][:missing_argument_2], @io.string
137
+ setup()
138
+
139
+ p = Pablo.new
140
+ p.command :foo do
141
+ p.wrong_argument('bar')
142
+ end
143
+
144
+ p.parse %w{foo}
145
+ assert_equal fixtures[:output][:wrong_argument], @io.string
146
+ setup()
147
+
148
+ p = Pablo.new
149
+ p.command :foo do
150
+ p.wrong_argument('bar', 'regular expression')
151
+ end
152
+
153
+ p.parse %w{foo}
154
+ assert_equal fixtures[:output][:wrong_argument_2], @io.string
155
+ end
156
+
157
+ def test_help_expansion
158
+ catch :abort do
159
+ p = Pablo.new :expand => true
160
+
161
+ p.help_command
162
+ p.command :foobar
163
+
164
+ p.parse %w{help foo}
165
+ end
166
+
167
+ assert_equal fixtures[:output][:help_expand], @io.string
168
+ end
169
+
170
+ def test_help_command
171
+ [%w{help}, %w{help foo}].each do |arr|
172
+ worked = false
173
+
174
+ catch :abort do
175
+ p = Pablo.new
176
+
177
+ p.help_command do |args, pablo|
178
+ # arr.length because 'help' is already consumed
179
+ assert_equal arr.length, args.length
180
+ worked = true
181
+ throw :abort
182
+ end
183
+
184
+ p.parse arr
185
+ end
186
+
187
+ assert worked
188
+ end
189
+ end
190
+
191
+ def test_ambiguity_command
192
+ p = Pablo.new :expand => true
193
+ p.ambiguity_command
194
+ p.command :foobar
195
+ p.command :foogoo
196
+
197
+ p.parse %w{foo}
198
+ assert_equal fixtures[:output][:ambiguity_command], @io.string
199
+ end
200
+
201
+ end
202
+
@@ -0,0 +1,115 @@
1
+ ###### DON'T PANIC License 1.1 ###########
2
+ #
3
+ # Don't panic, this piece of software is
4
+ # free, i.e. you can do with it whatever
5
+ # you like, including, but not limited to:
6
+ #
7
+ # * using it
8
+ # * copying it
9
+ # * (re)distributing it
10
+ # * burning/burying/shredding it
11
+ # * eating it
12
+ # * using it to obtain world domination
13
+ # * and ignoring it
14
+ #
15
+ # Under the sole condition that you
16
+ #
17
+ # * CONSIDER buying the author a strong
18
+ # brownian motion producer, say a nice
19
+ # hot cup of tea, should you ever meet
20
+ # him in person.
21
+ #
22
+ ##########################################
23
+
24
+ require "test/unit"
25
+ require "pablo"
26
+ require File.join File.dirname(__FILE__), 'help_fixtures'
27
+
28
+ class TestPablo < Test::Unit::TestCase
29
+
30
+ fixture :load_yaml
31
+
32
+ def test_load_yaml
33
+ yml = fixtures[:load_yaml].to_yaml
34
+
35
+ p = Pablo.new
36
+
37
+ p.load_yaml yml
38
+
39
+ assert_equal({
40
+ :program => 'program',
41
+ :desc => 'desc',
42
+ :version => 'version',
43
+ :longdesc => 'longdesc',
44
+ :usage => 'usage',
45
+ :capabilities => %w{capa bili ties},
46
+ :command_consumes_all => false,
47
+ :expand => true,
48
+ :license => 'license'
49
+ }, p.instance_variable_get(:@opts))
50
+
51
+ reg = p.instance_variable_get(:@registered)
52
+
53
+ assert_equal 5, reg.length
54
+
55
+ assert_equal [:foo, :foofoo], reg[0].names
56
+ assert_equal 'desc', reg[0].desc
57
+ assert_equal 'longdesc', reg[0].longdesc
58
+ assert_equal 'usage', reg[0].usage
59
+ assert_equal({
60
+ :consume_all => false
61
+ }, reg[0].instance_variable_get(:@opts))
62
+
63
+ assert_equal [:bar], reg[1].names
64
+ assert_equal({
65
+ :consume => %w{one two}
66
+ }, reg[1].instance_variable_get(:@opts))
67
+
68
+ assert_equal [:nobar], reg[2].names
69
+ assert_equal({
70
+ :negates => :bar
71
+ }, reg[2].instance_variable_get(:@opts))
72
+
73
+ assert_equal [], reg[3].names
74
+ assert_equal [:file?, /goo/], reg[3].instance_variable_get(:@verify)
75
+ assert_equal({
76
+ :default => 'moose',
77
+ :consume => 1,
78
+ }, reg[3].instance_variable_get(:@opts))
79
+
80
+ assert_equal [:help], reg[4].names
81
+ assert_equal 'desc', reg[4].desc
82
+ assert_equal({
83
+ :consume_all => false,
84
+ :nothing => 'nothing'
85
+ }, reg[4].instance_variable_get(:@opts))
86
+ end
87
+
88
+ def foo args, pablo
89
+ @worked << :token
90
+ end
91
+
92
+ def bar args, pablo
93
+ @worked << :option
94
+ end
95
+
96
+ def goo args, pablo
97
+ @worked << :command
98
+ end
99
+
100
+ def test_run_object
101
+ p = Pablo.new :run => self
102
+
103
+ @worked = []
104
+
105
+ p.token :foo
106
+ p.option :bar
107
+ p.command :goo
108
+
109
+ p.parse %w{foo --bar goo}
110
+
111
+ assert_equal [:token, :option, :command], @worked
112
+ end
113
+
114
+ end
115
+