zucker 1 → 2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +14 -0
- data/Rakefile +14 -4
- data/lib/zucker.rb +0 -4
- data/lib/zucker/2/D.rb +19 -0
- data/lib/zucker/2/aliases.rb +28 -0
- data/lib/zucker/2/all.rb +6 -0
- data/lib/zucker/2/array.rb +17 -0
- data/lib/zucker/2/array2proc.rb +16 -0
- data/lib/zucker/2/binding.rb +25 -0
- data/lib/zucker/2/blank.rb +26 -0
- data/lib/zucker/2/class2proc.rb +10 -0
- data/lib/zucker/2/debug.rb +4 -0
- data/lib/zucker/2/default.rb +25 -0
- data/lib/zucker/2/desc/D.yaml +23 -0
- data/lib/zucker/2/desc/aliases.yaml +16 -0
- data/lib/zucker/2/desc/array.yaml +14 -0
- data/lib/zucker/2/desc/array2proc.yaml +17 -0
- data/lib/zucker/2/desc/binding.yaml +10 -0
- data/lib/zucker/2/desc/blank.yaml +10 -0
- data/lib/zucker/2/desc/class2proc.yaml +12 -0
- data/lib/zucker/2/desc/egonil.yaml +17 -0
- data/lib/zucker/2/desc/enumerable.yaml +11 -0
- data/lib/zucker/2/desc/hash.yaml +14 -0
- data/lib/zucker/2/desc/hash2proc.yaml +11 -0
- data/lib/zucker/2/desc/info.yaml +12 -0
- data/lib/zucker/2/desc/iterate.yaml +17 -0
- data/lib/zucker/2/desc/ivars.yaml +20 -0
- data/lib/zucker/2/desc/kernel.yaml +13 -0
- data/lib/zucker/2/desc/mcopy.yaml +12 -0
- data/lib/zucker/2/desc/mm.yaml +12 -0
- data/lib/zucker/2/desc/regexp2proc.yaml +13 -0
- data/lib/zucker/2/desc/sandbox.yaml +19 -0
- data/lib/zucker/2/desc/square_brackets_for.yaml +23 -0
- data/lib/zucker/2/desc/string.yaml +13 -0
- data/lib/zucker/2/desc/unary.yaml +15 -0
- data/lib/zucker/2/desc/union.yaml +11 -0
- data/lib/zucker/2/doc/zucker_doc.html +1494 -0
- data/lib/zucker/2/doc/zucker_doc.rb +498 -0
- data/lib/zucker/2/egonil.rb +14 -0
- data/lib/zucker/2/enumerable.rb +12 -0
- data/lib/zucker/2/hash.rb +19 -0
- data/lib/zucker/2/hash2proc.rb +14 -0
- data/lib/zucker/2/info.rb +164 -0
- data/lib/zucker/2/iterate.rb +23 -0
- data/lib/zucker/2/ivars.rb +26 -0
- data/lib/zucker/2/kernel.rb +18 -0
- data/lib/zucker/2/mcopy.rb +9 -0
- data/lib/zucker/2/mm.rb +31 -0
- data/lib/zucker/2/regexp2proc.rb +10 -0
- data/lib/zucker/2/sandbox.rb +19 -0
- data/lib/zucker/2/spec/D_spec.rb +35 -0
- data/lib/zucker/2/spec/aliases_spec.rb +13 -0
- data/lib/zucker/2/spec/array2proc_spec.rb +12 -0
- data/lib/zucker/2/spec/array_spec.rb +23 -0
- data/lib/zucker/2/spec/binding_spec.rb +8 -0
- data/lib/zucker/2/spec/blank_spec.rb +20 -0
- data/lib/zucker/2/spec/class2proc_spec.rb +9 -0
- data/lib/zucker/2/spec/egonil_spec.rb +30 -0
- data/lib/zucker/2/spec/enumerable_spec.rb +8 -0
- data/lib/zucker/2/spec/hash2proc_spec.rb +12 -0
- data/lib/zucker/2/spec/hash_spec.rb +17 -0
- data/lib/zucker/2/spec/info_spec.rb +8 -0
- data/lib/zucker/2/spec/iterate_spec.rb +54 -0
- data/lib/zucker/2/spec/ivars_spec.rb +20 -0
- data/lib/zucker/2/spec/kernel_spec.rb +28 -0
- data/lib/zucker/2/spec/mcopy_spec.rb +13 -0
- data/lib/zucker/2/spec/mm_spec.rb +8 -0
- data/lib/zucker/2/spec/regexp2proc_spec.rb +9 -0
- data/lib/zucker/2/spec/sandbox_spec.rb +26 -0
- data/lib/zucker/2/spec/spec_helper.rb +18 -0
- data/lib/zucker/2/spec/square_brackets_for_spec.rb +59 -0
- data/lib/zucker/2/spec/string_spec.rb +29 -0
- data/lib/zucker/2/spec/unary_spec.rb +26 -0
- data/lib/zucker/2/spec/union_spec.rb +15 -0
- data/lib/zucker/2/square_brackets_for.rb +20 -0
- data/lib/zucker/2/string.rb +25 -0
- data/lib/zucker/2/unary.rb +22 -0
- data/lib/zucker/2/union.rb +14 -0
- data/lib/zucker/all.rb +2 -2
- data/lib/zucker/array2proc.rb +7 -1
- data/lib/zucker/debug.rb +3 -3
- data/lib/zucker/default.rb +1 -1
- data/lib/zucker/desc/D.yaml +1 -1
- data/lib/zucker/desc/array.yaml +1 -1
- data/lib/zucker/desc/array2proc.yaml +5 -1
- data/lib/zucker/desc/binding.yaml +2 -2
- data/lib/zucker/desc/blank.yaml +1 -3
- data/lib/zucker/desc/egonil.yaml +1 -1
- data/lib/zucker/desc/enumerable.yaml +1 -1
- data/lib/zucker/desc/hash.yaml +4 -2
- data/lib/zucker/desc/info.yaml +12 -0
- data/lib/zucker/desc/kernel.yaml +2 -2
- data/lib/zucker/desc/mm.yaml +3 -3
- data/lib/zucker/desc/regexp2proc.yaml +1 -1
- data/lib/zucker/desc/sandbox.yaml +6 -1
- data/lib/zucker/desc/string.yaml +1 -1
- data/lib/zucker/desc/unary.yaml +2 -2
- data/lib/zucker/desc/union.yaml +1 -1
- data/lib/zucker/doc/zucker_doc.html +441 -233
- data/lib/zucker/doc/zucker_doc.rb +32 -27
- data/lib/zucker/hash.rb +3 -3
- data/lib/zucker/info.rb +164 -0
- data/lib/zucker/ivars.rb +1 -1
- data/lib/zucker/kernel.rb +2 -4
- data/lib/zucker/sandbox.rb +7 -3
- data/lib/zucker/spec/D_spec.rb +15 -7
- data/lib/zucker/spec/aliases_spec.rb +3 -24
- data/lib/zucker/spec/array2proc_spec.rb +5 -1
- data/lib/zucker/spec/array_spec.rb +1 -1
- data/lib/zucker/spec/binding_spec.rb +4 -2
- data/lib/zucker/spec/blank_spec.rb +1 -1
- data/lib/zucker/spec/class2proc_spec.rb +1 -1
- data/lib/zucker/spec/egonil_spec.rb +1 -1
- data/lib/zucker/spec/enumerable_spec.rb +1 -1
- data/lib/zucker/spec/hash2proc_spec.rb +1 -1
- data/lib/zucker/spec/hash_spec.rb +10 -3
- data/lib/zucker/spec/info_spec.rb +8 -0
- data/lib/zucker/spec/iterate_spec.rb +1 -1
- data/lib/zucker/spec/ivars_spec.rb +1 -1
- data/lib/zucker/spec/kernel_spec.rb +1 -7
- data/lib/zucker/spec/mcopy_spec.rb +1 -1
- data/lib/zucker/spec/mm_spec.rb +4 -2
- data/lib/zucker/spec/regexp2proc_spec.rb +1 -1
- data/lib/zucker/spec/sandbox_spec.rb +22 -3
- data/lib/zucker/spec/spec_helper.rb +18 -0
- data/lib/zucker/spec/square_brackets_for_spec.rb +1 -1
- data/lib/zucker/spec/string_spec.rb +7 -7
- data/lib/zucker/spec/unary_spec.rb +1 -1
- data/lib/zucker/spec/union_spec.rb +8 -6
- metadata +207 -131
- data/lib/zucker/doc/meta.yaml +0 -4
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'zucker/blank'
|
2
|
+
|
3
|
+
describe 'Object#blank?' do
|
4
|
+
it 'should be blank for blank values' do
|
5
|
+
blank_values = [ nil, false, '', ' ', " \n\t \r ", [], {}, // ]
|
6
|
+
|
7
|
+
blank_values.each{ |blank|
|
8
|
+
blank.blank?.should == true
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should not be blank for non blank values' do
|
13
|
+
present_values = [ Object.new, true, 0, 1, 'a', [nil], { nil => nil } ]
|
14
|
+
|
15
|
+
present_values.each{ |present|
|
16
|
+
present.blank?.should == false
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'zucker/egonil'
|
2
|
+
|
3
|
+
describe 'egonil' do
|
4
|
+
it 'should not raise nil exceptions in the block' do
|
5
|
+
proc do
|
6
|
+
egonil{ nil.some_methods.that[:do].not.exist }
|
7
|
+
end.should_not raise_exception
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should return the nil_value if given' do
|
11
|
+
egonil(9){ nil.some_methods.that[:do].not.exist }.should == 9
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should restore default behaviour after the block' do
|
15
|
+
proc do
|
16
|
+
egonil{ nil.some_methods.that[:do].not.exist }
|
17
|
+
end.should_not raise_exception
|
18
|
+
|
19
|
+
proc do
|
20
|
+
nil.a_method
|
21
|
+
end.should raise_exception NoMethodError
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'raise NoMethodError for non-nil objects' do
|
25
|
+
proc do
|
26
|
+
egonil{ 5.a_method }
|
27
|
+
end.should raise_exception NoMethodError
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'zucker/hash2proc'
|
2
|
+
|
3
|
+
describe 'Hash#to_proc' do
|
4
|
+
it 'should run the proc given in the value for a key in the hash' do
|
5
|
+
[1,2,3,4].map(&{
|
6
|
+
4 => :to_s,
|
7
|
+
# 3 => [:to_s, 2] # "11" => if array2proc is used
|
8
|
+
2 => lambda {|e| e + 1000}
|
9
|
+
}).should == [1, 1002, 3, "4"]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'zucker/hash'
|
2
|
+
|
3
|
+
describe 'Hash.zip' do
|
4
|
+
it 'should zip together both given enumerables and take them as key=>values for a new hash' do
|
5
|
+
Hash.zip( [1,2,3], [4,5,6] ).should == { 1=>4, 2=>5, 3=>6 }
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe 'Hash#<<' do
|
10
|
+
it 'should append new elements to the hash' do
|
11
|
+
a = { 1=>4, 2=>5, 3=>6 }
|
12
|
+
a << { 4=>7 }
|
13
|
+
a << [5, 8]
|
14
|
+
a.should == { 1=>4, 2=>5, 3=>6, 4=>7, 5=>8 }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'zucker/iterate'
|
2
|
+
|
3
|
+
describe 'Object#iterate' do
|
4
|
+
let :a do [1, 2, 3] end
|
5
|
+
let :b do %w|a b c d| end
|
6
|
+
let :res do Hash.new {[]} end
|
7
|
+
|
8
|
+
it 'should behave like Enumerable#each for a single argument' do
|
9
|
+
iterate a do |ele|
|
10
|
+
res[:iter] << ele
|
11
|
+
end
|
12
|
+
|
13
|
+
a.each do |ele|
|
14
|
+
res[:each] << ele
|
15
|
+
end
|
16
|
+
|
17
|
+
res[:iter].should == res[:each]
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should pass the right params to the block' do
|
21
|
+
res = Hash.new {[]} # TODO: why?
|
22
|
+
res[:iter_a_b] = [] # ....
|
23
|
+
res[:iter_b_a] = [] # ....
|
24
|
+
|
25
|
+
|
26
|
+
iterate a, b do |e,f|
|
27
|
+
res[:iter_a_b] << [e, f]
|
28
|
+
# p res[:iter_a_b], e, f
|
29
|
+
end
|
30
|
+
|
31
|
+
res[:iter_a_b].should == [
|
32
|
+
[1, 'a'],
|
33
|
+
[2, 'b'],
|
34
|
+
[3, 'c'],
|
35
|
+
[nil, 'd'],
|
36
|
+
]
|
37
|
+
|
38
|
+
iterate b, a do |e,f|
|
39
|
+
res[:iter_b_a] << [e, f]
|
40
|
+
end
|
41
|
+
|
42
|
+
res[:iter_b_a].should == [
|
43
|
+
['a', 1],
|
44
|
+
['b', 2],
|
45
|
+
['c', 3],
|
46
|
+
['d', nil],
|
47
|
+
]
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should return enumerators if no block is applied' do
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'zucker/ivars'
|
2
|
+
|
3
|
+
describe 'instance_variables_from' do
|
4
|
+
it 'should tansform the given parameter to instance variables' do
|
5
|
+
def a_method(a = 1, b = 2)
|
6
|
+
instance_variables_from binding # assigns @a and @b
|
7
|
+
|
8
|
+
params = {:c => 3, :d => 4}
|
9
|
+
ivars params # # assigns @c and @d
|
10
|
+
end
|
11
|
+
|
12
|
+
a_method
|
13
|
+
@a.should == 1
|
14
|
+
@b.should == 2
|
15
|
+
@c.should == 3
|
16
|
+
@d.should == 4
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'zucker/kernel'
|
2
|
+
|
3
|
+
describe 'activate_warnings!' do
|
4
|
+
it 'should set $VERBOSE to true' do
|
5
|
+
activate_warnings!
|
6
|
+
$VERBOSE.should == true
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'deactivate_warnings!' do
|
11
|
+
it 'should set $VERBOSE to false' do
|
12
|
+
deactivate_warnings!
|
13
|
+
$VERBOSE.should == false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'library?' do
|
18
|
+
it 'should return false if the file is invoked directly' do
|
19
|
+
library?.should == ( __FILE__ != $PROGRAM_NAME )
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'ignore_sigint!' do
|
24
|
+
it 'should catch ctrl+c signals' do
|
25
|
+
# ...
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'zucker/sandbox'
|
2
|
+
|
3
|
+
describe 'sandbox' do
|
4
|
+
it 'should throw a SecurityError if bad commands are issued' do
|
5
|
+
proc do
|
6
|
+
sandbox do
|
7
|
+
`ls`
|
8
|
+
end
|
9
|
+
end.should raise_exception SecurityError
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should not throw an exception for errors, if the given parameter is not nil' do
|
13
|
+
proc do
|
14
|
+
sandbox( true ) do
|
15
|
+
`ls`
|
16
|
+
end
|
17
|
+
end.should_not raise_exception
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should run the proc passed as parameter for errors, if it is given' do
|
21
|
+
sandbox( lambda{|e| e.class } ) do
|
22
|
+
`ls`
|
23
|
+
end.should == SecurityError
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
|
3
|
+
def capture_stdout
|
4
|
+
capture = StringIO.new
|
5
|
+
restore, $stdout = $stdout, capture
|
6
|
+
yield
|
7
|
+
$stdout = restore
|
8
|
+
capture.string
|
9
|
+
end
|
10
|
+
|
11
|
+
def capture_stderr
|
12
|
+
capture = StringIO.new
|
13
|
+
restore, $stderr = $stderr, capture
|
14
|
+
yield
|
15
|
+
$stderr = restore
|
16
|
+
capture.string
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'zucker/square_brackets_for'
|
2
|
+
|
3
|
+
describe 'square_brackets_for' do
|
4
|
+
|
5
|
+
before do
|
6
|
+
class Klass
|
7
|
+
def initialize
|
8
|
+
@var = {
|
9
|
+
:a_key => 1,
|
10
|
+
:another_one => 2,
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
@eigenvar = {
|
15
|
+
:a_key => 99
|
16
|
+
}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should define a [] getter (not a setter) for an instance var, if the second parameter is false' do
|
21
|
+
|
22
|
+
class Klass
|
23
|
+
square_brackets_for :var, nil
|
24
|
+
end
|
25
|
+
|
26
|
+
a = Klass.new
|
27
|
+
a[:a_key].should == 1
|
28
|
+
|
29
|
+
proc do
|
30
|
+
a[:this_is] = 'not possible'
|
31
|
+
end.should raise_exception NoMethodError
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should define [] and []= for accessing an instance variable' do
|
35
|
+
|
36
|
+
class Klass
|
37
|
+
square_brackets_for :var
|
38
|
+
end
|
39
|
+
|
40
|
+
a = Klass.new
|
41
|
+
a[:a_key].should == 1
|
42
|
+
|
43
|
+
a[:this_is] = 'useful'
|
44
|
+
a[:this_is].should == 'useful'
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should also work for class-instance variables' do
|
48
|
+
|
49
|
+
class Klass
|
50
|
+
class << Klass
|
51
|
+
square_brackets_for :eigenvar
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Klass[:a_key].should == 99
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'zucker/string'
|
2
|
+
|
3
|
+
describe 'String#^' do
|
4
|
+
it 'should give C-like substring access to strings' do
|
5
|
+
string = 'Theoretische Informatik ist voll geil!'
|
6
|
+
|
7
|
+
(string^0).should == 'Theoretische Informatik ist voll geil!'
|
8
|
+
(string^1).should == 'heoretische Informatik ist voll geil!'
|
9
|
+
(string^13).should == 'Informatik ist voll geil!'
|
10
|
+
(string^-1).should == 'Theoretische Informatik ist voll geil'
|
11
|
+
(string^38).should == ''
|
12
|
+
(string^99).should == nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'String#lchomp' do
|
17
|
+
it 'should chomp on the left side' do
|
18
|
+
string = 'Theoretische Informatik ist voll geil!'
|
19
|
+
string.lchomp('T').should == 'heoretische Informatik ist voll geil!'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'String#ords' do
|
24
|
+
it 'should unpack characters' do
|
25
|
+
string = 'Theoretische Informatik ist voll geil!'
|
26
|
+
string.ords.should == [84, 104, 101, 111, 114, 101, 116, 105, 115, 99, 104, 101, 32, 73, 110, 102, 111, 114, 109, 97, 116, 105, 107, 32, 105, 115, 116, 32, 118, 111, 108, 108, 32, 103, 101, 105, 108, 33]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'zucker/unary'
|
2
|
+
|
3
|
+
describe Symbol, '#+@' do
|
4
|
+
it 'should convert to_s' do
|
5
|
+
+(:matz) == 'matz' # () 1.8 bug
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Symbol, '#-@' do
|
10
|
+
it 'should do nothing' do
|
11
|
+
-:matz == :matz
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe String, '#+@' do
|
16
|
+
it 'should do nothing' do
|
17
|
+
+'matz' == 'matz'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe String, '#-@' do
|
22
|
+
it 'should convert to_sym' do
|
23
|
+
-'matz' == :matz
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'zucker/union'
|
2
|
+
|
3
|
+
shared_examples_for "Regexp.union operator" do
|
4
|
+
it "should create an Regexp.union of both operands" do
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'Regexp#|' do
|
9
|
+
it_should_behave_like 'Regexp.union operator'
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'String#|' do
|
13
|
+
it_should_behave_like 'Regexp.union operator'
|
14
|
+
end
|
15
|
+
|