-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_syntax_test.rb
177 lines (143 loc) · 3.74 KB
/
config_syntax_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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require File.expand_path('../../test_helper', __FILE__)
require 'configurable'
class ConfigSyntaxTest < Test::Unit::TestCase
class ConfigClass
include Configurable
config :str, 'one'
end
def test_basic_syntax
c = ConfigClass.new
assert_equal 'one', c.str
c.str = 'two'
assert_equal 'two', c.config[:str]
c.config[:str] = 'three'
assert_equal 'three', c.str
assert_equal({:str => 'three'}, c.config.to_hash)
end
class InitClass
include Configurable
config :str, 'one'
def initialize(configs={})
initialize_config(configs)
end
end
def test_init
c = InitClass.new(:str => 'two')
assert_equal 'two', c.str
end
class KeyNameClass
include Configurable
config :key, 'val', :name => 'name'
end
def test_key_name
c = KeyNameClass.new
assert_equal 'val', c.name
assert_equal 'val', c.config[:key]
end
class ReaderWriterClass
include Configurable
config :str, 'one', :reader => :get, :writer => :set
def get
@ivar
end
def set(value)
@ivar = value
end
end
def test_reader_writer
c = ReaderWriterClass.new
assert_equal 'one', c.get
c.set 'two'
assert_equal 'two', c.config[:str]
c.config[:str] = 'three'
assert_equal 'three', c.get
assert_equal({:str => 'three'}, c.config.to_hash)
end
def test_import_export
configs = KeyNameClass.configs
defaults = configs.to_default
assert_equal({:key => 'val'}, defaults)
static_data = configs.export(defaults)
assert_equal({'name' => 'val'}, static_data)
active_hash = configs.import({'name' => 'VAL'})
assert_equal({:key => 'VAL'}, active_hash)
c = KeyNameClass.new
assert_equal({:key => 'val'}, c.config.to_hash)
assert_equal({'name' => 'val'}, c.config.export)
c.config.import({'name' => 'VAL'})
assert_equal({:key => 'VAL'}, c.config.to_hash)
end
class ListClass
include Configurable
config :integers, [1, 2, 3]
end
def test_list_class
c = ListClass.new
assert_equal [1, 2, 3], c.integers
c.config.import('integers' => ['7', '8'])
assert_equal [7, 8], c.config[:integers]
end
class Parent
include Configurable
config :a, {:key => 'hash'}
config :b do
config :key, 'block'
end
class Child
include Configurable
config :key, 'instance'
end
config :c, Child.new
end
def test_nesting
c = Parent.new
expected = {
:a => {:key => 'hash'},
:b => {:key => 'block'},
:c => {:key => 'instance'}
}
assert_equal expected, c.config.to_hash
assert_equal 'hash', c.a.key
assert_equal 'hash', c.config[:a][:key]
c.config[:a][:key] = 'HASH'
assert_equal 'HASH', c.a.key
assert_equal({:key => 'HASH'}, c.a.config.to_hash)
c.a = Parent::A.new
assert_equal({:key => 'hash'}, c.config[:a])
c.config[:a] = {:key => 'HASH'}
assert_equal({:key => 'HASH'}, c.a.config.to_hash)
c.config.import('b' => {'key' => 'BLOCK'})
expected = {
'a' => {'key' => 'HASH'},
'b' => {'key' => 'BLOCK'},
'c' => {'key' => 'instance'}
}
assert_equal expected, c.config.export
end
class A
include Configurable
config :a, 'one'
end
module B
include Configurable
config :b, 'two'
end
class C < A
include B
config :c, 'three'
end
class D < C
config :a, 'ONE'
undef_config :c
end
def test_inheritance
c = C.new
assert_equal 'one' , c.a
assert_equal 'two' , c.b
assert_equal 'three', c.c
assert_equal({:a => 'one', :b => 'two', :c => 'three'}, c.config.to_hash)
d = D.new
assert_equal false, d.respond_to?(:c)
assert_equal({:a => 'ONE', :b => 'two'}, d.config.to_hash)
end
end