Skip to content

Commit

Permalink
* lib/optparse.rb (OptionParser#order, #permute, #parse): allow an
Browse files Browse the repository at this point in the history
  array as argument.

* test/ruby/test_*.rb: moved invariants to left side in
  assert_equal, and use assert_nil, assert_raises and so on.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4516 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Sep 5, 2003
1 parent 01e3a55 commit 44785be
Show file tree
Hide file tree
Showing 32 changed files with 788 additions and 802 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Sat Sep 6 00:15:09 2003 Nobuyoshi Nakada <[email protected]>

* lib/optparse.rb (OptionParser#order, #permute, #parse): allow an
array as argument.

* test/ruby/test_*.rb: moved invariants to left side in
assert_equal, and use assert_nil, assert_raises and so on.

Sat Sep 6 00:00:20 2003 Nobuyoshi Nakada <[email protected]>

* lib/mkmf.rb (have_library, find_library): configure by library
Expand Down
17 changes: 15 additions & 2 deletions lib/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,14 @@ def ver
end
end

def warn(mesg = $!)
super(program_name + ': ' + mesg)
end

def abort(mesg = $!)
super(program_name + ': ' + mesg)
end

=begin
--- OptionParser#top
Subject of ((<on>))/((<on_head>)), ((<accept>))/((<reject>)).
Expand Down Expand Up @@ -1120,7 +1128,10 @@ def separator(string)
: (({block}))
called with each non-option argument.
=end #'#"#`#
def order(*argv, &block) order!(argv, &block) end
def order(*argv, &block)
argv = argv[0].dup if argv.size == 1 and Array === argv[0]
order!(argv, &block)
end

def order!(argv = ARGV, &nonopt)
opt, arg, sw, val, rest = nil
Expand Down Expand Up @@ -1198,6 +1209,7 @@ def order!(argv = ARGV, &nonopt)
command line arguments to be parsed.
=end #'#"#`#
def permute(*argv)
argv = argv[0].dup if argv.size == 1 and Array === argv[0]
permute!(argv)
end

Expand All @@ -1223,6 +1235,7 @@ def permute!(argv = ARGV)
command line arguments to be parsed.
=end #'#"#`#
def parse(*argv)
argv = argv[0].dup if argv.size == 1 and Array === argv[0]
parse!(argv)
end

Expand Down Expand Up @@ -1607,7 +1620,7 @@ def options
begin
yield @optparse
rescue ParseError
warn @optparse.program_name + ': ' + $!
@optparse.warn $!
nil
end
end
Expand Down
16 changes: 9 additions & 7 deletions test/csv/test_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def test_Row_to_a

r = CSV::Row[d(nil, true), d(2), d(3)]
assert_equal([nil, '2', '3'], r.to_a, 'Null in data')

r = CSV::Row[d(nil, true), d(nil, true), d(nil, true)]
assert_equal([nil, nil, nil], r.to_a, 'Nulls')

Expand All @@ -270,7 +270,7 @@ def test_Row_to_a


#### CSV::Reader unit test

def test_Reader_each
file = File.open(@infile, "rb")
begin
Expand Down Expand Up @@ -441,7 +441,7 @@ def test_Reader_s_parse


#### CSV::Writer unit test

def test_Writer_s_new
assert_raises(RuntimeError) do
CSV::Writer.new(nil)
Expand Down Expand Up @@ -1139,8 +1139,10 @@ def setupInputStream(size, bufSize = nil)
end

def setBufSize(size)
CSV::StreamBuf.module_eval('remove_const("BufSize")')
CSV::StreamBuf.module_eval("BufSize = #{ size }")
CSV::StreamBuf.module_eval do
remove_const(:BufSize)
const_set(:BufSize, size)
end
end

class StrBuf < CSV::StreamBuf
Expand Down Expand Up @@ -1282,7 +1284,7 @@ def test_StreamBuf_AREF_n # '[idx, n]'
# At first, check ruby's behaviour.
assert_equal("", "abc"[3, 1])
assert_equal(nil, "abc"[4, 1])

setupInputStream(22, 1024) do |s|
[0, 1, 9, 10, 19, 20, 21].each do |idx|
assert_equal(expStr(idx, 1), s[idx, 1], idx.to_s)
Expand Down Expand Up @@ -1346,7 +1348,7 @@ def test_StreamBuf_get
assert_equal(nil, s.get(-1))
end
end

def test_StreamBuf_get_n
setupInputStream(22, 1024) do |s|
[0, 1, 9, 10, 19, 20, 21].each do |idx|
Expand Down
10 changes: 5 additions & 5 deletions test/ruby/test_alias.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def quux

def test_alias
x = Alias2.new
assert_equal(x.bar, "foo")
assert_equal(x.baz, "foo+foo")
assert_equal("foo", x.bar)
assert_equal("foo+foo", x.baz)

# test_check for cache
assert_equal(x.baz, "foo+foo")
assert_equal("foo+foo", x.baz)

x = Alias3.new
assert(!x.foo)
assert(x.bar)
Expand Down
86 changes: 43 additions & 43 deletions test/ruby/test_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,47 @@

class TestArray < Test::Unit::TestCase
def test_array
assert_equal([1, 2] + [3, 4], [1, 2, 3, 4])
assert_equal([1, 2] * 2, [1, 2, 1, 2])
assert_equal([1, 2] * ":", "1:2")
assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
assert_equal([1, 2, 1, 2], [1, 2] * 2)
assert_equal("1:2", [1, 2] * ":")

assert_equal([1, 2].hash, [1, 2].hash)
assert_equal([1,2,3] & [2,3,4], [2,3])
assert_equal([1,2,3] | [2,3,4], [1,2,3,4])

assert_equal([2,3], [1,2,3] & [2,3,4])
assert_equal([1,2,3,4], [1,2,3] | [2,3,4])
assert_equal([1,2,3] - [2,3], [1])

$x = [0, 1, 2, 3, 4, 5]
assert_equal($x[2], 2)
assert_equal($x[1..3], [1, 2, 3])
assert_equal($x[1,3], [1, 2, 3])
assert_equal(2, $x[2])
assert_equal([1, 2, 3], $x[1..3])
assert_equal([1, 2, 3], $x[1,3])

$x[0, 2] = 10
assert($x[0] == 10 && $x[1] == 2)

$x[0, 0] = -1
assert($x[0] == -1 && $x[1] == 10)

$x[-1, 1] = 20
assert($x[-1] == 20 && $x.pop == 20)
end

def test_array_andor
assert_equal(([1,2,3]&[2,4,6]), [2])
assert_equal(([1,2,3]|[2,4,6]), [1,2,3,4,6])
assert_equal([2], ([1,2,3]&[2,4,6]))
assert_equal([1,2,3,4,6], ([1,2,3]|[2,4,6]))
end

def test_compact
$x = [nil, 1, nil, nil, 5, nil, nil]
$x.compact!
assert_equal($x, [1, 5])
assert_equal([1, 5], $x)
end

def test_uniq
$x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
$x.uniq!
assert_equal($x, [1, 4, 2, 5])
assert_equal([1, 4, 2, 5], $x)

# empty?
assert(!$x.empty?)
$x = []
Expand All @@ -54,50 +54,50 @@ def test_uniq
def test_sort
$x = ["it", "came", "to", "pass", "that", "..."]
$x = $x.sort.join(" ")
assert_equal($x, "... came it pass that to")
assert_equal("... came it pass that to", $x)
$x = [2,5,3,1,7]
$x.sort!{|a,b| a<=>b} # sort with condition
assert_equal($x, [1,2,3,5,7])
assert_equal([1,2,3,5,7], $x)
$x.sort!{|a,b| b-a} # reverse sort
assert_equal($x, [7,5,3,2,1])
assert_equal([7,5,3,2,1], $x)
end

def test_split
$x = "The Boassert of Mormon"
assert_equal($x.split(//).reverse!.join, $x.reverse)
assert_equal($x.reverse, $x.split(//).reverse!.join)
assert_equal($x.reverse, $x.reverse!)
assert_equal("1 byte string".split(//).reverse.join(":"), "g:n:i:r:t:s: :e:t:y:b: :1")
assert_equal("g:n:i:r:t:s: :e:t:y:b: :1", "1 byte string".split(//).reverse.join(":"))
$x = "a b c d"
assert_equal($x.split, ['a', 'b', 'c', 'd'])
assert_equal($x.split(' '), ['a', 'b', 'c', 'd'])
assert_equal(['a', 'b', 'c', 'd'], $x.split)
assert_equal(['a', 'b', 'c', 'd'], $x.split(' '))
end

def test_misc
assert(defined? "a".chomp)
assert_equal("abc".scan(/./), ["a", "b", "c"])
assert_equal("1a2b3c".scan(/(\d.)/), [["1a"], ["2b"], ["3c"]])
assert_equal(["a", "b", "c"], "abc".scan(/./))
assert_equal([["1a"], ["2b"], ["3c"]], "1a2b3c".scan(/(\d.)/))
# non-greedy match
assert_equal("a=12;b=22".scan(/(.*?)=(\d*);?/), [["a", "12"], ["b", "22"]])
assert_equal([["a", "12"], ["b", "22"]], "a=12;b=22".scan(/(.*?)=(\d*);?/))

$x = [1]
assert_equal(($x * 5).join(":"), '1:1:1:1:1')
assert_equal(($x * 1).join(":"), '1')
assert_equal(($x * 0).join(":"), '')
assert_equal('1:1:1:1:1', ($x * 5).join(":"))
assert_equal('1', ($x * 1).join(":"))
assert_equal('', ($x * 0).join(":"))

*$x = *(1..7).to_a
assert_equal($x.size, 7)
assert_equal($x, [1, 2, 3, 4, 5, 6, 7])
assert_equal(7, $x.size)
assert_equal([1, 2, 3, 4, 5, 6, 7], $x)

$x = [1,2,3]
$x[1,0] = $x
assert_equal($x, [1,1,2,3,2,3])
assert_equal([1,1,2,3,2,3], $x)

$x = [1,2,3]
$x[-1,0] = $x
assert_equal($x, [1,2,1,2,3,3])
assert_equal([1,2,1,2,3,3], $x)

$x = [1,2,3]
$x.concat($x)
assert_equal($x, [1,2,3,1,2,3])
assert_equal([1,2,3,1,2,3], $x)
end
end
Loading

0 comments on commit 44785be

Please sign in to comment.