benry-cmdapp 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,191 @@
1
+ # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
3
+
4
+
5
+ require_relative 'shared'
6
+
7
+
8
+ Oktest.scope do
9
+
10
+
11
+ topic Benry::CmdApp::BaseMetadata do
12
+
13
+ end
14
+
15
+
16
+ topic Benry::CmdApp::ActionMetadata do
17
+
18
+ topic '#hidden?()' do
19
+
20
+ spec "[!stied] returns true/false if `hidden:` kwarg provided." do
21
+ args = ["hello", "Hello", nil, MyAction, :hello]
22
+ md = Benry::CmdApp::ActionMetadata.new(*args, hidden: true)
23
+ ok {md.hidden?} == true
24
+ md = Benry::CmdApp::ActionMetadata.new(*args, hidden: false)
25
+ ok {md.hidden?} == false
26
+ end
27
+
28
+ spec "[!eumhz] returns true/false if method is private or not." do
29
+ args = ["hello", "Hello", nil, MyAction, :hello]
30
+ md = Benry::CmdApp::ActionMetadata.new(*args)
31
+ ok {md.hidden?} == false
32
+ MyAction.class_eval { private :hello }
33
+ at_end { MyAction.class_eval { public :hello } }
34
+ ok {md.hidden?} == true
35
+ end
36
+
37
+ end
38
+
39
+
40
+ topic '#option_empty?()' do
41
+
42
+ spec "[!14xgg] returns true if the action has no options." do
43
+ schema = Benry::CmdApp::ACTION_OPTION_SCHEMA_CLASS.new
44
+ args = ["hello", "Hello", schema, MyAction, :hello]
45
+ md = Benry::CmdApp::ActionMetadata.new(*args)
46
+ ok {md.option_empty?} == true
47
+ end
48
+
49
+ spec "[!dbtht] returns false if the action has at least one option." do
50
+ schema = Benry::CmdApp::ACTION_OPTION_SCHEMA_CLASS.new
51
+ schema.add(:verbose, "-v", "verbose")
52
+ args = ["hello", "Hello", schema, MyAction, :hello]
53
+ md = Benry::CmdApp::ActionMetadata.new(*args)
54
+ ok {md.option_empty?} == false
55
+ end
56
+
57
+ spec "[!wa315] considers hidden options if `all: true` passed." do
58
+ schema = Benry::CmdApp::ACTION_OPTION_SCHEMA_CLASS.new
59
+ schema.add(:debug, "-D", "debug", hidden: true)
60
+ args = ["hello", "Hello", schema, MyAction, :hello]
61
+ md = Benry::CmdApp::ActionMetadata.new(*args)
62
+ ok {md.option_empty?(all: false)} == true
63
+ ok {md.option_empty?(all: true)} == false
64
+ end
65
+
66
+ end
67
+
68
+
69
+ topic '#option_help()' do
70
+
71
+ before do
72
+ schema = Benry::CmdApp::ACTION_OPTION_SCHEMA_CLASS.new
73
+ schema.add(:lang, "-l <lang>", "language")
74
+ schema.add(:color, "--color[=<on|off>]", "color mode", type: TrueClass)
75
+ schema.add(:debug, "--debug", "debug mode", hidden: true)
76
+ args = ["hello", "Hello", schema, MyAction, :hello]
77
+ @metadata = Benry::CmdApp::ActionMetadata.new(*args)
78
+ end
79
+
80
+ spec "[!bpkwn] returns help message string of the action." do
81
+ ok {@metadata.option_help(" %-15s : %s")} == <<"END"
82
+ -l <lang> : language
83
+ --color[=<on|off>] : color mode
84
+ END
85
+ end
86
+
87
+ spec "[!76hni] includes hidden options in help message if `all:` is truthy." do
88
+ ok {@metadata.option_help(" %-15s : %s", all: true)} == <<"END"
89
+ -l <lang> : language
90
+ --color[=<on|off>] : color mode
91
+ --debug : debug mode
92
+ END
93
+ end
94
+
95
+ end
96
+
97
+
98
+ topic '#parse_options()' do
99
+
100
+ before do
101
+ schema = Benry::CmdApp::ACTION_OPTION_SCHEMA_CLASS.new
102
+ schema.add(:lang, "-l <lang>", "language")
103
+ schema.add(:color, "--color[=<on|off>]", "color mode", type: TrueClass)
104
+ schema.add(:debug, "--debug", "debug mode", hidden: true)
105
+ args = ["hello", "Hello", schema, MyAction, :hello]
106
+ @metadata = Benry::CmdApp::ActionMetadata.new(*args)
107
+ end
108
+
109
+ spec "[!gilca] returns parsed options." do
110
+ args = ["-len", "--color=on", "Alice"]
111
+ opts = @metadata.parse_options(args)
112
+ ok {opts} == {:lang=>"en", :color=>true}
113
+ ok {args} == ["Alice"]
114
+ end
115
+
116
+ spec "[!v34yk] raises OptionError if option has error." do
117
+ args = ["--lang=en", "--color=on", "Alice"]
118
+ pr = proc { @metadata.parse_options(args) }
119
+ ok {pr}.raise?(Benry::CmdApp::OptionError,
120
+ "--lang=en: Unknown long option.")
121
+ end
122
+
123
+ end
124
+
125
+
126
+ topic '#alias?()' do
127
+
128
+ spec "[!c1eq3] returns false which means that this is not an alias metadata." do
129
+ schema = Benry::CmdApp::ACTION_OPTION_SCHEMA_CLASS.new
130
+ args = ["hello", "Hello", schema, MyAction, :hello]
131
+ metadata = Benry::CmdApp::ActionMetadata.new(*args)
132
+ ok {metadata.alias?} == false
133
+ end
134
+
135
+ end
136
+
137
+
138
+ end
139
+
140
+
141
+ topic Benry::CmdApp::AliasMetadata do
142
+
143
+
144
+ topic '#initialize()' do
145
+
146
+ spec "[!qtb61] sets description string automatically." do
147
+ metadata = Benry::CmdApp::AliasMetadata.new("a9344", "hello", nil)
148
+ ok {metadata.desc} == "alias for 'hello'"
149
+ end
150
+
151
+ spec "[!kgic6] includes args value into description if provided." do
152
+ metadata = Benry::CmdApp::AliasMetadata.new("a1312", "hello", ["aa", "bb"])
153
+ ok {metadata.desc} == "alias for 'hello aa bb'"
154
+ end
155
+
156
+ end
157
+
158
+
159
+ topic '#alias?()' do
160
+
161
+ spec "[!c798o] returns true which means that this is an alias metadata." do
162
+ metadata = Benry::CmdApp::AliasMetadata.new("a2041", "hello", nil)
163
+ ok {metadata.alias?} == true
164
+ end
165
+
166
+ end
167
+
168
+
169
+ topic '#name_with_args()' do
170
+
171
+ spec "[!6kjuv] returns alias name if no args." do
172
+ metadata = Benry::CmdApp::AliasMetadata.new("a1549", "hello", nil)
173
+ ok {metadata.name_with_args} == "a1549"
174
+ metadata = Benry::CmdApp::AliasMetadata.new("a1549", "hello", [])
175
+ ok {metadata.name_with_args} == "a1549"
176
+ end
177
+
178
+ spec "[!d4xrb] returns alias name and args as combined." do
179
+ metadata = Benry::CmdApp::AliasMetadata.new("a1549", "hello", ["x"])
180
+ ok {metadata.name_with_args} == "a1549 (with 'x')"
181
+ metadata = Benry::CmdApp::AliasMetadata.new("a1549", "hello", ["x", "y"])
182
+ ok {metadata.name_with_args} == "a1549 (with 'x y')"
183
+ end
184
+
185
+ end
186
+
187
+
188
+ end
189
+
190
+
191
+ end
data/test/misc_test.rb ADDED
@@ -0,0 +1,175 @@
1
+ # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
3
+
4
+
5
+ require_relative 'shared'
6
+
7
+
8
+ Oktest.scope do
9
+
10
+
11
+ topic Benry::CmdApp::BaseError do
12
+
13
+
14
+ topic '#should_report_backtrace?()' do
15
+
16
+ spec "[!oj9x3] returns true in base exception class to report backtrace." do
17
+ exc = Benry::CmdApp::BaseError.new("")
18
+ ok {exc.should_report_backtrace?()} == true
19
+ end
20
+
21
+ end
22
+
23
+
24
+ end
25
+
26
+
27
+ topic Benry::CmdApp::OptionError do
28
+
29
+
30
+ topic '#should_report_backtrace?()' do
31
+
32
+ spec "[!6qvnc] returns false in OptionError class because no need to report backtrace." do
33
+ exc = Benry::CmdApp::OptionError.new("")
34
+ ok {exc.should_report_backtrace?()} == false
35
+ end
36
+
37
+ end
38
+
39
+
40
+ end
41
+
42
+
43
+ topic Benry::CmdApp::CommandError do
44
+
45
+
46
+ topic '#should_report_backtrace?()' do
47
+
48
+ spec "[!o9xu2] returns false in ComamndError class because no need to report backtrace." do
49
+ exc = Benry::CmdApp::CommandError.new("")
50
+ ok {exc.should_report_backtrace?()} == false
51
+ end
52
+
53
+ end
54
+
55
+
56
+ end
57
+
58
+
59
+ topic Benry::CmdApp::ActionOptionSchema do
60
+
61
+
62
+ topic '#initialize()' do
63
+
64
+ end
65
+
66
+ end
67
+
68
+
69
+ topic Benry::CmdApp::OptionParser do
70
+
71
+
72
+ topic '#parse()' do
73
+
74
+ spec "[!iaawe] raises OptionError if option error found." do
75
+ schema = Benry::CmdApp::ActionOptionSchema.new()
76
+ schema.add(:help, "-h, --help", "help message")
77
+ parser = Benry::CmdApp::OptionParser.new(schema)
78
+ pr = proc { parser.parse(["-x", "foo"]) }
79
+ ok {pr}.raise?(Benry::CmdApp::OptionError,
80
+ "-x: Unknown option.")
81
+ end
82
+
83
+ end
84
+
85
+ end
86
+
87
+
88
+ topic Benry::CmdApp::OptionSet do
89
+
90
+ before do
91
+ @optset = Benry::CmdApp::OptionSet.new
92
+ @schema = Benry::CmdApp::OptionSchema.new
93
+ @schema.add(:user , "-u, --user=<user>" , "user name")
94
+ @schema.add(:email, "-e, --email=<email>", "email address")
95
+ @schema.add(:port , "-p, --port=<number>", "port number", type: Integer)
96
+ end
97
+
98
+
99
+ topic '#copy_from()' do
100
+
101
+ spec "[!d9udc] copy option items from schema." do
102
+ items = []
103
+ @schema.each {|item| items << item }
104
+ #
105
+ @optset.copy_from(@schema)
106
+ new_items = @optset.instance_variable_get(:@items)
107
+ ok {new_items}.length(3)
108
+ ok {new_items[0]} == items[0]
109
+ ok {new_items[1]} == items[1]
110
+ ok {new_items[2]} == items[2]
111
+ end
112
+
113
+ spec "[!v1ok3] returns self." do
114
+ ok {@optset.copy_from(@schema)}.same?(@optset)
115
+ end
116
+
117
+ end
118
+
119
+
120
+ topic '#copy_into()' do
121
+
122
+ spec "[!n00r1] copy option items into schema." do
123
+ @optset.copy_from(@schema)
124
+ new_schema = Benry::CmdApp::OptionSchema.new
125
+ @optset.copy_into(new_schema)
126
+ new_items = []
127
+ new_schema.each {|item| new_items << item }
128
+ items = @optset.instance_variable_get(:@items)
129
+ ok {new_items}.length(3)
130
+ ok {new_items[0]} == items[0]
131
+ ok {new_items[1]} == items[1]
132
+ ok {new_items[2]} == items[2]
133
+ end
134
+
135
+ spec "[!ynn1m] returns self." do
136
+ @optset.copy_from(@schema)
137
+ new_schema = Benry::CmdApp::OptionSchema.new
138
+ ok {@optset.copy_into(new_schema)}.same?(@optset)
139
+ end
140
+
141
+ end
142
+
143
+
144
+ topic '#select()' do
145
+
146
+ spec "[!mqkzf] creates new OptionSet object with filtered options." do
147
+ @optset.copy_from(@schema)
148
+ newone = @optset.select(:user, :email)
149
+ ok {newone}.is_a?(Benry::CmdApp::OptionSet)
150
+ items = newone.instance_variable_get(:@items)
151
+ ok {items}.length(2)
152
+ ok {items.collect(&:key).sort} == [:email, :user]
153
+ end
154
+
155
+ end
156
+
157
+
158
+ topic '#exclude()' do
159
+
160
+ spec "[!oey0q] creates new OptionSet object with remained options." do
161
+ @optset.copy_from(@schema)
162
+ newone = @optset.exclude(:user, :email)
163
+ ok {newone}.is_a?(Benry::CmdApp::OptionSet)
164
+ items = newone.instance_variable_get(:@items)
165
+ ok {items}.length(1)
166
+ ok {items.collect(&:key).sort} == [:port]
167
+ end
168
+
169
+ end
170
+
171
+
172
+ end
173
+
174
+
175
+ end