class ActiveModel::Type::DecimalTest

Public Instance Methods

test_changed?() click to toggle source
# File activemodel/test/cases/type/decimal_test.rb, line 58
def test_changed?
  type = Decimal.new

  assert type.changed?(5.0, 5.0, "5.0wibble")
  assert_not type.changed?(5.0, 5.0, "5.0")
  assert_not type.changed?(-5.0, -5.0, "-5.0")
end
test_scale_is_applied_before_precision_to_prevent_rounding_errors() click to toggle source
# File activemodel/test/cases/type/decimal_test.rb, line 66
def test_scale_is_applied_before_precision_to_prevent_rounding_errors
  type = Decimal.new(precision: 5, scale: 3)

  assert_equal BigDecimal("1.250"), type.cast(1.250473853637869)
  assert_equal BigDecimal("1.250"), type.cast("1.250473853637869")
end
test_type_cast_decimal() click to toggle source
# File activemodel/test/cases/type/decimal_test.rb, line 9
def test_type_cast_decimal
  type = Decimal.new
  assert_equal BigDecimal.new("0"), type.cast(BigDecimal.new("0"))
  assert_equal BigDecimal.new("123"), type.cast(123.0)
  assert_equal BigDecimal.new("1"), type.cast(:"1")
end
test_type_cast_decimal_from_float_with_large_precision() click to toggle source
# File activemodel/test/cases/type/decimal_test.rb, line 24
def test_type_cast_decimal_from_float_with_large_precision
  type = Decimal.new(precision: ::Float::DIG + 2)
  assert_equal BigDecimal.new("123.0"), type.cast(123.0)
end
test_type_cast_decimal_from_invalid_string() click to toggle source
# File activemodel/test/cases/type/decimal_test.rb, line 16
def test_type_cast_decimal_from_invalid_string
  type = Decimal.new
  assert_nil type.cast("")
  assert_equal BigDecimal.new("1"), type.cast("1ignore")
  assert_equal BigDecimal.new("0"), type.cast("bad1")
  assert_equal BigDecimal.new("0"), type.cast("bad")
end
test_type_cast_decimal_from_object_responding_to_d() click to toggle source
# File activemodel/test/cases/type/decimal_test.rb, line 49
def test_type_cast_decimal_from_object_responding_to_d
  value = Object.new
  def value.to_d
    BigDecimal.new("1")
  end
  type = Decimal.new
  assert_equal BigDecimal("1"), type.cast(value)
end
test_type_cast_decimal_from_rational_with_precision() click to toggle source
# File activemodel/test/cases/type/decimal_test.rb, line 34
def test_type_cast_decimal_from_rational_with_precision
  type = Decimal.new(precision: 2)
  assert_equal BigDecimal("0.33"), type.cast(Rational(1, 3))
end
test_type_cast_decimal_from_rational_with_precision_and_scale() click to toggle source
# File activemodel/test/cases/type/decimal_test.rb, line 39
def test_type_cast_decimal_from_rational_with_precision_and_scale
  type = Decimal.new(precision: 4, scale: 2)
  assert_equal BigDecimal("0.33"), type.cast(Rational(1, 3))
end
test_type_cast_decimal_from_rational_without_precision_defaults_to_18_36() click to toggle source
# File activemodel/test/cases/type/decimal_test.rb, line 44
def test_type_cast_decimal_from_rational_without_precision_defaults_to_18_36
  type = Decimal.new
  assert_equal BigDecimal("0.333333333333333333E0"), type.cast(Rational(1, 3))
end
test_type_cast_from_float_with_unspecified_precision() click to toggle source
# File activemodel/test/cases/type/decimal_test.rb, line 29
def test_type_cast_from_float_with_unspecified_precision
  type = Decimal.new
  assert_equal 22.68.to_d, type.cast(22.68)
end