aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/configuration_test.rb
blob: a172fa26a37b96b4cfc37a283a19d5e2d5a24836 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require 'cases/helper'

class ConfigurationOnModuleTest < ActiveModel::TestCase
  def setup
    @mod = mod = Module.new do
      extend ActiveSupport::Concern
      extend ActiveModel::Configuration

      config_attribute :omg
      self.omg = "default"

      config_attribute :wtf, global: true
      self.wtf = "default"

      config_attribute :boolean

      config_attribute :lol, instance_writer: true
    end

    @klass = Class.new do
      include mod
    end

    @subklass = Class.new(@klass)
  end

  test "default" do
    assert_equal "default", @mod.omg
    assert_equal "default", @klass.omg
    assert_equal "default", @klass.new.omg
  end

  test "setting" do
    @mod.omg = "lol"
    assert_equal "lol", @mod.omg
  end

  test "setting on class including the module" do
    @klass.omg = "lol"
    assert_equal "lol", @klass.omg
    assert_equal "lol", @klass.new.omg
    assert_equal "default", @mod.omg
  end

  test "setting on subclass of class including the module" do
    @subklass.omg = "lol"
    assert_equal "lol", @subklass.omg
    assert_equal "default", @klass.omg
    assert_equal "default", @mod.omg
  end

  test "setting on instance" do
    assert !@klass.new.respond_to?(:omg=)

    @klass.lol = "lol"
    obj = @klass.new
    assert_equal "lol", obj.lol
    obj.lol = "omg"
    assert_equal "omg", obj.lol
    assert_equal "lol", @klass.lol
    assert_equal "lol", @klass.new.lol
    obj.lol = false
    assert !obj.lol?
  end

  test "global attribute" do
    assert_equal "default", @mod.wtf
    assert_equal "default", @klass.wtf

    @mod.wtf = "wtf"

    assert_equal "wtf", @mod.wtf
    assert_equal "wtf", @klass.wtf

    @klass.wtf = "lol"

    assert_equal "lol", @mod.wtf
    assert_equal "lol", @klass.wtf
  end

  test "boolean" do
    assert_equal false, @mod.boolean?
    assert_equal false, @klass.new.boolean?
    @mod.boolean = true
    assert_equal true, @mod.boolean?
    assert_equal true, @klass.new.boolean?
  end
end

class ConfigurationOnClassTest < ActiveModel::TestCase
  def setup
    @klass = Class.new do
      extend ActiveModel::Configuration

      config_attribute :omg
      self.omg = "default"

      config_attribute :wtf, global: true
      self.wtf = "default"

      config_attribute :omg2, instance_writer: true
      config_attribute :wtf2, instance_writer: true, global: true
    end

    @subklass = Class.new(@klass)
  end

  test "defaults" do
    assert_equal "default", @klass.omg
    assert_equal "default", @klass.wtf
    assert_equal "default", @subklass.omg
    assert_equal "default", @subklass.wtf
  end

  test "changing" do
    @klass.omg = "lol"
    assert_equal "lol", @klass.omg
    assert_equal "lol", @subklass.omg
  end

  test "changing in subclass" do
    @subklass.omg = "lol"
    assert_equal "lol", @subklass.omg
    assert_equal "default", @klass.omg
  end

  test "changing global" do
    @klass.wtf = "wtf"
    assert_equal "wtf", @klass.wtf
    assert_equal "wtf", @subklass.wtf

    @subklass.wtf = "lol"
    assert_equal "lol", @klass.wtf
    assert_equal "lol", @subklass.wtf
  end

  test "instance_writer" do
    obj = @klass.new

    @klass.omg2 = "omg"
    @klass.wtf2 = "wtf"

    assert_equal "omg", obj.omg2
    assert_equal "wtf", obj.wtf2

    obj.omg2 = "lol"
    obj.wtf2 = "lol"

    assert_equal "lol", obj.omg2
    assert_equal "lol", obj.wtf2
    assert_equal "omg", @klass.omg2
    assert_equal "lol", @klass.wtf2
  end
end