[ruby/bigdecimal] Tweak check_rounding_mode_option

https://github.com/ruby/bigdecimal/commit/e1c6c9be25
This commit is contained in:
Kenta Murata 2022-11-13 11:01:09 +09:00 committed by git
parent 74c6e6e565
commit ef1c6109b1
2 changed files with 22 additions and 13 deletions

View File

@ -630,18 +630,19 @@ check_rounding_mode_option(VALUE const opts)
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)
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);
noopt:
invalid:
rb_raise(rb_eArgError, "invalid rounding mode (%"PRIsVALUE")", mode);
no_opt:
return VpGetRoundMode();
}

View File

@ -1370,8 +1370,18 @@ class TestBigDecimal < Test::Unit::TestCase
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