diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2019-07-29 01:05:26 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-07-29 01:05:26 +0900 |
| commit | 2ebc6d5b6f84c1e09cdd8be14ab173048ddeb043 (patch) | |
| tree | dd9ea74cec6a456ec87173745f10fea9e3de5017 /test/t | |
| parent | cd8fc9bcb81a8e9e7e76413f3132b0515c8bd218 (diff) | |
| parent | 1fb635ac03d3948898623126a8b3d7705e9cdb0f (diff) | |
| download | mruby-2ebc6d5b6f84c1e09cdd8be14ab173048ddeb043.tar.gz mruby-2ebc6d5b6f84c1e09cdd8be14ab173048ddeb043.zip | |
Merge pull request #4407 from shuujii/add-assert_raise_with_message-and-assert_raise_with_message_pattern
Add `assert_raise_with_message` and `assert_raise_with_message_pattern`
Diffstat (limited to 'test/t')
| -rw-r--r-- | test/t/kernel.rb | 10 | ||||
| -rw-r--r-- | test/t/module.rb | 24 |
2 files changed, 21 insertions, 13 deletions
diff --git a/test/t/kernel.rb b/test/t/kernel.rb index ecfb863a8..c2eeee1a5 100644 --- a/test/t/kernel.rb +++ b/test/t/kernel.rb @@ -355,17 +355,15 @@ assert('Kernel#method_missing', '15.3.1.3.30') do end end no_super_test = NoSuperMethodTestClass.new - begin + msg = "undefined method 'no_super_method_named_this'" + assert_raise_with_message(NoMethodError, msg) do no_super_test.no_super_method_named_this - rescue NoMethodError => e - assert_equal "undefined method 'no_super_method_named_this'", e.message end a = String.new - begin + msg = "undefined method 'no_method_named_this'" + assert_raise_with_message(NoMethodError, msg) do a.no_method_named_this - rescue NoMethodError => e - assert_equal "undefined method 'no_method_named_this'", e.message end end diff --git a/test/t/module.rb b/test/t/module.rb index 0155ec190..12b7f1344 100644 --- a/test/t/module.rb +++ b/test/t/module.rb @@ -21,6 +21,14 @@ def labeled_class(name, supklass = Object, &block) end end +def assert_uninitialized_const(&block) + assert_raise_with_message_pattern(NameError, "uninitialized constant *", &block) +end + +def assert_wrong_const_name(&block) + assert_raise_with_message_pattern(NameError, "wrong constant name *", &block) +end + assert('Module', '15.2.2') do assert_equal Class, Module.class end @@ -222,7 +230,7 @@ assert('Module#const_defined?', '15.2.2.4.20') do assert_true Test4ConstDefined.const_defined?(:Const4Test4ConstDefined) assert_false Test4ConstDefined.const_defined?(:NotExisting) - assert_raise(NameError){ Test4ConstDefined.const_defined?(:wrong_name) } + assert_wrong_const_name{ Test4ConstDefined.const_defined?(:wrong_name) } end assert('Module#const_get', '15.2.2.4.21') do @@ -235,9 +243,9 @@ assert('Module#const_get', '15.2.2.4.21') do assert_equal 42, Object.const_get("Test4ConstGet::Const4Test4ConstGet") assert_raise(TypeError){ Test4ConstGet.const_get(123) } - assert_raise(NameError){ Test4ConstGet.const_get(:I_DO_NOT_EXIST) } - assert_raise(NameError){ Test4ConstGet.const_get("I_DO_NOT_EXIST::ME_NEITHER") } - assert_raise(NameError){ Test4ConstGet.const_get(:wrong_name) } + assert_uninitialized_const{ Test4ConstGet.const_get(:I_DO_NOT_EXIST) } + assert_uninitialized_const{ Test4ConstGet.const_get("I_DO_NOT_EXIST::ME_NEITHER") } + assert_wrong_const_name{ Test4ConstGet.const_get(:wrong_name) } end assert('Module#const_set', '15.2.2.4.23') do @@ -248,7 +256,7 @@ assert('Module#const_set', '15.2.2.4.23') do assert_equal 23, Test4ConstSet.const_set(:Const4Test4ConstSet, 23) assert_equal 23, Test4ConstSet.const_get(:Const4Test4ConstSet) ["", "wrongNAME", "Wrong-Name"].each do |n| - assert_raise(NameError) { Test4ConstSet.const_set(n, 1) } + assert_wrong_const_name { Test4ConstSet.const_set(n, 1) } end end @@ -259,9 +267,11 @@ assert('Module#remove_const', '15.2.2.4.40') do assert_equal 23, Test4RemoveConst.remove_const(:ExistingConst) assert_false Test4RemoveConst.const_defined?(:ExistingConst) - assert_raise(NameError) { Test4RemoveConst.remove_const(:NonExistingConst) } + assert_raise_with_message_pattern(NameError, "constant * not defined") do + Test4RemoveConst.remove_const(:NonExistingConst) + end %i[x X!].each do |n| - assert_raise(NameError) { Test4RemoveConst.remove_const(n) } + assert_wrong_const_name { Test4RemoveConst.remove_const(n) } end assert_raise(FrozenError) { Test4RemoveConst.freeze.remove_const(:A) } end |
