cmd-utils 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cmd-utils.rb +187 -0
- data/lib/lookup.rb +71 -0
- data/tests/test-cmd-utils.rb +206 -0
- data/tests/test-lookup.rb +55 -0
- metadata +51 -0
data/lib/cmd-utils.rb
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
# cmd-utils.rb -- simple utilities for ruby command line tools
|
2
|
+
#
|
3
|
+
# Alan K. Stebbens <aks@stebbens.org>
|
4
|
+
#
|
5
|
+
#
|
6
|
+
# require 'cmd-utils'
|
7
|
+
#
|
8
|
+
# Utilities for output, and running commands.
|
9
|
+
#
|
10
|
+
# The output and run methods rely on some external variables:
|
11
|
+
#
|
12
|
+
# $verbose -- causes certain commands to talk more
|
13
|
+
# $norun -- causes the "run" command to print its argument, but not actually run it.
|
14
|
+
# $quiet -- causes certain commands to talk less
|
15
|
+
#
|
16
|
+
# These routines provide conditional output. The arguments can be given as part of the
|
17
|
+
# the function calls, or, can be provided as the return value of a block. The advantage
|
18
|
+
# of using a block is that the block is not evaluated unless the conditions requiring
|
19
|
+
# output are met. So, if the expression to compute a value that _might_ be printed is
|
20
|
+
# expensive, do the computation inside a block.
|
21
|
+
#
|
22
|
+
##
|
23
|
+
# talk - Print msg on STDERR unless `$quiet` is set
|
24
|
+
#
|
25
|
+
# :call-seq:
|
26
|
+
# talk msg
|
27
|
+
# talk { msg }
|
28
|
+
# talkf fmt, args ...
|
29
|
+
# talkf fmt { [ args ... ] }
|
30
|
+
|
31
|
+
def talk msg=nil
|
32
|
+
if !$quiet && (msg || block_given?)
|
33
|
+
$stderr.puts(msg || yield)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def talkf fmt='%s', *args
|
38
|
+
args = yield if args.size == 0 && block_given?
|
39
|
+
talk { sprintf(fmt, *args) }
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# qtalk - Print msg on STDERR only if `$quiet` is set
|
44
|
+
#
|
45
|
+
# :call-seq:
|
46
|
+
# qtalk msg
|
47
|
+
# qtalk { msg }
|
48
|
+
# qtalkf fmt, args ..
|
49
|
+
# qtalkf fmt { [ args .. ] }
|
50
|
+
|
51
|
+
def qtalk msg=nil
|
52
|
+
if $quiet && (msg || block_given?)
|
53
|
+
$stderr.puts(msg || yield)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def qtalkf fmt='%s', *args
|
58
|
+
args = yield if args.size == 0 && block_given?
|
59
|
+
qtalk { sprintf(fmt, *args) }
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# vtalk -- Print msg on STDERR if `$verbose` is set
|
64
|
+
#
|
65
|
+
# :call-seq:
|
66
|
+
# vtalk msg
|
67
|
+
# vtalk { msg }
|
68
|
+
# vtalkf fmt, args ..
|
69
|
+
# vtalkf fmt { args .. }
|
70
|
+
|
71
|
+
def vtalk msg=nil
|
72
|
+
if $verbose && (msg || block_given?)
|
73
|
+
$stderr.puts(msg || yield)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def vtalkf fmt='%s', *args
|
78
|
+
args = yield if args.size == 0 && block_given?
|
79
|
+
vtalk { sprintf(fmt, *args) }
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# nvtalk -- Print msg on STDERR unless `$verbose` is set
|
84
|
+
#
|
85
|
+
# :call-seq:
|
86
|
+
# nvtalk msg
|
87
|
+
# nvtalk { msg }
|
88
|
+
|
89
|
+
def nvtalk msg=nil
|
90
|
+
unless $verbose && (msg || block_given?)
|
91
|
+
$stderr.puts(msg || yield)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def nvtalkf fmt='%s', *args
|
96
|
+
args = yield if args.size == 0 && block_given?
|
97
|
+
nvtalk { sprintf(fmt, *args) }
|
98
|
+
end
|
99
|
+
|
100
|
+
##
|
101
|
+
# nrtalk -- Print msg on STDERR only if `$norun` is set
|
102
|
+
#
|
103
|
+
# :call-seq:
|
104
|
+
# nrtalk msg
|
105
|
+
# nrtalk { msg }
|
106
|
+
# nrtalkf fmt, msg
|
107
|
+
# nrtalkf fmt { msg }
|
108
|
+
|
109
|
+
def nrtalk msg=nil
|
110
|
+
if $norun
|
111
|
+
$stderr.puts(msg || yield)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def nrtalkf *args
|
116
|
+
args = yield if args.size == 0 && block_given?
|
117
|
+
nrtalk { sprintf(*args) }
|
118
|
+
end
|
119
|
+
|
120
|
+
##
|
121
|
+
# error -- print an error message on STDERR, and then exit.
|
122
|
+
# :call-seq:
|
123
|
+
# error [code], msg
|
124
|
+
# errof [code], fmt, args
|
125
|
+
#
|
126
|
+
# Code defaults to 1 if not given.
|
127
|
+
|
128
|
+
def error *args
|
129
|
+
args = yield if args.size == 0 && block_given?
|
130
|
+
code = args.size > 0 && args[0].class == Fixnum ? args.shift : 1
|
131
|
+
$stderr.puts(*args)
|
132
|
+
$stderr.flush
|
133
|
+
exit code
|
134
|
+
end
|
135
|
+
|
136
|
+
def errorf *args
|
137
|
+
args = yield if args.size == 0 && block_given?
|
138
|
+
code = args.size > 0 && args[0].class == Fixnum ? args.shift : 1
|
139
|
+
$stderr.printf(*args)
|
140
|
+
$stderr.flush
|
141
|
+
exit code
|
142
|
+
end
|
143
|
+
|
144
|
+
##
|
145
|
+
# run -- run a command with support for testing, diagnostics and verbosity
|
146
|
+
# safe_run -- run a command with support for diagnostics and verbosity
|
147
|
+
#
|
148
|
+
# :call-seq:
|
149
|
+
# run cmd
|
150
|
+
# run { cmd }
|
151
|
+
# safe_run cmd
|
152
|
+
# safe_run { cmd }
|
153
|
+
#
|
154
|
+
# if `$norun` is set, print `(norun) ` followed by `cmd` on `STDERR`, and
|
155
|
+
# return.
|
156
|
+
#
|
157
|
+
# if `$verbose` is set, print `>> ` followed by `cmd` on `STDERR`.
|
158
|
+
#
|
159
|
+
# Invoke the `cmd` with the `system()` call.
|
160
|
+
#
|
161
|
+
# If there is an error, show the command (preceded by `>> `) if `$verbose` is
|
162
|
+
# not set, then show the error code.
|
163
|
+
#
|
164
|
+
# The `cmd` can be given either as an argument, or as the returned value from a
|
165
|
+
# block.
|
166
|
+
|
167
|
+
def run cmd=nil
|
168
|
+
cmd ||= block_given? && yield
|
169
|
+
if $norun
|
170
|
+
$stderr.printf "(norun) %s\n", cmd
|
171
|
+
else
|
172
|
+
safe_run cmd
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def safe_run cmd=nil
|
177
|
+
cmd ||= block_given? && yield
|
178
|
+
vtalkf { [ ">> %s\n", cmd ] }
|
179
|
+
system cmd
|
180
|
+
if $? > 0
|
181
|
+
qtalkf { [ ">> %s\n", cmd ] }
|
182
|
+
errorf $?, "Command failed with code %d!\n", $?
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
# end of cmd-utils.sh
|
187
|
+
# vim: set ai sw=2
|
data/lib/lookup.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# lookup.rb -- simple keyword lookup routine
|
2
|
+
#
|
3
|
+
# Alan K. Stebbens <aks@stebbens.org>
|
4
|
+
#
|
5
|
+
# require 'lookup'
|
6
|
+
#
|
7
|
+
# lookup - lookup a keyword in a list, in a case-insensitive, disambiguous way
|
8
|
+
#
|
9
|
+
# :call-seq:
|
10
|
+
# result = lookup list, key, err_notfound="%s not found", err_ambig="% is ambiguous"
|
11
|
+
# result = list.lookup( key, err_notfound, err_ambig )
|
12
|
+
# result = list.lookup( key, err_notfound )
|
13
|
+
# result = list.lookup( key )
|
14
|
+
#
|
15
|
+
# Lookup key in list, which can be an array or a hash. Return the one that
|
16
|
+
# matches exactly, or matches using case-insensitive, unambiguous matches, or
|
17
|
+
# raise a LookupError with a message.
|
18
|
+
#
|
19
|
+
# LookupError is a subclass of StandardError.
|
20
|
+
#
|
21
|
+
# LookupNotFoundError, a subclass of LookupError, is raised when a keyword is
|
22
|
+
# not found, and only if `err_notfound` is not nil.
|
23
|
+
#
|
24
|
+
# LookupAmbigError, a subsclass of LookupError, is raised when a keyword search
|
25
|
+
# matches multiple entries from the list, and only if `err_ambig` is not nil.
|
26
|
+
#
|
27
|
+
# If err_notfound is nil, do not raise a LookupNotFoundError error, and return
|
28
|
+
# nil.
|
29
|
+
#
|
30
|
+
# If err_ambigmsg is nil, do not raise a LookupAmbigError, and return the list
|
31
|
+
# of possible results.
|
32
|
+
|
33
|
+
class LookupError < StandardError ; end
|
34
|
+
class LookupNotFoundError < LookupError ; end
|
35
|
+
class LookupAmbigError < LookupError ; end
|
36
|
+
|
37
|
+
def key_lookup list, key, err_notfound="%s not found\n", err_ambig="%s is ambiguous\n"
|
38
|
+
keylist = list.is_a?(Hash) ? list.keys : list
|
39
|
+
if exact = keylist.grep(/^#{key}$/i) # exact match?
|
40
|
+
return exact.shift if exact && exact.size == 1
|
41
|
+
end
|
42
|
+
keys = keylist.grep(/^#{key}/i)
|
43
|
+
case keys.size
|
44
|
+
when 0
|
45
|
+
unless err_notfound.nil?
|
46
|
+
raise LookupNotFoundError, sprintf(err_notfound, key)
|
47
|
+
end
|
48
|
+
return nil
|
49
|
+
when 1
|
50
|
+
return keys[0]
|
51
|
+
else
|
52
|
+
unless err_ambig.nil?
|
53
|
+
raise LookupAmbigError, sprintf(err_ambig, key)
|
54
|
+
end
|
55
|
+
return keys
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
alias lookup key_lookup
|
60
|
+
|
61
|
+
class Array
|
62
|
+
def lookup key, err_notfound="%s not found\n", err_ambig="%s is ambiguous\n"
|
63
|
+
key_lookup self, key, err_notfound, err_ambig
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class Hash
|
68
|
+
def lookup key, err_notfound="%s not found\n", err_ambig="%s is ambiguous\n"
|
69
|
+
self.keys.lookup(key, err_notfound, err_ambig)
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,206 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# test-cmd-utils.rb -- simple tester for cmd-utils
|
3
|
+
#
|
4
|
+
require 'rubygems'
|
5
|
+
require 'minitest'
|
6
|
+
require 'minitest/autorun'
|
7
|
+
require 'cmd-utils'
|
8
|
+
|
9
|
+
# these routines produce output on STDERR depending on $norun, $verbose, and $quiet.
|
10
|
+
|
11
|
+
class TestCmdUtils < MiniTest::Test
|
12
|
+
|
13
|
+
def gen_test name, norun, verbose, quiet, output
|
14
|
+
$norun = norun > 0
|
15
|
+
$verbose = verbose > 0
|
16
|
+
$quiet = quiet > 0
|
17
|
+
out, err = capture_io { yield }
|
18
|
+
assert_empty(out, "#{name} $stdout should be empty")
|
19
|
+
case output
|
20
|
+
when TrueClass
|
21
|
+
refute_empty(err, "#{name} $stderr should not be empty")
|
22
|
+
when FalseClass
|
23
|
+
assert_empty(err, "#{name} $stderr should be empty")
|
24
|
+
when String
|
25
|
+
assert_match(out, output, "#{name} $stderr should match #{output}")
|
26
|
+
end
|
27
|
+
true
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def test_talk_arg
|
32
|
+
# nr, verb, quiet, output?
|
33
|
+
gen_test('talk_arg', 0, 0, 0, true) { talk "hello" }
|
34
|
+
gen_test('talk_arg', 0, 0, 1, false) { talk "hello" }
|
35
|
+
gen_test('talk_arg', 0, 1, 0, true) { talk "hello" }
|
36
|
+
gen_test('talk_arg', 0, 1, 1, false) { talk "hello" }
|
37
|
+
gen_test('talk_arg', 1, 0, 0, true) { talk "hello" }
|
38
|
+
gen_test('talk_arg', 1, 0, 1, false) { talk "hello" }
|
39
|
+
gen_test('talk_arg', 1, 1, 0, true) { talk "hello" }
|
40
|
+
gen_test('talk_arg', 1, 1, 1, false) { talk "hello" }
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_talk_content
|
44
|
+
gen_test('talk_content', 0, 0, 0, "hello") { talk "hello" }
|
45
|
+
gen_test('talk_content_block', 0, 0, 0, "hello") { talk { "hello" } }
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_talk_block
|
49
|
+
# nr, verb, quiet, output?
|
50
|
+
gen_test('talk', 0, 0, 0, true) { talk { "hello" } }
|
51
|
+
gen_test('talk', 0, 0, 1, false) { talk { "hello" } }
|
52
|
+
gen_test('talk', 0, 1, 0, true) { talk { "hello" } }
|
53
|
+
gen_test('talk', 0, 1, 1, false) { talk { "hello" } }
|
54
|
+
gen_test('talk', 1, 0, 0, true) { talk { "hello" } }
|
55
|
+
gen_test('talk', 1, 0, 1, false) { talk { "hello" } }
|
56
|
+
gen_test('talk', 1, 1, 0, true) { talk { "hello" } }
|
57
|
+
gen_test('talk', 1, 1, 1, false) { talk { "hello" } }
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_talkf_arg
|
61
|
+
# nr, verb, quiet, output?
|
62
|
+
gen_test('talkf_arg', 0, 0, 0, true) { talkf "%s", "hello" }
|
63
|
+
gen_test('talkf_arg', 0, 0, 1, false) { talkf "%s", "hello" }
|
64
|
+
gen_test('talkf_arg', 0, 1, 0, true) { talkf "%s", "hello" }
|
65
|
+
gen_test('talkf_arg', 0, 1, 1, false) { talkf "%s", "hello" }
|
66
|
+
gen_test('talkf_arg', 1, 0, 0, true) { talkf "%s", "hello" }
|
67
|
+
gen_test('talkf_arg', 1, 0, 1, false) { talkf "%s", "hello" }
|
68
|
+
gen_test('talkf_arg', 1, 1, 0, true) { talkf "%s", "hello" }
|
69
|
+
gen_test('talkf_arg', 1, 1, 1, false) { talkf "%s", "hello" }
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_talkf_block
|
73
|
+
# nr, verb, quiet, output?
|
74
|
+
gen_test('talkf_block', 0, 0, 0, true) { talkf("%s") { "hello" } }
|
75
|
+
gen_test('talkf_block', 0, 0, 1, false) { talkf("%s") { "hello" } }
|
76
|
+
gen_test('talkf_block', 0, 1, 0, true) { talkf("%s") { "hello" } }
|
77
|
+
gen_test('talkf_block', 0, 1, 1, false) { talkf("%s") { "hello" } }
|
78
|
+
gen_test('talkf_block', 1, 0, 0, true) { talkf("%s") { "hello" } }
|
79
|
+
gen_test('talkf_block', 1, 0, 1, false) { talkf("%s") { "hello" } }
|
80
|
+
gen_test('talkf_block', 1, 1, 0, true) { talkf("%s") { "hello" } }
|
81
|
+
gen_test('talkf_block', 1, 1, 1, false) { talkf("%s") { "hello" } }
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_talkf_content
|
85
|
+
gen_test('talkf_content', 0, 0, 0, "-hello-") { talkf "-%s-", "hello" }
|
86
|
+
gen_test('talkf_content', 0, 0, 0, "-hello-") { talkf("-%s-") { "hello" } }
|
87
|
+
gen_test('talkf_default_content', 0, 0, 0, 'hello') { talkf "hello" }
|
88
|
+
gen_test('talkf_default_content', 0, 0, 0, 'hello') { talkf { "hello" } }
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_qtalk_arg
|
92
|
+
# nr,vrb,q, out?
|
93
|
+
gen_test('qtalk', 0, 0, 0, false) { qtalk "hello" }
|
94
|
+
gen_test('qtalk', 0, 0, 1, true) { qtalk "hello" }
|
95
|
+
gen_test('qtalk', 0, 1, 0, false) { qtalk "hello" }
|
96
|
+
gen_test('qtalk', 0, 1, 1, true) { qtalk "hello" }
|
97
|
+
gen_test('qtalk', 1, 0, 0, false) { qtalk "hello" }
|
98
|
+
gen_test('qtalk', 1, 0, 1, true) { qtalk "hello" }
|
99
|
+
gen_test('qtalk', 1, 1, 0, false) { qtalk "hello" }
|
100
|
+
gen_test('qtalk', 1, 1, 1, true) { qtalk "hello" }
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_qtalk_block
|
104
|
+
# nr, verb, quiet, output?
|
105
|
+
gen_test('qtalk', 0, 0, 0, false) { qtalk { "hello" } }
|
106
|
+
gen_test('qtalk', 0, 0, 1, true) { qtalk { "hello" } }
|
107
|
+
gen_test('qtalk', 0, 1, 0, false) { qtalk { "hello" } }
|
108
|
+
gen_test('qtalk', 0, 1, 1, true) { qtalk { "hello" } }
|
109
|
+
gen_test('qtalk', 1, 0, 0, false) { qtalk { "hello" } }
|
110
|
+
gen_test('qtalk', 1, 0, 1, true) { qtalk { "hello" } }
|
111
|
+
gen_test('qtalk', 1, 1, 0, false) { qtalk { "hello" } }
|
112
|
+
gen_test('qtalk', 1, 1, 1, true) { qtalk { "hello" } }
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_qtalk_content
|
116
|
+
gen_test('qtalk_content', 0, 0, 1, "-hello-") { qtalk "hello" }
|
117
|
+
gen_test('qtalk_content', 0, 0, 1, "-hello-") { qtalk { "hello" } }
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_qtalkf_content
|
121
|
+
gen_test('qtalkf_content', 0, 0, 1, "-hello-") { qtalkf "-%s-", "hello" }
|
122
|
+
gen_test('qtalkf_content', 0, 0, 1, "-hello-") { qtalkf("-%s-") { "hello" } }
|
123
|
+
gen_test('qtalkf_default_content', 0, 0, 1, "-hello-") { qtalkf "hello" }
|
124
|
+
gen_test('qtalkf_default_content', 0, 0, 1, "-hello-") { qtalkf { "hello" } }
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_vtalk_arg
|
128
|
+
# nr, verb, quiet, output?
|
129
|
+
gen_test('vtalk_arg', 0, 0, 0, false) { vtalk "hello" }
|
130
|
+
gen_test('vtalk_arg', 0, 0, 1, false) { vtalk "hello" }
|
131
|
+
gen_test('vtalk_arg', 0, 1, 0, true) { vtalk "hello" }
|
132
|
+
gen_test('vtalk_arg', 0, 1, 1, true) { vtalk "hello" }
|
133
|
+
gen_test('vtalk_arg', 1, 0, 0, false) { vtalk "hello" }
|
134
|
+
gen_test('vtalk_arg', 1, 0, 1, false) { vtalk "hello" }
|
135
|
+
gen_test('vtalk_arg', 1, 1, 0, true) { vtalk "hello" }
|
136
|
+
gen_test('vtalk_arg', 1, 1, 1, true) { vtalk "hello" }
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_vtalk_block
|
140
|
+
# nr, verb, quiet, output?
|
141
|
+
gen_test('vtalk_block', 0, 0, 0, false) { vtalk { "hello" } }
|
142
|
+
gen_test('vtalk_block', 0, 0, 1, false) { vtalk { "hello" } }
|
143
|
+
gen_test('vtalk_block', 0, 1, 0, true) { vtalk { "hello" } }
|
144
|
+
gen_test('vtalk_block', 0, 1, 1, true) { vtalk { "hello" } }
|
145
|
+
gen_test('vtalk_block', 1, 0, 0, false) { vtalk { "hello" } }
|
146
|
+
gen_test('vtalk_block', 1, 0, 1, false) { vtalk { "hello" } }
|
147
|
+
gen_test('vtalk_block', 1, 1, 0, true) { vtalk { "hello" } }
|
148
|
+
gen_test('vtalk_block', 1, 1, 1, true) { vtalk { "hello" } }
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_nvtalk_arg
|
152
|
+
# nr, verb, quiet, output?
|
153
|
+
gen_test('nvtalk_arg', 0, 0, 0, true) { nvtalk "hello" }
|
154
|
+
gen_test('nvtalk_arg', 0, 0, 1, true) { nvtalk "hello" }
|
155
|
+
gen_test('nvtalk_arg', 0, 1, 0, false) { nvtalk "hello" }
|
156
|
+
gen_test('nvtalk_arg', 0, 1, 1, false) { nvtalk "hello" }
|
157
|
+
gen_test('nvtalk_arg', 1, 0, 0, true) { nvtalk "hello" }
|
158
|
+
gen_test('nvtalk_arg', 1, 0, 1, true) { nvtalk "hello" }
|
159
|
+
gen_test('nvtalk_arg', 1, 1, 0, false) { nvtalk "hello" }
|
160
|
+
gen_test('nvtalk_arg', 1, 1, 1, false) { nvtalk "hello" }
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_nvtalk_block
|
164
|
+
# nr, verb, quiet, output?
|
165
|
+
gen_test('nvtalk_block', 0, 0, 0, true) { nvtalk { "hello" } }
|
166
|
+
gen_test('nvtalk_block', 0, 0, 1, true) { nvtalk { "hello" } }
|
167
|
+
gen_test('nvtalk_block', 0, 1, 0, false) { nvtalk { "hello" } }
|
168
|
+
gen_test('nvtalk_block', 0, 1, 1, false) { nvtalk { "hello" } }
|
169
|
+
gen_test('nvtalk_block', 1, 0, 0, true) { nvtalk { "hello" } }
|
170
|
+
gen_test('nvtalk_block', 1, 0, 1, true) { nvtalk { "hello" } }
|
171
|
+
gen_test('nvtalk_block', 1, 1, 0, false) { nvtalk { "hello" } }
|
172
|
+
gen_test('nvtalk_block', 1, 1, 1, false) { nvtalk { "hello" } }
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_nrtalk_arg
|
176
|
+
# nr, verb, quiet, output?
|
177
|
+
gen_test('nrtalk_arg', 0, 0, 0, false) { nrtalk "hello" }
|
178
|
+
gen_test('nrtalk_arg', 0, 0, 1, false) { nrtalk "hello" }
|
179
|
+
gen_test('nrtalk_arg', 0, 1, 0, false) { nrtalk "hello" }
|
180
|
+
gen_test('nrtalk_arg', 0, 1, 1, false) { nrtalk "hello" }
|
181
|
+
gen_test('nrtalk_arg', 1, 0, 0, true) { nrtalk "hello" }
|
182
|
+
gen_test('nrtalk_arg', 1, 0, 1, true) { nrtalk "hello" }
|
183
|
+
gen_test('nrtalk_arg', 1, 1, 0, true) { nrtalk "hello" }
|
184
|
+
gen_test('nrtalk_arg', 1, 1, 1, true) { nrtalk "hello" }
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_nrtalk_block
|
188
|
+
# nr, verb, quiet, output?
|
189
|
+
gen_test('nrtalk_block', 0, 0, 0, false) { nrtalk { "hello" } }
|
190
|
+
gen_test('nrtalk_block', 0, 0, 1, false) { nrtalk { "hello" } }
|
191
|
+
gen_test('nrtalk_block', 0, 1, 0, false) { nrtalk { "hello" } }
|
192
|
+
gen_test('nrtalk_block', 0, 1, 1, false) { nrtalk { "hello" } }
|
193
|
+
gen_test('nrtalk_block', 1, 0, 0, true) { nrtalk { "hello" } }
|
194
|
+
gen_test('nrtalk_block', 1, 0, 1, true) { nrtalk { "hello" } }
|
195
|
+
gen_test('nrtalk_block', 1, 1, 0, true) { nrtalk { "hello" } }
|
196
|
+
gen_test('nrtalk_block', 1, 1, 1, true) { nrtalk { "hello" } }
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_nrtalkf_content
|
200
|
+
gen_test('nrtalkf_content', 0, 0, 1, "-hello-") { nrtalkf "-%s-", "hello" }
|
201
|
+
gen_test('nrtalkf_content', 0, 0, 1, "-hello-") { nrtalkf("-%s-") { "hello" } }
|
202
|
+
gen_test('nrtalkf_default_content', 0, 0, 1, "-hello-") { nrtalkf "hello" }
|
203
|
+
gen_test('nrtalkf_default_content', 0, 0, 1, "-hello-") { nrtalkf { "hello" } }
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# test-cmd-utils.rb -- simple tester for cmd-utils
|
3
|
+
#
|
4
|
+
require 'rubygems'
|
5
|
+
gem 'minitest'
|
6
|
+
require 'minitest/autorun'
|
7
|
+
require 'lookup'
|
8
|
+
|
9
|
+
class NilClass ; def to_s ; '' ; end ; end
|
10
|
+
|
11
|
+
class TestLookup < MiniTest::Test
|
12
|
+
|
13
|
+
# do_lookup input-text, output-text, true-if-notfound, true-if-ambiguous
|
14
|
+
|
15
|
+
def do_lookup input, output, notfound=nil, ambig=nil
|
16
|
+
found = nil
|
17
|
+
if notfound
|
18
|
+
assert_raises(LookupNotFoundError) {
|
19
|
+
found = lookup(@keywords, input)
|
20
|
+
}
|
21
|
+
elsif ambig
|
22
|
+
assert_raises(LookupAmbigError) {
|
23
|
+
found = lookup(@keywords, input)
|
24
|
+
}
|
25
|
+
else
|
26
|
+
found = lookup(@keywords, input)
|
27
|
+
#found = found.first if found && found.size == 1
|
28
|
+
assert_equal found, output, "Input = #{input}\nOutput = #{output}\n"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_lookup_many
|
33
|
+
@keywords = %w( set get show edit reset delete count )
|
34
|
+
do_lookup 'se', 'set'
|
35
|
+
do_lookup 'set', 'set'
|
36
|
+
do_lookup 'SET', 'set'
|
37
|
+
do_lookup 'show', 'show'
|
38
|
+
do_lookup 'showme', nil, true
|
39
|
+
do_lookup 'sh', 'show'
|
40
|
+
do_lookup 'e', 'edit'
|
41
|
+
do_lookup 'ed', 'edit'
|
42
|
+
do_lookup 's', [%w( set show )], nil, true
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_lookup_exact
|
46
|
+
@keywords = %w( email emails reason reasons )
|
47
|
+
do_lookup 'email', 'email'
|
48
|
+
do_lookup 'emails', 'emails'
|
49
|
+
do_lookup 'emai', 'email', nil, true
|
50
|
+
do_lookup 'rea', 'reason', nil, true
|
51
|
+
do_lookup 'reason', 'reason'
|
52
|
+
do_lookup 'reasons', 'reasons'
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cmd-utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.6
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alan K. Stebbens
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Some handy utilities for writing command line scripts in ruby.
|
15
|
+
email: aks@stebbens.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/cmd-utils.rb
|
21
|
+
- lib/lookup.rb
|
22
|
+
- tests/test-cmd-utils.rb
|
23
|
+
- tests/test-lookup.rb
|
24
|
+
homepage: http://github.com/aks/cmd-utils
|
25
|
+
licenses:
|
26
|
+
- GPL-2
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 1.8.23
|
46
|
+
signing_key:
|
47
|
+
specification_version: 3
|
48
|
+
summary: Utilities for ruby command line scripts
|
49
|
+
test_files:
|
50
|
+
- tests/test-cmd-utils.rb
|
51
|
+
- tests/test-lookup.rb
|