aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/type/float_test.rb
blob: 2b2c99b8ced72e65f6e2f756b7488651ae9424d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true

require "cases/helper"

module ActiveModel
  module Type
    class FloatTest < ActiveSupport::TestCase
      def test_type_cast_float
        type = Type::Float.new
        assert_equal 1.0, type.cast("1")
      end

      def test_type_cast_float_from_invalid_string
        type = Type::Float.new
        assert_nil type.cast("")
        assert_equal 1.0, type.cast("1ignore")
        assert_equal 0.0, type.cast("bad1")
        assert_equal 0.0, type.cast("bad")
      end

      def test_changing_float
        type = Type::Float.new

        assert type.changed?(0.0, 0, "wibble")
        assert type.changed?(5.0, 0, "wibble")
        assert_not type.changed?(5.0, 5.0, "5wibble")
        assert_not type.changed?(5.0, 5.0, "5")
        assert_not type.changed?(5.0, 5.0, "5.0")
        assert_not type.changed?(500.0, 500.0, "0.5E+4")
        assert_not type.changed?(nil, nil, nil)
      end
    end
  end
end