diff options
| -rw-r--r-- | mrbgems/mruby-string-ext/mrblib/string.rb | 22 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/test/string.rb | 20 | ||||
| -rw-r--r-- | test/t/array.rb | 13 | ||||
| -rw-r--r-- | test/t/class.rb | 67 |
4 files changed, 121 insertions, 1 deletions
diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb index 005438b38..a517aa209 100644 --- a/mrbgems/mruby-string-ext/mrblib/string.rb +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -49,4 +49,26 @@ class String def casecmp(str) self.downcase <=> str.downcase end + + def partition(sep) + raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String + n = index(sep) + unless n.nil? + m = n + sep.size + [ slice(0, n), sep, slice(m, size - m) ] + else + [ self, "", "" ] + end + end + + def rpartition(sep) + raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String + n = rindex(sep) + unless n.nil? + m = n + sep.size + [ slice(0, n), sep, slice(m, size - m) ] + else + [ "", "", self ] + end + end end diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb index 3ab959437..67ed628c0 100644 --- a/mrbgems/mruby-string-ext/test/string.rb +++ b/mrbgems/mruby-string-ext/test/string.rb @@ -114,3 +114,23 @@ assert('String#end_with?') do assert_true !"ng".end_with?("ing", "mng") assert_raise TypeError do "hello".end_with?(true) end end + +assert('String#partition') do + assert_equal ["a", "x", "axa"], "axaxa".partition("x") + assert_equal ["aaaaa", "", ""], "aaaaa".partition("x") + assert_equal ["", "", "aaaaa"], "aaaaa".partition("") + assert_equal ["", "a", "aaaa"], "aaaaa".partition("a") + assert_equal ["aaaa", "b", ""], "aaaab".partition("b") + assert_equal ["", "b", "aaaa"], "baaaa".partition("b") + assert_equal ["", "", ""], "".partition("a") +end + +assert('String#rpartition') do + assert_equal ["axa", "x", "a"], "axaxa".rpartition("x") + assert_equal ["", "", "aaaaa"], "aaaaa".rpartition("x") + assert_equal ["aaaaa", "", ""], "aaaaa".rpartition("") + assert_equal ["aaaa", "a", ""], "aaaaa".rpartition("a") + assert_equal ["aaaa", "b", ""], "aaaab".rpartition("b") + assert_equal ["", "b", "aaaa"], "baaaa".rpartition("b") + assert_equal ["", "", ""], "".rpartition("a") +end diff --git a/test/t/array.rb b/test/t/array.rb index 643a975b0..8ef1eee3f 100644 --- a/test/t/array.rb +++ b/test/t/array.rb @@ -46,6 +46,9 @@ assert('Array#[]', '15.2.12.5.4') do end assert_equal(2, [1,2,3].[](1)) + assert_equal(nil, [1,2,3].[](4)) + assert_equal(3, [1,2,3].[](-1)) + assert_equal(nil, [1,2,3].[](-4)) end assert('Array#[]=', '15.2.12.5.5') do @@ -81,8 +84,14 @@ end assert('Array#delete_at', '15.2.12.5.9') do a = [1,2,3] - a.delete_at(1) + assert_equal(2, a.delete_at(1)) assert_equal([1,3], a) + assert_equal(nil, a.delete_at(3)) + assert_equal([1,3], a) + assert_equal(nil, a.delete_at(-3)) + assert_equal([1,3], a) + assert_equal(3, a.delete_at(-1)) + assert_equal([1], a) end assert('Array#each', '15.2.12.5.10') do @@ -129,6 +138,7 @@ assert('Array#index', '15.2.12.5.14') do a = [1,2,3] assert_equal(1, a.index(2)) + assert_equal(nil, a.index(0)) end assert('Array#initialize', '15.2.12.5.15') do @@ -225,6 +235,7 @@ assert('Array#rindex', '15.2.12.5.26') do a = [1,2,3] assert_equal(1, a.rindex(2)) + assert_equal(nil, a.rindex(0)) end assert('Array#shift', '15.2.12.5.27') do diff --git a/test/t/class.rb b/test/t/class.rb index 680cd253c..a6ba336e3 100644 --- a/test/t/class.rb +++ b/test/t/class.rb @@ -235,6 +235,23 @@ assert('class to return the last value') do assert_equal(m, :m) end +assert('raise when superclass is not a class') do + module FirstModule; end + assert_raise(TypeError, 'should raise TypeError') do + class FirstClass < FirstModule; end + end + + class SecondClass; end + assert_raise(TypeError, 'should raise TypeError') do + class SecondClass < false; end + end + + class ThirdClass; end + assert_raise(TypeError, 'should raise TypeError') do + class ThirdClass < ThirdClass; end + end +end + assert('Class#inherited') do class Foo @@subclass_name = nil @@ -260,25 +277,40 @@ assert('Class#inherited') do end assert('singleton tests') do + module FooMod + def run_foo_mod + 100 + end + end + bar = String.new baz = class << bar + extend FooMod def self.run_baz 200 end end + assert_false baz.singleton_methods.include? :run_foo_mod assert_false baz.singleton_methods.include? :run_baz assert_raise(NoMethodError, 'should raise NoMethodError') do + baz.run_foo_mod + end + assert_raise(NoMethodError, 'should raise NoMethodError') do baz.run_baz end assert_raise(NoMethodError, 'should raise NoMethodError') do + bar.run_foo_mod + end + assert_raise(NoMethodError, 'should raise NoMethodError') do bar.run_baz end baz = class << bar + extend FooMod def self.run_baz 300 end @@ -286,9 +318,44 @@ assert('singleton tests') do end assert_true baz.singleton_methods.include? :run_baz + assert_true baz.singleton_methods.include? :run_foo_mod + assert_equal 100, baz.run_foo_mod assert_equal 300, baz.run_baz assert_raise(NoMethodError, 'should raise NoMethodError') do + bar.run_foo_mod + end + assert_raise(NoMethodError, 'should raise NoMethodError') do bar.run_baz end + + fv = false + class << fv + def self.run_false + 5 + end + end + + nv = nil + class << nv + def self.run_nil + 6 + end + end + + tv = true + class << tv + def self.run_nil + 7 + end + end + + assert_raise(TypeError, 'should raise TypeError') do + num = 1.0 + class << num + def self.run_nil + 7 + end + end + end end |
