Skip to content

Commit

Permalink
Fix exception message raised in Kernel.BigDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkn committed Jan 12, 2021
1 parent 14e53ed commit d163f17
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
29 changes: 19 additions & 10 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ cannot_be_coerced_into_BigDecimal(VALUE exc_class, VALUE v)
static inline VALUE BigDecimal_div2(VALUE, VALUE, VALUE);
static VALUE rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception);
static VALUE rb_rational_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception);
static VALUE rb_cstr_convert_to_BigDecimal(const char *cstr, size_t digs, int raise_exception);
static VALUE rb_cstr_convert_to_BigDecimal(const char *c_str, size_t digs, int raise_exception);

static Real*
GetVpValueWithPrec(VALUE v, long prec, int must)
Expand Down Expand Up @@ -2863,10 +2863,15 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
case Qnil:
case Qtrue:
case Qfalse:
if (!raise_exception)
return Qnil;
rb_raise(rb_eTypeError,
"can't convert %"PRIsVALUE" into BigDecimal", val);
if (raise_exception) {
const char *cname = NIL_P(val) ? "nil" :
val == Qtrue ? "true" :
val == Qfalse ? "false" :
NULL;
rb_raise(rb_eTypeError,
"can't convert %s into BigDecimal", cname);
}
return Qnil;

default:
break;
Expand Down Expand Up @@ -2905,13 +2910,17 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
else if (RB_TYPE_P(val, T_STRING)) {
return rb_str_convert_to_BigDecimal(val, digs, raise_exception);
}

/* TODO: chheck to_d */
/* TODO: chheck to_int */
if (!raise_exception) {
VALUE str = rb_check_convert_type(val, T_STRING, "String", "to_str");
if (NIL_P(str))
return Qnil;
val = str;

VALUE str = rb_check_convert_type(val, T_STRING, "String", "to_str");
if (!RB_TYPE_P(str, T_STRING)) {
if (raise_exception) {
rb_raise(rb_eTypeError,
"can't convert %"PRIsVALUE" into BigDecimal", rb_obj_class(val));
}
return Qnil;
}
return rb_str_convert_to_BigDecimal(val, digs, raise_exception);
}
Expand Down
14 changes: 13 additions & 1 deletion test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,18 @@ def test_BigDecimal_with_exception_keyword
# assert_nothing_raised(RangeError) {
# assert_equal(nil, BigDecimal(1i, exception: false))
# }
assert_raise(TypeError) {
assert_raise_with_message(TypeError, "can't convert nil into BigDecimal") {
BigDecimal(nil, exception: true)
}
assert_raise_with_message(TypeError, "can't convert true into BigDecimal") {
BigDecimal(true, exception: true)
}
assert_raise_with_message(TypeError, "can't convert false into BigDecimal") {
BigDecimal(false, exception: true)
}
assert_raise_with_message(TypeError, "can't convert Object into BigDecimal") {
BigDecimal(Object.new, exception: true)
}
assert_nothing_raised(TypeError) {
assert_equal(nil, BigDecimal(nil, exception: false))
}
Expand All @@ -241,6 +250,9 @@ def test_BigDecimal_with_exception_keyword
assert_nothing_raised(TypeError) {
assert_equal(nil, BigDecimal(Object.new, exception: false))
}
assert_nothing_raised(TypeError) {
assert_equal(nil, BigDecimal(Object.new, exception: false))
}
# TODO: support to_d
# assert_nothing_raised(TypeError) {
# o = Object.new
Expand Down

0 comments on commit d163f17

Please sign in to comment.