ruby-changes:74457
From: Kenta <ko1@a...>
Date: Sun, 13 Nov 2022 11:02:19 +0900 (JST)
Subject: [ruby-changes:74457] ef1c6109b1 (master): [ruby/bigdecimal] Tweak check_rounding_mode_option
https://git.ruby-lang.org/ruby.git/commit/?id=ef1c6109b1 From ef1c6109b12b78926b7cb31cd8b0d27b629a35c4 Mon Sep 17 00:00:00 2001 From: Kenta Murata <mrkn@m...> Date: Sun, 13 Nov 2022 11:01:09 +0900 Subject: [ruby/bigdecimal] Tweak check_rounding_mode_option https://github.com/ruby/bigdecimal/commit/e1c6c9be25 --- ext/bigdecimal/bigdecimal.c | 19 +++++++++---------- test/bigdecimal/test_bigdecimal.rb | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 40a825dfc7..5ae0b96ab4 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -630,18 +630,19 @@ check_rounding_mode_option(VALUE const opts) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L630 assert(RB_TYPE_P(opts, T_HASH)); if (NIL_P(opts)) - goto noopt; + goto no_opt; mode = rb_hash_lookup2(opts, ID2SYM(id_half), Qundef); if (mode == Qundef || NIL_P(mode)) - goto noopt; + goto no_opt; if (SYMBOL_P(mode)) mode = rb_sym2str(mode); else if (!RB_TYPE_P(mode, T_STRING)) { - VALUE str_mode = rb_check_string_type(mode); - if (NIL_P(str_mode)) goto invalid; - mode = str_mode; + VALUE str_mode = rb_check_string_type(mode); + if (NIL_P(str_mode)) + goto invalid; + mode = str_mode; } s = RSTRING_PTR(mode); l = RSTRING_LEN(mode); @@ -659,13 +660,11 @@ check_rounding_mode_option(VALUE const opts) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L660 default: break; } + invalid: - if (NIL_P(mode)) - rb_raise(rb_eArgError, "invalid rounding mode: nil"); - else - rb_raise(rb_eArgError, "invalid rounding mode: %"PRIsVALUE, mode); + rb_raise(rb_eArgError, "invalid rounding mode (%"PRIsVALUE")", mode); - noopt: + no_opt: return VpGetRoundMode(); } diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 0cd85249ad..0228240506 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -1370,8 +1370,18 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L1370 end def test_round_half_invalid_option - assert_raise_with_message(ArgumentError, "invalid rounding mode: invalid") { BigDecimal('12.5').round(half: :invalid) } - assert_raise_with_message(ArgumentError, "invalid rounding mode: invalid") { BigDecimal('2.15').round(1, half: :invalid) } + assert_raise_with_message(ArgumentError, "invalid rounding mode (upp)") do + BigDecimal('12.5').round(half: :upp) + end + assert_raise_with_message(ArgumentError, "invalid rounding mode (evenn)") do + BigDecimal('2.15').round(1, half: :evenn) + end + assert_raise_with_message(ArgumentError, "invalid rounding mode (downn)") do + BigDecimal('2.15').round(1, half: :downn) + end + assert_raise_with_message(ArgumentError, "invalid rounding mode (42)") do + BigDecimal('2.15').round(1, half: 42) + end end def test_truncate -- cgit v1.2.3 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/