aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/type/boolean_test.rb
blob: 30c41ca80cb01211d62b5ece640871d72a550e49 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

require "cases/helper"

module ActiveModel
  module Type
    class BooleanTest < ActiveSupport::TestCase
      def test_type_cast_boolean
        type = Type::Boolean.new
        assert_predicate type.cast(""), :nil?
        assert_predicate type.cast(nil), :nil?

        assert type.cast(true)
        assert type.cast(1)
        assert type.cast("1")
        assert type.cast("t")
        assert type.cast("T")
        assert type.cast("true")
        assert type.cast("TRUE")
        assert type.cast("on")
        assert type.cast("ON")
        assert type.cast(" ")
        assert type.cast("\u3000\r\n")
        assert type.cast("\u0000")
        assert type.cast("SOMETHING RANDOM")
        assert type.cast(:"1")
        assert type.cast(:t)
        assert type.cast(:T)
        assert type.cast(:true)
        assert type.cast(:TRUE)
        assert type.cast(:on)
        assert type.cast(:ON)

        # explicitly check for false vs nil
        assert_equal false, type.cast(false)
        assert_equal false, type.cast(0)
        assert_equal false, type.cast("0")
        assert_equal false, type.cast("f")
        assert_equal false, type.cast("F")
        assert_equal false, type.cast("false")
        assert_equal false, type.cast("FALSE")
        assert_equal false, type.cast("off")
        assert_equal false, type.cast("OFF")
        assert_equal false, type.cast(:"0")
        assert_equal false, type.cast(:f)
        assert_equal false, type.cast(:F)
        assert_equal false, type.cast(:false)
        assert_equal false, type.cast(:FALSE)
        assert_equal false, type.cast(:off)
        assert_equal false, type.cast(:OFF)
      end
    end
  end
end