From 1fb635ac03d3948898623126a8b3d7705e9cdb0f Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Fri, 26 Apr 2019 21:36:29 +0900 Subject: Add `assert_raise_with_message` and `assert_raise_with_message_pattern` --- test/assert.rb | 36 ++++++++++++++++++++++++++++++------ test/t/kernel.rb | 10 ++++------ test/t/module.rb | 24 +++++++++++++++++------- 3 files changed, 51 insertions(+), 19 deletions(-) (limited to 'test') diff --git a/test/assert.rb b/test/assert.rb index 121bd0a8e..da313bf6a 100644 --- a/test/assert.rb +++ b/test/assert.rb @@ -6,13 +6,17 @@ $skip_test = 0 $asserts = [] $test_start = Time.now if Object.const_defined?(:Time) +# For bintest on Ruby unless RUBY_ENGINE == "mruby" - # For bintest on Ruby def t_print(*args) print(*args) $stdout.flush nil end + + def _str_match?(pattern, str) + File.fnmatch?(pattern, str, File::FNM_EXTGLOB|File::FNM_DOTMATCH) + end end ## @@ -180,10 +184,7 @@ end def assert_match(*args); _assert_match(true, *args) end def assert_not_match(*args); _assert_match(false, *args) end def _assert_match(affirmed, pattern, str, msg = nil) - receiver, *args = RUBY_ENGINE == "mruby" ? - [self, :_str_match?, pattern, str] : - [File, :fnmatch?, pattern, str, File::FNM_EXTGLOB|File::FNM_DOTMATCH] - unless ret = !receiver.__send__(*args) == !affirmed + unless ret = _str_match?(pattern, str) == affirmed diff = " Expected #{pattern.inspect} to #{'not ' unless affirmed}match #{str.inspect}." end assert_true(ret, msg, diff) @@ -217,8 +218,9 @@ def assert_raise(*exc) exc = exc.empty? ? StandardError : exc.size == 1 ? exc[0] : exc begin yield - rescue *exc + rescue *exc => e pass + e rescue Exception => e diff = " #{exc} exception expected, not\n" \ " Class: <#{e.class}>\n" \ @@ -243,6 +245,28 @@ def assert_nothing_raised(msg = nil) end end +def assert_raise_with_message(*args, &block) + _assert_raise_with_message(:plain, *args, &block) +end +def assert_raise_with_message_pattern(*args, &block) + _assert_raise_with_message(:pattern, *args, &block) +end +def _assert_raise_with_message(type, exc, exp_msg, msg = nil, &block) + e = msg ? assert_raise(exc, msg, &block) : assert_raise(exc, &block) + e ? ($mrbtest_assert_idx -= 1) : (return e) + + err_msg = e.message + unless ret = type == :pattern ? _str_match?(exp_msg, err_msg) : exp_msg == err_msg + diff = " Expected Exception(#{exc}) was raised, but the message doesn't match.\n" + if type == :pattern + diff += " Expected #{exp_msg.inspect} to match #{err_msg.inspect}." + else + diff += assertion_diff(exp_msg, err_msg) + end + end + assert_true(ret, msg, diff) +end + def pass assert_true(true) end diff --git a/test/t/kernel.rb b/test/t/kernel.rb index bf7dbe94c..7fc8cd2e7 100644 --- a/test/t/kernel.rb +++ b/test/t/kernel.rb @@ -346,17 +346,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 09613e1bc..5c659f149 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 @@ -221,7 +229,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 @@ -234,9 +242,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 @@ -247,7 +255,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 @@ -258,9 +266,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 end -- cgit v1.2.3