serializable_proc 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/HISTORY.txt +9 -0
- data/README.rdoc +28 -10
- data/VERSION +1 -1
- data/lib/serializable_proc/binding.rb +27 -32
- data/lib/serializable_proc/fixes.rb +1 -1
- data/lib/serializable_proc/isolatable.rb +59 -0
- data/lib/serializable_proc/parsers/pt.rb +2 -5
- data/lib/serializable_proc/parsers/rp.rb +29 -29
- data/lib/serializable_proc/parsers.rb +22 -0
- data/lib/serializable_proc.rb +76 -10
- data/serializable_proc.gemspec +51 -21
- data/spec/bounded_vars/class_vars_spec.rb +60 -0
- data/spec/bounded_vars/class_vars_within_block_scope_spec.rb +31 -0
- data/spec/bounded_vars/errors_spec.rb +27 -0
- data/spec/bounded_vars/global_vars_spec.rb +60 -0
- data/spec/bounded_vars/global_vars_within_block_scope_spec.rb +31 -0
- data/spec/bounded_vars/instance_vars_spec.rb +60 -0
- data/spec/bounded_vars/instance_vars_within_block_scope_spec.rb +31 -0
- data/spec/bounded_vars/local_vars_spec.rb +60 -0
- data/spec/bounded_vars/local_vars_within_block_scope_spec.rb +31 -0
- data/spec/code_block/errors_spec.rb +65 -0
- data/spec/{multiple_arities_serializable_proc_spec.rb → code_block/multiple_arities_spec.rb} +2 -2
- data/spec/{optional_arity_serializable_proc_spec.rb → code_block/optional_arity_spec.rb} +2 -2
- data/spec/code_block/renaming_vars_spec.rb +160 -0
- data/spec/{one_arity_serializable_proc_spec.rb → code_block/single_arity_spec.rb} +2 -2
- data/spec/{zero_arity_serializable_proc_spec.rb → code_block/zero_arity_spec.rb} +2 -2
- data/spec/proc_like/extras_spec.rb +82 -0
- data/spec/proc_like/invoking_with_args_spec.rb +58 -0
- data/spec/proc_like/invoking_with_class_vars_spec.rb +96 -0
- data/spec/proc_like/invoking_with_global_vars_spec.rb +79 -0
- data/spec/proc_like/invoking_with_instance_vars_spec.rb +79 -0
- data/spec/proc_like/invoking_with_local_vars_spec.rb +79 -0
- data/spec/{marshalling_spec.rb → proc_like/marshalling_spec.rb} +1 -1
- data/spec/proc_like/others_spec.rb +106 -0
- data/spec/spec_helper.rb +10 -0
- metadata +52 -22
- data/lib/serializable_proc/sandboxer.rb +0 -24
- data/spec/extracting_bound_variables_spec.rb +0 -99
- data/spec/initializing_errors_spec.rb +0 -95
- data/spec/proc_like_spec.rb +0 -191
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serializable_proc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- NgTzeYang
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-18 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -68,21 +68,36 @@ files:
|
|
68
68
|
- lib/serializable_proc.rb
|
69
69
|
- lib/serializable_proc/binding.rb
|
70
70
|
- lib/serializable_proc/fixes.rb
|
71
|
+
- lib/serializable_proc/isolatable.rb
|
71
72
|
- lib/serializable_proc/marshalable.rb
|
72
73
|
- lib/serializable_proc/parsers.rb
|
73
74
|
- lib/serializable_proc/parsers/pt.rb
|
74
75
|
- lib/serializable_proc/parsers/rp.rb
|
75
|
-
- lib/serializable_proc/sandboxer.rb
|
76
76
|
- serializable_proc.gemspec
|
77
|
-
- spec/
|
78
|
-
- spec/
|
79
|
-
- spec/
|
80
|
-
- spec/
|
81
|
-
- spec/
|
82
|
-
- spec/
|
83
|
-
- spec/
|
77
|
+
- spec/bounded_vars/class_vars_spec.rb
|
78
|
+
- spec/bounded_vars/class_vars_within_block_scope_spec.rb
|
79
|
+
- spec/bounded_vars/errors_spec.rb
|
80
|
+
- spec/bounded_vars/global_vars_spec.rb
|
81
|
+
- spec/bounded_vars/global_vars_within_block_scope_spec.rb
|
82
|
+
- spec/bounded_vars/instance_vars_spec.rb
|
83
|
+
- spec/bounded_vars/instance_vars_within_block_scope_spec.rb
|
84
|
+
- spec/bounded_vars/local_vars_spec.rb
|
85
|
+
- spec/bounded_vars/local_vars_within_block_scope_spec.rb
|
86
|
+
- spec/code_block/errors_spec.rb
|
87
|
+
- spec/code_block/multiple_arities_spec.rb
|
88
|
+
- spec/code_block/optional_arity_spec.rb
|
89
|
+
- spec/code_block/renaming_vars_spec.rb
|
90
|
+
- spec/code_block/single_arity_spec.rb
|
91
|
+
- spec/code_block/zero_arity_spec.rb
|
92
|
+
- spec/proc_like/extras_spec.rb
|
93
|
+
- spec/proc_like/invoking_with_args_spec.rb
|
94
|
+
- spec/proc_like/invoking_with_class_vars_spec.rb
|
95
|
+
- spec/proc_like/invoking_with_global_vars_spec.rb
|
96
|
+
- spec/proc_like/invoking_with_instance_vars_spec.rb
|
97
|
+
- spec/proc_like/invoking_with_local_vars_spec.rb
|
98
|
+
- spec/proc_like/marshalling_spec.rb
|
99
|
+
- spec/proc_like/others_spec.rb
|
84
100
|
- spec/spec_helper.rb
|
85
|
-
- spec/zero_arity_serializable_proc_spec.rb
|
86
101
|
has_rdoc: true
|
87
102
|
homepage: http://github.com/ngty/serializable_proc
|
88
103
|
licenses: []
|
@@ -118,12 +133,27 @@ signing_key:
|
|
118
133
|
specification_version: 3
|
119
134
|
summary: Proc that can be serialized (as the name suggests)
|
120
135
|
test_files:
|
121
|
-
- spec/
|
122
|
-
- spec/
|
123
|
-
- spec/
|
124
|
-
- spec/
|
125
|
-
- spec/
|
126
|
-
- spec/
|
127
|
-
- spec/
|
136
|
+
- spec/proc_like/extras_spec.rb
|
137
|
+
- spec/proc_like/invoking_with_local_vars_spec.rb
|
138
|
+
- spec/proc_like/invoking_with_instance_vars_spec.rb
|
139
|
+
- spec/proc_like/invoking_with_class_vars_spec.rb
|
140
|
+
- spec/proc_like/invoking_with_args_spec.rb
|
141
|
+
- spec/proc_like/others_spec.rb
|
142
|
+
- spec/proc_like/invoking_with_global_vars_spec.rb
|
143
|
+
- spec/proc_like/marshalling_spec.rb
|
144
|
+
- spec/code_block/multiple_arities_spec.rb
|
145
|
+
- spec/code_block/zero_arity_spec.rb
|
146
|
+
- spec/code_block/errors_spec.rb
|
147
|
+
- spec/code_block/renaming_vars_spec.rb
|
148
|
+
- spec/code_block/single_arity_spec.rb
|
149
|
+
- spec/code_block/optional_arity_spec.rb
|
150
|
+
- spec/bounded_vars/global_vars_within_block_scope_spec.rb
|
151
|
+
- spec/bounded_vars/instance_vars_within_block_scope_spec.rb
|
152
|
+
- spec/bounded_vars/errors_spec.rb
|
153
|
+
- spec/bounded_vars/local_vars_within_block_scope_spec.rb
|
154
|
+
- spec/bounded_vars/class_vars_spec.rb
|
155
|
+
- spec/bounded_vars/local_vars_spec.rb
|
156
|
+
- spec/bounded_vars/global_vars_spec.rb
|
157
|
+
- spec/bounded_vars/instance_vars_spec.rb
|
158
|
+
- spec/bounded_vars/class_vars_within_block_scope_spec.rb
|
128
159
|
- spec/spec_helper.rb
|
129
|
-
- spec/proc_like_spec.rb
|
@@ -1,24 +0,0 @@
|
|
1
|
-
class SerializableProc
|
2
|
-
module Sandboxer
|
3
|
-
class << self
|
4
|
-
|
5
|
-
def fsexp(sexp)
|
6
|
-
n_sexp, t_sexp = nil, sexp.inspect
|
7
|
-
while m = t_sexp.match(/^(.*?s\(:)((i|l|c|g)(asgn|var|vdecl))(,\ :)((|@|@@|\$)([\w]+))(\)|,)/)
|
8
|
-
orig, prepend, _, type, declare, join, _, _, name, append = m[0..-1]
|
9
|
-
declare.sub!('vdecl','asgn')
|
10
|
-
n_sexp = "#{n_sexp}#{prepend}l#{declare}#{join}#{type}var_#{name}#{append}"
|
11
|
-
t_sexp.sub!(orig,'')
|
12
|
-
end
|
13
|
-
eval(n_sexp ? "#{n_sexp}#{t_sexp}" : sexp.inspect)
|
14
|
-
end
|
15
|
-
|
16
|
-
def fvar(var)
|
17
|
-
@translate_var_maps ||= {'@' => 'ivar_', '@@' => 'cvar_', '$' => 'gvar_', '' => 'lvar_'}
|
18
|
-
m = var.to_s.match(/^(|@|@@|\$)(\w+)$/)
|
19
|
-
var.to_s.sub(m[1], @translate_var_maps[m[1]]).to_sym
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,99 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
|
3
|
-
describe 'Extracting bound variables' do
|
4
|
-
|
5
|
-
class << self
|
6
|
-
def m1 ; 'm1' ; end
|
7
|
-
def m2(x) ; 'm2(%s)' % x ; end
|
8
|
-
end
|
9
|
-
|
10
|
-
describe '>> extracting local variables' do
|
11
|
-
|
12
|
-
extend SerializableProc::Spec::Helpers
|
13
|
-
|
14
|
-
should "handle outer-scoped ones" do
|
15
|
-
x, y = 'awe', 'some'
|
16
|
-
should_have_expected_binding \
|
17
|
-
SerializableProc.new{ x + y }, {:lvar_x => x, :lvar_y => y}
|
18
|
-
end
|
19
|
-
|
20
|
-
should "handle inner-scoped ones" do
|
21
|
-
x, y = 'awe', 'some'
|
22
|
-
should_have_expected_binding \
|
23
|
-
SerializableProc.new{|x| z = 'wonder' ; %w{a b}.each{|y| puts z, x, y } },
|
24
|
-
{:lvar_x => x, :lvar_y => y, :lvar_z => nil}
|
25
|
-
end
|
26
|
-
|
27
|
-
# NOTE: Errors checking are found under ./initializing_errors_spec.rb
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
describe '>> extracting instance variables' do
|
32
|
-
|
33
|
-
extend SerializableProc::Spec::Helpers
|
34
|
-
|
35
|
-
should 'handle outer-scoped ones' do
|
36
|
-
@x, @y = 'awe', 'some'
|
37
|
-
should_have_expected_binding \
|
38
|
-
SerializableProc.new{ @x + @y }, {:ivar_x => @x, :ivar_y => @y}
|
39
|
-
end
|
40
|
-
|
41
|
-
should "handle inner-scoped ones" do
|
42
|
-
@x, @y = 'awe', 'some'
|
43
|
-
should_have_expected_binding \
|
44
|
-
SerializableProc.new{ @z = 'wonder' ; %w{a b}.each{ puts @z, @x, @y } },
|
45
|
-
{:ivar_x => @x, :ivar_y => @y, :ivar_z => nil}
|
46
|
-
end
|
47
|
-
|
48
|
-
# NOTE: Errors checking are found under ./initializing_errors_spec.rb
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
describe '>> extracting class variables' do
|
53
|
-
|
54
|
-
extend SerializableProc::Spec::Helpers
|
55
|
-
|
56
|
-
should 'handle outer-scoped ones' do
|
57
|
-
@@x, @@y = 'awe', 'some'
|
58
|
-
should_have_expected_binding \
|
59
|
-
SerializableProc.new{ @@x + @@y }, {:cvar_x => @@x, :cvar_y => @@y}
|
60
|
-
end
|
61
|
-
|
62
|
-
should "handle inner-scoped ones" do
|
63
|
-
@@x, @@y = 'awe', 'some'
|
64
|
-
should_have_expected_binding \
|
65
|
-
SerializableProc.new{ @@z = 'wonder' ; %w{a b}.each{ puts @@z, @@x, @@y } },
|
66
|
-
{:cvar_x => @@x, :cvar_y => @@y, :cvar_z => nil}
|
67
|
-
end
|
68
|
-
|
69
|
-
# NOTE: Errors checking are found under ./initializing_errors_spec.rb
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
describe '>> extracting global variables' do
|
74
|
-
|
75
|
-
extend SerializableProc::Spec::Helpers
|
76
|
-
|
77
|
-
should 'handle outer-scoped ones' do
|
78
|
-
$x, $y = 'awe', 'some'
|
79
|
-
should_have_expected_binding SerializableProc.new{ $x + $y }, {:gvar_x => $x, :gvar_y => $y}
|
80
|
-
end
|
81
|
-
|
82
|
-
should "handle inner-scoped ones" do
|
83
|
-
$x, $y = 'awe', 'some'
|
84
|
-
should_have_expected_binding \
|
85
|
-
SerializableProc.new{ $z = 'wonder' ; %w{a b}.each{ puts $z, $x, $y } },
|
86
|
-
{:gvar_x => $x, :gvar_y => $y, :gvar_z => nil}
|
87
|
-
end
|
88
|
-
|
89
|
-
# NOTE: Errors checking are found under ./initializing_errors_spec.rb
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
describe '>> extracting method returns' do
|
94
|
-
should "not handle" do
|
95
|
-
SerializableProc.new { m1 + m2(3) }.binding_dump.should.be.empty
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|
@@ -1,95 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
|
3
|
-
describe 'Initializing errors' do
|
4
|
-
|
5
|
-
unless $parse_tree_installed
|
6
|
-
describe '>> SerializableProc::CannotAnalyseCodeError' do
|
7
|
-
|
8
|
-
extend SerializableProc::Spec::Helpers
|
9
|
-
|
10
|
-
should "raise if line has more than 1 'SerializableProc.new'" do
|
11
|
-
lambda {
|
12
|
-
SerializableProc.new {} ; SerializableProc.new { |arg| %w{a b}.map{|x| puts x } }
|
13
|
-
}.should.be raising_cannot_analyse_error("'SerializableProc.new'")
|
14
|
-
end
|
15
|
-
|
16
|
-
should "raise if line has more than 1 'Proc.new'" do
|
17
|
-
lambda {
|
18
|
-
p1 = Proc.new {} ; p2 = Proc.new { |arg| %w{a b}.map{|x| puts x } }
|
19
|
-
SerializableProc.new(&p2)
|
20
|
-
}.should.be raising_cannot_analyse_error("'lambda'/'proc'/'Proc.new'")
|
21
|
-
end
|
22
|
-
|
23
|
-
should "raise if line has more than 1 'lambda'" do
|
24
|
-
lambda {
|
25
|
-
p1 = lambda {} ; p2 = lambda { |arg| %w{a b}.map{|x| puts x } }
|
26
|
-
SerializableProc.new(&p2)
|
27
|
-
}.should.be raising_cannot_analyse_error("'lambda'/'proc'/'Proc.new'")
|
28
|
-
end
|
29
|
-
|
30
|
-
should "raise if line has more than 1 'proc'" do
|
31
|
-
lambda {
|
32
|
-
p1 = proc {} ; p2 = proc { |arg| %w{a b}.map{|x| puts x } }
|
33
|
-
SerializableProc.new(&p2)
|
34
|
-
}.should.be raising_cannot_analyse_error("'lambda'/'proc'/'Proc.new'")
|
35
|
-
end
|
36
|
-
|
37
|
-
should "raise if line mixes 'lambda' & 'proc'" do
|
38
|
-
lambda {
|
39
|
-
p1 = lambda {} ; p2 = proc { |arg| %w{a b}.map{|x| puts x } }
|
40
|
-
SerializableProc.new(&p2)
|
41
|
-
}.should.be raising_cannot_analyse_error("'lambda'/'proc'/'Proc.new'")
|
42
|
-
end
|
43
|
-
|
44
|
-
should "raise if line mixes 'Proc.new' & 'proc'" do
|
45
|
-
lambda {
|
46
|
-
p1 = Proc.new {} ; p2 = proc { |arg| %w{a b}.map{|x| puts x } }
|
47
|
-
SerializableProc.new(&p2)
|
48
|
-
}.should.be raising_cannot_analyse_error("'lambda'/'proc'/'Proc.new'")
|
49
|
-
end
|
50
|
-
|
51
|
-
should "raise if line mixes 'Proc.new' & 'lambda'" do
|
52
|
-
lambda {
|
53
|
-
p1 = Proc.new {} ; p2 = lambda { |arg| %w{a b}.map{|x| puts x } }
|
54
|
-
SerializableProc.new(&p2)
|
55
|
-
}.should.be raising_cannot_analyse_error("'lambda'/'proc'/'Proc.new'")
|
56
|
-
end
|
57
|
-
|
58
|
-
should "raise if line does not have lambda, proc, Proc.new or SerializableProc.new" do
|
59
|
-
lambda {
|
60
|
-
def create_serializable_proc(&block) ; SerializableProc.new(&block) ; end
|
61
|
-
create_serializable_proc { %w{a b}.map{|x| puts x } }
|
62
|
-
}.should.raise(SerializableProc::CannotAnalyseCodeError).
|
63
|
-
message.should.equal('Cannot find specified initializer !!')
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
describe '>> SerializableProc::CannotSerializeVariableError' do
|
70
|
-
|
71
|
-
extend SerializableProc::Spec::Helpers
|
72
|
-
|
73
|
-
should "raise if local variable cannot be marshalled" do
|
74
|
-
f = Tempfile.new('fake')
|
75
|
-
lambda { SerializableProc.new{ f } }.should.be raising_cannot_serialize_variable_error('f')
|
76
|
-
end
|
77
|
-
|
78
|
-
should "raise if class variable cannot be marshalled" do
|
79
|
-
@@f = Tempfile.new('fake')
|
80
|
-
lambda { SerializableProc.new{ @@f } }.should.be raising_cannot_serialize_variable_error('@@f')
|
81
|
-
end
|
82
|
-
|
83
|
-
should "raise if instance variable cannot be marshalled" do
|
84
|
-
@f = Tempfile.new('fake')
|
85
|
-
lambda { SerializableProc.new{ @f } }.should.be raising_cannot_serialize_variable_error('@f')
|
86
|
-
end
|
87
|
-
|
88
|
-
should "raise if global variable cannot be marshalled" do
|
89
|
-
$f = Tempfile.new('fake')
|
90
|
-
lambda { SerializableProc.new{ $f } }.should.be raising_cannot_serialize_variable_error('$f')
|
91
|
-
end
|
92
|
-
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
data/spec/proc_like_spec.rb
DELETED
@@ -1,191 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
|
3
|
-
describe 'Being proc like' do
|
4
|
-
|
5
|
-
describe '>> ==' do
|
6
|
-
|
7
|
-
before do
|
8
|
-
@proc = SerializableProc.new{ %w{a b}.map{|x| x } }
|
9
|
-
end
|
10
|
-
|
11
|
-
should 'return true if comparing to itself' do
|
12
|
-
@proc.should.equal(@proc)
|
13
|
-
end
|
14
|
-
|
15
|
-
should 'return true if another SerializableProc has the same code' do
|
16
|
-
SerializableProc.new{ %w{a b}.map{|x| x } }.should.equal(@proc)
|
17
|
-
end
|
18
|
-
|
19
|
-
should 'return false if another SerializableProc does not have the same code' do
|
20
|
-
SerializableProc.new{ %w{b c}.map{|x| x } }.should.not.equal(@proc)
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
describe '>> call (alias [])' do
|
26
|
-
|
27
|
-
should 'return yield result given no arg' do
|
28
|
-
s_proc = SerializableProc.new { %w{b c}.map{|x| x } }
|
29
|
-
expected = %w{b c}
|
30
|
-
s_proc.call.should.equal(expected)
|
31
|
-
s_proc[].should.equal(expected)
|
32
|
-
end
|
33
|
-
|
34
|
-
should 'reflect bound instance variable value (unaffected by outside-scope change)' do
|
35
|
-
x, y = 'awe', 'some'
|
36
|
-
expected = 'hand' + y
|
37
|
-
s_proc = SerializableProc.new { x.sub!('awe','hand'); x + y }
|
38
|
-
x, y = 'wonder', 'ful'
|
39
|
-
s_proc.call.should.equal(expected)
|
40
|
-
s_proc[].should.equal(expected)
|
41
|
-
end
|
42
|
-
|
43
|
-
should 'not affect any outside-scope change to instance variable' do
|
44
|
-
x, y = 'awe', 'some'
|
45
|
-
s_proc = SerializableProc.new { x.sub!('awe','hand'); x + y }
|
46
|
-
x, y = 'wonder', 'ful'
|
47
|
-
s_proc.call ; s_proc[]
|
48
|
-
x.should.equal('wonder')
|
49
|
-
y.should.equal('ful')
|
50
|
-
end
|
51
|
-
|
52
|
-
should 'reflect bound instance variable value (unaffected by outside-scope change)' do
|
53
|
-
@x, @y = 'awe', 'some'
|
54
|
-
expected = 'hand' + @y
|
55
|
-
s_proc = SerializableProc.new { @x.sub!('awe','hand'); @x + @y }
|
56
|
-
@x, @y = 'wonder', 'ful'
|
57
|
-
s_proc.call.should.equal(expected)
|
58
|
-
s_proc[].should.equal(expected)
|
59
|
-
end
|
60
|
-
|
61
|
-
should 'not affect any outside-scope change to instance variable' do
|
62
|
-
@x, @y = 'awe', 'some'
|
63
|
-
s_proc = SerializableProc.new { @x.sub!('awe','hand'); @x + @y }
|
64
|
-
@x, @y = 'wonder', 'ful'
|
65
|
-
s_proc.call ; s_proc[]
|
66
|
-
@x.should.equal('wonder')
|
67
|
-
@y.should.equal('ful')
|
68
|
-
end
|
69
|
-
|
70
|
-
should 'reflect bound class variable value (unaffected by outside-scope change)' do
|
71
|
-
@@x, @@y = 'awe', 'some'
|
72
|
-
expected = 'hand' + @@y
|
73
|
-
s_proc = SerializableProc.new { @@x.sub!('awe','hand'); @@x + @@y }
|
74
|
-
@@x, @@y = 'wonder', 'ful'
|
75
|
-
s_proc.call.should.equal(expected)
|
76
|
-
s_proc[].should.equal(expected)
|
77
|
-
end
|
78
|
-
|
79
|
-
should 'not affect any outside-scope change to class variable' do
|
80
|
-
@@x, @@y = 'awe', 'some'
|
81
|
-
s_proc = SerializableProc.new { @@x.sub!('awe','hand'); @@x + @@y }
|
82
|
-
@@x, @@y = 'wonder', 'ful'
|
83
|
-
s_proc.call ; s_proc[]
|
84
|
-
@@x.should.equal('wonder')
|
85
|
-
@@y.should.equal('ful')
|
86
|
-
end
|
87
|
-
|
88
|
-
should 'reflect bound global variable value (unaffected by outside-scope change)' do
|
89
|
-
$x, $y = 'awe', 'some'
|
90
|
-
expected = 'hand' + $y
|
91
|
-
s_proc = SerializableProc.new { $x.sub!('awe','hand'); $x + $y }
|
92
|
-
$x, $y = 'wonder', 'ful'
|
93
|
-
s_proc.call.should.equal(expected)
|
94
|
-
s_proc[].should.equal(expected)
|
95
|
-
end
|
96
|
-
|
97
|
-
should 'not affect any outside-scope change to global variable' do
|
98
|
-
$x, $y = 'awe', 'some'
|
99
|
-
s_proc = SerializableProc.new { $x.sub!('awe','hand'); $x + $y }
|
100
|
-
$x, $y = 'wonder', 'ful'
|
101
|
-
s_proc.call ; s_proc[]
|
102
|
-
$x.should.equal('wonder')
|
103
|
-
$y.should.equal('ful')
|
104
|
-
end
|
105
|
-
|
106
|
-
end
|
107
|
-
|
108
|
-
describe '>> clone' do
|
109
|
-
should 'return a serializable proc that yields +ve ==' do
|
110
|
-
s_proc = SerializableProc.new { %w{b c}.map{|x| x } }
|
111
|
-
clone = s_proc.clone
|
112
|
-
clone.should.equal(s_proc)
|
113
|
-
clone.object_id.should.not.equal(s_proc.object_id)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
describe '>> binding' do
|
118
|
-
should 'raise NotImplementedError' do
|
119
|
-
lambda { SerializableProc.new { x }.binding }.should.raise(NotImplementedError)
|
120
|
-
end
|
121
|
-
# should 'return binding that contains duplicated contextual reference values' do
|
122
|
-
# x, @x, @@x, $x = 'lx', 'ix', 'cx', 'gx'
|
123
|
-
# expected = {'x' => x.dup, '@x' => @x.dup, '@@x' => @@x.dup, '$x' => $x.dup}
|
124
|
-
# s_proc = SerializableProc.new { [x, @x, @@x, $x] }
|
125
|
-
# x, @x, @@x, $x = 'ly', 'iy', 'cy', 'gy'
|
126
|
-
# expected.each{|k,v| s_proc.binding.eval(k).should.equal(v) }
|
127
|
-
# end
|
128
|
-
end
|
129
|
-
|
130
|
-
describe '>> to_proc' do
|
131
|
-
|
132
|
-
class << self
|
133
|
-
def work(&block) ; yield ; end
|
134
|
-
end
|
135
|
-
|
136
|
-
should 'return a non-serializable proc' do
|
137
|
-
o_proc = lambda { %w{b c}.map{|x| x } }
|
138
|
-
s_proc = SerializableProc.new(&o_proc)
|
139
|
-
n_proc = s_proc.to_proc
|
140
|
-
s_proc.should.not == n_proc
|
141
|
-
n_proc.class.should == Proc
|
142
|
-
n_proc.call.should.equal(o_proc.call)
|
143
|
-
end
|
144
|
-
|
145
|
-
should "support passing to a method using '&' char" do
|
146
|
-
s_proc = SerializableProc.new { %w{b c}.map{|x| x } }
|
147
|
-
work(&s_proc).should.equal(%w{b c})
|
148
|
-
end
|
149
|
-
|
150
|
-
end
|
151
|
-
|
152
|
-
describe '>> to_s' do
|
153
|
-
|
154
|
-
extend SerializableProc::Spec::Helpers
|
155
|
-
|
156
|
-
should 'return extracted code when debug is not specified' do
|
157
|
-
x, @x, @@x, $x = 'lx', 'ix', 'cx', 'gx'
|
158
|
-
SerializableProc.new{ [x,@x,@@x,$x] }.to_s.should.be \
|
159
|
-
having_same_semantics_as('lambda { [x, @x, @@x, $x] }')
|
160
|
-
end
|
161
|
-
|
162
|
-
should 'return extracted code when debug is turned off' do
|
163
|
-
x, @x, @@x, $x = 'lx', 'ix', 'cx', 'gx'
|
164
|
-
SerializableProc.new{ [x,@x,@@x,$x] }.to_s(false).should.be \
|
165
|
-
having_same_semantics_as('lambda { [x, @x, @@x, $x] }')
|
166
|
-
end
|
167
|
-
|
168
|
-
should 'return runnable code when debug is turned on' do
|
169
|
-
x, @x, @@x, $x = 'lx', 'ix', 'cx', 'gx'
|
170
|
-
SerializableProc.new{ [x,@x,@@x,$x] }.to_s(true).should.be \
|
171
|
-
having_same_semantics_as('lambda { [lvar_x, ivar_x, cvar_x, gvar_x] }')
|
172
|
-
end
|
173
|
-
|
174
|
-
end
|
175
|
-
|
176
|
-
describe '>> arity' do
|
177
|
-
{
|
178
|
-
__LINE__ => lambda { },
|
179
|
-
__LINE__ => lambda {|x| },
|
180
|
-
__LINE__ => lambda {|x,y| },
|
181
|
-
__LINE__ => lambda {|*x| },
|
182
|
-
__LINE__ => lambda {|x, *y| },
|
183
|
-
__LINE__ => lambda {|(x,y)| },
|
184
|
-
}.each do |debug, block|
|
185
|
-
should "return arity of initializing block [##{debug}]" do
|
186
|
-
SerializableProc.new(&block).arity.should.equal(block.arity)
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
end
|