riel 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +0 -0
- data/lib/riel/ansicolor.rb +93 -0
- data/lib/riel/array.rb +20 -0
- data/lib/riel/command.rb +30 -0
- data/lib/riel/date.rb +16 -0
- data/lib/riel/dir.rb +90 -0
- data/lib/riel/enumerable.rb +66 -0
- data/lib/riel/env.rb +49 -0
- data/lib/riel/file.rb +212 -0
- data/lib/riel/filetype.rb +189 -0
- data/lib/riel/hash.rb +12 -0
- data/lib/riel/io.rb +20 -0
- data/lib/riel/log.rb +548 -0
- data/lib/riel/matchdata.rb +13 -0
- data/lib/riel/optproc.rb +369 -0
- data/lib/riel/pathname.rb +16 -0
- data/lib/riel/rcfile.rb +35 -0
- data/lib/riel/regexp.rb +152 -0
- data/lib/riel/setdiff.rb +53 -0
- data/lib/riel/size_converter.rb +62 -0
- data/lib/riel/string.rb +81 -0
- data/lib/riel/tempfile.rb +28 -0
- data/lib/riel/text.rb +408 -0
- data/lib/riel/timer.rb +52 -0
- data/lib/riel.rb +13 -0
- data/test/riel/array_test.rb +22 -0
- data/test/riel/command_test.rb +28 -0
- data/test/riel/date_test.rb +17 -0
- data/test/riel/dir_test.rb +98 -0
- data/test/riel/enumerable_test.rb +27 -0
- data/test/riel/env_test.rb +52 -0
- data/test/riel/file_test.rb +242 -0
- data/test/riel/filetype_test.rb +32 -0
- data/test/riel/hash_test.rb +12 -0
- data/test/riel/io_test.rb +22 -0
- data/test/riel/log_test.rb +184 -0
- data/test/riel/matchdata_test.rb +15 -0
- data/test/riel/optproc_test.rb +233 -0
- data/test/riel/pathname_test.rb +36 -0
- data/test/riel/rcfile_test.rb +44 -0
- data/test/riel/regexp_test.rb +24 -0
- data/test/riel/setdiff_test.rb +26 -0
- data/test/riel/size_converter_test.rb +64 -0
- data/test/riel/string_test.rb +58 -0
- data/test/riel/tempfile_test.rb +16 -0
- data/test/riel/text_test.rb +102 -0
- data/test/riel/timer_test.rb +43 -0
- metadata +134 -0
@@ -0,0 +1,233 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'pathname'
|
5
|
+
require 'tempfile'
|
6
|
+
require 'rubyunit'
|
7
|
+
require 'riel/optproc'
|
8
|
+
|
9
|
+
class OptProcTestCase < RUNIT::TestCase
|
10
|
+
|
11
|
+
def setup
|
12
|
+
# ignore what they have in ENV[HOME]
|
13
|
+
ENV['HOME'] = '/this/should/not/exist'
|
14
|
+
end
|
15
|
+
|
16
|
+
def do_test(args, exp, &blk)
|
17
|
+
expected = DEFAULTS.merge(exp)
|
18
|
+
|
19
|
+
# ignore what they have in ENV[HOME]
|
20
|
+
ENV['HOME'] = '/this/should/not/exist'
|
21
|
+
|
22
|
+
origargs = args.dup
|
23
|
+
|
24
|
+
gopt = GlarkOptions.instance
|
25
|
+
gopt.run(args)
|
26
|
+
|
27
|
+
expected.sort { |a, b| a[0].to_s <=> b[0].to_s }.each do |opt, exval|
|
28
|
+
meth = gopt.method(opt)
|
29
|
+
val = meth.call
|
30
|
+
if val.kind_of? Array
|
31
|
+
assert_equal exval.length, val.length, "#{opt} => #{exval.class}.length #{exval.inspect}; #{val.class}.length #{val.inspect}; #{origargs.inspect}"
|
32
|
+
(0 ... exval.length).each do |idx|
|
33
|
+
assert_equal exval[idx], val[idx], "#{opt}[#{idx}] => #{exval.class}.length #{exval.inspect}; #{val.class}.length #{val.inspect}; #{origargs.inspect}"
|
34
|
+
end
|
35
|
+
else
|
36
|
+
assert_equal exval, val, "#{opt} => #{exval.class} #{exval.inspect}; #{val.class} #{val.inspect}; #{origargs.inspect}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
blk.call(gopt) if blk
|
41
|
+
|
42
|
+
gopt.reset
|
43
|
+
end
|
44
|
+
|
45
|
+
def do_match_tag_test(opt, exp, tag)
|
46
|
+
m = opt.match([ tag ])
|
47
|
+
match = nil
|
48
|
+
if exp.respond_to? :include?
|
49
|
+
match = exp.include? m
|
50
|
+
else
|
51
|
+
match = exp == m
|
52
|
+
end
|
53
|
+
assert match, "match_tag(#{tag}): expected: #{exp.inspect}; actual: #{m.inspect}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_match_tag
|
57
|
+
@after = "nothing"
|
58
|
+
opt = OptProc::Option.new(:tags => %w{ --after-context -A }, :arg => [ :integer ])
|
59
|
+
|
60
|
+
%w{ --after-context --after-context=3 -A }.each do |tag|
|
61
|
+
do_match_tag_test(opt, 1.0, tag)
|
62
|
+
end
|
63
|
+
|
64
|
+
do_match_tag_test(opt, nil, '-b')
|
65
|
+
|
66
|
+
# we don't support case insensitivity (which is insensitive of us):
|
67
|
+
%w{ --After-Context --AFTER-CONTEXT=3 -a }.each do |tag|
|
68
|
+
do_match_tag_test(opt, nil, tag)
|
69
|
+
end
|
70
|
+
|
71
|
+
%w{ --after --after=3 }.each do |tag|
|
72
|
+
do_match_tag_test(opt, 0.07, tag)
|
73
|
+
end
|
74
|
+
|
75
|
+
%w{ --after-cont --after-cont=3 }.each do |tag|
|
76
|
+
do_match_tag_test(opt, 0.12, tag)
|
77
|
+
end
|
78
|
+
|
79
|
+
%w{ --aft --aft=3 }.each do |tag|
|
80
|
+
do_match_tag_test(opt, 0.05, tag)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def do_match_value_test(opt, exp, val)
|
85
|
+
m = opt.match_value(val)
|
86
|
+
assert !!m == !!exp, "match value #{val}; expected: #{exp.inspect}; actual: #{m.inspect}"
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_value_none
|
90
|
+
opt = OptProc::Option.new(:arg => [ :none ])
|
91
|
+
|
92
|
+
{
|
93
|
+
'34' => nil,
|
94
|
+
'43' => nil,
|
95
|
+
'34.12' => nil
|
96
|
+
}.each do |val, exp|
|
97
|
+
do_match_value_test(opt, exp, val)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_value_integer
|
102
|
+
opt = OptProc::Option.new(:arg => [ :integer ])
|
103
|
+
|
104
|
+
{
|
105
|
+
'34' => true,
|
106
|
+
'43' => true,
|
107
|
+
'34.12' => nil,
|
108
|
+
'-34' => true,
|
109
|
+
'+34' => true,
|
110
|
+
}.each do |val, exp|
|
111
|
+
do_match_value_test(opt, exp, val)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_value_float
|
116
|
+
opt = OptProc::Option.new(:arg => [ :float ])
|
117
|
+
|
118
|
+
{
|
119
|
+
'34' => true,
|
120
|
+
'43' => true,
|
121
|
+
'34.12' => true,
|
122
|
+
'.12' => true,
|
123
|
+
'.' => false,
|
124
|
+
'12.' => false,
|
125
|
+
}.each do |val, exp|
|
126
|
+
do_match_value_test(opt, exp, val)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_value_string
|
131
|
+
opt = OptProc::Option.new(:arg => [ :string ])
|
132
|
+
|
133
|
+
{
|
134
|
+
'34' => true,
|
135
|
+
'43' => true,
|
136
|
+
'34.12' => true,
|
137
|
+
'.12' => true,
|
138
|
+
'.' => true,
|
139
|
+
'12.' => true,
|
140
|
+
'hello' => true,
|
141
|
+
'a b c' => true,
|
142
|
+
'' => true,
|
143
|
+
}.each do |val, exp|
|
144
|
+
[
|
145
|
+
'"' + val + '"',
|
146
|
+
"'" + val + "'",
|
147
|
+
val,
|
148
|
+
].each do |qval|
|
149
|
+
do_match_value_test(opt, exp, qval)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_after_context_float
|
155
|
+
@after = nil
|
156
|
+
opt = OptProc::Option.new(:tags => %w{ --after-context -A },
|
157
|
+
:arg => [ :required, :float ],
|
158
|
+
:set => Proc.new { |val| @after = val })
|
159
|
+
[
|
160
|
+
%w{ --after-context 3 },
|
161
|
+
%w{ --after-context=3 },
|
162
|
+
%w{ -A 3 },
|
163
|
+
].each do |args|
|
164
|
+
@after = nil
|
165
|
+
|
166
|
+
m = opt.match(args)
|
167
|
+
assert_equal 1.0, m, "args: #{args.inspect}"
|
168
|
+
# curr = args.shift
|
169
|
+
opt.set_value(args)
|
170
|
+
assert_equal 3.0, @after
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_regexp_option
|
175
|
+
@after = nil
|
176
|
+
opt = OptProc::Option.new(:res => %r{ ^ - ([1-9]\d*) $ }x,
|
177
|
+
:tags => %w{ --context -C },
|
178
|
+
:arg => [ :optional, :integer ],
|
179
|
+
:set => Proc.new { |val| @ctx = val })
|
180
|
+
[
|
181
|
+
%w{ --context 3 },
|
182
|
+
%w{ --context=3 },
|
183
|
+
%w{ -C 3 },
|
184
|
+
].each do |args|
|
185
|
+
@ctx = nil
|
186
|
+
|
187
|
+
m = opt.match(args)
|
188
|
+
assert_equal 1.0, m, "args: #{args.inspect}"
|
189
|
+
opt.set_value(args)
|
190
|
+
assert_equal 3, @ctx
|
191
|
+
end
|
192
|
+
|
193
|
+
vals = (1 .. 10).to_a | (1 .. 16).collect { |x| 2 ** x }
|
194
|
+
# vals = [ 4 ]
|
195
|
+
vals.each do |val|
|
196
|
+
args = [ '-' + val.to_s, 'foo' ]
|
197
|
+
|
198
|
+
@ctx = nil
|
199
|
+
|
200
|
+
m = opt.match(args)
|
201
|
+
assert_equal 1.0, m, "args: #{args.inspect}"
|
202
|
+
opt.set_value(args)
|
203
|
+
assert_equal val, @ctx
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_value_regexp
|
208
|
+
@range_start = nil
|
209
|
+
opt = OptProc::Option.new(:tags => %w{ --after },
|
210
|
+
:arg => [ :required, :regexp, %r{ ^ (\d+%?) $ }x ],
|
211
|
+
:set => Proc.new { |md| @range_start = md[1] })
|
212
|
+
|
213
|
+
%w{ 5 5% 10 90% }.each do |rg|
|
214
|
+
[
|
215
|
+
[ '--after', rg ],
|
216
|
+
[ '--after=' + rg ]
|
217
|
+
].each do |args|
|
218
|
+
@range_start = nil
|
219
|
+
|
220
|
+
m = opt.match(args)
|
221
|
+
assert_equal 1.0, m, "args: #{args.inspect}"
|
222
|
+
opt.set_value(args)
|
223
|
+
assert_equal rg, @range_start
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
end
|
229
|
+
|
230
|
+
|
231
|
+
if __FILE__ == $0
|
232
|
+
Log.level = Log::DEBUG
|
233
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubyunit'
|
5
|
+
require 'riel/pathname'
|
6
|
+
|
7
|
+
|
8
|
+
class PathnameTestCase < RUNIT::TestCase
|
9
|
+
|
10
|
+
def do_rootname_test(exp, path)
|
11
|
+
pn = Pathname.new(path)
|
12
|
+
assert_equals(exp, pn.rootname)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_rootname
|
16
|
+
[
|
17
|
+
"/this/is/a.test",
|
18
|
+
"/a.test",
|
19
|
+
"a.test",
|
20
|
+
"/this/is/a",
|
21
|
+
"/a",
|
22
|
+
"a"
|
23
|
+
].each do |path|
|
24
|
+
do_rootname_test("a", path)
|
25
|
+
end
|
26
|
+
|
27
|
+
[
|
28
|
+
"/this/is/.atest",
|
29
|
+
"/.atest",
|
30
|
+
".atest"
|
31
|
+
].each do |path|
|
32
|
+
do_rootname_test(".atest", path)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubyunit'
|
5
|
+
require 'riel/rcfile'
|
6
|
+
require 'riel/tempfile'
|
7
|
+
|
8
|
+
class RCFileTestCase < RUNIT::TestCase
|
9
|
+
|
10
|
+
def test
|
11
|
+
separators = %w{ = : }
|
12
|
+
leading_spaces = [ "", " ", " " ]
|
13
|
+
trailing_spaces = leading_spaces.dup
|
14
|
+
comments = [ "", "# a comment" ]
|
15
|
+
|
16
|
+
num = 0
|
17
|
+
tempfile = Tempfile.open("rcfile_test") do |tf|
|
18
|
+
|
19
|
+
tf.puts "# this is a comment"
|
20
|
+
tf.puts ""
|
21
|
+
|
22
|
+
separators.each do |sep|
|
23
|
+
leading_spaces.each do |lsp|
|
24
|
+
trailing_spaces.each do |tsp|
|
25
|
+
comments.each do |cmt|
|
26
|
+
tf.puts "#{lsp}name#{num}#{sep}value#{num}#{tsp}#{cmt}"
|
27
|
+
num += 1
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
rc = RCFile.new(tempfile)
|
35
|
+
(0 ... num).each do |i|
|
36
|
+
key = "name#{i}"
|
37
|
+
assert_not_nil rc.settings[i]
|
38
|
+
pair = rc.settings[i]
|
39
|
+
|
40
|
+
assert_equals [ "name#{i}", "value#{i}" ], rc.settings[i]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubyunit'
|
5
|
+
require 'riel/regexp'
|
6
|
+
|
7
|
+
class RegexpTestCase < RUNIT::TestCase
|
8
|
+
|
9
|
+
def test_unixre_to_string
|
10
|
+
assert_equal("a[b-z]", Regexp.unixre_to_string("a[b-z]"))
|
11
|
+
assert_equal("ab\\.z", Regexp.unixre_to_string("ab.z"))
|
12
|
+
assert_equal("ab\\.z.*", Regexp.unixre_to_string("ab.z*"))
|
13
|
+
assert_equal("a.c", Regexp.unixre_to_string("a?c"))
|
14
|
+
assert_equal("ab\\$", Regexp.unixre_to_string("ab$"))
|
15
|
+
assert_equal("a\\/c", Regexp.unixre_to_string("a/c"))
|
16
|
+
assert_equal("a\\(bc\\)", Regexp.unixre_to_string("a(bc)"))
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_negated
|
20
|
+
assert(NegatedRegexp.new("a[b-z]").match("aa"))
|
21
|
+
assert(NegatedRegexp.new(".+").match(""))
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubyunit'
|
5
|
+
require 'riel/setdiff'
|
6
|
+
|
7
|
+
class SetDiffTestCase < RUNIT::TestCase
|
8
|
+
|
9
|
+
def do_test(a, b, expected)
|
10
|
+
sd = SetDiff.new(a, b)
|
11
|
+
|
12
|
+
assert_equal(expected, sd.diff_type)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_all
|
16
|
+
do_test(%w{ one two three }, %w{ two one three }, :identical)
|
17
|
+
do_test(%w{ one two }, %w{ two one three }, :b_contains_a)
|
18
|
+
do_test(%w{ one two three }, %w{ two }, :a_contains_b)
|
19
|
+
do_test(%w{ one two three }, %w{ four five six }, :no_common)
|
20
|
+
do_test(%w{ one two three }, %w{ }, :no_common)
|
21
|
+
do_test(%w{ }, %w{ one two three }, :no_common)
|
22
|
+
do_test(%w{ }, %w{ }, :no_common)
|
23
|
+
do_test(%w{ one two three}, %w{ two three four }, :common)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubyunit'
|
5
|
+
require 'riel/size_converter'
|
6
|
+
|
7
|
+
class SizeConverterTestCase < RUNIT::TestCase
|
8
|
+
|
9
|
+
SIZES = [
|
10
|
+
# 0123456789012345 0123456789012345 0123456789012345 0123456789012345 0123456789012345 0123456789012345 0123456789012345
|
11
|
+
# base10.0 base10.1 base10.2 si.0 si.1 si.2 num ]
|
12
|
+
[ "1", "1.0", "1.00", "1", "1.0", "1.00", 1 ],
|
13
|
+
|
14
|
+
[ "1K", "1.0K", "1.02K", "1KiB", "1.0KiB", "1.00KiB", 1024 ],
|
15
|
+
[ "1K", "1.0K", "1.00K", "1000", "1000.0", "1000.00", 1000 ],
|
16
|
+
[ "2K", "2.0K", "2.05K", "2KiB", "2.0KiB", "2.00KiB", 2048 ],
|
17
|
+
[ "2K", "2.0K", "2.00K", "2KiB", "2.0KiB", "1.95KiB", 2000 ],
|
18
|
+
|
19
|
+
[ "1M", "1.0M", "1.00M", "977KiB", "976.6KiB", "976.56KiB", 10 ** 6 ],
|
20
|
+
[ "2M", "2.0M", "2.00M", "2MiB", "1.9MiB", "1.91MiB", 2 * 10 ** 6 ],
|
21
|
+
[ "1M", "1.0M", "1.05M", "1MiB", "1.0MiB", "1.00MiB", 2 ** 20 ],
|
22
|
+
[ "2M", "2.1M", "2.10M", "2MiB", "2.0MiB", "2.00MiB", 2 * 2 ** 20 ],
|
23
|
+
|
24
|
+
[ "1G", "1.0G", "1.00G", "954MiB", "953.7MiB", "953.67MiB", 10 ** 9 ],
|
25
|
+
[ "2G", "2.0G", "2.00G", "2GiB", "1.9GiB", "1.86GiB", 2 * 10 ** 9 ],
|
26
|
+
|
27
|
+
[ "1G", "1.1G", "1.07G", "1GiB", "1.0GiB", "1.00GiB", 2 ** 30 ],
|
28
|
+
[ "2G", "2.1G", "2.15G", "2GiB", "2.0GiB", "2.00GiB", 2 * 2 ** 30 ],
|
29
|
+
|
30
|
+
[ "1T", "1.0T", "1.00T", "931GiB", "931.3GiB", "931.32GiB", 10 ** 12 ],
|
31
|
+
[ "2T", "2.0T", "2.00T", "2TiB", "1.8TiB", "1.82TiB", 2 * 10 ** 12 ],
|
32
|
+
|
33
|
+
[ "1T", "1.1T", "1.10T", "1TiB", "1.0TiB", "1.00TiB", 2 ** 40 ],
|
34
|
+
[ "2T", "2.2T", "2.20T", "2TiB", "2.0TiB", "2.00TiB", 2 * 2 ** 40 ],
|
35
|
+
]
|
36
|
+
|
37
|
+
def assert_conversion(cls, data, offset, didx, sidx, dec_places = nil)
|
38
|
+
numstr = data[-1]
|
39
|
+
conv = dec_places.nil? ? cls.convert(numstr) : cls.convert(numstr, dec_places)
|
40
|
+
assert_equals data[offset + didx], conv, "size index: #{sidx}; data index: #{didx}; offset: #{offset}; decimal_places: #{dec_places}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def do_test(cls, offset)
|
44
|
+
SIZES.each_with_index do |data, sidx|
|
45
|
+
assert_conversion(cls, data, offset, 1, sidx)
|
46
|
+
(0 .. 2).each do |dec_places|
|
47
|
+
assert_conversion(cls, data, offset, dec_places, sidx, dec_places)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_default
|
53
|
+
do_test(SizeConverter, 0)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_human
|
57
|
+
do_test(SizeConverter::Human, 0)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_si
|
61
|
+
do_test(SizeConverter::SI, 3)
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'pathname'
|
5
|
+
require 'rubyunit'
|
6
|
+
require 'riel/string'
|
7
|
+
|
8
|
+
class StringToRangeTestCase < RUNIT::TestCase
|
9
|
+
|
10
|
+
def do_string_to_range_test(exp, str, args = Hash.new)
|
11
|
+
String.to_ranges(str, args).each_with_index do |rg, idx|
|
12
|
+
assert_equal exp[idx], rg
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_string_to_range
|
17
|
+
do_string_to_range_test([ ], "")
|
18
|
+
do_string_to_range_test([ 1 .. 1 ], "1")
|
19
|
+
do_string_to_range_test([ 1 .. 2 ], "1..2")
|
20
|
+
do_string_to_range_test([ 1 .. 1, 2 .. 2 ], "1,2")
|
21
|
+
do_string_to_range_test([ 1 .. 2 ], "1,2", :collapse => true)
|
22
|
+
|
23
|
+
do_string_to_range_test([ 1 .. 4 ], "1-4")
|
24
|
+
do_string_to_range_test([ -String::Infinity .. 4 ], "-4")
|
25
|
+
do_string_to_range_test([ 4 .. String::Infinity ], "4-")
|
26
|
+
|
27
|
+
do_string_to_range_test([ -8 .. 4 ], "-4", :min => -8)
|
28
|
+
do_string_to_range_test([ 0 .. 4 ], "-4", :min => 0)
|
29
|
+
do_string_to_range_test([ 1 .. 8 ], "1-", :min => 0, :max => 8)
|
30
|
+
do_string_to_range_test([ 1 .. 5 ], "1-4,5", :collapse => true)
|
31
|
+
do_string_to_range_test([ 1 .. 4, 5 .. 5 ], "1-4,5", :collapse => false)
|
32
|
+
do_string_to_range_test([ 1 .. 4, 5 .. 5 ], "1-4,5")
|
33
|
+
do_string_to_range_test([ 1 .. 3, 5 .. 8 ], "1,2,3,5-", :min => 0, :max => 8, :collapse => true)
|
34
|
+
do_string_to_range_test([ 1 .. 1, 2 .. 2, 3 .. 3, 5 .. 8 ], "1,2,3,5-", :min => 0, :max => 8)
|
35
|
+
do_string_to_range_test([ 1 .. 3, 5 .. 6 ], "1-3,5,6", :min => 0, :max => 8, :collapse => true)
|
36
|
+
do_string_to_range_test([ 1 .. 3, 5 .. 5, 6 .. 6 ], "1-3,5,6", :min => 0, :max => 8)
|
37
|
+
do_string_to_range_test([ 1 .. 3, 5 .. 6, 10 .. 12 ], "1-3,5-6,10,11,12,", :collapse => true)
|
38
|
+
do_string_to_range_test([ 1 .. 3, 5 .. 6, 10 .. 10, 11 .. 11, 12 .. 12 ], "1-3,5-6,10,11,12,")
|
39
|
+
|
40
|
+
do_string_to_range_test([ ], "abc")
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_num
|
44
|
+
assert_equals(1, "1".num)
|
45
|
+
assert_equals(nil, "one".num)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_minus
|
49
|
+
assert_equals("foo", "food" - "d")
|
50
|
+
assert_equals("fd", "food" - %r{o+})
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_highlight
|
54
|
+
assert_equals("...\e[34mthis\e[0m... is blue", "...this... is blue".highlight(%r{this}, "blue"))
|
55
|
+
assert_equals("...\e[34m\e[42mthis\e[0m... is blue", "...this... is blue".highlight(%r{this}, "blue on green"))
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubyunit'
|
5
|
+
require 'riel/text'
|
6
|
+
|
7
|
+
class TextTestCase < RUNIT::TestCase
|
8
|
+
|
9
|
+
def do_ansi_test(str, input, *chars)
|
10
|
+
escape_sequence = chars.collect { |ch| "\e[#{ch}m" }.join("")
|
11
|
+
assert_equals "#{escape_sequence}#{str}\e[0m", input
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_ansi_highlight
|
15
|
+
String.highlighter = "ANSI"
|
16
|
+
|
17
|
+
str = "precision"
|
18
|
+
|
19
|
+
do_ansi_test(str, str.none, 0)
|
20
|
+
do_ansi_test(str, str.bold, 1)
|
21
|
+
do_ansi_test(str, str.underline, 4)
|
22
|
+
do_ansi_test(str, str.underscore, 4)
|
23
|
+
do_ansi_test(str, str.blink, 5) # every geocities site circa 1998
|
24
|
+
do_ansi_test(str, str.negative, 7)
|
25
|
+
do_ansi_test(str, str.concealed, 8)
|
26
|
+
do_ansi_test(str, str.black, 30)
|
27
|
+
do_ansi_test(str, str.red, 31)
|
28
|
+
do_ansi_test(str, str.green, 32)
|
29
|
+
do_ansi_test(str, str.yellow, 33)
|
30
|
+
do_ansi_test(str, str.blue, 34)
|
31
|
+
do_ansi_test(str, str.magenta, 35)
|
32
|
+
do_ansi_test(str, str.cyan, 36)
|
33
|
+
do_ansi_test(str, str.white, 37)
|
34
|
+
do_ansi_test(str, str.on_black, 40)
|
35
|
+
do_ansi_test(str, str.on_red, 41)
|
36
|
+
do_ansi_test(str, str.on_green, 42)
|
37
|
+
do_ansi_test(str, str.on_yellow, 43)
|
38
|
+
do_ansi_test(str, str.on_blue, 44)
|
39
|
+
do_ansi_test(str, str.on_magenta, 45)
|
40
|
+
do_ansi_test(str, str.on_cyan, 46)
|
41
|
+
do_ansi_test(str, str.on_white, 47)
|
42
|
+
do_ansi_test(str, str.none_on_white, 0, 47)
|
43
|
+
do_ansi_test(str, str.none_on_black, 0, 40)
|
44
|
+
do_ansi_test(str, str.none_on_blue, 0, 44)
|
45
|
+
do_ansi_test(str, str.red_on_white, 31, 47)
|
46
|
+
do_ansi_test(str, str.bold_red_on_white, 1, 31, 47)
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
def do_html_test(expected, input)
|
51
|
+
assert_equals expected, input
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_html_highlight
|
55
|
+
String.highlighter = "HTML"
|
56
|
+
|
57
|
+
str = "precision"
|
58
|
+
|
59
|
+
do_html_test(str, str.none)
|
60
|
+
do_html_test("<b>" + str + "</b>", str.bold)
|
61
|
+
|
62
|
+
[ str.underline, str.underscore ].each do |input|
|
63
|
+
do_html_test("<u>" + str + "</u>", input)
|
64
|
+
end
|
65
|
+
|
66
|
+
do_html_test("<blink>" + str + "</blink>", str.blink)
|
67
|
+
do_html_test("<span style=\"color: white; background-color: black\">" + str + "</span>", str.negative)
|
68
|
+
do_html_test("<!-- " + str + " -->", str.concealed)
|
69
|
+
do_html_test("<span style=\"color: black\">" + str + "</span>", str.black)
|
70
|
+
do_html_test("<span style=\"color: red\">" + str + "</span>", str.red)
|
71
|
+
do_html_test("<span style=\"color: green\">" + str + "</span>", str.green)
|
72
|
+
do_html_test("<span style=\"color: yellow\">" + str + "</span>", str.yellow)
|
73
|
+
do_html_test("<span style=\"color: blue\">" + str + "</span>", str.blue)
|
74
|
+
do_html_test("<span style=\"color: #FF00FF\">" + str + "</span>", str.magenta)
|
75
|
+
do_html_test("<span style=\"color: #00FFFF\">" + str + "</span>", str.cyan)
|
76
|
+
do_html_test("<span style=\"color: white\">" + str + "</span>", str.white)
|
77
|
+
do_html_test("<span style=\"background-color: black\">" + str + "</span>", str.on_black)
|
78
|
+
do_html_test("<span style=\"background-color: red\">" + str + "</span>", str.on_red)
|
79
|
+
do_html_test("<span style=\"background-color: green\">" + str + "</span>", str.on_green)
|
80
|
+
do_html_test("<span style=\"background-color: yellow\">" + str + "</span>", str.on_yellow)
|
81
|
+
do_html_test("<span style=\"background-color: blue\">" + str + "</span>", str.on_blue)
|
82
|
+
do_html_test("<span style=\"background-color: #FF00FF\">" + str + "</span>", str.on_magenta)
|
83
|
+
do_html_test("<span style=\"background-color: #00FFFF\">" + str + "</span>", str.on_cyan)
|
84
|
+
do_html_test("<span style=\"background-color: white\">" + str + "</span>", str.on_white)
|
85
|
+
do_html_test("<span style=\"background-color: white\">" + str + "</span>", str.none_on_white)
|
86
|
+
do_html_test("<span style=\"background-color: black\">" + str + "</span>", str.none_on_black)
|
87
|
+
do_html_test("<span style=\"background-color: blue\">" + str + "</span>", str.none_on_blue)
|
88
|
+
do_html_test("<span style=\"color: red\">" + "<span style=\"background-color: white\">" + str + "</span>" + "</span>", str.red_on_white)
|
89
|
+
do_html_test("<b><span style=\"color: red\">" + "<span style=\"background-color: white\">" + str + "</span>" + "</span></b>", str.bold_red_on_white)
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_string_reverse
|
94
|
+
String.highlighter = "HTML"
|
95
|
+
|
96
|
+
str = "precision"
|
97
|
+
|
98
|
+
# this tests that string.reverse does not mean ANSI reverse
|
99
|
+
assert_equal("noisicerp", "precision".reverse)
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubyunit'
|
5
|
+
require 'riel/timer'
|
6
|
+
require 'stringio'
|
7
|
+
|
8
|
+
class TimerTestCase < RUNIT::TestCase
|
9
|
+
|
10
|
+
TIMER_STRING = "sleep for a second"
|
11
|
+
|
12
|
+
def do_assertions(io)
|
13
|
+
assert_not_nil io
|
14
|
+
str = io.string
|
15
|
+
assert_not_nil str
|
16
|
+
|
17
|
+
assert str.index(TIMER_STRING + " start time:")
|
18
|
+
assert str.index(TIMER_STRING + " end time:")
|
19
|
+
assert str.index(TIMER_STRING + " elapsed :")
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_to_stdout
|
23
|
+
orig_out = $stdout
|
24
|
+
$stdout = StringIO.new
|
25
|
+
timer = Timer.new(TIMER_STRING) do
|
26
|
+
sleep(0.1)
|
27
|
+
end
|
28
|
+
|
29
|
+
do_assertions($stdout)
|
30
|
+
|
31
|
+
$stdout = orig_out
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_to_io
|
35
|
+
stringio = StringIO.new
|
36
|
+
timer = Timer.new("sleep for a second", :io => stringio) do
|
37
|
+
sleep(0.1)
|
38
|
+
end
|
39
|
+
|
40
|
+
do_assertions(stringio)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|