-
Notifications
You must be signed in to change notification settings - Fork 0
/
nest_config_test.rb
83 lines (62 loc) · 1.87 KB
/
nest_config_test.rb
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
require File.expand_path('../../../test_helper', __FILE__)
require 'configurable/config_classes'
require 'configurable/config_hash'
require 'ostruct'
class NestConfigTest < Test::Unit::TestCase
include Configurable::ConfigClasses
ConfigHash = Configurable::ConfigHash
class Parent < OpenStruct
end
class Child
class << self; attr_accessor :configs; end
attr_accessor :config
def initialize(store={})
@config = ConfigHash.new(store, self)
end
def initialize_copy(orig)
super
@config = ConfigHash.new(orig.config.store.dup, self)
end
end
attr_accessor :nest
def setup
Child.configs = {}
@nest = NestConfig.new(:key, :default => Child.new)
end
#
# get test
#
def test_get_returns_configs_on_child_configurable
child = Child.new
parent = Parent.new(:key => child)
assert_equal child.config, nest.get(parent)
end
#
# set test
#
def test_set_sets_child_configurable_on_receiver
child = Child.new
parent = Parent.new
nest.set(parent, child)
assert_equal child, parent.key
end
def test_set_merges_non_configurable_values_on_existing_child
child = Child.new(:one => 'one')
parent = Parent.new(:key => child)
assert_equal 'one', child.config[:one]
assert_equal nil, child.config[:two]
nest.set(parent, {:one => 'ONE', :two => 'TWO'})
assert_equal 'ONE', child.config[:one]
assert_equal 'TWO', child.config[:two]
end
def test_set_duplicates_configurable_to_initialize_missing_child
nest.configurable.config[:one] = 'one'
parent = Parent.new
assert_equal nil, parent.key
nest.set(parent, {:two => 'two'})
child = parent.key
assert_equal 'one', child.config[:one]
assert_equal 'two', child.config[:two]
assert_equal nil, nest.configurable.config[:two]
end
end