attr_bool 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/ext_test.rb ADDED
@@ -0,0 +1,432 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ #--
5
+ # This file is part of AttrBool.
6
+ # Copyright (c) 2020 Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: MIT
9
+ #++
10
+
11
+ require 'test_helper'
12
+
13
+ describe AttrBool::Ext do
14
+ it 'does not monkey-patch the core Class & Module by default' do
15
+ _(Class.ancestors).wont_include(AttrBool::Ext)
16
+ _(Module.ancestors).wont_include(AttrBool::Ext)
17
+
18
+ # rubocop:disable Lint/ConstantDefinitionInBlock
19
+ expect do
20
+ class BadTestBag
21
+ attr_bool :acc01
22
+ end
23
+ end.must_raise(NoMethodError)
24
+ expect do
25
+ module BadTestBagMixin
26
+ attr_bool :acc01
27
+ end
28
+ end.must_raise(NoMethodError)
29
+ # rubocop:enable all
30
+
31
+ %i[BadTestBag BadTestBagMixin].each do |name|
32
+ Object.send(:remove_const,name) if Object.const_defined?(name)
33
+ end
34
+ end
35
+
36
+ def self.it_has_the_attr_bools
37
+ it 'has the attr accessors' do
38
+ @sut.acc01 = :acc01_value
39
+ @sut.acc02 = :acc02_value
40
+ @sut.acc03 = :acc0304_value
41
+ @sut.acc04 = :acc0304_value
42
+
43
+ _(@sut.acc01?).must_equal(:acc01_value)
44
+ _(@sut.acc02?).must_equal(:acc02_value)
45
+ _(@sut.acc03?).must_equal(:acc0304_value)
46
+ _(@sut.acc04?).must_equal(:acc0304_value)
47
+
48
+ @sut.acc07 = :acc07_value
49
+ @sut.acc08 = :acc08_value
50
+ @sut.acc09 = :acc0910_value
51
+ @sut.acc10 = :acc0910_value
52
+
53
+ _(@sut.acc07?).must_equal(true)
54
+ _(@sut.acc08?).must_equal(true)
55
+ _(@sut.acc09?).must_equal(true)
56
+ _(@sut.acc10?).must_equal(true)
57
+ end
58
+
59
+ it 'has the private attr accessors' do
60
+ pub_methods = @sut.public_methods
61
+ priv_methods = @sut.private_methods
62
+
63
+ [
64
+ ['acc05',:acc05_value],
65
+ ['acc06',:acc06_value],
66
+ ['acc11',true],
67
+ ['acc12',true],
68
+ ].each do |name,exp_value|
69
+ name_q = :"#{name}?"
70
+ name_eq = :"#{name}="
71
+
72
+ _(pub_methods).wont_include(name_q)
73
+ _(pub_methods).wont_include(name_eq)
74
+
75
+ _(priv_methods).must_include(name_q)
76
+ _(priv_methods).must_include(name_eq)
77
+
78
+ @sut.send(name_eq,:"#{name}_value")
79
+
80
+ _(@sut.send(name_q)).must_equal(exp_value)
81
+ end
82
+ end
83
+
84
+ it 'has the attr readers' do
85
+ _(@sut.read01?).must_equal(:read01_value)
86
+ _(@sut.read02?).must_equal(:read02_value)
87
+ _(@sut.read03?).must_equal(:read0304_value)
88
+ _(@sut.read04?).must_equal(:read0304_value)
89
+
90
+ _(@sut.read07?).must_equal(true)
91
+ _(@sut.read08?).must_equal(true)
92
+ _(@sut.read09?).must_equal(true)
93
+ _(@sut.read10?).must_equal(true)
94
+ end
95
+
96
+ it 'has the private attr readers' do
97
+ pub_methods = @sut.public_methods
98
+ priv_methods = @sut.private_methods
99
+
100
+ [
101
+ ['read05',:read05_value],
102
+ ['read06',:read06_value],
103
+ ['read11',true],
104
+ ['read12',true],
105
+ ].each do |name,exp_value|
106
+ name_q = :"#{name}?"
107
+
108
+ _(pub_methods).wont_include(name_q)
109
+ _(priv_methods).must_include(name_q)
110
+
111
+ @sut.instance_variable_set(:"@#{name}",:"#{name}_value")
112
+
113
+ _(@sut.send(name_q)).must_equal(exp_value)
114
+ end
115
+ end
116
+
117
+ it 'has the attr writers' do
118
+ @sut.write01 = :write01_value
119
+ @sut.write02 = :write02_value
120
+ @sut.write03 = :write0304_value
121
+ @sut.write04 = :write0304_value
122
+
123
+ _(@sut.instance_variable_get(:@write01)).must_equal(:write01_value)
124
+ _(@sut.instance_variable_get(:@write02)).must_equal(:write02_value)
125
+ _(@sut.instance_variable_get(:@write0304)).must_equal(:write0304_value)
126
+
127
+ @sut.write07 = :write07_value
128
+ @sut.write08 = :write08_value
129
+ @sut.write09 = :write0910_value
130
+ @sut.write10 = :write0910_value
131
+
132
+ _(@sut.instance_variable_get(:@write07)).must_equal(true)
133
+ _(@sut.instance_variable_get(:@write08)).must_equal(true)
134
+ _(@sut.instance_variable_get(:@write0910)).must_equal(true)
135
+ end
136
+
137
+ it 'has the private attr writers' do
138
+ pub_methods = @sut.public_methods
139
+ priv_methods = @sut.private_methods
140
+
141
+ [
142
+ ['write05',:write05_value],
143
+ ['write06',:write06_value],
144
+ ['write11',true],
145
+ ['write12',true],
146
+ ].each do |name,exp_value|
147
+ name_eq = :"#{name}="
148
+
149
+ _(pub_methods).wont_include(name_eq)
150
+ _(priv_methods).must_include(name_eq)
151
+
152
+ @sut.send(name_eq,:"#{name}_value")
153
+
154
+ _(@sut.instance_variable_get(:"@#{name}")).must_equal(exp_value)
155
+ end
156
+ end
157
+ end
158
+
159
+ describe 'extended class' do
160
+ before do
161
+ @sut = ExtTest::TestBag.new
162
+ end
163
+
164
+ it_has_the_attr_bools
165
+ end
166
+
167
+ describe 'extended class child' do
168
+ before do
169
+ @sut = ExtTest::TestBagChild.new
170
+ end
171
+
172
+ it_has_the_attr_bools
173
+
174
+ it 'has the child attr bools' do
175
+ _(@sut.read13?).must_equal(true)
176
+ end
177
+ end
178
+
179
+ describe 'extended module' do
180
+ before do
181
+ @sut = ExtTest::TestBagWithMixin.new
182
+ end
183
+
184
+ it_has_the_attr_bools
185
+ end
186
+
187
+ describe 'extended module child' do
188
+ before do
189
+ @sut = ExtTest::TestBagWithMixinChild.new
190
+ end
191
+
192
+ it_has_the_attr_bools
193
+
194
+ it 'has the child attr bools' do
195
+ _(@sut.read13?).must_equal(true)
196
+ _(@sut.read14?).must_equal(true)
197
+ end
198
+ end
199
+
200
+ describe 'extended module that was prepended' do
201
+ before do
202
+ @sut = ExtTest::TestBagWithMixinPrepended.new
203
+ end
204
+
205
+ it_has_the_attr_bools
206
+
207
+ it 'has the child attr bools' do
208
+ _(@sut.read13?).must_equal(true)
209
+ end
210
+ end
211
+
212
+ describe 'invalid attr names' do
213
+ it 'fails fast for attr_accessor?' do
214
+ expect do
215
+ Class.new do
216
+ extend AttrBool::Ext
217
+
218
+ attr_accessor? 'bad name'
219
+ end
220
+ end.must_raise(NameError)
221
+ end
222
+
223
+ it 'fails fast for attr_reader?' do
224
+ expect do
225
+ Class.new do
226
+ extend AttrBool::Ext
227
+
228
+ attr_reader? 'bad name'
229
+ end
230
+ end.must_raise(NameError)
231
+ end
232
+
233
+ it 'fails fast for attr_writer?' do
234
+ expect do
235
+ Class.new do
236
+ extend AttrBool::Ext
237
+
238
+ attr_writer? 'bad name'
239
+ end
240
+ end.must_raise(NameError)
241
+ end
242
+
243
+ it 'fails fast for attr_bool' do
244
+ expect do
245
+ Class.new do
246
+ extend AttrBool::Ext
247
+
248
+ attr_bool 'bad name'
249
+ end
250
+ end.must_raise(NameError)
251
+ end
252
+
253
+ it 'fails fast for attr_bool?' do
254
+ expect do
255
+ Class.new do
256
+ extend AttrBool::Ext
257
+
258
+ attr_bool? 'bad name'
259
+ end
260
+ end.must_raise(NameError)
261
+ end
262
+
263
+ it 'fails fast for attr_bool!' do
264
+ expect do
265
+ Class.new do
266
+ extend AttrBool::Ext
267
+
268
+ attr_bool! 'bad name'
269
+ end
270
+ end.must_raise(NameError)
271
+ end
272
+ end
273
+ end
274
+
275
+ # rubocop:disable Style/AccessModifierDeclarations
276
+ # noinspection RubyArgCount, RubyMismatchedArgumentType
277
+ module ExtTest
278
+ class TestBag
279
+ extend AttrBool::Ext
280
+
281
+ attr_accessor? :acc01,'acc02'
282
+ attr_accessor? :acc03,'acc04',
283
+ reader: -> { @acc0304 },
284
+ writer: ->(value) { @acc0304 = value }
285
+ private attr_accessor? :acc05,'acc06'
286
+
287
+ attr_bool :acc07,'acc08'
288
+ attr_bool :acc09,'acc10',
289
+ reader: -> { @acc0910 },
290
+ writer: ->(value) { @acc0910 = value }
291
+ private attr_bool :acc11,'acc12'
292
+
293
+ attr_reader? :read01,'read02'
294
+ attr_reader?(:read03,'read04') { @read0304 }
295
+ private attr_reader? :read05,'read06'
296
+
297
+ attr_bool? :read07,'read08'
298
+ attr_bool?(:read09,'read10') { @read0910 }
299
+ private attr_bool? :read11,'read12'
300
+
301
+ attr_writer? :write01,'write02'
302
+ attr_writer?(:write03,'write04') { |value| @write0304 = value }
303
+ private attr_writer? :write05,'write06'
304
+
305
+ attr_bool! :write07,'write08'
306
+ attr_bool!(:write09,'write10') { |value| @write0910 = value }
307
+ private attr_bool! :write11,'write12'
308
+
309
+ def initialize
310
+ super
311
+
312
+ @read01 = :read01_value
313
+ @read02 = :read02_value
314
+ @read0304 = :read0304_value
315
+ @read05 = :read05_value
316
+ @read06 = :read06_value
317
+
318
+ @read07 = :read07_value
319
+ @read08 = :read08_value
320
+ @read0910 = :read0910_value
321
+ @read11 = :read11_value
322
+ @read12 = :read12_value
323
+ end
324
+ end
325
+
326
+ class TestBagChild < TestBag
327
+ attr_bool? :read13
328
+
329
+ def initialize
330
+ super
331
+
332
+ @read13 = :read13_value
333
+ end
334
+ end
335
+
336
+ module TestBagMixin
337
+ extend AttrBool::Ext
338
+
339
+ attr_accessor? :acc01,'acc02'
340
+ attr_accessor? :acc03,'acc04',
341
+ reader: -> { @acc0304 },
342
+ writer: ->(value) { @acc0304 = value }
343
+ private attr_accessor? :acc05,'acc06'
344
+
345
+ attr_bool :acc07,'acc08'
346
+ attr_bool :acc09,'acc10',
347
+ reader: -> { @acc0910 },
348
+ writer: ->(value) { @acc0910 = value }
349
+ private attr_bool :acc11,'acc12'
350
+
351
+ attr_reader? :read01,'read02'
352
+ attr_reader?(:read03,'read04') { @read0304 }
353
+ private attr_reader? :read05,'read06'
354
+
355
+ attr_bool? :read07,'read08'
356
+ attr_bool?(:read09,'read10') { @read0910 }
357
+ private attr_bool? :read11,'read12'
358
+
359
+ attr_writer? :write01,'write02'
360
+ attr_writer?(:write03,'write04') { |value| @write0304 = value }
361
+ private attr_writer? :write05,'write06'
362
+
363
+ attr_bool! :write07,'write08'
364
+ attr_bool!(:write09,'write10') { |value| @write0910 = value }
365
+ private attr_bool! :write11,'write12'
366
+
367
+ def initialize
368
+ super
369
+
370
+ @read01 = :read01_value
371
+ @read02 = :read02_value
372
+ @read0304 = :read0304_value
373
+ @read05 = :read05_value
374
+ @read06 = :read06_value
375
+
376
+ @read07 = :read07_value
377
+ @read08 = :read08_value
378
+ @read0910 = :read0910_value
379
+ @read11 = :read11_value
380
+ @read12 = :read12_value
381
+ end
382
+ end
383
+
384
+ class TestBagWithMixin
385
+ include TestBagMixin
386
+
387
+ attr_bool? :read13
388
+
389
+ def initialize
390
+ super
391
+
392
+ @read13 = :read13_value
393
+ end
394
+ end
395
+
396
+ module TestBagMixinChild
397
+ include TestBagMixin
398
+
399
+ attr_bool? :read13
400
+
401
+ def initialize
402
+ super
403
+
404
+ @read13 = :read13_value
405
+ end
406
+ end
407
+
408
+ class TestBagWithMixinChild
409
+ include TestBagMixinChild
410
+
411
+ attr_bool? :read14
412
+
413
+ def initialize
414
+ super
415
+
416
+ @read14 = :read14_value
417
+ end
418
+ end
419
+
420
+ class TestBagWithMixinPrepended
421
+ prepend TestBagMixin
422
+
423
+ attr_bool? :read13
424
+
425
+ def initialize
426
+ super
427
+
428
+ @read13 = :read13_value
429
+ end
430
+ end
431
+ end
432
+ # rubocop:enable all
data/test/ref_test.rb ADDED
@@ -0,0 +1,142 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ #--
5
+ # This file is part of AttrBool.
6
+ # Copyright (c) 2025 Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: MIT
9
+ #++
10
+
11
+ require 'test_helper'
12
+
13
+ describe AttrBool::Ref do
14
+ def self.it_refines_the_core_class_and_module
15
+ it 'does not refine the core Class & Module outside of its scope' do
16
+ _(Class.used_modules).wont_include(AttrBool::Ref)
17
+ _(Module.used_modules).wont_include(AttrBool::Ref)
18
+
19
+ expect do
20
+ Class.new do
21
+ attr_bool :acc01
22
+ end
23
+ end.must_raise(NoMethodError)
24
+ expect do
25
+ Module.new do
26
+ attr_bool :acc01
27
+ end
28
+ end.must_raise(NoMethodError)
29
+ end
30
+
31
+ it 'refines the core Class & Module inside of its scope only once' do
32
+ # JRuby doesn't implement used_modules() currently.
33
+ if RUBY_PLATFORM != 'java'
34
+ _(@sut.class_used_modules.count(AttrBool::Ref)).must_equal(1)
35
+ _(@sut.module_used_modules.count(AttrBool::Ref)).must_equal(1)
36
+ end
37
+ end
38
+ end
39
+
40
+ def self.it_has_the_attr_bools
41
+ it 'has the attr accessors' do
42
+ _(@sut).must_respond_to(:acc01=)
43
+ _(@sut).must_respond_to(:acc02=)
44
+
45
+ @sut.acc01 = :acc01_value
46
+ @sut.acc02 = :acc02_value
47
+
48
+ _(@sut.acc01?).must_equal(:acc01_value)
49
+ _(@sut.acc02?).must_equal(true)
50
+ end
51
+
52
+ it 'has the attr readers' do
53
+ _(@sut.read01?).must_equal(:read01_value)
54
+ _(@sut.read02?).must_equal(true)
55
+ end
56
+
57
+ it 'has the attr writers' do
58
+ _(@sut).must_respond_to(:write01=)
59
+ _(@sut).must_respond_to(:write02=)
60
+
61
+ @sut.write01 = :write01_value
62
+ @sut.write02 = :write02_value
63
+
64
+ _(@sut.instance_variable_get(:@write01)).must_equal(:write01_value)
65
+ _(@sut.instance_variable_get(:@write02)).must_equal(true)
66
+ end
67
+ end
68
+
69
+ describe 'refined class' do
70
+ before do
71
+ @sut = RefTest::TestBag.new
72
+ end
73
+
74
+ it_refines_the_core_class_and_module
75
+ it_has_the_attr_bools
76
+ end
77
+
78
+ describe 'refined module' do
79
+ before do
80
+ @sut = RefTest::TestBagWithMixin.new
81
+ end
82
+
83
+ it_refines_the_core_class_and_module
84
+ it_has_the_attr_bools
85
+ end
86
+ end
87
+
88
+ module RefTest
89
+ using AttrBool::Ref
90
+
91
+ class TestBag
92
+ attr_accessor? :acc01
93
+ attr_bool :acc02
94
+ attr_reader? :read01
95
+ attr_bool? :read02
96
+ attr_writer? :write01
97
+ attr_bool! :write02
98
+
99
+ def initialize
100
+ super
101
+
102
+ @read01 = :read01_value
103
+ @read02 = :read02_value
104
+ end
105
+
106
+ def class_used_modules
107
+ return Class.used_modules
108
+ end
109
+
110
+ def module_used_modules
111
+ return Module.used_modules
112
+ end
113
+ end
114
+
115
+ module TestBagMixin
116
+ attr_accessor? :acc01
117
+ attr_bool :acc02
118
+ attr_reader? :read01
119
+ attr_bool? :read02
120
+ attr_writer? :write01
121
+ attr_bool! :write02
122
+
123
+ def initialize
124
+ super
125
+
126
+ @read01 = :read01_value
127
+ @read02 = :read02_value
128
+ end
129
+
130
+ def class_used_modules
131
+ return Class.used_modules
132
+ end
133
+
134
+ def module_used_modules
135
+ return Module.used_modules
136
+ end
137
+ end
138
+
139
+ class TestBagWithMixin
140
+ include TestBagMixin
141
+ end
142
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ #--
5
+ # This file is part of AttrBool.
6
+ # Copyright (c) 2020 Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: MIT
9
+ #++
10
+
11
+ require 'minitest/autorun'
12
+ require 'simplecov'
13
+
14
+ SimpleCov.start do
15
+ enable_coverage :branch
16
+ end
17
+
18
+ require 'attr_bool'