partialruby 0.1.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/AUTHORS +1 -0
- data/CHANGELOG +3 -0
- data/LICENSE +674 -0
- data/README +89 -0
- data/Rakefile +47 -0
- data/TODO +3 -0
- data/lib/partialruby.rb +468 -0
- data/spec/block_spec.rb +83 -0
- data/spec/call_spec.rb +26 -0
- data/spec/class_spec.rb +34 -0
- data/spec/const_spec.rb +33 -0
- data/spec/def_spec.rb +35 -0
- data/spec/eval_spec.rb +85 -0
- data/spec/exception_spec.rb +53 -0
- data/spec/flow_control_spec.rb +38 -0
- data/spec/gvar_spec.rb +18 -0
- data/spec/ivar_spec.rb +18 -0
- data/spec/literal_spec.rb +24 -0
- data/spec/operator_spec.rb +39 -0
- data/spec/xstr_spec.rb +14 -0
- metadata +102 -0
data/spec/block_spec.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require "partialruby"
|
2
|
+
|
3
|
+
include PartialRuby
|
4
|
+
|
5
|
+
describe Context, "PartialRuby context" do
|
6
|
+
|
7
|
+
class BlockTest
|
8
|
+
def self.foo(*args)
|
9
|
+
yield(*args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.bar(*args)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should pass block statements" do
|
17
|
+
BlockTest.should_receive :bar
|
18
|
+
PartialRuby.eval("BlockTest.foo{ BlockTest.bar }", binding)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.test_block_argument(args, args_result)
|
22
|
+
it "should pass block statements with arguments #{args_result}" do
|
23
|
+
BlockTest.should_receive(:bar).with(*args_result)
|
24
|
+
PartialRuby.eval("BlockTest.foo(#{args}) { |x| BlockTest.bar(x) }", binding)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.test_block_arguments(args, args_result)
|
29
|
+
it "should pass block statements with arguments #{args_result}" do
|
30
|
+
BlockTest.should_receive(:bar).with(*args_result)
|
31
|
+
PartialRuby.eval("BlockTest.foo(#{args}) { |*x| BlockTest.bar(*x) }", binding)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
test_block_argument "1", [1]
|
36
|
+
test_block_argument "'test'", ["test"]
|
37
|
+
|
38
|
+
test_block_arguments "1,2", [1,2]
|
39
|
+
test_block_arguments "'test',2", ["test",2]
|
40
|
+
|
41
|
+
it "should implement yield statement" do
|
42
|
+
def foo
|
43
|
+
PartialRuby.eval("yield", binding)
|
44
|
+
end
|
45
|
+
foo{}
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should implement yield statement with one argument" do
|
49
|
+
def foo_2
|
50
|
+
PartialRuby.eval("yield(4)", binding)
|
51
|
+
end
|
52
|
+
|
53
|
+
a = nil
|
54
|
+
foo_2{|x| a = x }
|
55
|
+
a.should be == 4
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should implement yield statement with two argument" do
|
59
|
+
def foo_2
|
60
|
+
PartialRuby.eval("yield(4,5)", binding)
|
61
|
+
end
|
62
|
+
|
63
|
+
a = nil
|
64
|
+
b = nil
|
65
|
+
foo_2{|x,y| a = x; b = y }
|
66
|
+
a.should be == 4
|
67
|
+
b.should be == 5
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should implement yield statement with multiple arguments" do
|
71
|
+
def foo_2
|
72
|
+
x = [4,5]
|
73
|
+
PartialRuby.eval("yield(*x)", binding)
|
74
|
+
end
|
75
|
+
|
76
|
+
a = nil
|
77
|
+
b = nil
|
78
|
+
foo_2{|x,y| a = x; b = y }
|
79
|
+
a.should be == 4
|
80
|
+
b.should be == 5
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
data/spec/call_spec.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "partialruby"
|
2
|
+
|
3
|
+
include PartialRuby
|
4
|
+
|
5
|
+
describe Context, "PartialRuby context" do
|
6
|
+
class X
|
7
|
+
def self.foo(*args)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should make call without arguments" do
|
12
|
+
|
13
|
+
X.should_receive(:foo)
|
14
|
+
PartialRuby.eval("X.foo", binding)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.test_args(args, args_result)
|
18
|
+
it "should make call without arguments #{args} and receive #{args_result.inspect}" do
|
19
|
+
|
20
|
+
X.should_receive(:foo).with(*args_result)
|
21
|
+
PartialRuby.eval("X.foo(#{args})", binding)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
test_args "1,2,3", [1,2,3]
|
26
|
+
end
|
data/spec/class_spec.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "partialruby"
|
2
|
+
|
3
|
+
include PartialRuby
|
4
|
+
|
5
|
+
describe Context, "PartialRuby context" do
|
6
|
+
|
7
|
+
def self.test_classname(classname)
|
8
|
+
it "should declare class with name #{classname}" do
|
9
|
+
PartialRuby.eval("class #{classname}; end", binding)
|
10
|
+
newclass = eval(classname)
|
11
|
+
newclass.should be == newclass
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.test_modulename(modulename)
|
16
|
+
it "should declare module with name #{modulename}" do
|
17
|
+
PartialRuby.eval("module #{modulename}; end", binding)
|
18
|
+
newmodl = eval(modulename)
|
19
|
+
newmodl.should be == newmodl
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
test_classname "X"
|
25
|
+
|
26
|
+
module TestModuleA
|
27
|
+
end
|
28
|
+
test_classname "TestModuleA::X"
|
29
|
+
test_classname "::X"
|
30
|
+
|
31
|
+
test_modulename "TestModuleA::Y"
|
32
|
+
test_modulename "::Y"
|
33
|
+
|
34
|
+
end
|
data/spec/const_spec.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "partialruby"
|
2
|
+
|
3
|
+
include PartialRuby
|
4
|
+
|
5
|
+
describe Context, "PartialRuby context" do
|
6
|
+
def self.test_const_read(*const_names)
|
7
|
+
const_names.each do |const_name|
|
8
|
+
it "should read constant of name #{const_name}" do
|
9
|
+
eval("#{const_name} = 9")
|
10
|
+
PartialRuby.eval("#{const_name}", binding).should be == 9
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.test_const_assign(*const_names)
|
16
|
+
const_names.each do |const_name|
|
17
|
+
it "should assign constant of name #{const_name}" do
|
18
|
+
PartialRuby.eval("#{const_name} = 10", binding)
|
19
|
+
eval("#{const_name}").should be == 10
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
test_const_read "TEST_CONST"
|
25
|
+
test_const_assign "TEST_CONST2"
|
26
|
+
|
27
|
+
test_const_read "Fixnum::TEST_CONST"
|
28
|
+
test_const_assign "Fixnum::TEST_CONST2"
|
29
|
+
|
30
|
+
test_const_read "::TEST_CONST3"
|
31
|
+
test_const_assign "::TEST_CONST4"
|
32
|
+
|
33
|
+
end
|
data/spec/def_spec.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "partialruby"
|
2
|
+
|
3
|
+
include PartialRuby
|
4
|
+
|
5
|
+
describe Context, "PartialRuby context" do
|
6
|
+
|
7
|
+
def self.test_method_def(method_name, arguments=nil, arguments_value=nil)
|
8
|
+
|
9
|
+
if arguments
|
10
|
+
it "should define method of name #{method_name} with arguments #{arguments}: #{arguments_value}" do
|
11
|
+
PartialRuby.eval("def #{method_name}(#{arguments}); end; #{method_name}(#{arguments_value})", binding)
|
12
|
+
end
|
13
|
+
else
|
14
|
+
it "should define method of name #{method_name} with no arguments" do
|
15
|
+
PartialRuby.eval("def #{method_name}; end; #{method_name}()", binding)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
["foo", "bar"].each do |method_name|
|
21
|
+
test_method_def method_name
|
22
|
+
{ "a" => "1", "a,b" => "1,2"}.each do |k,v|
|
23
|
+
test_method_def method_name, k, v
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should define singleton method" do
|
28
|
+
x = "test object"
|
29
|
+
PartialRuby.eval("def x.foo; end", binding)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should compile return statements" do
|
33
|
+
PartialRuby.eval("def test_foo; return 4; end; test_foo", binding).should be == 4
|
34
|
+
end
|
35
|
+
end
|
data/spec/eval_spec.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require "partialruby"
|
2
|
+
|
3
|
+
include PartialRuby
|
4
|
+
|
5
|
+
describe Context, "PartialRuby context" do
|
6
|
+
|
7
|
+
it "should eval literal 'hello world'" do
|
8
|
+
PartialRuby.eval('"hello world"', binding).should be == "hello world"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should eval hello world string" do
|
12
|
+
PartialRuby.eval 'print "hello world\n"', binding
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should define a class" do
|
16
|
+
|
17
|
+
module TestEmptyClass
|
18
|
+
PartialRuby.eval 'class X; end', binding
|
19
|
+
end
|
20
|
+
|
21
|
+
TestEmptyClass::X.should be == TestEmptyClass::X
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should define a class with a method" do
|
25
|
+
|
26
|
+
module TestClassWithFoo
|
27
|
+
PartialRuby.eval 'class X; def foo; end; end', binding
|
28
|
+
end
|
29
|
+
|
30
|
+
TestClassWithFoo::X.should be == TestClassWithFoo::X
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should define a class with a method and the method must be callable" do
|
34
|
+
|
35
|
+
module TestClassWithFoo2
|
36
|
+
PartialRuby.eval 'class X; def foo; end; end', binding
|
37
|
+
end
|
38
|
+
|
39
|
+
x = TestClassWithFoo2::X.new
|
40
|
+
x.foo.should be == nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should define a class with two methods and the methods must be callable" do
|
44
|
+
|
45
|
+
module TestClassWithFoo3
|
46
|
+
PartialRuby.eval 'class X; def foo; end; def bar; end; end', binding
|
47
|
+
end
|
48
|
+
|
49
|
+
x = TestClassWithFoo3::X.new
|
50
|
+
x.foo.should be == nil
|
51
|
+
x.bar.should be == nil
|
52
|
+
end
|
53
|
+
|
54
|
+
# This does not work in the original ruby!
|
55
|
+
# it "should write a local variable" do
|
56
|
+
# PartialRuby.eval "a = 5", binding
|
57
|
+
# a.should be == 5
|
58
|
+
# end
|
59
|
+
|
60
|
+
def self.test_local_with_value(value)
|
61
|
+
it "should allow represent literal #{value}" do
|
62
|
+
PartialRuby.eval(value, binding).should be == eval(value)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should read a local variable with value #{value}" do
|
66
|
+
a = eval(value)
|
67
|
+
PartialRuby.eval("a", binding).should be == eval(value)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should declare and read a local variable with value #{value}" do
|
71
|
+
PartialRuby.eval("a = #{value}; a", binding).should be == eval(value)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
test_local_with_value "5"
|
76
|
+
test_local_with_value "5.5"
|
77
|
+
test_local_with_value "'teststring'"
|
78
|
+
test_local_with_value "[5]"
|
79
|
+
test_local_with_value "[5,6,7,8,9]"
|
80
|
+
test_local_with_value "[[9,6],7,8,9]"
|
81
|
+
|
82
|
+
test_local_with_value "{ 1 => 3, 5 => 9}"
|
83
|
+
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "partialruby"
|
2
|
+
|
3
|
+
include PartialRuby
|
4
|
+
|
5
|
+
describe Context, "PartialRuby context" do
|
6
|
+
|
7
|
+
def self.assert_ruby_expr(expr, expected = nil)
|
8
|
+
|
9
|
+
exception = nil
|
10
|
+
|
11
|
+
begin
|
12
|
+
expected ||= eval(expr)
|
13
|
+
rescue Exception => e
|
14
|
+
exception = e
|
15
|
+
end
|
16
|
+
|
17
|
+
if exception
|
18
|
+
it "should raise #{exception} on expresion #{expr}" do
|
19
|
+
lambda{
|
20
|
+
PartialRuby.eval(expr,binding)
|
21
|
+
}.should raise_error(exception.class)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
it "should return #{expected} on expresion #{expr}" do
|
25
|
+
PartialRuby.eval(expr,binding).to_s.should be == expected.to_s
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
assert_ruby_expr("raise Exception")
|
31
|
+
assert_ruby_expr("begin; raise Exception; rescue; 5; end")
|
32
|
+
assert_ruby_expr("begin; raise Exception; rescue Exception; 4; end")
|
33
|
+
assert_ruby_expr("begin; raise Exception; rescue Exception => e; 9; ensure; 4; end")
|
34
|
+
assert_ruby_expr("begin; raise '999'; rescue RuntimeError; end")
|
35
|
+
assert_ruby_expr("begin; raise '999'; rescue RuntimeError; end; 5")
|
36
|
+
assert_ruby_expr("begin; raise '999'; rescue RuntimeError, Exception; end")
|
37
|
+
assert_ruby_expr("begin; raise '999'; rescue RuntimeError, Exception; end; 5")
|
38
|
+
|
39
|
+
[
|
40
|
+
"rescue RuntimeError; end",
|
41
|
+
"rescue RuntimeError => e; end",
|
42
|
+
"rescue Errno::EINVAL; end",
|
43
|
+
"rescue Errno::EINVAL => e; end",
|
44
|
+
"rescue RuntimeError, Errno::EINVAL; end",
|
45
|
+
"rescue RuntimeError, Errno::EINVAL => e; e; end",
|
46
|
+
].each do |x|
|
47
|
+
assert_ruby_expr("begin; raise Errno::EINVAL; #{x}")
|
48
|
+
assert_ruby_expr("begin; raise RuntimeError; #{x}")
|
49
|
+
assert_ruby_expr("begin; raise Errno::EINVAL; #{x}")
|
50
|
+
assert_ruby_expr("begin; raise RuntimeError; #{x}")
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "partialruby"
|
2
|
+
|
3
|
+
include PartialRuby
|
4
|
+
|
5
|
+
describe Context, "PartialRuby context" do
|
6
|
+
|
7
|
+
def self.assert_ruby_expr(expr, expected = nil)
|
8
|
+
|
9
|
+
expected ||= eval(expr)
|
10
|
+
|
11
|
+
it "should return #{expected} on expresion #{expr}" do
|
12
|
+
PartialRuby.eval(expr,binding).should be == expected
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.test_if(keyword)
|
17
|
+
assert_ruby_expr keyword + " true; true; else; false; end"
|
18
|
+
assert_ruby_expr keyword + " true; true; else; true; end"
|
19
|
+
assert_ruby_expr keyword + " true; false; else; true; end"
|
20
|
+
assert_ruby_expr keyword + " true; false; else; false; end"
|
21
|
+
end
|
22
|
+
|
23
|
+
test_if "if"
|
24
|
+
test_if "unless"
|
25
|
+
|
26
|
+
assert_ruby_expr "i = 5 ; while(i>0); i=i-1; end; i"
|
27
|
+
assert_ruby_expr "i = 5 ; until(i==0); i=i-1; end; i"
|
28
|
+
|
29
|
+
|
30
|
+
(1..4).each do |x|
|
31
|
+
assert_ruby_expr "case #{x}; when 1; 2; end"
|
32
|
+
assert_ruby_expr "case #{x}; when 1; 2; when 2; 3; end"
|
33
|
+
assert_ruby_expr "case #{x}; when 1; 2; when 2; 3; end"
|
34
|
+
assert_ruby_expr "case #{x}; when 1; 2; when 2; 3; when 3; 4; else; 9; end"
|
35
|
+
assert_ruby_expr "case #{x}; when (if true; 1; else; false; end),9; 2; when 2; 3; else; 9; end"
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/spec/gvar_spec.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "partialruby"
|
2
|
+
|
3
|
+
include PartialRuby
|
4
|
+
|
5
|
+
describe Context, "PartialRuby context" do
|
6
|
+
|
7
|
+
def self.assert_ruby_expr(expr, expected = nil)
|
8
|
+
|
9
|
+
expected ||= eval(expr)
|
10
|
+
|
11
|
+
it "should return #{expected} on expresion #{expr}" do
|
12
|
+
PartialRuby.eval(expr,binding).should be == expected
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
assert_ruby_expr "$b"
|
17
|
+
assert_ruby_expr "$a = 4; $a"
|
18
|
+
end
|
data/spec/ivar_spec.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "partialruby"
|
2
|
+
|
3
|
+
include PartialRuby
|
4
|
+
|
5
|
+
describe Context, "PartialRuby context" do
|
6
|
+
|
7
|
+
def self.assert_ruby_expr(expr, expected = nil)
|
8
|
+
|
9
|
+
expected ||= eval(expr)
|
10
|
+
|
11
|
+
it "should return #{expected} on expresion #{expr}" do
|
12
|
+
PartialRuby.eval(expr,binding).should be == expected
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
assert_ruby_expr "@b"
|
17
|
+
assert_ruby_expr "@a = 4; @a"
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "partialruby"
|
2
|
+
|
3
|
+
include PartialRuby
|
4
|
+
|
5
|
+
describe Context, "PartialRuby context" do
|
6
|
+
|
7
|
+
def self.assert_ruby_expr(expr, expected = nil)
|
8
|
+
|
9
|
+
expected ||= eval(expr)
|
10
|
+
|
11
|
+
it "should return #{expected} on expresion #{expr}" do
|
12
|
+
PartialRuby.eval(expr,binding).should be == expected
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
assert_ruby_expr "'xxx'"
|
17
|
+
assert_ruby_expr "43"
|
18
|
+
assert_ruby_expr "(0..9)"
|
19
|
+
assert_ruby_expr "{ 1 => 2, 3 => 4 }"
|
20
|
+
assert_ruby_expr "[1,2,3,4,5]"
|
21
|
+
assert_ruby_expr "/aaa/"
|
22
|
+
|
23
|
+
assert_ruby_expr 'a="x"; "xxxxx#{a}"'
|
24
|
+
end
|