y_support 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/lib/y_support/all.rb +40 -0
- data/lib/y_support/core_ext/array/misc.rb +45 -0
- data/lib/y_support/core_ext/array.rb +1 -0
- data/lib/y_support/core_ext/enumerable/misc.rb +32 -0
- data/lib/y_support/core_ext/enumerable.rb +1 -0
- data/lib/y_support/core_ext/hash/misc.rb +90 -0
- data/lib/y_support/core_ext/hash.rb +1 -0
- data/lib/y_support/core_ext/module/misc.rb +43 -0
- data/lib/y_support/core_ext/module.rb +2 -0
- data/lib/y_support/core_ext/numeric/misc.rb +13 -0
- data/lib/y_support/core_ext/numeric.rb +1 -0
- data/lib/y_support/core_ext/object/misc.rb +31 -0
- data/lib/y_support/core_ext/object.rb +1 -0
- data/lib/y_support/core_ext/string/misc.rb +80 -0
- data/lib/y_support/core_ext/string.rb +1 -0
- data/lib/y_support/core_ext/symbol/misc.rb +19 -0
- data/lib/y_support/core_ext/symbol.rb +1 -0
- data/lib/y_support/core_ext.rb +5 -0
- data/lib/y_support/inert_recorder.rb +51 -0
- data/lib/y_support/local_object.rb +39 -0
- data/lib/y_support/misc.rb +28 -0
- data/lib/y_support/name_magic.rb +373 -0
- data/lib/y_support/null_object.rb +96 -0
- data/lib/y_support/respond_to.rb +32 -0
- data/lib/y_support/stdlib_ext/matrix/misc.rb +134 -0
- data/lib/y_support/stdlib_ext/matrix.rb +2 -0
- data/lib/y_support/stdlib_ext.rb +3 -0
- data/lib/y_support/typing/array/typing.rb +17 -0
- data/lib/y_support/typing/array.rb +1 -0
- data/lib/y_support/typing/enumerable/typing.rb +75 -0
- data/lib/y_support/typing/enumerable.rb +1 -0
- data/lib/y_support/typing/hash/typing.rb +76 -0
- data/lib/y_support/typing/hash.rb +1 -0
- data/lib/y_support/typing/module/typing.rb +42 -0
- data/lib/y_support/typing/module.rb +1 -0
- data/lib/y_support/typing/object/typing.rb +178 -0
- data/lib/y_support/typing/object.rb +1 -0
- data/lib/y_support/typing.rb +43 -0
- data/lib/y_support/unicode.rb +76 -0
- data/lib/y_support/version.rb +3 -0
- data/lib/y_support.rb +33 -0
- data/test/inert_recorder_test.rb +34 -0
- data/test/local_object_test.rb +37 -0
- data/test/misc_test/test_module/fixture_class.rb +8 -0
- data/test/misc_test.rb +289 -0
- data/test/name_magic_test.rb +57 -0
- data/test/null_object_test.rb +50 -0
- data/test/respond_to_test.rb +46 -0
- data/test/typing_test.rb +213 -0
- data/test/unicode_test.rb +39 -0
- data/y_support.gemspec +22 -0
- metadata +137 -0
data/test/misc_test.rb
ADDED
@@ -0,0 +1,289 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'shoulda'
|
6
|
+
|
7
|
+
class MiscTest < Test::Unit::TestCase
|
8
|
+
context "Object" do
|
9
|
+
setup do
|
10
|
+
require 'y_support/core_ext/object'
|
11
|
+
end
|
12
|
+
|
13
|
+
should "have #const_set_if_not_defined" do
|
14
|
+
( ◉ = Object.new ).const_set_if_not_defined :KOKO, 42
|
15
|
+
assert_equal 42, ◉.singleton_class::KOKO
|
16
|
+
◉.const_set_if_not_defined :KOKO, 43
|
17
|
+
assert_equal 42, ◉.singleton_class::KOKO
|
18
|
+
end
|
19
|
+
|
20
|
+
should "have #const_redef_without_warning" do
|
21
|
+
( ◉ = Object.new ).const_set_if_not_defined :KOKO, 42
|
22
|
+
◉.const_redefine_without_warning :KOKO, 43
|
23
|
+
assert_equal 43, ◉.singleton_class::KOKO
|
24
|
+
end
|
25
|
+
end # context Object
|
26
|
+
|
27
|
+
context "Module" do
|
28
|
+
setup do
|
29
|
+
require 'y_support/core_ext/module'
|
30
|
+
end
|
31
|
+
|
32
|
+
should "have working #attr_accessor_with_default" do
|
33
|
+
class Klass
|
34
|
+
attr_accessor_with_default :hello do "world" end
|
35
|
+
end
|
36
|
+
assert_equal "world", Klass.new.hello
|
37
|
+
instance = Klass.new
|
38
|
+
instance.hello = "certain string" # testing setter
|
39
|
+
assert_equal "certain string", instance.hello # testing getter
|
40
|
+
end
|
41
|
+
|
42
|
+
should "have working autoreq" do
|
43
|
+
module TestModule
|
44
|
+
autoreq :fixture_class, descending_path: '.', ascending_path_prefix: '.'
|
45
|
+
end
|
46
|
+
assert_equal "world", TestModule::FixtureClass.new.hello
|
47
|
+
end
|
48
|
+
end # context Module
|
49
|
+
|
50
|
+
context "Enumerable" do
|
51
|
+
setup do
|
52
|
+
require 'y_support/core_ext/enumerable'
|
53
|
+
end
|
54
|
+
|
55
|
+
should "introduce #all_kind_of? collection qualifier" do
|
56
|
+
assert_equal true, [ 1, 1.0 ].all_kind_of?( Numeric )
|
57
|
+
assert_equal false, [ 1, [1.0] ].all_kind_of?( Numeric )
|
58
|
+
end
|
59
|
+
|
60
|
+
should "introduce #all_numeric? collection qualifier" do
|
61
|
+
assert_equal true, [1, 1.0].all_numeric?
|
62
|
+
assert_equal false, [:a, 1].all_numeric?
|
63
|
+
end
|
64
|
+
|
65
|
+
should "have #subset_of? collection qualifier" do
|
66
|
+
assert_equal( true, [1,2].subset_of?( [1,2,3,4] ) )
|
67
|
+
assert_equal( false, [1,2].subset_of?( [2,3] ) )
|
68
|
+
assert_equal( true, [1,2].subset_of?( [1,2] ) )
|
69
|
+
assert_equal( true, [1, 1.0].subset_of?( [1.0, 2.0] ) )
|
70
|
+
end
|
71
|
+
end # context Enumerable
|
72
|
+
|
73
|
+
context "Array" do
|
74
|
+
setup do
|
75
|
+
require 'y_support/core_ext/array'
|
76
|
+
end
|
77
|
+
|
78
|
+
should "have #to_hash" do
|
79
|
+
assert_equal( {a: :b, c: :d}, [[:a, :b],[:c, :d]].to_hash )
|
80
|
+
assert_equal( {k: :kokot, p: :pica}, [[:k, :o, :kokot], [:p, :i, :pica]].to_hash(2) )
|
81
|
+
end
|
82
|
+
|
83
|
+
should "have #each_consecutive_pair iterator" do
|
84
|
+
assert_kind_of Enumerator, [].each_consecutive_pair
|
85
|
+
assert_kind_of Enumerator, [:a].each_consecutive_pair
|
86
|
+
assert_kind_of Enumerator, [:a, :b].each_consecutive_pair
|
87
|
+
assert_kind_of Enumerator, [:a, :b, :c].each_consecutive_pair
|
88
|
+
assert_equal [], [].each_consecutive_pair.collect{|e| e }
|
89
|
+
assert_equal [], [:a].each_consecutive_pair.collect{|e| e }
|
90
|
+
assert_equal [[:a, :b]], [:a, :b].each_consecutive_pair.collect{|e| e }
|
91
|
+
assert_equal [[:a, :b], [:b, :c]], [:a, :b, :c].each_consecutive_pair.collect{|e| e }
|
92
|
+
end
|
93
|
+
|
94
|
+
should "have #to_proc in style &[ function, *arguments ]" do
|
95
|
+
assert_equal [2, 3], [1, 2].map( &[:+, 1] )
|
96
|
+
end
|
97
|
+
end # context Array
|
98
|
+
|
99
|
+
context "Hash" do
|
100
|
+
setup do
|
101
|
+
require 'y_support/core_ext/hash'
|
102
|
+
end
|
103
|
+
|
104
|
+
should "have #default! custom defaulter" do
|
105
|
+
defaults = { a: 1, b: nil }
|
106
|
+
test = {}
|
107
|
+
result = test.default!( defaults )
|
108
|
+
assert_equal defaults, result
|
109
|
+
assert_equal result.object_id, test.object_id
|
110
|
+
test = { a: 11, b: 22 }
|
111
|
+
assert_equal( { a: 11, b: 22 }, test.default!( defaults ) )
|
112
|
+
test = { a: 11, c: 22 }
|
113
|
+
assert_equal( { a: 11, b: nil, c: 22 }, test.default!( defaults ) )
|
114
|
+
end
|
115
|
+
|
116
|
+
should "have #with_keys and #modify_keys" do
|
117
|
+
assert_equal( {"a" => :b, "c" => :d}, {a: :b, c: :d}.with_keys( &:to_s ) )
|
118
|
+
assert_equal( {"a1" => 1, "c2" => 2}, {a: 1, c: 2}.modify_keys { |k, v|
|
119
|
+
k.to_s + v.to_s } )
|
120
|
+
assert_equal( {"a1" => 1, "c2" => 2}, {a: 1, c: 2}.modify_keys {|p|
|
121
|
+
p[0].to_s + p[1].to_s } )
|
122
|
+
assert_equal( {2 => 1, 4 => 2}, {1 => 1, 2 => 2}.modify_keys { |k, v|
|
123
|
+
k + v } )
|
124
|
+
assert_equal( {2 => 1, 4 => 2}, {1 => 1, 2 => 2}.modify_keys { |p|
|
125
|
+
p[0] + p[1] } )
|
126
|
+
end
|
127
|
+
|
128
|
+
should "have #with_values and #modify_values" do
|
129
|
+
assert_equal( { a: "b", c: "d" }, {a: :b, c: :d}.with_values( &:to_s ) )
|
130
|
+
assert_equal( {a: "ab", c: "cd"}, {a: :b, c: :d}.modify_values { |k, v|
|
131
|
+
k.to_s + v.to_s } )
|
132
|
+
assert_equal( {a: "ab", c: "cd"}, {a: :b, c: :d}.modify_values { |p|
|
133
|
+
p[0].to_s + p[1].to_s } )
|
134
|
+
hh = { a: 1, b: 2 }
|
135
|
+
hh.with_values! &:to_s
|
136
|
+
assert_equal ["1", "2"], hh.values
|
137
|
+
hh.modify_values! &:join
|
138
|
+
assert_equal ["a1", "b2"], hh.values
|
139
|
+
end
|
140
|
+
|
141
|
+
should "have #modify" do
|
142
|
+
assert_equal( { ab: "ba", cd: "dc" },
|
143
|
+
{ a: :b, c: :d }
|
144
|
+
.modify { |k, v| ["#{k}#{v}".to_sym, "#{v}#{k}"] } )
|
145
|
+
end
|
146
|
+
|
147
|
+
should "have #dot! meta patcher for dotted access to keys" do
|
148
|
+
h = Hash.new.merge!(aaa: 1, taint: 2)
|
149
|
+
assert_raise ArgumentError do h.dot! end
|
150
|
+
assert_nothing_raised do h.dot!( overwrite_methods: true ) end
|
151
|
+
assert_equal( {aaa: 1}, {aaa: 1}.dot! )
|
152
|
+
end
|
153
|
+
end # context Hash
|
154
|
+
|
155
|
+
context "Matrix" do
|
156
|
+
setup do
|
157
|
+
require 'y_support/stdlib_ext/matrix'
|
158
|
+
end
|
159
|
+
|
160
|
+
should "have #pp method" do
|
161
|
+
assert_respond_to Matrix[[1, 2], [3, 4]], :pretty_print
|
162
|
+
assert_respond_to Matrix[[1, 2], [3, 4]], :pp
|
163
|
+
end
|
164
|
+
|
165
|
+
should "have #correspondence_matrix method" do
|
166
|
+
assert_respond_to Matrix, :correspondence_matrix
|
167
|
+
assert_equal Matrix[[1, 0, 0], [0, 1, 0]],
|
168
|
+
Matrix.correspondence_matrix( [:a, :b, :c], [:a, :b] )
|
169
|
+
assert_equal Matrix.column_vector( [1, 2] ),
|
170
|
+
Matrix.correspondence_matrix( [:a, :b, :c], [:a, :b] ) *
|
171
|
+
Matrix.column_vector( [1, 2, 3] )
|
172
|
+
assert_equal 2, Matrix.correspondence_matrix( [1, 2], [1] ).column_size
|
173
|
+
end
|
174
|
+
|
175
|
+
should "have #column_to_a & #row_to_a" do
|
176
|
+
assert_equal [1, 2, 3], Matrix[[1], [2], [3]].column_to_a
|
177
|
+
assert_equal [2, 3, 4], Matrix[[1, 2], [2, 3], [3, 4]].column_to_a( 1 )
|
178
|
+
assert_equal nil, Matrix.empty( 5, 0 ).column_to_a
|
179
|
+
assert_equal [1], Matrix[[1], [2], [3]].row_to_a
|
180
|
+
assert_equal [3], Matrix[[1], [2], [3]].row_to_a( 2 )
|
181
|
+
end
|
182
|
+
|
183
|
+
should "have aliased #row_vector, #column_vector methods" do
|
184
|
+
assert_equal Matrix.column_vector( [1, 2, 3] ),
|
185
|
+
Matrix.cv( [1, 2, 3] )
|
186
|
+
assert_equal Matrix.row_vector( [1, 2, 3] ),
|
187
|
+
Matrix.rv( [1, 2, 3] )
|
188
|
+
end
|
189
|
+
|
190
|
+
should "have #join_bottom and #join_right" do
|
191
|
+
assert_equal Matrix[[1, 2], [3, 4]],
|
192
|
+
Matrix[[1, 2]].join_bottom( Matrix[[3, 4]] )
|
193
|
+
assert_equal Matrix[[1, 2, 3, 4]],
|
194
|
+
Matrix[[1, 2]].join_right( Matrix[[3, 4]] )
|
195
|
+
end
|
196
|
+
|
197
|
+
should "have aliased #row_size, #column_size methods" do
|
198
|
+
assert_equal 3, Matrix.zero(3, 2).height
|
199
|
+
assert_equal 3, Matrix.zero(3, 2).number_of_rows
|
200
|
+
assert_equal 2, Matrix.zero(3, 2).width
|
201
|
+
assert_equal 2, Matrix.zero(3, 2).number_of_columns
|
202
|
+
end
|
203
|
+
end # context Matrix
|
204
|
+
|
205
|
+
context "String" do
|
206
|
+
setup do
|
207
|
+
require 'y_support/core_ext/string'
|
208
|
+
end
|
209
|
+
|
210
|
+
should "have #can_be_integer? returning the integer or false if not convertible" do
|
211
|
+
assert_equal 33, " 33".to_Integer
|
212
|
+
assert_equal 8, " 010 ".to_Integer
|
213
|
+
assert_equal false, "garbage".to_Integer
|
214
|
+
end
|
215
|
+
|
216
|
+
should "have #can_be_float? returning the float or false if not convertible" do
|
217
|
+
assert_equal 22.2, ' 2.22e1'.to_Float
|
218
|
+
assert_equal 10, " 010 ".to_Float
|
219
|
+
assert_equal false, "garbage".to_Float
|
220
|
+
end
|
221
|
+
|
222
|
+
should "have #default! defaulter" do
|
223
|
+
assert_equal "default", "".default!("default")
|
224
|
+
assert_equal "default", " ".default!(:default)
|
225
|
+
assert_equal "default", " \n ".default!("default")
|
226
|
+
assert_equal "kokot", "kokot".default!("default")
|
227
|
+
a = ""
|
228
|
+
assert_equal a.object_id, a.default!("tata").object_id
|
229
|
+
end
|
230
|
+
|
231
|
+
should "have #stripn upgrade of #strip, which also strips newlines" do
|
232
|
+
assert_equal "test test", " \n test test \n\n \n ".stripn
|
233
|
+
end
|
234
|
+
|
235
|
+
should "have #compact for joining indented lines (esp. heredocs)" do
|
236
|
+
assert_equal "test test test",
|
237
|
+
"test\n test\n\n \n test\n ".wring_heredoc
|
238
|
+
funny_string = <<-FUNNY_STRING.wring_heredoc
|
239
|
+
This
|
240
|
+
is
|
241
|
+
a funny string.
|
242
|
+
FUNNY_STRING
|
243
|
+
assert_equal( 'This is a funny string.', funny_string )
|
244
|
+
end
|
245
|
+
|
246
|
+
should "be able #underscore_spaces" do
|
247
|
+
assert_equal "te_st_test", "te st test".underscore_spaces
|
248
|
+
end
|
249
|
+
|
250
|
+
should "have #symbolize stripping, removing capitalization and diacritics " \
|
251
|
+
'as if to make a suitable symbol material' do
|
252
|
+
assert_equal "Yes_sir!", " \nYes, sir!.; \n \n".standardize
|
253
|
+
end
|
254
|
+
|
255
|
+
should "have #to_standardized_sym chaining #standardize and #to_sym" do
|
256
|
+
assert_equal :Yes, " \nYes,.; \n \n".to_standardized_sym
|
257
|
+
end
|
258
|
+
end # context String
|
259
|
+
|
260
|
+
context "Symbol" do
|
261
|
+
setup do
|
262
|
+
require 'y_support/core_ext/symbol'
|
263
|
+
end
|
264
|
+
|
265
|
+
should "have #default! defaulter going through String#default!" do
|
266
|
+
assert_equal :default, "".to_sym.default!(:default)
|
267
|
+
assert_equal :default, "".to_sym.default!("default")
|
268
|
+
assert_equal :default, " ".to_sym.default!("default")
|
269
|
+
assert_equal :default, " \n ".to_sym.default!("default")
|
270
|
+
assert_equal :kokot, :kokot.default!("default")
|
271
|
+
end
|
272
|
+
|
273
|
+
should "have #to_standardized_sym" do
|
274
|
+
assert_equal :Yes, (:" \nYes, \n").to_standardized_sym
|
275
|
+
end
|
276
|
+
end # context Symbol
|
277
|
+
|
278
|
+
context "Numeric" do
|
279
|
+
setup do
|
280
|
+
require 'y_support/core_ext/numeric'
|
281
|
+
end
|
282
|
+
|
283
|
+
should "have #zero public class methods" do
|
284
|
+
assert_equal 0, Integer.zero
|
285
|
+
assert_equal 0.0, Float.zero
|
286
|
+
assert_equal Complex(0, 0), Complex.zero
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end # class YSupportTest
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'shoulda'
|
6
|
+
require 'minitest/spec'
|
7
|
+
require 'minitest/autorun'
|
8
|
+
require 'y_support/name_magic'
|
9
|
+
|
10
|
+
describe NameMagic do
|
11
|
+
before do
|
12
|
+
mod = Module.new do include NameMagic end
|
13
|
+
@ç = Class.new do include mod end
|
14
|
+
@reporter = Object.new
|
15
|
+
puts "..."
|
16
|
+
@reporter.singleton_class.class_exec { attr_reader :report, :naming }
|
17
|
+
@ç.new_instance_closure do |instance|
|
18
|
+
@reporter.define_singleton_method :report do
|
19
|
+
"New instance reported"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
@ç.name_set_closure do |name, instance, old_name|
|
23
|
+
@reporter.define_singleton_method :name_set do
|
24
|
+
"Name of the new instance was #{name}"
|
25
|
+
end
|
26
|
+
name
|
27
|
+
end
|
28
|
+
@ç.name_get_closure do |name_object|
|
29
|
+
@reporter.define_singleton_method :name_get do
|
30
|
+
"Name get closure called on #{name_object}"
|
31
|
+
end
|
32
|
+
name_object
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should work" do
|
37
|
+
@ç.must_respond_to :const_magic
|
38
|
+
@ç.instances.must_be_empty
|
39
|
+
@ç.nameless_instances.must_be_empty
|
40
|
+
@reporter.report.must_equal nil
|
41
|
+
x = @ç.new( name: "Boris" )
|
42
|
+
@reporter.report.must_equal "New instance reported"
|
43
|
+
@reporter.name_set.must_equal "Name of the new instance was Boris"
|
44
|
+
x.name.must_equal :Boris
|
45
|
+
@reporter.name_get.must_equal "Name get closure called on Boris"
|
46
|
+
ufo = @ç.new
|
47
|
+
@ç.nameless_instances.must_equal [ufo]
|
48
|
+
UFO = @ç.new
|
49
|
+
@reporter.report.must_equal "New instance reported"
|
50
|
+
@reporter.name_set.must_equal "Name of the new instance was Boris"
|
51
|
+
UFO.name
|
52
|
+
@reporter.name_set.must_equal "Name of the new instance was UFO"
|
53
|
+
@reporter.name_get.must_equal "Name get closure called on UFO"
|
54
|
+
Elaine = @ç.new
|
55
|
+
Elaine.name.must_equal :Elaine
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'shoulda'
|
6
|
+
|
7
|
+
class NullObjectTest < Test::Unit::TestCase
|
8
|
+
context 'Object' do
|
9
|
+
setup do
|
10
|
+
require 'y_support/null_object'
|
11
|
+
end
|
12
|
+
|
13
|
+
should "Object have #null_object? (alias #null?)" do
|
14
|
+
require 'y_support/null_object'
|
15
|
+
assert_equal true, (class Koko < NullObject; self end).new.null_object?
|
16
|
+
assert_equal true, (class Koko < NullObject; self end).new.null?
|
17
|
+
assert_equal [false, false], [nil.null_object?, nil.null?]
|
18
|
+
assert_equal true, NullObject.new( :koko ).null?( :koko )
|
19
|
+
assert_equal false, NullObject.new( :koko ).null?( :pipi )
|
20
|
+
end
|
21
|
+
|
22
|
+
should "Object have #Maybe() constructor for something / NullObject" do
|
23
|
+
assert_equal NullObject, Maybe(nil).class
|
24
|
+
assert_equal 42, Maybe(42)
|
25
|
+
end
|
26
|
+
|
27
|
+
should "Object have #Null() constructor always returning NullObject" do
|
28
|
+
assert_equal NullObject, Null().class
|
29
|
+
end
|
30
|
+
end # context Object
|
31
|
+
|
32
|
+
context "NullObject" do
|
33
|
+
setup do
|
34
|
+
require 'y_support/null_object'
|
35
|
+
end
|
36
|
+
|
37
|
+
should "NullObject exist and comply" do
|
38
|
+
n = NullObject.new
|
39
|
+
assert_equal [[], "#<NullObject>", 0.0, 0], [n.to_a, n.to_s, n.to_f, n.to_i]
|
40
|
+
assert_equal [false, true], [n.present?, n.empty?]
|
41
|
+
assert_nothing_raised { NullObject.new.
|
42
|
+
must_have_attr_reader( :recorded_messages ) }
|
43
|
+
assert_respond_to NullObject.new, :arbitrary_message
|
44
|
+
n = NullObject.new :x
|
45
|
+
n.arbitrary_message( :a, :b ) { "hello" }
|
46
|
+
assert_equal :x, n.null_object_signature
|
47
|
+
assert_equal "#<NullObject kokotina>", NullObject.new( :kokotina ).inspect
|
48
|
+
end
|
49
|
+
end # context NullObject
|
50
|
+
end # class NullObjectTest
|
@@ -0,0 +1,46 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'shoulda'
|
6
|
+
|
7
|
+
class RespondToTest < Test::Unit::TestCase
|
8
|
+
context "Object" do
|
9
|
+
setup do
|
10
|
+
require 'y_support/respond_to'
|
11
|
+
end
|
12
|
+
|
13
|
+
should "have RespondTo() constructor" do
|
14
|
+
assert_equal RespondTo, RespondTo( :inspect ).class
|
15
|
+
end
|
16
|
+
end # context Object
|
17
|
+
|
18
|
+
context "RespondTo" do
|
19
|
+
should "work" do
|
20
|
+
assert_respond_to( RespondTo.new(:hello), :=== )
|
21
|
+
assert RespondTo.new(:each_char) === "arbitrary string"
|
22
|
+
assert ! ( RespondTo.new(:each_char) === Object.new )
|
23
|
+
assert ! ( RespondTo.new(:improbab_method_name) === Object.new )
|
24
|
+
# Now testing the intended RespondTo usage in case statements.
|
25
|
+
assert case ?x
|
26
|
+
when RespondTo.new(:each_char) then 1
|
27
|
+
else false end
|
28
|
+
assert ! case ?x
|
29
|
+
when RespondTo.new(:improbab_method_name) then 1
|
30
|
+
else false end
|
31
|
+
end
|
32
|
+
end # context RespondTo
|
33
|
+
|
34
|
+
context "Symbol" do
|
35
|
+
should "have Symbol#~@ for .respond_to? case statements" do
|
36
|
+
assert_kind_of RespondTo, ~:hello
|
37
|
+
assert RespondTo(:<<) === "testing"
|
38
|
+
assert case ?x
|
39
|
+
when ~:each_char then 1
|
40
|
+
else false end
|
41
|
+
assert ! case ?x
|
42
|
+
when ~:improbab_method_name then 1
|
43
|
+
else false end
|
44
|
+
end
|
45
|
+
end # context Symbol
|
46
|
+
end # class RespondToTest
|
data/test/typing_test.rb
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'shoulda'
|
6
|
+
require 'y_support/typing'
|
7
|
+
|
8
|
+
class TypingTest < Test::Unit::TestCase
|
9
|
+
P = Class.new
|
10
|
+
K = Class.new
|
11
|
+
L = Module.new
|
12
|
+
|
13
|
+
context "with some classes" do
|
14
|
+
setup do
|
15
|
+
@p = P.new
|
16
|
+
@k = K.new
|
17
|
+
@l = L
|
18
|
+
end
|
19
|
+
|
20
|
+
should "have working class compliance methods" do
|
21
|
+
assert @p.class_complies?( @p.class )
|
22
|
+
assert ! @p.class_complies?( @k.class )
|
23
|
+
@p.declare_class_compliance! @k.class
|
24
|
+
assert @p.class_declares_compliance?( @k.class )
|
25
|
+
assert_equal [ @k.class ], @p.declared_class_compliance
|
26
|
+
assert_equal [], @k.declared_class_compliance
|
27
|
+
assert @p.class_complies? Object
|
28
|
+
o = Object.new
|
29
|
+
assert_equal false, o.class_complies?( @l )
|
30
|
+
o.extend @l
|
31
|
+
assert_equal true, o.class_complies?( @l )
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "general" do
|
36
|
+
should "have AErr alias for ArgumentError" do
|
37
|
+
assert_equal ::ArgumentError, ::AErr
|
38
|
+
end
|
39
|
+
|
40
|
+
should "have #aE raising ArgumentError if block falsey" do
|
41
|
+
assert_raise TErr do 0.aT "yada yada" do self == 1 end end
|
42
|
+
assert_nothing_raised do 0.aT "yada yada" do self == 0 end end
|
43
|
+
assert_equal( "hello",
|
44
|
+
"hello".aT( "have 4 unique letters" ) {
|
45
|
+
each_char.map { |e| e }.uniq.join.size == 4
|
46
|
+
} )
|
47
|
+
assert_nothing_raised do 2.aT &:even? end
|
48
|
+
assert_raise TErr do 3.aT &:even? end
|
49
|
+
assert_raise TErr do nil.aT end
|
50
|
+
end
|
51
|
+
|
52
|
+
should "have #aT_not raising TypeError if block truey" do
|
53
|
+
assert_raise TErr do 0.aT_not { self < 1 } end
|
54
|
+
assert_nothing_raised do 1.aT_not { self == 2 } end
|
55
|
+
assert_equal( "hello", "hello".aT_not( "have x" ) { include? 'x' } )
|
56
|
+
assert_nothing_raised do 3.aT_not &:even? end
|
57
|
+
assert_raise TErr do 2.aT_not &:even? end
|
58
|
+
assert_raise TErr do "".aT_not end
|
59
|
+
end
|
60
|
+
|
61
|
+
should "have #aT_kind_of, alias #aT_is_a TErr enforcers" do
|
62
|
+
assert_raise TErr do :o.aT_kind_of Numeric end
|
63
|
+
assert_nothing_raised do 0.aT_kind_of Numeric end
|
64
|
+
assert_equal( "hello", "hello".aT_kind_of( String ) )
|
65
|
+
assert_raise TErr do :o.aT_is_a Numeric end
|
66
|
+
assert_nothing_raised do 0.aT_is_a Numeric end
|
67
|
+
assert_equal( "hello", "hello".aT_is_a( String ) )
|
68
|
+
end
|
69
|
+
|
70
|
+
should "have #aT_complies" do
|
71
|
+
Koko = Class.new; Pipi = Class.new
|
72
|
+
koko = Koko.new; pipi = Pipi.new
|
73
|
+
pipi.declare_class_compliance! koko.class
|
74
|
+
assert_raise TErr do koko.aT_class_complies pipi.class end
|
75
|
+
assert_nothing_raised do koko.aT_class_complies koko.class end
|
76
|
+
assert_nothing_raised do pipi.aT_class_complies pipi.class end
|
77
|
+
assert_nothing_raised do pipi.aT_class_complies koko.class end
|
78
|
+
assert_raise TErr do koko.aT_class_complies Pipi end
|
79
|
+
assert_nothing_raised do pipi.aT_class_complies Pipi end
|
80
|
+
assert_nothing_raised do pipi.aT_class_complies Koko end
|
81
|
+
assert_equal koko, koko.aT_class_complies( Koko )
|
82
|
+
end
|
83
|
+
|
84
|
+
should "have #aT_respond_to enforcer" do
|
85
|
+
assert_raise TErr do :o.aT_respond_to :each end
|
86
|
+
assert_nothing_raised do {}.aT_respond_to :each end
|
87
|
+
assert_equal( [:hello], [:hello].aT_respond_to( :each ) )
|
88
|
+
end
|
89
|
+
|
90
|
+
should "have #aT_equal enforcer" do
|
91
|
+
assert_raise TErr do 0.aT_equal 1 end
|
92
|
+
assert_nothing_raised do 1.aT_equal 2.0/2.0 end
|
93
|
+
assert_equal( "hello", "hello".aT_equal( " hello ".strip ) )
|
94
|
+
end
|
95
|
+
|
96
|
+
should "have #aT_not_equal enforcer" do
|
97
|
+
assert_raise TErr do 1.aT_not_equal 1.0 end
|
98
|
+
assert_nothing_raised do 7.aT_not_equal 42 end
|
99
|
+
assert_equal( "hello", "hello".aT_not_equal( "goodbye" ) )
|
100
|
+
end
|
101
|
+
|
102
|
+
should "have #aT_blank enforcer" do
|
103
|
+
assert_raise TErr do "x".aT_blank end
|
104
|
+
assert_nothing_raised do ["", []].each{|e| e.aT_blank } end
|
105
|
+
assert_equal( {}, {}.aT_blank )
|
106
|
+
end
|
107
|
+
|
108
|
+
should "have #aT_present enforcer" do
|
109
|
+
assert_raise TErr do nil.aT_present end
|
110
|
+
assert_nothing_raised do 0.aT_present end
|
111
|
+
assert_equal( "hello", "hello".aT_present )
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "Enumerable" do
|
116
|
+
should "have #aT_all enforcer" do
|
117
|
+
assert_raise TErr do [1, 2, 7].aT_all { |e| e < 5 } end
|
118
|
+
assert_nothing_raised do [1, 2, 4].aT_all { |e| e < 5 } end
|
119
|
+
end
|
120
|
+
|
121
|
+
should "have #aT_all_kind_of enforcer" do
|
122
|
+
assert_raise TErr do [1.0, 2.0, :a].aT_all_kind_of Numeric end
|
123
|
+
assert_nothing_raised do [1.0, 2.0, 3].aT_all_kind_of Numeric end
|
124
|
+
end
|
125
|
+
|
126
|
+
should "have #aT_all_comply class compliance enforcer" do
|
127
|
+
assert_raise TErr do [1.0, 2.0, :a].aT_all_comply Numeric end
|
128
|
+
assert_nothing_raised do [1.0, 2.0, 3].aT_all_comply Numeric end
|
129
|
+
end
|
130
|
+
|
131
|
+
should "have #aT_all_numeric enforcer" do
|
132
|
+
assert_raise TErr do [:a].aT_all_numeric end
|
133
|
+
assert_nothing_raised do [1, 2.0].aT_all_numeric end
|
134
|
+
end
|
135
|
+
|
136
|
+
should "have #aT_subset_of enforcer" do
|
137
|
+
assert_raise TErr do [6].aT_subset_of [*0..5] end
|
138
|
+
assert_nothing_raised do [1,2].aT_subset_of [*0..5] end
|
139
|
+
end
|
140
|
+
end # context Enumerable
|
141
|
+
|
142
|
+
context "Array" do
|
143
|
+
should "have #aT_includes (alias #aT_include) enforcer" do
|
144
|
+
assert_raise TErr do [1, 2, 4].aT_includes 3 end
|
145
|
+
assert_nothing_raised do [1, 2, 4].aT_includes( 4 ).aT_include( 4 ) end
|
146
|
+
assert_equal [6, 7], [6, 7].aT_includes( 6 )
|
147
|
+
assert_equal [6, 7], [6, 7].aT_include( 6 )
|
148
|
+
end
|
149
|
+
end # context Array
|
150
|
+
|
151
|
+
context "Hash" do
|
152
|
+
should "have #merge_synonym_keys! method" do
|
153
|
+
a = { a: 'a', b: 'b', k: 'k', o: 'k', t: 'k' }
|
154
|
+
old = a.dup
|
155
|
+
assert_respond_to a, :merge_synonym_keys!
|
156
|
+
assert_equal false, a.merge_synonym_keys!( :a, nil )
|
157
|
+
assert_equal old, a
|
158
|
+
assert_equal nil, a.merge_synonym_keys!( :z, nil )
|
159
|
+
assert_equal old, a
|
160
|
+
assert_equal true, a.merge_synonym_keys!( :k, :o, :t )
|
161
|
+
assert_equal( { a: 'a', b: 'b', k: 'k' }, a )
|
162
|
+
old = a.dup
|
163
|
+
assert_raise TErr do a.merge_synonym_keys!( :a, :b ) end
|
164
|
+
assert_equal old, a
|
165
|
+
assert_equal true, a.merge_synonym_keys!( :c, :b )
|
166
|
+
assert_equal( { a: 'a', c: 'b', k: 'k' }, a )
|
167
|
+
end
|
168
|
+
|
169
|
+
should "have #may_have synonym key merger" do
|
170
|
+
a = { a: 'a', b: 'b', k: 'k', o: 'k', t: 'k' }
|
171
|
+
assert_respond_to a, :may_have
|
172
|
+
old = a.dup
|
173
|
+
assert_nothing_raised do a.may_have :z end
|
174
|
+
assert_nothing_raised do a.has? :z end
|
175
|
+
assert_raises TErr do a.may_have( :a, syn!: :b ) end
|
176
|
+
assert_raises TErr do a.has?( :a, syn!: :b ) end
|
177
|
+
assert_equal false, a.has?( :z )
|
178
|
+
assert_equal nil, a.may_have( :z )
|
179
|
+
assert_equal false, a.has?( :z )
|
180
|
+
assert_equal true, a.has?( :a )
|
181
|
+
assert_equal a[:a], a.may_have( :a )
|
182
|
+
assert_equal true, a.has?( :a )
|
183
|
+
assert_equal old, a
|
184
|
+
assert_equal 'k', a.may_have( :k, syn!: [:o, :t] )
|
185
|
+
assert_equal true, a.has?( :k, syn!: [:o, :t] )
|
186
|
+
assert_equal 'b', a.may_have( :c, syn!: :b )
|
187
|
+
assert_equal true, a.has?( :c, syn!: :b )
|
188
|
+
assert_equal( { a: 'a', c: 'b', k: 'k' }, a )
|
189
|
+
end
|
190
|
+
|
191
|
+
should "have #aT_has synonymizing enforcer" do
|
192
|
+
a = { infile: 'a', csv_out_file: 'b', k: 'k', o: 'k', t: 'k' }
|
193
|
+
assert_respond_to a, :aT_has
|
194
|
+
old = a.dup
|
195
|
+
assert_raises TErr do a.aT_has :z end
|
196
|
+
assert_nothing_raised do a.aT_has :infile end
|
197
|
+
assert_nothing_raised do a.aT_has :csv_out_file end
|
198
|
+
class TestClass; def initialize( args )
|
199
|
+
args.aT_has :infile
|
200
|
+
args.aT_has :csv_out_file
|
201
|
+
args.aT_has :k
|
202
|
+
end end
|
203
|
+
assert_nothing_raised do TestClass.new a end
|
204
|
+
assert_raises TErr do a.aT_has( :a, syn!: :b ) end
|
205
|
+
assert_equal "a", a.aT_has( :infile )
|
206
|
+
assert_equal "k", a.aT_has( :k, syn!: [:o, :t] )
|
207
|
+
assert_equal "b", a.aT_has( :c, syn!: :csv_out_file )
|
208
|
+
assert_equal( { infile: 'a', c: 'b', k: 'k' }, a )
|
209
|
+
assert_raises TErr do a.aT_has(:c) {|val| val == 'c'} end
|
210
|
+
assert_nothing_raised do a.aT_has(:c) {|val| val == 'b'} end
|
211
|
+
end
|
212
|
+
end # context Hash
|
213
|
+
end # class ScruplesTest
|
@@ -0,0 +1,39 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'shoulda'
|
6
|
+
# require 'y_support/all'
|
7
|
+
|
8
|
+
class UnicodeTest < Test::Unit::TestCase
|
9
|
+
context "Object" do
|
10
|
+
setup do
|
11
|
+
require 'y_support/unicode'
|
12
|
+
end
|
13
|
+
|
14
|
+
should "have a very limited number of one-character Unicode aliases" do
|
15
|
+
◉ = Object.new
|
16
|
+
assert ◉.singleton_class == ◉.©
|
17
|
+
assert ◉.singleton_class == ◉.ⓒ
|
18
|
+
assert ◉.ç == ◉.class
|
19
|
+
assert 2 == √( 4 )
|
20
|
+
assert 10 == ∑(1..4)
|
21
|
+
assert 10 == Σ(1..4)
|
22
|
+
assert 24 == ∏(1..4)
|
23
|
+
assert 24 == Π(1..4)
|
24
|
+
ɱ = Module.new
|
25
|
+
ɱ.ç_variable_set :@@meaning, 42
|
26
|
+
assert ɱ.class_variable_get( :@@meaning ) == 42
|
27
|
+
assert ɱ.ç_variable_get( :@@meaning ) == 42
|
28
|
+
assert ɱ.ç_variable_defined?( :@@meaning )
|
29
|
+
ɱ.remove_ç_variable :@@meaning
|
30
|
+
assert ! ɱ.ç_variable_defined?( :@@meaning )
|
31
|
+
ll = λ{}
|
32
|
+
assert ll.is_a? Proc
|
33
|
+
assert ll.lambda?
|
34
|
+
lL = Λ{}
|
35
|
+
assert lL.is_a? Proc
|
36
|
+
assert ! lL.lambda?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|