aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/type/string_test.rb
blob: 9cc530e8db93b373f67fa1cb0800eb8cef2307f9 (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
# frozen_string_literal: true

require "cases/helper"

module ActiveModel
  module Type
    class StringTest < ActiveModel::TestCase
      test "type casting" do
        type = Type::String.new
        assert_equal "t", type.cast(true)
        assert_equal "f", type.cast(false)
        assert_equal "123", type.cast(123)
      end

      test "type casting for database" do
        type = Type::String.new
        object, array, hash = Object.new, [true], { a: :b }
        assert_equal object, type.serialize(object)
        assert_equal array, type.serialize(array)
        assert_equal hash, type.serialize(hash)
      end

      test "cast strings are mutable" do
        type = Type::String.new

        s = +"foo"
        assert_equal false, type.cast(s).frozen?
        assert_equal false, s.frozen?

        f = -"foo"
        assert_equal false, type.cast(f).frozen?
        assert_equal true, f.frozen?
      end

      test "values are duped coming out" do
        type = Type::String.new

        s = "foo"
        assert_not_same s, type.cast(s)
        assert_equal s, type.cast(s)
        assert_not_same s, type.deserialize(s)
        assert_equal s, type.deserialize(s)
      end
    end
  end
end