y_support 2.4.6 → 2.5.1
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/all.rb +1 -7
- data/lib/y_support/{inert_recorder.rb → misc/inert_recorder.rb} +1 -1
- data/lib/y_support/{local_object.rb → misc/local_object.rb} +1 -1
- data/lib/y_support/{null_object.rb → misc/null_object.rb} +1 -1
- data/lib/y_support/{respond_to.rb → misc/respond_to.rb} +1 -1
- data/lib/y_support/misc.rb +5 -26
- data/lib/y_support/name_magic/namespace.rb +5 -16
- data/lib/y_support/name_magic.rb +0 -1
- data/lib/y_support/version.rb +1 -1
- data/test/{inert_recorder_test.rb → misc/inert_recorder_test.rb} +2 -4
- data/test/{local_object_test.rb → misc/local_object_test.rb} +2 -4
- data/test/{null_object_test.rb → misc/null_object_test.rb} +2 -4
- data/test/{respond_to_test.rb → misc/respond_to_test.rb} +2 -2
- data/test/misc_test.rb +4 -386
- metadata +14 -25
- data/lib/y_support/kde.rb +0 -14
- data/lib/y_support/literate.rb +0 -253
- data/lib/y_support/x.rb +0 -86
- data/test/kde_test.rb +0 -11
- data/test/literate_test.rb +0 -231
- data/test/misc_test/test_module/fixture_class.rb +0 -8
- data/test/x_test.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2e9ed33873ba6f65cec12b20001eb56cb3e217c
|
4
|
+
data.tar.gz: ea33118ef8cebea1b7a1cf8ceeeb6327a611628e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e4cfcf183d1d8d24611d3b923e1da4b7f02c29f1b3e1d10ad9b8db9c933cb99f9f712c266dbec3f259b19853f69f5ec09c1f9a59deb3d747a3965692471b6f2
|
7
|
+
data.tar.gz: ab06499e3ac0deea21a0817215fe0c45748794b67dc5876dfd6f198aea5586e349a7df5ccce2d568369a0e6c8d52fa94e73acc1934b0b3adea0846274a3aabf1
|
data/lib/y_support/all.rb
CHANGED
@@ -3,12 +3,6 @@
|
|
3
3
|
require 'y_support'
|
4
4
|
require 'y_support/flex_coerce'
|
5
5
|
require 'y_support/name_magic'
|
6
|
-
require 'y_support/typing'
|
7
6
|
require 'y_support/unicode'
|
8
|
-
require 'y_support/respond_to'
|
9
|
-
require 'y_support/null_object'
|
10
|
-
require 'y_support/inert_recorder'
|
11
|
-
require 'y_support/local_object'
|
12
|
-
require 'y_support/literate'
|
13
|
-
require 'y_support/kde'
|
14
7
|
require 'y_support/misc'
|
8
|
+
require 'y_support/typing'
|
data/lib/y_support/misc.rb
CHANGED
@@ -1,27 +1,6 @@
|
|
1
|
-
|
1
|
+
require_relative '../y_support'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
# provides support for <em>typing by declaration</em> and <em>duck typing</em>.
|
8
|
-
#
|
9
|
-
# 1. Using method <b>declare_compliance</b>, a module can explicitly declare that
|
10
|
-
# it provides the interface compliant with another module. Corresponding inquirer
|
11
|
-
# methods are <b>declared_compliance</b> (returning a list of modules with which
|
12
|
-
# the receiver declares compliance or implicitly complies) and
|
13
|
-
# <b>declares_compliance?( other_module )</b>, which anwers whether the receiver
|
14
|
-
# complies with other_module. An object always implicitly complies with its class
|
15
|
-
# and class ancestry.
|
16
|
-
#
|
17
|
-
# 2. Duck type enforcement for method parameters is supported by a collection of
|
18
|
-
# enforcer methods (aka. run-time assertions). These methods look very much like
|
19
|
-
# assertions, but they start with <b>tE_...</b>, meaning "enforce by raising
|
20
|
-
# TypeError".
|
21
|
-
|
22
|
-
[ :core_ext, :stdlib_ext ].each do |ext|
|
23
|
-
Dir["#{File.dirname( __FILE__ )}/#{ext}/*/misc.rb"].sort.each { |path|
|
24
|
-
dir = File.dirname( path ).match( "y_support/#{ext}" ).post_match
|
25
|
-
require "y_support/#{ext}#{dir}/misc"
|
26
|
-
}
|
27
|
-
end
|
3
|
+
require_relative 'misc/respond_to'
|
4
|
+
require_relative 'misc/null_object'
|
5
|
+
require_relative 'misc/inert_recorder'
|
6
|
+
require_relative 'misc/local_object'
|
@@ -360,22 +360,11 @@ module NameMagic::Namespace
|
|
360
360
|
# Checks whether a name is acceptable as a constant name.
|
361
361
|
#
|
362
362
|
def validate_name name
|
363
|
-
|
364
|
-
#
|
365
|
-
|
366
|
-
#
|
367
|
-
|
368
|
-
# Error message (when error occurs) is constructed from
|
369
|
-
# the #try description and the #note strings, which act at
|
370
|
-
# the same time as code comments. End of advertisement for
|
371
|
-
# 'y_support/literate'.
|
372
|
-
#
|
373
|
-
name.to_s.try "to validate the suggested instance name" do
|
374
|
-
note "rejecting non-capitalized names"
|
375
|
-
fail NameError unless ( ?A..?Z ) === chars.first
|
376
|
-
note "rejecting names with spaces"
|
377
|
-
fail NameError if chars.include? ' '
|
378
|
-
end
|
363
|
+
n = name.to_s
|
364
|
+
# rejecting non-capitalized names
|
365
|
+
fail NameError unless ( ?A..?Z ) === n.chars.first
|
366
|
+
# rejecting names with spaces
|
367
|
+
fail NameError if n.chars.include? ' '
|
379
368
|
# Return value is the validated name.
|
380
369
|
return name
|
381
370
|
end
|
data/lib/y_support/name_magic.rb
CHANGED
data/lib/y_support/version.rb
CHANGED
@@ -3,11 +3,9 @@
|
|
3
3
|
|
4
4
|
require 'minitest/autorun'
|
5
5
|
|
6
|
-
|
7
|
-
before do
|
8
|
-
require './../lib/y_support/inert_recorder'
|
9
|
-
end
|
6
|
+
require_relative '../../lib/y_support/misc/inert_recorder'
|
10
7
|
|
8
|
+
describe "InertRecorder" do
|
11
9
|
describe "Object" do
|
12
10
|
it "should have #InertRecorder() constructor" do
|
13
11
|
assert_equal InertRecorder, InertRecorder( :bull ).class
|
@@ -3,11 +3,9 @@
|
|
3
3
|
|
4
4
|
require 'minitest/autorun'
|
5
5
|
|
6
|
-
|
7
|
-
before do
|
8
|
-
require './../lib/y_support/local_object'
|
9
|
-
end
|
6
|
+
require_relative '../../lib/y_support/misc/local_object'
|
10
7
|
|
8
|
+
describe "LocalObject" do
|
11
9
|
describe "Object" do
|
12
10
|
it "should have constructor #LocalObject, alias #L!" do
|
13
11
|
assert_equal LocalObject, LocalObject().class
|
@@ -3,11 +3,9 @@
|
|
3
3
|
|
4
4
|
require 'minitest/autorun'
|
5
5
|
|
6
|
-
|
7
|
-
before do
|
8
|
-
require './../lib/y_support/null_object'
|
9
|
-
end
|
6
|
+
require_relative '../../lib/y_support/misc/null_object'
|
10
7
|
|
8
|
+
describe "y_support/null_object" do
|
11
9
|
describe Object do
|
12
10
|
it "should have #null_object? (alias #null?)" do
|
13
11
|
assert ( class Koko < NullObject; self end ).new.null_object?
|
data/test/misc_test.rb
CHANGED
@@ -3,389 +3,7 @@
|
|
3
3
|
|
4
4
|
require 'minitest/autorun'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
71
|
-
describe Class do
|
72
|
-
before do
|
73
|
-
require './../lib/y_support/core_ext/class'
|
74
|
-
end
|
75
|
-
|
76
|
-
it "has #selector alias for #attr_reader method" do
|
77
|
-
o = Class.new do
|
78
|
-
selector :a
|
79
|
-
def initialize a; @a = a end
|
80
|
-
end.new( 42 )
|
81
|
-
o.a.must_equal( 42 )
|
82
|
-
end
|
83
|
-
|
84
|
-
it "has #parametrize method" do
|
85
|
-
a = Class.new
|
86
|
-
-> { a.foo }.must_raise NoMethodError
|
87
|
-
b = a.parametrize foo: 42
|
88
|
-
b.foo.must_equal 42
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
|
93
|
-
describe Enumerable do
|
94
|
-
before do
|
95
|
-
require './../lib/y_support/core_ext/enumerable'
|
96
|
-
end
|
97
|
-
|
98
|
-
it "should introduce #all_kind_of? collection qualifier" do
|
99
|
-
assert_equal true, [ 1, 1.0 ].all_kind_of?( Numeric )
|
100
|
-
assert_equal false, [ 1, [1.0] ].all_kind_of?( Numeric )
|
101
|
-
end
|
102
|
-
|
103
|
-
it "should introduce #all_numeric? collection qualifier" do
|
104
|
-
assert_equal true, [1, 1.0].all_numeric?
|
105
|
-
assert_equal false, [:a, 1].all_numeric?
|
106
|
-
end
|
107
|
-
|
108
|
-
it "should have #subset_of? collection qualifier" do
|
109
|
-
assert_equal( true, [1,2].subset_of?( [1,2,3,4] ) )
|
110
|
-
assert_equal( false, [1,2].subset_of?( [2,3] ) )
|
111
|
-
assert_equal( true, [1,2].subset_of?( [1,2] ) )
|
112
|
-
assert_equal( true, [1, 1.0].subset_of?( [1.0, 2.0] ) )
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
|
117
|
-
describe Array do
|
118
|
-
before do
|
119
|
-
require './../lib/y_support/core_ext/array'
|
120
|
-
end
|
121
|
-
|
122
|
-
it "has #arrays_to_hash" do
|
123
|
-
[ [ :a, 1 ], [ :b, 2 ] ].arrays_to_hash
|
124
|
-
.must_equal( { a: 1, b: 2 } )
|
125
|
-
[ [ :a, 1, 2 ], [ :b, 2, 3 ] ].arrays_to_hash
|
126
|
-
.must_equal( { a: [ 1, 2 ], b: [ 2, 3 ] } )
|
127
|
-
end
|
128
|
-
|
129
|
-
it "has #zip_to_hash" do
|
130
|
-
assert_equal( {a: 1, b: 2}, [:a, :b].zip_to_hash( [1, 2] ) )
|
131
|
-
assert_equal( {a: "a"}, [:a].zip_to_hash( &:to_s ) )
|
132
|
-
end
|
133
|
-
|
134
|
-
it "has #>>" do
|
135
|
-
assert_equal( {a: 1, b: 2}, [:a, :b] >> [1, 2] )
|
136
|
-
end
|
137
|
-
|
138
|
-
it "has #ascending_floor" do
|
139
|
-
a = 1, 2, 3
|
140
|
-
a.ascending_floor( 0.5 ).must_equal nil
|
141
|
-
a.ascending_floor( 1 ).must_equal 1
|
142
|
-
a.ascending_floor( 1.5 ).must_equal 1
|
143
|
-
a.ascending_floor( 3.5 ).must_equal 3
|
144
|
-
a.ascending_floor( 1, false ).must_equal nil
|
145
|
-
a.ascending_floor( 3, false ).must_equal 2
|
146
|
-
end
|
147
|
-
|
148
|
-
it "has #ascending_ceiling" do
|
149
|
-
a = 1, 2, 3
|
150
|
-
a.ascending_ceiling( 0.5 ).must_equal 1
|
151
|
-
a.ascending_ceiling( 1.5 ).must_equal 2
|
152
|
-
a.ascending_ceiling( 3 ).must_equal 3
|
153
|
-
a.ascending_ceiling( 3.1 ).must_equal nil
|
154
|
-
a.ascending_ceiling( 3, false ).must_equal nil
|
155
|
-
a.ascending_ceiling( 2, false ).must_equal 3
|
156
|
-
end
|
157
|
-
|
158
|
-
it "has #to_proc in style &[function, *args]" do
|
159
|
-
assert_equal [2, 3], [1, 2].map( &[:+, 1] )
|
160
|
-
end
|
161
|
-
|
162
|
-
it "has #push/pop_ordered/named" do
|
163
|
-
a = [1, 2, foo: 3]
|
164
|
-
a.pop_named( :foo ).must_equal 3
|
165
|
-
a.pop_named( :bar ).must_equal nil
|
166
|
-
a.pop_ordered.must_equal 2
|
167
|
-
a.push_ordered( 2 ).must_equal [1, 2]
|
168
|
-
a.push_named( foo: 3 ).must_equal [1, 2, foo: 3]
|
169
|
-
a.push_named( bar: 4 ).must_equal [1, 2, foo: 3, bar: 4]
|
170
|
-
a.pop_named( :foo ).must_equal 3
|
171
|
-
a.push_ordered( 42 ).must_equal [1, 2, 42, bar: 4]
|
172
|
-
end
|
173
|
-
|
174
|
-
it "has #to_column_vector" do
|
175
|
-
[1, 2, 3].to_column_vector.must_equal Matrix[[1], [2], [3]]
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
describe Hash do
|
180
|
-
before do
|
181
|
-
require './../lib/y_support/core_ext/hash'
|
182
|
-
end
|
183
|
-
|
184
|
-
it "should have #default! custom defaulter" do
|
185
|
-
defaults = { a: 1, b: nil }
|
186
|
-
test = {}
|
187
|
-
result = test.default!( defaults )
|
188
|
-
assert_equal defaults, result
|
189
|
-
assert_equal result.object_id, test.object_id
|
190
|
-
test = { a: 11, b: 22 }
|
191
|
-
assert_equal( { a: 11, b: 22 }, test.default!( defaults ) )
|
192
|
-
test = { a: 11, c: 22 }
|
193
|
-
{ a: 11, b: nil, c: 22 }.must_equal test.default! defaults
|
194
|
-
end
|
195
|
-
|
196
|
-
it "should have #with_keys and #with_keys!" do
|
197
|
-
test = { "a" => :b, "c" => :d }
|
198
|
-
test.with_keys( &:to_sym ).must_equal( { a: :b, c: :d } )
|
199
|
-
test.must_equal( { "a" => :b, "c" => :d } )
|
200
|
-
test.with_keys! &:to_sym
|
201
|
-
test.must_equal( { a: :b, c: :d } )
|
202
|
-
end
|
203
|
-
|
204
|
-
it "should have #change_keys" do
|
205
|
-
test = { a: 1, c: 2 }
|
206
|
-
test.change_keys { |k, v| k.to_s + v.to_s }
|
207
|
-
.must_equal( { "a1" => 1, "c2" => 2 } )
|
208
|
-
end
|
209
|
-
|
210
|
-
it "should have #with_values and #with_values!" do
|
211
|
-
test = { a: :b, c: :d }
|
212
|
-
test.with_values( &:to_s ).must_equal( { a: "b", c: "d" } )
|
213
|
-
test.must_equal( { a: :b, c: :d } )
|
214
|
-
test.with_values!( &:to_s )
|
215
|
-
test.must_equal( { a: "b", c: "d" } )
|
216
|
-
end
|
217
|
-
|
218
|
-
it "should have #change_values and #change_values!" do
|
219
|
-
test = { a: :b, c: :d }
|
220
|
-
test.modify_values do |k, v| k.to_s + v.to_s end
|
221
|
-
.must_equal( {a: "ab", c: "cd"} )
|
222
|
-
test.must_equal( { a: :b, c: :d } )
|
223
|
-
test.modify_values! { |k, v| k.to_s + v.to_s }
|
224
|
-
test.must_equal( {a: "ab", c: "cd"} )
|
225
|
-
end
|
226
|
-
|
227
|
-
it "should have #modify" do
|
228
|
-
assert_equal( { ab: "ba", cd: "dc" },
|
229
|
-
{ a: :b, c: :d }
|
230
|
-
.modify { |k, v| ["#{k}#{v}".to_sym, "#{v}#{k}"] } )
|
231
|
-
end
|
232
|
-
|
233
|
-
it "should have #slice" do
|
234
|
-
{ a: 1, b: 2, c: 3 }.slice( [:a, :b] ).must_equal( { a: 1, b: 2 } )
|
235
|
-
{ 1 => :a, 2 => :b, 3 => :c, 4 => :d }.slice( 2..3.5 )
|
236
|
-
.must_equal( { 2 => :b, 3 => :c } )
|
237
|
-
{ 0.0 => :a, 1.1 => :b }.slice( 1..2 ).must_equal( { 1.1 => :b } )
|
238
|
-
end
|
239
|
-
|
240
|
-
it "should have #dot! meta patcher for dotted access to keys" do
|
241
|
-
h = Hash.new.merge!(aaa: 1, taint: 2)
|
242
|
-
-> { h.dot! }.must_raise ArgumentError
|
243
|
-
h.dot!( overwrite_methods: true ) # instead of #assert_nothing_raised
|
244
|
-
assert_equal( {aaa: 1}, {aaa: 1}.dot! )
|
245
|
-
end
|
246
|
-
|
247
|
-
it "should be safeguarded against redefining #slice" do
|
248
|
-
m = Hash.instance_method :slice
|
249
|
-
class Hash; def slice( *args ); fail "This should not happen!" end end
|
250
|
-
{}.slice( :a )
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
|
255
|
-
describe "Matrix" do
|
256
|
-
before do
|
257
|
-
require 'matrix'
|
258
|
-
require './../lib/y_support/stdlib_ext/matrix'
|
259
|
-
end
|
260
|
-
|
261
|
-
it "should have #pp method" do
|
262
|
-
assert_respond_to Matrix[[1, 2], [3, 4]], :pretty_print
|
263
|
-
assert_respond_to Matrix[[1, 2], [3, 4]], :pp
|
264
|
-
end
|
265
|
-
|
266
|
-
it "should have #correspondence_matrix method" do
|
267
|
-
assert_respond_to Matrix, :correspondence_matrix
|
268
|
-
assert_equal Matrix[[1, 0, 0], [0, 1, 0]],
|
269
|
-
Matrix.correspondence_matrix( [:a, :b, :c], [:a, :b] )
|
270
|
-
assert_equal Matrix.column_vector( [1, 2] ),
|
271
|
-
Matrix.correspondence_matrix( [:a, :b, :c], [:a, :b] ) *
|
272
|
-
Matrix.column_vector( [1, 2, 3] )
|
273
|
-
assert_equal 2, Matrix.correspondence_matrix( [1, 2], [1] ).column_size
|
274
|
-
end
|
275
|
-
|
276
|
-
it "should have #column_to_a & #row_to_a" do
|
277
|
-
assert_equal [1, 2, 3], Matrix[[1], [2], [3]].column_to_a
|
278
|
-
assert_equal [2, 3, 4], Matrix[[1, 2], [2, 3], [3, 4]].column_to_a( 1 )
|
279
|
-
assert_equal nil, Matrix.empty( 5, 0 ).column_to_a
|
280
|
-
assert_equal [1], Matrix[[1], [2], [3]].row_to_a
|
281
|
-
assert_equal [3], Matrix[[1], [2], [3]].row_to_a( 2 )
|
282
|
-
end
|
283
|
-
|
284
|
-
it "should have aliased #row_vector, #column_vector methods" do
|
285
|
-
assert_equal Matrix.column_vector( [1, 2, 3] ),
|
286
|
-
Matrix.cv( [1, 2, 3] )
|
287
|
-
assert_equal Matrix.row_vector( [1, 2, 3] ),
|
288
|
-
Matrix.rv( [1, 2, 3] )
|
289
|
-
end
|
290
|
-
|
291
|
-
it "should have #join_bottom and #join_right" do
|
292
|
-
assert_equal Matrix[[1, 2], [3, 4]],
|
293
|
-
Matrix[[1, 2]].join_bottom( Matrix[[3, 4]] )
|
294
|
-
assert_equal Matrix[[1, 2, 3, 4]],
|
295
|
-
Matrix[[1, 2]].join_right( Matrix[[3, 4]] )
|
296
|
-
end
|
297
|
-
|
298
|
-
it "should have aliased #row_size, #column_size methods" do
|
299
|
-
assert_equal 3, Matrix.zero(3, 2).height
|
300
|
-
assert_equal 3, Matrix.zero(3, 2).number_of_rows
|
301
|
-
assert_equal 2, Matrix.zero(3, 2).width
|
302
|
-
assert_equal 2, Matrix.zero(3, 2).number_of_columns
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
describe String do
|
307
|
-
before do
|
308
|
-
require './../lib/y_support/core_ext/string'
|
309
|
-
end
|
310
|
-
|
311
|
-
it "should have #can_be_integer? returning the integer or false if not convertible" do
|
312
|
-
assert_equal 33, " 33".to_Integer
|
313
|
-
assert_equal 8, " 010 ".to_Integer
|
314
|
-
assert_equal false, "garbage".to_Integer
|
315
|
-
end
|
316
|
-
|
317
|
-
it "should have #can_be_float? returning the float or false if not convertible" do
|
318
|
-
assert_equal 22.2, ' 2.22e1'.to_Float
|
319
|
-
assert_equal 10, " 010 ".to_Float
|
320
|
-
assert_equal false, "garbage".to_Float
|
321
|
-
end
|
322
|
-
|
323
|
-
it "should have #default! defaulter" do
|
324
|
-
assert_equal "default", "".default!("default")
|
325
|
-
assert_equal "default", " ".default!(:default)
|
326
|
-
assert_equal "default", " \n ".default!("default")
|
327
|
-
assert_equal "kokot", "kokot".default!("default")
|
328
|
-
a = ""
|
329
|
-
assert_equal a.object_id, a.default!("tata").object_id
|
330
|
-
end
|
331
|
-
|
332
|
-
it "should have #stripn upgrade of #strip, which also strips newlines" do
|
333
|
-
assert_equal "test test", " \n test test \n\n \n ".stripn
|
334
|
-
end
|
335
|
-
|
336
|
-
it "should have #compact for joining indented lines (esp. heredocs)" do
|
337
|
-
assert_equal "test test test",
|
338
|
-
"test\n test\n\n \n test\n ".wring_heredoc
|
339
|
-
funny_string = <<-FUNNY_STRING.wring_heredoc
|
340
|
-
This
|
341
|
-
is
|
342
|
-
a funny string.
|
343
|
-
FUNNY_STRING
|
344
|
-
assert_equal( 'This is a funny string.', funny_string )
|
345
|
-
end
|
346
|
-
|
347
|
-
it "should be able #underscore_spaces" do
|
348
|
-
assert_equal "te_st_test", "te st test".underscore_spaces
|
349
|
-
end
|
350
|
-
|
351
|
-
it "should have #symbolize stripping, removing capitalization and diacritics " \
|
352
|
-
'as if to make a suitable symbol material' do
|
353
|
-
assert_equal "Yes_sir!", " \nYes, sir!.; \n \n".standardize
|
354
|
-
end
|
355
|
-
|
356
|
-
it "should have #to_standardized_sym chaining #standardize and #to_sym" do
|
357
|
-
assert_equal :Yes, " \nYes,.; \n \n".to_standardized_sym
|
358
|
-
end
|
359
|
-
end
|
360
|
-
|
361
|
-
|
362
|
-
describe Symbol do
|
363
|
-
before do
|
364
|
-
require_relative '../lib/y_support/core_ext/symbol'
|
365
|
-
end
|
366
|
-
|
367
|
-
it "should have #default! defaulter going through String#default!" do
|
368
|
-
assert_equal :default, "".to_sym.default!(:default)
|
369
|
-
assert_equal :default, "".to_sym.default!("default")
|
370
|
-
assert_equal :default, " ".to_sym.default!("default")
|
371
|
-
assert_equal :default, " \n ".to_sym.default!("default")
|
372
|
-
assert_equal :kokot, :kokot.default!("default")
|
373
|
-
end
|
374
|
-
|
375
|
-
it "should have #to_standardized_sym" do
|
376
|
-
assert_equal :Yes, (:" \nYes, \n").to_standardized_sym
|
377
|
-
end
|
378
|
-
end
|
379
|
-
|
380
|
-
|
381
|
-
describe Numeric do
|
382
|
-
before do
|
383
|
-
require './../lib/y_support/core_ext/numeric'
|
384
|
-
end
|
385
|
-
|
386
|
-
it "should have #zero public class methods" do
|
387
|
-
assert_equal 0, Integer.zero
|
388
|
-
assert_equal 0.0, Float.zero
|
389
|
-
assert_equal Complex(0, 0), Complex.zero
|
390
|
-
end
|
391
|
-
end
|
6
|
+
require_relative 'misc/inert_recorder_test'
|
7
|
+
require_relative 'misc/local_object_test'
|
8
|
+
require_relative 'misc/null_object_test'
|
9
|
+
require_relative 'misc/respond_to_test'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: y_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- boris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -91,18 +91,16 @@ files:
|
|
91
91
|
- lib/y_support/flex_coerce/class_methods.rb
|
92
92
|
- lib/y_support/flex_coerce/flex_proxy.rb
|
93
93
|
- lib/y_support/flex_coerce/module_methods.rb
|
94
|
-
- lib/y_support/inert_recorder.rb
|
95
|
-
- lib/y_support/kde.rb
|
96
|
-
- lib/y_support/literate.rb
|
97
|
-
- lib/y_support/local_object.rb
|
98
94
|
- lib/y_support/misc.rb
|
95
|
+
- lib/y_support/misc/inert_recorder.rb
|
96
|
+
- lib/y_support/misc/local_object.rb
|
97
|
+
- lib/y_support/misc/null_object.rb
|
98
|
+
- lib/y_support/misc/respond_to.rb
|
99
99
|
- lib/y_support/name_magic.rb
|
100
100
|
- lib/y_support/name_magic/array_methods.rb
|
101
101
|
- lib/y_support/name_magic/class_methods.rb
|
102
102
|
- lib/y_support/name_magic/hash_methods.rb
|
103
103
|
- lib/y_support/name_magic/namespace.rb
|
104
|
-
- lib/y_support/null_object.rb
|
105
|
-
- lib/y_support/respond_to.rb
|
106
104
|
- lib/y_support/stdlib_ext.rb
|
107
105
|
- lib/y_support/stdlib_ext/matrix.rb
|
108
106
|
- lib/y_support/stdlib_ext/matrix/misc.rb
|
@@ -119,21 +117,16 @@ files:
|
|
119
117
|
- lib/y_support/typing/object/typing.rb
|
120
118
|
- lib/y_support/unicode.rb
|
121
119
|
- lib/y_support/version.rb
|
122
|
-
- lib/y_support/x.rb
|
123
120
|
- test/core_ext_test.rb
|
124
121
|
- test/flex_coerce_test.rb
|
125
|
-
- test/inert_recorder_test.rb
|
126
|
-
- test/
|
127
|
-
- test/
|
128
|
-
- test/
|
122
|
+
- test/misc/inert_recorder_test.rb
|
123
|
+
- test/misc/local_object_test.rb
|
124
|
+
- test/misc/null_object_test.rb
|
125
|
+
- test/misc/respond_to_test.rb
|
129
126
|
- test/misc_test.rb
|
130
|
-
- test/misc_test/test_module/fixture_class.rb
|
131
127
|
- test/name_magic_test.rb
|
132
|
-
- test/null_object_test.rb
|
133
|
-
- test/respond_to_test.rb
|
134
128
|
- test/typing_test.rb
|
135
129
|
- test/unicode_test.rb
|
136
|
-
- test/x_test.rb
|
137
130
|
- y_support.gemspec
|
138
131
|
homepage: ''
|
139
132
|
licenses:
|
@@ -162,15 +155,11 @@ summary: Support library used by Y gems.
|
|
162
155
|
test_files:
|
163
156
|
- test/core_ext_test.rb
|
164
157
|
- test/flex_coerce_test.rb
|
165
|
-
- test/inert_recorder_test.rb
|
166
|
-
- test/
|
167
|
-
- test/
|
168
|
-
- test/
|
158
|
+
- test/misc/inert_recorder_test.rb
|
159
|
+
- test/misc/local_object_test.rb
|
160
|
+
- test/misc/null_object_test.rb
|
161
|
+
- test/misc/respond_to_test.rb
|
169
162
|
- test/misc_test.rb
|
170
|
-
- test/misc_test/test_module/fixture_class.rb
|
171
163
|
- test/name_magic_test.rb
|
172
|
-
- test/null_object_test.rb
|
173
|
-
- test/respond_to_test.rb
|
174
164
|
- test/typing_test.rb
|
175
165
|
- test/unicode_test.rb
|
176
|
-
- test/x_test.rb
|
data/lib/y_support/kde.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require_relative '../y_support'
|
4
|
-
|
5
|
-
module YSupport::KDE
|
6
|
-
class << self
|
7
|
-
# Open a file with kioclient (using KDE file-viewer associations).
|
8
|
-
#
|
9
|
-
def show_file_with_kioclient( fɴ_with_path )
|
10
|
-
system "sleep 0.2; kioclient exec 'file:%s'" % fɴ_with_path
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end # module YSupport::KDE
|
14
|
-
|
data/lib/y_support/literate.rb
DELETED
@@ -1,253 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require_relative '../y_support'
|
4
|
-
require_relative '../y_support/core_ext/array/misc'
|
5
|
-
|
6
|
-
# Did you ever get in the situation that your code does not work
|
7
|
-
# and you don't know what the problem is? In Ruby, error messages
|
8
|
-
# are supposed to help you, but how often have you seen error
|
9
|
-
# messages telling you completely unhelpful things, such as that
|
10
|
-
# some method is not defined for nil:NilClass? Anyone who
|
11
|
-
# programmed in Ruby for some time knows that raising good error
|
12
|
-
# messages is a pain and the code that raises them tends to make
|
13
|
-
# all your methods look ugly. YSupport's answer to this is Literate
|
14
|
-
# (loaded by require 'y_support/literate'). Literate offers +#try+
|
15
|
-
# block, inside which you can construct verbose error messages with
|
16
|
-
# k +#note+ statements. Note that this +#try+ method has nothing to
|
17
|
-
# do with the infamous error-swallowing +#try+ method seen and
|
18
|
-
# denounced elsewhere. Literate's +#try+ method does not swallow
|
19
|
-
# errors – on the contrary, it helps to raise them. Comments you
|
20
|
-
# make via +#note+ method (or its alias #») inside +#try+ blocks
|
21
|
-
# will be used to construct more informative error messages. Good
|
22
|
-
# error messages ultimately lead to better code and more scalable
|
23
|
-
# development. Simple example:
|
24
|
-
#
|
25
|
-
# "lorem ipsum dolor sit amet".try "to split it into words" do
|
26
|
-
# note is: "a natural language sentence"
|
27
|
-
# note has: "#{size} characters"
|
28
|
-
# words = split ' '
|
29
|
-
# end
|
30
|
-
#
|
31
|
-
# This is not really such a good example, because #try method is
|
32
|
-
# intended for risky operation, and splitting a string into words
|
33
|
-
# is not risky at all. But it clearly shows how Literate works.
|
34
|
-
# Since the era of programs written on punch cards, programmers
|
35
|
-
# already understood that writing code comments is a good
|
36
|
-
# practice. The keystrokes you waste by literate coding will pay
|
37
|
-
# off as soon as you start doing any serious programming. With
|
38
|
-
# Literate, you can take literate coding to the next level by
|
39
|
-
# writing self-documenting code, in which comments introduced by
|
40
|
-
# #note method are read by the interpeter and used to construct
|
41
|
-
# error messages. All you need to learn is how to use +#try+ and
|
42
|
-
# +#note+ methods.
|
43
|
-
#
|
44
|
-
# Literate relies on +Literate::Attempt+ class, which stays hidden
|
45
|
-
# behind the scene, but defines all the error-raising capabilities
|
46
|
-
# of +#try+ blocks.
|
47
|
-
#
|
48
|
-
module Literate
|
49
|
-
# Represents a commented attempt to perform a risky operation,
|
50
|
-
# which may result in errors. The operation code is supplied as
|
51
|
-
# a block. Method #comment defined by this class helps to
|
52
|
-
# increase the informative value of error messages.
|
53
|
-
#
|
54
|
-
class Attempt < BasicObject
|
55
|
-
# String construction closures are defined below.
|
56
|
-
CONSTRUCT = -> string, prefix: '', postfix: '' do
|
57
|
-
s = string.to_s
|
58
|
-
if s.empty? then '' else prefix + s + postfix end
|
59
|
-
end
|
60
|
-
|
61
|
-
# Hash of transitive verb forms.
|
62
|
-
TRANSITIVE = ::Hash.new do |_, key| "#{key}ing %s" end
|
63
|
-
.update( is: "being %s", has: "having %s" )
|
64
|
-
|
65
|
-
# Hash for construction of statements (error message parts).
|
66
|
-
STATE = ::Hash.new do |ꜧ, key| "#{key} %s" end
|
67
|
-
.update( is: "%s", has: "has %s" )
|
68
|
-
|
69
|
-
# An Attempt instance has 4 properties.
|
70
|
-
attr_reader :__subject__ # Grammatical subject of the attempt.
|
71
|
-
attr_reader :__block__ # Block that performs the attempt.
|
72
|
-
attr_reader :__text__ # NL description of the attempt.
|
73
|
-
attr_reader :__knowledge_base__
|
74
|
-
|
75
|
-
# Attempt constructor expects two parameters (:subject and
|
76
|
-
# :text) and one block. Argument of the :subject parameter is
|
77
|
-
# the main subject of the risky operation attempted inside the
|
78
|
-
# block. Argument of the :text parameter is the natural
|
79
|
-
# language textual description of the risky opration.
|
80
|
-
#
|
81
|
-
def initialize( subject: nil, text: nil, &block )
|
82
|
-
@__subject__ = subject
|
83
|
-
@__text__ = text
|
84
|
-
@__block__ = block
|
85
|
-
# Knowledge base is a list of subjects and facts known
|
86
|
-
# about them.
|
87
|
-
@__knowledge_base__ = ::Hash.new do |hash, missing_key|
|
88
|
-
hash[ missing_key ] = [ {} ]
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
# Method #note is available inside the #try block
|
93
|
-
#
|
94
|
-
# note "Concatenation of Foo and Bar",
|
95
|
-
# is: "FooBar",
|
96
|
-
# has: "6 letters"
|
97
|
-
#
|
98
|
-
def note *subjects, **statements, &block
|
99
|
-
if statements.empty? then
|
100
|
-
# No statements were supplied to #note.
|
101
|
-
subjects.each { |subject|
|
102
|
-
# Fill in the knowledge base ...
|
103
|
-
# FIXME: I wonder how the code here works.
|
104
|
-
__knowledge_base__[ subject ].push_ordered subject
|
105
|
-
}
|
106
|
-
# FIXME: I wonder whether returning this is OK.
|
107
|
-
return subjects
|
108
|
-
end
|
109
|
-
# Here, we know that variable statements is not empty.
|
110
|
-
# If subjects variable is empty, assume main subject.
|
111
|
-
subjects << __subject__ if subjects.empty?
|
112
|
-
# Fill the knowledge base ...
|
113
|
-
# FIXME: I wonder how the code here works.
|
114
|
-
subjects.each { |subject|
|
115
|
-
statements.each do |verb, object|
|
116
|
-
__knowledge_base__[ subject ].push_named verb => object
|
117
|
-
end
|
118
|
-
}
|
119
|
-
# Return the second element of the first statement.
|
120
|
-
return statements.first[ 1 ]
|
121
|
-
end
|
122
|
-
|
123
|
-
# Alias of #note method that allows comments such as this:
|
124
|
-
#
|
125
|
-
# a = []
|
126
|
-
# a.try "to insert number one in it" do
|
127
|
-
# » is: "an empty array"
|
128
|
-
# push 1
|
129
|
-
# end
|
130
|
-
#
|
131
|
-
alias » note
|
132
|
-
|
133
|
-
# Runs the attempt.
|
134
|
-
#
|
135
|
-
def __run__ *args
|
136
|
-
begin
|
137
|
-
instance_exec *args, &__block__
|
138
|
-
rescue ::StandardError => error
|
139
|
-
# Error has occured. Show time for Literate::Attempt.
|
140
|
-
raise error, __error_message__( error )
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
# Facilitating error messages is the main purpose of Literate.
|
145
|
-
# This method is invoked when an error occurs inside the block
|
146
|
-
# run by Literate::Attempt instance and it constructs a verbose
|
147
|
-
# error message using the facts the Attempt instance knows.
|
148
|
-
#
|
149
|
-
def __error_message__ error
|
150
|
-
# Write the first part of the error message.
|
151
|
-
part1 = "When trying #{__text__}"
|
152
|
-
# Get the description of the main subject.
|
153
|
-
subject, statements = __describe__( __subject__ )
|
154
|
-
# Write the 2nd part of the error message.
|
155
|
-
part2 = CONSTRUCT.( subject, prefix: ' ' )
|
156
|
-
# Construct the descriptive string of the main subject.
|
157
|
-
subject_description = statements.map { |verb, object|
|
158
|
-
# Generate the statement string.
|
159
|
-
STATE[ verb ] % object
|
160
|
-
}.join ', ' # join the statement strings with commas
|
161
|
-
# Write the third part of the error message.
|
162
|
-
part3 = CONSTRUCT.( subject_description,
|
163
|
-
prefix: ' (', postfix: ')' )
|
164
|
-
# Write the fourth part of the error message.
|
165
|
-
part4 = CONSTRUCT.( __circumstances__, prefix: ', ' )
|
166
|
-
# Write the fifth part of the error message.
|
167
|
-
part5 = ": #{error}"
|
168
|
-
part6 = ['.', '!', '?'].include?( part5[-1] ) ? '' : '!'
|
169
|
-
return [ part1, part2, part3, part4, part5, part6 ].join
|
170
|
-
end
|
171
|
-
|
172
|
-
# Inside the block, +#try method is delegated to the subject
|
173
|
-
# of the attempt.
|
174
|
-
#
|
175
|
-
def try *args, &block
|
176
|
-
__subject__.try *args, &block
|
177
|
-
end
|
178
|
-
|
179
|
-
# Method missing delegates all methods not recognized by
|
180
|
-
# Literate::Attempt class (which is a subclass of BasicObject)
|
181
|
-
# to the subject of the attempt.
|
182
|
-
#
|
183
|
-
def method_missing symbol, *args
|
184
|
-
__subject__.send symbol, *args
|
185
|
-
end
|
186
|
-
|
187
|
-
# Produces a description of the subject supplied to the method.
|
188
|
-
#
|
189
|
-
def __describe__ subject
|
190
|
-
# Start with the facts known about the subject.
|
191
|
-
facts = __knowledge_base__[ subject ].dup
|
192
|
-
# FIXME: I wonder what this method is *really* doing.
|
193
|
-
# It seems that I wrote this library too quickly and didn't
|
194
|
-
# bother with properly defining its list of facts.
|
195
|
-
# I did not define what is a "fact", what is a "statement"
|
196
|
-
# etc., I just go around using the words and hoping I will
|
197
|
-
# understand it after myself later. I found it's quite hard.
|
198
|
-
statements = if facts.last.is_a? ::Hash then
|
199
|
-
facts.pop
|
200
|
-
else {} end
|
201
|
-
fs = facts.join ', '
|
202
|
-
if statements.empty? then
|
203
|
-
return fs, statements
|
204
|
-
else
|
205
|
-
return facts.empty? ? subject.to_s : fs, statements
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
# Produces description of the circumstances, that is,
|
210
|
-
# concatenated descriptions of all facts known to the Attempt
|
211
|
-
# instance except those about the main subject of the attempt.
|
212
|
-
#
|
213
|
-
def __circumstances__
|
214
|
-
# Start with all facts known to the Attempt instance.
|
215
|
-
base = __knowledge_base__
|
216
|
-
# Ignore the facts about the main subject.
|
217
|
-
circumstances = base.reject { |s, _| s == __subject__ }
|
218
|
-
# Construct descriptive strings of the remaining facts.
|
219
|
-
fact_strings = circumstances.map { |subject, _|
|
220
|
-
subject, statements = __describe__( subject )
|
221
|
-
statements = statements.map { |verb, object|
|
222
|
-
TRANSITIVE[ verb ] % object
|
223
|
-
}.join( ', ' )
|
224
|
-
# Create the fact string.
|
225
|
-
subject + CONSTRUCT.( statements, prefix: ' ' )
|
226
|
-
}
|
227
|
-
# Concatenate the fact strings and return the result.
|
228
|
-
return fact_strings.join( ', ' )
|
229
|
-
end
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
class Object
|
234
|
-
# Try method takes two textual arguments and one block. The first
|
235
|
-
# (optional) argument is a natural language description of the
|
236
|
-
# method's receiver (with #to_s of the receiver used by
|
237
|
-
# default). The second argument is a natural language description
|
238
|
-
# of the supplied block's _contract_ -- in other words, what the
|
239
|
-
# supplied block tries to do. Finally, the block contains the
|
240
|
-
# code to perform the described risky action. Inside the block,
|
241
|
-
# +#note+ method is available, which builds up the context
|
242
|
-
# information for a good error message, should the risky action
|
243
|
-
# raise one.
|
244
|
-
#
|
245
|
-
def try attempt_description, &block
|
246
|
-
# Construct a new Attempt instance.
|
247
|
-
attempt = Literate::Attempt.new subject: self,
|
248
|
-
text: attempt_description,
|
249
|
-
&block
|
250
|
-
# Run the block.
|
251
|
-
attempt.__run__
|
252
|
-
end
|
253
|
-
end
|
data/lib/y_support/x.rb
DELETED
@@ -1,86 +0,0 @@
|
|
1
|
-
require_relative '../y_support'
|
2
|
-
require 'gtk2'
|
3
|
-
|
4
|
-
# Assets related to X windows control.
|
5
|
-
#
|
6
|
-
module YSupport::X
|
7
|
-
# ==================================================================
|
8
|
-
#
|
9
|
-
# Notice: Assets related to X windows control do not really belong
|
10
|
-
# to YSupport. I should keep them somewhere else. I will keep this
|
11
|
-
# file in YSupport 2.x for historical reasons. Module YSupport::X
|
12
|
-
# will be removed in YSupport 3.0, which will no longer cover tools
|
13
|
-
# for X windows control.
|
14
|
-
#
|
15
|
-
warn "YSupport::X is obsolete!"
|
16
|
-
#
|
17
|
-
# ==================================================================
|
18
|
-
|
19
|
-
# Echo a string to the primary X clip; `xsel -b -i`.
|
20
|
-
#
|
21
|
-
def echo_primary_clipboard string
|
22
|
-
s = 'echo -n ' + string.inspect + ' | xsel -b -i'
|
23
|
-
system s
|
24
|
-
end
|
25
|
-
|
26
|
-
# Echo a string to the secondary X clip; `xsel -b -i`.
|
27
|
-
#
|
28
|
-
def echo_secondary_clipboard string
|
29
|
-
s = 'echo -n ' + string.inspect + ' | xsel -s -i'
|
30
|
-
system s
|
31
|
-
end
|
32
|
-
|
33
|
-
# Dialog box querying for a string.
|
34
|
-
#
|
35
|
-
def query_box( dialog_text, prompt: '> ', title_bar: 'User input required', &block )
|
36
|
-
title_bar_text = title_bar
|
37
|
-
prompt_label_text = prompt
|
38
|
-
|
39
|
-
w = Gtk::Window.new( Gtk::Window::TOPLEVEL )
|
40
|
-
w.set_title( title_bar_text )
|
41
|
-
w.border_width = 10
|
42
|
-
w.signal_connect 'delete_event' do Gtk.main_quit end # cc
|
43
|
-
|
44
|
-
tlabel_widget = Gtk::Label.new( dialog_text )
|
45
|
-
plabel_widget = Gtk::Label.new( prompt_label_text )
|
46
|
-
ebox_widget = Gtk::Entry.new
|
47
|
-
ebox_widget.visibility = true # cc
|
48
|
-
|
49
|
-
hbox = Gtk::HBox.new(false, 5) # cc
|
50
|
-
hbox.pack_start_defaults( plabel_widget ) # cc
|
51
|
-
hbox.pack_start_defaults( ebox_widget )
|
52
|
-
vbox = Gtk::VBox.new(false, 5)
|
53
|
-
vbox.pack_start_defaults( tlabel_widget )
|
54
|
-
vbox.pack_start_defaults( hbox )
|
55
|
-
w.add(vbox)
|
56
|
-
|
57
|
-
memo = ""
|
58
|
-
memo_closure = -> txt { memo << txt }
|
59
|
-
|
60
|
-
ebox_widget.signal_connect "key-release-event" do |sender, event| # cc
|
61
|
-
kn = Gdk::Keyval.to_name( event.keyval )
|
62
|
-
if kn == "Return"
|
63
|
-
memo_closure.( sender.text )
|
64
|
-
block.( sender.text ) if block
|
65
|
-
Gtk.main_quit
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
memo.tap { w.show_all; Gtk.main }
|
70
|
-
end
|
71
|
-
|
72
|
-
# Message box.
|
73
|
-
#
|
74
|
-
def message_box( message="Press any key to close this window!" )
|
75
|
-
w = Gtk::Window.new
|
76
|
-
w.add_events Gdk::Event::KEY_PRESS
|
77
|
-
w.add Gtk::Label.new( message )
|
78
|
-
w.signal_connect "key-release-event" do Gtk.main_quit end
|
79
|
-
w.set_default_size( 600, 120 )
|
80
|
-
w.show_all
|
81
|
-
Gtk.main
|
82
|
-
end
|
83
|
-
alias popup message_box
|
84
|
-
|
85
|
-
extend self
|
86
|
-
end # module YSupport::X
|
data/test/kde_test.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
#! /usr/bin/ruby
|
2
|
-
#encoding: utf-8
|
3
|
-
|
4
|
-
require 'minitest/autorun'
|
5
|
-
require './../lib/y_support/kde'
|
6
|
-
|
7
|
-
describe YSupport::KDE do
|
8
|
-
it "should at least respond to certain messages" do
|
9
|
-
assert_respond_to YSupport::KDE, :show_file_with_kioclient
|
10
|
-
end
|
11
|
-
end
|
data/test/literate_test.rb
DELETED
@@ -1,231 +0,0 @@
|
|
1
|
-
#! /usr/bin/ruby
|
2
|
-
# coding: utf-8
|
3
|
-
|
4
|
-
require 'minitest/autorun'
|
5
|
-
# require 'y_support/literate' # tested component itself
|
6
|
-
require './../lib/y_support/literate'
|
7
|
-
|
8
|
-
describe Literate::Attempt do
|
9
|
-
before do
|
10
|
-
@str = "lorem ipsum dolor sit amet"
|
11
|
-
@txt = "split the sentence into words"
|
12
|
-
# A simple attempt block.
|
13
|
-
@a = Literate::Attempt.new subject: @str, text: @txt do
|
14
|
-
note is: "natural language sentence"
|
15
|
-
split ' '
|
16
|
-
end
|
17
|
-
# More verbose attempt block.
|
18
|
-
chosen_word = 3
|
19
|
-
@b = Literate::Attempt.new subject: @str, text: @txt do
|
20
|
-
» is: "natural language sentence"
|
21
|
-
» has: "#{size} characters"
|
22
|
-
words = split ' '
|
23
|
-
» "number of words", is: words.size
|
24
|
-
» "chosen word", is: words[ chosen_word ]
|
25
|
-
words
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should have 4 basic properties" do
|
30
|
-
@a.__text__.must_equal @txt
|
31
|
-
@a.__subject__.must_equal @str
|
32
|
-
@a.__block__.must_be_kind_of Proc
|
33
|
-
@a.__knowledge_base__.must_be_empty
|
34
|
-
# Fact list is empty because the block hasn't been run yet.
|
35
|
-
# Let's run the block:
|
36
|
-
@a.__run__
|
37
|
-
# Now the fact list includes the comment made in the block.
|
38
|
-
@a.__knowledge_base__.size.must_equal 1
|
39
|
-
end
|
40
|
-
|
41
|
-
describe "#__run__" do
|
42
|
-
it "must run the attempt's block" do
|
43
|
-
expected_result = "lorem", "ipsum", "dolor", "sit", "amet"
|
44
|
-
@a.__run__.must_equal expected_result
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
describe "Object#try method" do
|
49
|
-
before do
|
50
|
-
error = false
|
51
|
-
@error_switch = -> arg { error = arg }
|
52
|
-
@text = "to split into words"
|
53
|
-
@block = proc do
|
54
|
-
note is: "a string with several words"
|
55
|
-
fail error if error
|
56
|
-
split ' '
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
it "must execute the block allowing subject's methods" do
|
61
|
-
"hello world".try( @text, &@block )
|
62
|
-
.must_equal [ "hello", "world" ]
|
63
|
-
@error_switch.( TypeError )
|
64
|
-
-> { "hello world".try @text, &@block }.must_raise TypeError
|
65
|
-
@error_switch.( NameError )
|
66
|
-
-> { "hello world".try @text, &@block }.must_raise NameError
|
67
|
-
end
|
68
|
-
|
69
|
-
it "must allow Literate::Attempt#comment inside the block" do
|
70
|
-
begin
|
71
|
-
@error_switch.( TypeError )
|
72
|
-
"hello world".try @text, &@block
|
73
|
-
rescue => error
|
74
|
-
# Proof that Literate::Attemp#comment activated.
|
75
|
-
m = error.message
|
76
|
-
m.must_include "When trying to split into words"
|
77
|
-
m.must_include "a string with several words"
|
78
|
-
else
|
79
|
-
flunk "Error expected but nothing raised!"
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
describe "#note" do
|
85
|
-
it "must write entries into @__knowledge_base__" do
|
86
|
-
@a.__run__
|
87
|
-
x = { @str => [ is: "natural language sentence" ] }
|
88
|
-
@a.__knowledge_base__.must_equal x
|
89
|
-
end
|
90
|
-
|
91
|
-
it "must write entries into @__knowledge_base__" do
|
92
|
-
@b.__run__
|
93
|
-
x = { @str => [ is: "natural language sentence",
|
94
|
-
has: "26 characters" ],
|
95
|
-
"number of words" => [ is: 5 ],
|
96
|
-
"chosen word" => [ is: "sit" ] }
|
97
|
-
@b.__knowledge_base__ .must_equal x
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
describe "#__describe__" do
|
102
|
-
it "must produce description of the main subject" do
|
103
|
-
@a.__run__
|
104
|
-
@a.__describe__( @a.__subject__ )
|
105
|
-
.must_equal [ "lorem ipsum dolor sit amet",
|
106
|
-
{ is: "natural language sentence" } ]
|
107
|
-
@b.__run__
|
108
|
-
@b.__describe__( @a.__subject__ )
|
109
|
-
.must_equal [ "lorem ipsum dolor sit amet",
|
110
|
-
{ is: "natural language sentence",
|
111
|
-
has: "26 characters" } ]
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
describe "#__circumstances__" do
|
116
|
-
it "..." do
|
117
|
-
@a.__run__
|
118
|
-
@a.__circumstances__.must_equal ""
|
119
|
-
@b.__run__
|
120
|
-
@b.__circumstances__
|
121
|
-
.must_equal "number of words being 5, " +
|
122
|
-
"chosen word being sit"
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe "usecase 1: name validator" do
|
128
|
-
before do
|
129
|
-
@name_validator = Object.new
|
130
|
-
@name_validator.define_singleton_method :validate do |name|
|
131
|
-
name.to_s.try "to validate the requested name" do
|
132
|
-
note "rejecting non-capitalized names"
|
133
|
-
fail NameError unless ( ?A..?Z ) === self[0]
|
134
|
-
note "rejecting names with spaces"
|
135
|
-
fail NameError unless split( ' ' ).size == 1
|
136
|
-
end
|
137
|
-
return name
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
it "should validate good names" do
|
142
|
-
@name_validator.validate( :Fred ).must_equal :Fred
|
143
|
-
@name_validator.validate( "Su_san" ).must_equal "Su_san"
|
144
|
-
end
|
145
|
-
|
146
|
-
it "should reject bad names with a good error message" do
|
147
|
-
expected_message =
|
148
|
-
"When trying to validate the requested name, rejecting " +
|
149
|
-
"non-capitalized names: NameError!"
|
150
|
-
begin
|
151
|
-
@name_validator.validate( :fred )
|
152
|
-
rescue => error
|
153
|
-
error.message.must_equal expected_message
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
# describe 'case 1' do
|
159
|
-
# it "should work" do
|
160
|
-
# # begin
|
161
|
-
# # @a.__run__
|
162
|
-
# # rescue TypeError => err
|
163
|
-
# # expected_msg = "When trying concatenation of an array " +
|
164
|
-
# # "with 2 elements, TypeError occurred: " +
|
165
|
-
# # "Lorem ipsum dolor sit amet!"
|
166
|
-
# # # Make sure the last 45 characters is OK.
|
167
|
-
# # assert_equal expected_msg[-45..-1], err.message[-45..-1]
|
168
|
-
# # # # Make sure that whole message is as expected.
|
169
|
-
# # # assert_equal expected_msg, err.message
|
170
|
-
# # else
|
171
|
-
# # flunk "Expected TypeError error not raised!"
|
172
|
-
# # end
|
173
|
-
# end
|
174
|
-
# end
|
175
|
-
|
176
|
-
# describe 'case 2' do
|
177
|
-
# it "should work" do
|
178
|
-
# begin
|
179
|
-
# try "to call constant Nothing" do Nothing end
|
180
|
-
# rescue NameError => err
|
181
|
-
# expected_msg = 'When trying to call constant Nothing, ' +
|
182
|
-
# 'NameError occurred: uninitialized constant Nothing'
|
183
|
-
# assert_equal( expected_msg, err.message )
|
184
|
-
# else
|
185
|
-
# flunk "Expected NameError error not raised!"
|
186
|
-
# end
|
187
|
-
# end
|
188
|
-
# end
|
189
|
-
|
190
|
-
# describe 'case 3' do
|
191
|
-
# it "should work" do
|
192
|
-
# o = Object.new
|
193
|
-
# class << o
|
194
|
-
# def to_s; "THIS OBJECT" end
|
195
|
-
# def hello!; "hello hello" end
|
196
|
-
# end
|
197
|
-
# # Object's methods must be callable
|
198
|
-
# o.try "to say hello" do hello! end.must_equal "hello hello"
|
199
|
-
# begin
|
200
|
-
# o.try "to call a weird method" do goodbye! end
|
201
|
-
# rescue NoMethodError => err
|
202
|
-
# err.message.must_include "When trying to call a weird " +
|
203
|
-
# "method, NoMethodError occurred: undefined method"
|
204
|
-
# err.message.must_include "goodbye!"
|
205
|
-
# end
|
206
|
-
# end
|
207
|
-
# end
|
208
|
-
|
209
|
-
# describe 'case 4' do
|
210
|
-
# it "should work" do
|
211
|
-
# begin
|
212
|
-
# "FooBar".try "to do something" do
|
213
|
-
# comment has: "#{size} letters",
|
214
|
-
# is: "a #{self.class} instance"
|
215
|
-
# unless include? "Quux"
|
216
|
-
# comment "Quux", is: "not a part of it"
|
217
|
-
# try "to append Quux to it" do
|
218
|
-
# self << "Quux"
|
219
|
-
# fail "EPIC FAIL"
|
220
|
-
# end
|
221
|
-
# end
|
222
|
-
# end
|
223
|
-
# rescue => err
|
224
|
-
# err.message.must_equal "When trying to do something, " +
|
225
|
-
# "FooBar having 6 letters, being a String instance, " +
|
226
|
-
# "Quux being not a part of it, RuntimeError occurred: " +
|
227
|
-
# "When trying to append Quux to it, RuntimeError " +
|
228
|
-
# "occurred: EPIC FAIL"
|
229
|
-
# end
|
230
|
-
# end
|
231
|
-
# end
|
data/test/x_test.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
#! /usr/local/bin/ruby
|
2
|
-
# encoding: utf-8
|
3
|
-
|
4
|
-
require 'minitest/autorun'
|
5
|
-
|
6
|
-
describe "y_support/x" do
|
7
|
-
before do
|
8
|
-
require './../lib/y_support/x'
|
9
|
-
YSupport::X.echo_primary_clipboard '"foo & bar"'
|
10
|
-
YSupport::X.echo_secondary_clipboard "baz"
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should have clipboard as expected" do
|
14
|
-
assert_equal '"foo & bar" baz', [`xsel -b`, `xsel -s`].join(' ')
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should know #query_box and #message_box methods" do
|
18
|
-
assert_respond_to YSupport::X, :query_box
|
19
|
-
assert_respond_to YSupport::X, :message_box
|
20
|
-
end
|
21
|
-
end # describe "y_support/x"
|