y_support 2.4.4 → 2.4.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/y_support/core_ext.rb +9 -3
- data/lib/y_support/version.rb +1 -1
- data/test/core_ext_test.rb +389 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efb272c5a64d6c3e2445a0196993be87b3bca7f1
|
4
|
+
data.tar.gz: 71b2ec446b5c48c42ed4776e7f0602a55fe5eb00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4f2fd5cd0bc150b1f46616d11d6c82c5fa27ac188c3c00c7169fa4aec161eaaf4eac31a66c031db5454b077591893b9e4d09f62670f4717e9c42602d38376ed
|
7
|
+
data.tar.gz: 3b7e1aeaa93ac9d3135f1dfe6c25af4fb0e0c2e274221ddccb06c3cc8eda86374b7ec2e905b82c202f4429ca3d307fa554af7ba57a17f8411ad9c547f7669bb2
|
data/lib/y_support/core_ext.rb
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require_relative 'core_ext/array'
|
2
|
+
require_relative 'core_ext/class'
|
3
|
+
require_relative 'core_ext/enumerable'
|
4
|
+
require_relative 'core_ext/hash'
|
5
|
+
require_relative 'core_ext/module'
|
6
|
+
require_relative 'core_ext/numeric'
|
7
|
+
require_relative 'core_ext/object'
|
8
|
+
require_relative 'core_ext/string'
|
9
|
+
require_relative 'core_ext/symbol'
|
data/lib/y_support/version.rb
CHANGED
@@ -0,0 +1,389 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'minitest/autorun'
|
5
|
+
|
6
|
+
describe Object do
|
7
|
+
before do
|
8
|
+
require './../lib/y_support/core_ext/object'
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should have #param_class" do
|
12
|
+
o = Object.new
|
13
|
+
m = Module.new
|
14
|
+
o.param_class( { Array: Array, foo: Hash, bar: m }, with: { mother: o } )
|
15
|
+
assert o.Array < Array
|
16
|
+
o.Array.mother.must_equal( o )
|
17
|
+
o.foo.mother.must_equal( o )
|
18
|
+
o.bar.ancestors[1].must_equal( m )
|
19
|
+
o.bar.mother.must_equal( o )
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should have #param_class!" do
|
23
|
+
o = Object.new
|
24
|
+
m = Module.new
|
25
|
+
o.param_class!( { Array: Array, foo: Hash, bar: m }, with: { mother: o } )
|
26
|
+
assert o.Array < Array
|
27
|
+
o.Array.mother.must_equal( o )
|
28
|
+
o.foo.mother.must_equal( o )
|
29
|
+
o.bar.ancestors[1].must_equal( m )
|
30
|
+
o.bar.mother.must_equal( o )
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should have #insp method to facilitate inspection" do
|
34
|
+
module Quux; class Foo; def to_s; "bar" end end end
|
35
|
+
Quux::Foo.new.y_inspect.must_equal "Quux::Foo:bar"
|
36
|
+
Quux::Foo.new.y_inspect( :full ).must_equal "#<Quux::Foo:bar>"
|
37
|
+
Quux::Foo.new.y_inspect( :short ).must_equal "Foo:bar"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
describe Module do
|
43
|
+
before do
|
44
|
+
require './../lib/y_support/core_ext/module'
|
45
|
+
end
|
46
|
+
|
47
|
+
it "has #const_set_if_not_defined and #const_reset!" do
|
48
|
+
m = Module.new
|
49
|
+
hm = m.heir_module( p1: 1, p2: 2 )
|
50
|
+
hm.ancestors[1].must_equal m
|
51
|
+
hm.p1.must_equal 1
|
52
|
+
hm.p2.must_equal 2
|
53
|
+
hc = m.heir_class( Array, q1: 1, q2: 2 )
|
54
|
+
hc.new.class.ancestors[1].must_equal m
|
55
|
+
hc.q1.must_equal 1
|
56
|
+
hc.q2.must_equal 2
|
57
|
+
m.const_set_if_not_defined :Foo, 42
|
58
|
+
m::Foo.must_equal 42
|
59
|
+
m.const_reset! :Foo, 43
|
60
|
+
m::Foo.must_equal 43
|
61
|
+
m.module_exec do
|
62
|
+
selector :a
|
63
|
+
def initialize; @a = 42 end
|
64
|
+
chain b: :a, &:to_s
|
65
|
+
end
|
66
|
+
Class.new do include m end.new.b.must_equal "42"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe Class do
|
71
|
+
before do
|
72
|
+
require './../lib/y_support/core_ext/class'
|
73
|
+
end
|
74
|
+
|
75
|
+
it "has #selector alias for #attr_reader method" do
|
76
|
+
o = Class.new do
|
77
|
+
selector :a
|
78
|
+
def initialize a; @a = a end
|
79
|
+
end.new( 42 )
|
80
|
+
o.a.must_equal( 42 )
|
81
|
+
end
|
82
|
+
|
83
|
+
it "has #parametrize method" do
|
84
|
+
a = Class.new
|
85
|
+
-> { a.foo }.must_raise NoMethodError
|
86
|
+
b = a.parametrize foo: 42
|
87
|
+
b.foo.must_equal 42
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
describe Enumerable do
|
93
|
+
before do
|
94
|
+
require './../lib/y_support/core_ext/enumerable'
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should introduce #all_kind_of? collection qualifier" do
|
98
|
+
assert_equal true, [ 1, 1.0 ].all_kind_of?( Numeric )
|
99
|
+
assert_equal false, [ 1, [1.0] ].all_kind_of?( Numeric )
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should introduce #all_numeric? collection qualifier" do
|
103
|
+
assert_equal true, [1, 1.0].all_numeric?
|
104
|
+
assert_equal false, [:a, 1].all_numeric?
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should have #subset_of? collection qualifier" do
|
108
|
+
assert_equal( true, [1,2].subset_of?( [1,2,3,4] ) )
|
109
|
+
assert_equal( false, [1,2].subset_of?( [2,3] ) )
|
110
|
+
assert_equal( true, [1,2].subset_of?( [1,2] ) )
|
111
|
+
assert_equal( true, [1, 1.0].subset_of?( [1.0, 2.0] ) )
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
describe Array do
|
117
|
+
before do
|
118
|
+
require './../lib/y_support/core_ext/array'
|
119
|
+
end
|
120
|
+
|
121
|
+
it "has #arrays_to_hash" do
|
122
|
+
[ [ :a, 1 ], [ :b, 2 ] ].arrays_to_hash
|
123
|
+
.must_equal( { a: 1, b: 2 } )
|
124
|
+
[ [ :a, 1, 2 ], [ :b, 2, 3 ] ].arrays_to_hash
|
125
|
+
.must_equal( { a: [ 1, 2 ], b: [ 2, 3 ] } )
|
126
|
+
end
|
127
|
+
|
128
|
+
it "has #zip_to_hash" do
|
129
|
+
assert_equal( {a: 1, b: 2}, [:a, :b].zip_to_hash( [1, 2] ) )
|
130
|
+
assert_equal( {a: "a"}, [:a].zip_to_hash( &:to_s ) )
|
131
|
+
end
|
132
|
+
|
133
|
+
it "has #>>" do
|
134
|
+
assert_equal( {a: 1, b: 2}, [:a, :b] >> [1, 2] )
|
135
|
+
end
|
136
|
+
|
137
|
+
it "has #ascending_floor" do
|
138
|
+
a = 1, 2, 3
|
139
|
+
a.ascending_floor( 0.5 ).must_equal nil
|
140
|
+
a.ascending_floor( 1 ).must_equal 1
|
141
|
+
a.ascending_floor( 1.5 ).must_equal 1
|
142
|
+
a.ascending_floor( 3.5 ).must_equal 3
|
143
|
+
a.ascending_floor( 1, false ).must_equal nil
|
144
|
+
a.ascending_floor( 3, false ).must_equal 2
|
145
|
+
end
|
146
|
+
|
147
|
+
it "has #ascending_ceiling" do
|
148
|
+
a = 1, 2, 3
|
149
|
+
a.ascending_ceiling( 0.5 ).must_equal 1
|
150
|
+
a.ascending_ceiling( 1.5 ).must_equal 2
|
151
|
+
a.ascending_ceiling( 3 ).must_equal 3
|
152
|
+
a.ascending_ceiling( 3.1 ).must_equal nil
|
153
|
+
a.ascending_ceiling( 3, false ).must_equal nil
|
154
|
+
a.ascending_ceiling( 2, false ).must_equal 3
|
155
|
+
end
|
156
|
+
|
157
|
+
it "has #to_proc in style &[function, *args]" do
|
158
|
+
assert_equal [2, 3], [1, 2].map( &[:+, 1] )
|
159
|
+
end
|
160
|
+
|
161
|
+
it "has #push/pop_ordered/named" do
|
162
|
+
a = [1, 2, foo: 3]
|
163
|
+
a.pop_named( :foo ).must_equal 3
|
164
|
+
a.pop_named( :bar ).must_equal nil
|
165
|
+
a.pop_ordered.must_equal 2
|
166
|
+
a.push_ordered( 2 ).must_equal [1, 2]
|
167
|
+
a.push_named( foo: 3 ).must_equal [1, 2, foo: 3]
|
168
|
+
a.push_named( bar: 4 ).must_equal [1, 2, foo: 3, bar: 4]
|
169
|
+
a.pop_named( :foo ).must_equal 3
|
170
|
+
a.push_ordered( 42 ).must_equal [1, 2, 42, bar: 4]
|
171
|
+
end
|
172
|
+
|
173
|
+
it "has #to_column_vector" do
|
174
|
+
[1, 2, 3].to_column_vector.must_equal Matrix[[1], [2], [3]]
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
describe Hash do
|
179
|
+
before do
|
180
|
+
require './../lib/y_support/core_ext/hash'
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should have #default! custom defaulter" do
|
184
|
+
defaults = { a: 1, b: nil }
|
185
|
+
test = {}
|
186
|
+
result = test.default!( defaults )
|
187
|
+
assert_equal defaults, result
|
188
|
+
assert_equal result.object_id, test.object_id
|
189
|
+
test = { a: 11, b: 22 }
|
190
|
+
assert_equal( { a: 11, b: 22 }, test.default!( defaults ) )
|
191
|
+
test = { a: 11, c: 22 }
|
192
|
+
{ a: 11, b: nil, c: 22 }.must_equal test.default! defaults
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should have #with_keys and #with_keys!" do
|
196
|
+
test = { "a" => :b, "c" => :d }
|
197
|
+
test.with_keys( &:to_sym ).must_equal( { a: :b, c: :d } )
|
198
|
+
test.must_equal( { "a" => :b, "c" => :d } )
|
199
|
+
test.with_keys! &:to_sym
|
200
|
+
test.must_equal( { a: :b, c: :d } )
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should have #change_keys" do
|
204
|
+
test = { a: 1, c: 2 }
|
205
|
+
test.change_keys { |k, v| k.to_s + v.to_s }
|
206
|
+
.must_equal( { "a1" => 1, "c2" => 2 } )
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should have #with_values and #with_values!" do
|
210
|
+
test = { a: :b, c: :d }
|
211
|
+
test.with_values( &:to_s ).must_equal( { a: "b", c: "d" } )
|
212
|
+
test.must_equal( { a: :b, c: :d } )
|
213
|
+
test.with_values!( &:to_s )
|
214
|
+
test.must_equal( { a: "b", c: "d" } )
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should have #change_values and #change_values!" do
|
218
|
+
test = { a: :b, c: :d }
|
219
|
+
test.modify_values do |k, v| k.to_s + v.to_s end
|
220
|
+
.must_equal( {a: "ab", c: "cd"} )
|
221
|
+
test.must_equal( { a: :b, c: :d } )
|
222
|
+
test.modify_values! { |k, v| k.to_s + v.to_s }
|
223
|
+
test.must_equal( {a: "ab", c: "cd"} )
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should have #modify" do
|
227
|
+
assert_equal( { ab: "ba", cd: "dc" },
|
228
|
+
{ a: :b, c: :d }
|
229
|
+
.modify { |k, v| ["#{k}#{v}".to_sym, "#{v}#{k}"] } )
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should have #slice" do
|
233
|
+
{ a: 1, b: 2, c: 3 }.slice( [:a, :b] ).must_equal( { a: 1, b: 2 } )
|
234
|
+
{ 1 => :a, 2 => :b, 3 => :c, 4 => :d }.slice( 2..3.5 )
|
235
|
+
.must_equal( { 2 => :b, 3 => :c } )
|
236
|
+
{ 0.0 => :a, 1.1 => :b }.slice( 1..2 ).must_equal( { 1.1 => :b } )
|
237
|
+
end
|
238
|
+
|
239
|
+
it "should have #dot! meta patcher for dotted access to keys" do
|
240
|
+
h = Hash.new.merge!(aaa: 1, taint: 2)
|
241
|
+
-> { h.dot! }.must_raise ArgumentError
|
242
|
+
h.dot!( overwrite_methods: true ) # instead of #assert_nothing_raised
|
243
|
+
assert_equal( {aaa: 1}, {aaa: 1}.dot! )
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should be safeguarded against redefining #slice" do
|
247
|
+
m = Hash.instance_method :slice
|
248
|
+
class Hash; def slice( *args ); fail "This should not happen!" end end
|
249
|
+
{}.slice( :a )
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
|
254
|
+
describe "Matrix" do
|
255
|
+
before do
|
256
|
+
require 'matrix'
|
257
|
+
require './../lib/y_support/stdlib_ext/matrix'
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should have #pp method" do
|
261
|
+
assert_respond_to Matrix[[1, 2], [3, 4]], :pretty_print
|
262
|
+
assert_respond_to Matrix[[1, 2], [3, 4]], :pp
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should have #correspondence_matrix method" do
|
266
|
+
assert_respond_to Matrix, :correspondence_matrix
|
267
|
+
assert_equal Matrix[[1, 0, 0], [0, 1, 0]],
|
268
|
+
Matrix.correspondence_matrix( [:a, :b, :c], [:a, :b] )
|
269
|
+
assert_equal Matrix.column_vector( [1, 2] ),
|
270
|
+
Matrix.correspondence_matrix( [:a, :b, :c], [:a, :b] ) *
|
271
|
+
Matrix.column_vector( [1, 2, 3] )
|
272
|
+
assert_equal 2, Matrix.correspondence_matrix( [1, 2], [1] ).column_size
|
273
|
+
end
|
274
|
+
|
275
|
+
it "should have #column_to_a & #row_to_a" do
|
276
|
+
assert_equal [1, 2, 3], Matrix[[1], [2], [3]].column_to_a
|
277
|
+
assert_equal [2, 3, 4], Matrix[[1, 2], [2, 3], [3, 4]].column_to_a( 1 )
|
278
|
+
assert_equal nil, Matrix.empty( 5, 0 ).column_to_a
|
279
|
+
assert_equal [1], Matrix[[1], [2], [3]].row_to_a
|
280
|
+
assert_equal [3], Matrix[[1], [2], [3]].row_to_a( 2 )
|
281
|
+
end
|
282
|
+
|
283
|
+
it "should have aliased #row_vector, #column_vector methods" do
|
284
|
+
assert_equal Matrix.column_vector( [1, 2, 3] ),
|
285
|
+
Matrix.cv( [1, 2, 3] )
|
286
|
+
assert_equal Matrix.row_vector( [1, 2, 3] ),
|
287
|
+
Matrix.rv( [1, 2, 3] )
|
288
|
+
end
|
289
|
+
|
290
|
+
it "should have #join_bottom and #join_right" do
|
291
|
+
assert_equal Matrix[[1, 2], [3, 4]],
|
292
|
+
Matrix[[1, 2]].join_bottom( Matrix[[3, 4]] )
|
293
|
+
assert_equal Matrix[[1, 2, 3, 4]],
|
294
|
+
Matrix[[1, 2]].join_right( Matrix[[3, 4]] )
|
295
|
+
end
|
296
|
+
|
297
|
+
it "should have aliased #row_size, #column_size methods" do
|
298
|
+
assert_equal 3, Matrix.zero(3, 2).height
|
299
|
+
assert_equal 3, Matrix.zero(3, 2).number_of_rows
|
300
|
+
assert_equal 2, Matrix.zero(3, 2).width
|
301
|
+
assert_equal 2, Matrix.zero(3, 2).number_of_columns
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
describe String do
|
306
|
+
before do
|
307
|
+
require './../lib/y_support/core_ext/string'
|
308
|
+
end
|
309
|
+
|
310
|
+
it "should have #can_be_integer? returning the integer or false if not convertible" do
|
311
|
+
assert_equal 33, " 33".to_Integer
|
312
|
+
assert_equal 8, " 010 ".to_Integer
|
313
|
+
assert_equal false, "garbage".to_Integer
|
314
|
+
end
|
315
|
+
|
316
|
+
it "should have #can_be_float? returning the float or false if not convertible" do
|
317
|
+
assert_equal 22.2, ' 2.22e1'.to_Float
|
318
|
+
assert_equal 10, " 010 ".to_Float
|
319
|
+
assert_equal false, "garbage".to_Float
|
320
|
+
end
|
321
|
+
|
322
|
+
it "should have #default! defaulter" do
|
323
|
+
assert_equal "default", "".default!("default")
|
324
|
+
assert_equal "default", " ".default!(:default)
|
325
|
+
assert_equal "default", " \n ".default!("default")
|
326
|
+
assert_equal "kokot", "kokot".default!("default")
|
327
|
+
a = ""
|
328
|
+
assert_equal a.object_id, a.default!("tata").object_id
|
329
|
+
end
|
330
|
+
|
331
|
+
it "should have #stripn upgrade of #strip, which also strips newlines" do
|
332
|
+
assert_equal "test test", " \n test test \n\n \n ".stripn
|
333
|
+
end
|
334
|
+
|
335
|
+
it "should have #compact for joining indented lines (esp. heredocs)" do
|
336
|
+
assert_equal "test test test",
|
337
|
+
"test\n test\n\n \n test\n ".wring_heredoc
|
338
|
+
funny_string = <<-FUNNY_STRING.wring_heredoc
|
339
|
+
This
|
340
|
+
is
|
341
|
+
a funny string.
|
342
|
+
FUNNY_STRING
|
343
|
+
assert_equal( 'This is a funny string.', funny_string )
|
344
|
+
end
|
345
|
+
|
346
|
+
it "should be able #underscore_spaces" do
|
347
|
+
assert_equal "te_st_test", "te st test".underscore_spaces
|
348
|
+
end
|
349
|
+
|
350
|
+
it "should have #symbolize stripping, removing capitalization and diacritics " \
|
351
|
+
'as if to make a suitable symbol material' do
|
352
|
+
assert_equal "Yes_sir!", " \nYes, sir!.; \n \n".standardize
|
353
|
+
end
|
354
|
+
|
355
|
+
it "should have #to_standardized_sym chaining #standardize and #to_sym" do
|
356
|
+
assert_equal :Yes, " \nYes,.; \n \n".to_standardized_sym
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
360
|
+
|
361
|
+
describe Symbol do
|
362
|
+
before do
|
363
|
+
require './../lib/y_support/core_ext/symbol'
|
364
|
+
end
|
365
|
+
|
366
|
+
it "should have #default! defaulter going through String#default!" do
|
367
|
+
assert_equal :default, "".to_sym.default!(:default)
|
368
|
+
assert_equal :default, "".to_sym.default!("default")
|
369
|
+
assert_equal :default, " ".to_sym.default!("default")
|
370
|
+
assert_equal :default, " \n ".to_sym.default!("default")
|
371
|
+
assert_equal :kokot, :kokot.default!("default")
|
372
|
+
end
|
373
|
+
|
374
|
+
it "should have #to_standardized_sym" do
|
375
|
+
assert_equal :Yes, (:" \nYes, \n").to_standardized_sym
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
379
|
+
describe Numeric do
|
380
|
+
before do
|
381
|
+
require './../lib/y_support/core_ext/numeric'
|
382
|
+
end
|
383
|
+
|
384
|
+
it "should have #zero public class methods" do
|
385
|
+
assert_equal 0, Integer.zero
|
386
|
+
assert_equal 0.0, Float.zero
|
387
|
+
assert_equal Complex(0, 0), Complex.zero
|
388
|
+
end
|
389
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: y_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.4.
|
4
|
+
version: 2.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- boris
|
@@ -134,6 +134,7 @@ files:
|
|
134
134
|
- lib/y_support/unicode.rb
|
135
135
|
- lib/y_support/version.rb
|
136
136
|
- lib/y_support/x.rb
|
137
|
+
- test/core_ext_test.rb
|
137
138
|
- test/flex_coerce_test.rb
|
138
139
|
- test/inert_recorder_test.rb
|
139
140
|
- test/kde_test.rb
|
@@ -173,6 +174,7 @@ signing_key:
|
|
173
174
|
specification_version: 4
|
174
175
|
summary: Support library used by Y gems.
|
175
176
|
test_files:
|
177
|
+
- test/core_ext_test.rb
|
176
178
|
- test/flex_coerce_test.rb
|
177
179
|
- test/inert_recorder_test.rb
|
178
180
|
- test/kde_test.rb
|