rVM 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/lib/rake/helpers/code_statistics.rb +167 -0
  2. data/lib/rvm/classes.rb +4 -0
  3. data/lib/rvm/classes/string.rb +1 -0
  4. data/lib/rvm/functions/array.rb +3 -0
  5. data/lib/rvm/functions/array/set_at.rb +0 -4
  6. data/lib/rvm/functions/collection/size.rb +2 -2
  7. data/lib/rvm/functions/logic/and.rb +1 -1
  8. data/lib/rvm/functions/logic/or.rb +2 -2
  9. data/lib/rvm/interpreter.rb +84 -44
  10. data/lib/rvm/languages/ecma.rb +581 -501
  11. data/spec/base/class_spec.rb +27 -0
  12. data/spec/base/function_spec.rb +25 -0
  13. data/spec/base/interpreter/assignment_spec.rb +22 -0
  14. data/spec/base/interpreter/condition_spec.rb +47 -0
  15. data/spec/base/interpreter/constant_spec.rb +31 -0
  16. data/spec/base/interpreter/enviroment_spec.rb +51 -0
  17. data/spec/base/interpreter/function_call_spec.rb +72 -0
  18. data/spec/base/interpreter/interpreter_spec.rb +11 -0
  19. data/spec/base/interpreter/parameter_spec.rb +24 -0
  20. data/spec/base/interpreter/sequence_spec.rb +20 -0
  21. data/spec/base/interpreter/variable_spec.rb +24 -0
  22. data/spec/base/plugin_spec.rb +10 -0
  23. data/spec/classes/atom/association_spec.rb +39 -0
  24. data/spec/classes/atom/block_spec.rb +27 -0
  25. data/spec/classes/atom/boolean_spec.rb +67 -0
  26. data/spec/classes/atom/error_spec.rb +43 -0
  27. data/spec/classes/atom/list_spec.rb +68 -0
  28. data/spec/classes/atom/number_spec.rb +132 -0
  29. data/spec/classes/atom/string_spec.rb +175 -0
  30. data/spec/functions/association/assoc_get_spec.rb +41 -0
  31. data/spec/functions/association/assoc_set_spec.rb +43 -0
  32. data/spec/functions/collection/get_spec.rb +12 -0
  33. data/spec/functions/collection/set_spec.rb +10 -0
  34. data/spec/functions/collection/size_spec.rb +10 -0
  35. data/spec/functions/list/split_spec.rb +47 -0
  36. data/spec/functions/string/ansi_spec.rb +44 -0
  37. data/spec/functions/string/capstr_spec.rb +42 -0
  38. data/spec/functions/string/center_spec.rb +49 -0
  39. data/spec/functions/string/ljust_spec.rb +49 -0
  40. data/spec/functions/string/regmatch_spec.rb +52 -0
  41. data/spec/functions/string/rjust_spec.rb +49 -0
  42. data/spec/languages/ecma_spec.rb +337 -0
  43. data/spec/languages/math/compiler_spec.rb +49 -0
  44. data/spec/languages/math/tokenizer_spec.rb +73 -0
  45. data/spec/languages/math/tree_spec.rb +153 -0
  46. metadata +42 -5
@@ -0,0 +1,27 @@
1
+ require 'lib/rvm/classes'
2
+ require File.dirname(__FILE__) + '/helpers/plugin_helper'
3
+ require File.dirname(__FILE__) + '/helpers/class_helper'
4
+
5
+ describe RVM::Classes do
6
+ it_should_behave_like "a plugin host"
7
+ end
8
+
9
+ describe RVM::Classes::Class do
10
+ describe "(plugin)" do
11
+ before do
12
+ @object = RVM::Classes::Class
13
+ end
14
+ it_should_behave_like "a plugin"
15
+
16
+ end
17
+
18
+ describe "(class)" do
19
+ before do
20
+ @object = RVM::Classes::Class.new
21
+ end
22
+
23
+ it_should_behave_like "a class"
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,25 @@
1
+ require 'lib/rvm/functions'
2
+ require File.dirname(__FILE__) + '/helpers/plugin_helper'
3
+ require File.dirname(__FILE__) + '/helpers/function_helper'
4
+
5
+ describe RVM::Functions do
6
+ it_should_behave_like "a plugin host"
7
+ end
8
+
9
+ describe RVM::Functions::Function do
10
+ describe "(plugin)" do
11
+ before do
12
+ @object = RVM::Functions::Function
13
+ end
14
+ it_should_behave_like "a plugin"
15
+
16
+ end
17
+
18
+ describe "(function)" do
19
+ before do
20
+ @object = RVM::Functions::Function
21
+ end
22
+ it_should_behave_like "a function"
23
+ end
24
+
25
+ end
@@ -0,0 +1,22 @@
1
+ require 'lib/rvm/interpreter'
2
+
3
+ describe RVM::Interpreter::Assignment do
4
+ before do
5
+ @name = mock('name mock')
6
+ @value = mock('value mock')
7
+ @env = mock('env mock')
8
+ @object = RVM::Interpreter::Assignment.new(@name, @value)
9
+ end
10
+
11
+ it "should have the type of it's value" do
12
+ @value.should_receive(:data_type).with(no_args).once.and_return(:example)
13
+ @object.data_type.should == :example
14
+ end
15
+
16
+ it "should execute first the name then the value and store it in env" do
17
+ @name.should_receive(:execute).with(@env).once.and_return('name')
18
+ @value.should_receive(:execute).with(@env).once.and_return('value')
19
+ @env.should_receive(:[]=).with('name','value').once
20
+ @object.execute(@env).should == 'value'
21
+ end
22
+ end
@@ -0,0 +1,47 @@
1
+ require 'lib/rvm/interpreter'
2
+
3
+ describe RVM::Interpreter::Condition do
4
+ before do
5
+ @true_case = mock('true case mock')
6
+ @false_case = mock('false case mock')
7
+ @condition = mock('condition mock')
8
+ @env = RVM::Interpreter.env
9
+ @object = RVM::Interpreter::Condition.new(@condition, @true_case, @false_case)
10
+ end
11
+
12
+ it "should execute the condition if true execute the true case, retring it's value" do
13
+ result = mock('result mock')
14
+ result.should_receive(:is_true?).with(no_args).once.and_return(true)
15
+ @true_case.should_receive(:execute).with(@env).once.and_return('true case')
16
+ @condition.should_receive(:execute).with(@env).once.and_return(result)
17
+ @object.execute(@env).should == 'true case'
18
+ end
19
+
20
+ it "should execute the condition if false execute the false case, retring it's value" do
21
+ result = mock('result mock')
22
+ result.should_receive(:is_true?).with(no_args).once.and_return(false)
23
+ @false_case.should_receive(:execute).with(@env).once.and_return('false case')
24
+ @condition.should_receive(:execute).with(@env).once.and_return(result)
25
+ @object.execute(@env).should == 'false case'
26
+ end
27
+
28
+ it "should execute the condition if false and no false condition is given return nil" do
29
+ @object = RVM::Interpreter::Condition.new(@condition, @true_case)
30
+ result = mock('result mock')
31
+ result.should_receive(:is_true?).with(no_args).once.and_return(false)
32
+ @condition.should_receive(:execute).with(@env).once.and_return(result)
33
+ @object.execute(@env).should be_nil
34
+ end
35
+
36
+ it "should return the type of it's cases if they are the same" do
37
+ @true_case.should_receive(:data_type).with(no_args).once.and_return('type1')
38
+ @false_case.should_receive(:data_type).with(no_args).at_least(:once).and_return('type1')
39
+ @object.data_type.should == 'type1'
40
+ end
41
+
42
+ it "should return the :any type if the types are not the same" do
43
+ @true_case.should_receive(:data_type).with(no_args).once.and_return('type1')
44
+ @false_case.should_receive(:data_type).with(no_args).once.and_return('type2')
45
+ @object.data_type.should == :any
46
+ end
47
+ end
@@ -0,0 +1,31 @@
1
+ require 'lib/rvm/interpreter'
2
+
3
+ describe RVM::Interpreter::Constant do
4
+ before do
5
+ @value = mock('value mock')
6
+ @env = RVM::Interpreter.env
7
+ @object = RVM::Interpreter::Constant.new(@value)
8
+ end
9
+
10
+ it "should return the value when executed." do
11
+ @object.execute(@env).should be(@value)
12
+ end
13
+
14
+ it "should have the data type of it's value" do
15
+ @value.should_receive(:data_type).with(no_args).once.and_return(:example)
16
+ @object.data_type.should == :example
17
+ end
18
+
19
+ it "should compare with other constants based on the value" do
20
+ @object.==(@object).should be_true
21
+ object2 = RVM::Interpreter::Constant.new(@value)
22
+ @object.==(object2).should be_true
23
+ object3 = RVM::Interpreter::Constant.new(1)
24
+ @object.==(object3).should be_false
25
+ end
26
+
27
+ it "should not compare to other types" do
28
+ @object.==(1).should be_false
29
+ end
30
+
31
+ end
@@ -0,0 +1,51 @@
1
+ require File.dirname(__FILE__) + '/../../../lib/rvm/interpreter'
2
+
3
+ shared_examples_for "a enviroment" do
4
+ it "should get the locals" do
5
+ @env['1'].val.should == 'one'
6
+ end
7
+
8
+ it "should return nil for unset locals" do
9
+ @env['2'].should == nil
10
+ end
11
+
12
+ it "should allow to change locals" do
13
+ @env['2'] = 'two'
14
+ @env['2'].val.should == 'two'
15
+ end
16
+ end
17
+
18
+ describe RVM::Interpreter::Enviroment do
19
+ before :each do
20
+ @env = RVM::Interpreter::Enviroment.new({:params => [1,2], :locals => {'1' => 'one'}})
21
+ end
22
+
23
+ it_should_behave_like "a enviroment"
24
+
25
+ it "should autocast locals to variable storages on creation" do
26
+ env = RVM::Interpreter::Enviroment.new({:params => [1,2], :locals => {'1' => 'one'}})
27
+ env['1'].should be_kind_of(RVM::Interpreter::VariableStorage)
28
+ end
29
+
30
+ it "should get the params" do
31
+ @env.param(0).should be(1)
32
+ @env.param(1).should be(2)
33
+ end
34
+
35
+ describe "(parented)" do
36
+ before do
37
+ @old_env = @env
38
+ @env = RVM::Interpreter::Enviroment.new({}, @old_env)
39
+ end
40
+
41
+ it_should_behave_like "a enviroment"
42
+
43
+ it "should apply changes to already set variables to the parent" do
44
+ @env['1'].val.should == 'one'
45
+ @env['1'] = 'ONE!'
46
+ @env['1'].val.should == 'ONE!'
47
+ @old_env['1'].val.should == 'ONE!'
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,72 @@
1
+ require 'lib/rvm/interpreter'
2
+ require 'lib/rvm/classes/block'
3
+
4
+ class FunctionMock < RVM::Functions::Function
5
+ register_for :mock
6
+ class << self
7
+ def call *args
8
+ 'result'
9
+ end
10
+ def execargs
11
+ @execargs
12
+ end
13
+ def execargs=v
14
+ @execargs = v
15
+ end
16
+ end
17
+ end
18
+
19
+ class RVM::Interpreter::FunctionCall
20
+ def fun
21
+ @function
22
+ end
23
+ def fun= f
24
+ @function = f
25
+ end
26
+ end
27
+
28
+ describe RVM::Interpreter::FunctionCall do
29
+ before do
30
+ @param1 = mock('param 1 mock')
31
+ @param2 = mock('param 2 mock')
32
+ @function = mock('function mock')
33
+ @env = mock('env mock')
34
+ end
35
+
36
+
37
+ describe "(execution)" do
38
+ before do
39
+ @object = RVM::Interpreter::FunctionCall.new(@function, [@param1,@param2])
40
+ end
41
+
42
+ it "shold execute it's params when the function.execargs returns true" do
43
+ FunctionMock.execargs = true
44
+ @object = RVM::Interpreter::FunctionCall.new('mock', [@param1,@param2])
45
+ @env.should_receive(:function).once.with('mock').and_return(nil)
46
+ @param1.should_receive(:execute).once.with(@env).and_return('param 1')
47
+ @param2.should_receive(:execute).once.with(@env).and_return('param 2')
48
+ @object.execute(@env).should == 'result'
49
+ end
50
+
51
+ it "shold not execute it's params when the function.execargs returns false" do
52
+ FunctionMock.execargs = false
53
+ @object = RVM::Interpreter::FunctionCall.new('mock', [@param1,@param2])
54
+ @env.should_receive(:function).once.with('mock').and_return(nil)
55
+ @object.execute(@env).should == 'result'
56
+ end
57
+
58
+ it "should return the data type of the function" do
59
+ @function.should_receive(:data_type).once.with(no_args).and_return(:type)
60
+ @object.data_type.should == :type
61
+ end
62
+
63
+ it "should not compare with random objects" do
64
+ @object.==(1).should be_false
65
+ end
66
+
67
+ it "should compare with other function if args and params are true it is the same" do
68
+ @object = RVM::Interpreter::FunctionCall.new(:mock, [@param1,@param2])
69
+ @object.==(@object).should be_true
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,11 @@
1
+ require 'lib/rvm/interpreter'
2
+
3
+ describe RVM::Interpreter do
4
+
5
+ it "should create a constant" do
6
+ c = RVM::Interpreter.const(:string, 'wobble')
7
+ c.should be_instance_of(RVM::Interpreter::Constant)
8
+ c.value.should be_instance_of(RVM::Classes[:string])
9
+ c.value.should == 'wobble'
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ require 'lib/rvm/interpreter'
2
+
3
+ describe RVM::Interpreter::Parameter do
4
+ before do
5
+ @value = mock('value mock')
6
+ @num = mock('name mock')
7
+ @env = mock('env mock')
8
+ @object = RVM::Interpreter::Parameter.new(@num)
9
+ end
10
+
11
+ it "should give the values type if name is a constant" do
12
+ @object.data_type.should == :any
13
+ end
14
+
15
+ it "should return the correct value and store it's type" do
16
+ @num.should_receive(:execute).with(@env).once.and_return(0)
17
+ @env.should_receive(:param).with(0).once.and_return(@value)
18
+ @value.should_receive(:data_type).with(no_args).once.and_return(:type)
19
+ @object.execute(@env).should be(@value)
20
+ @object.data_type.should == :type
21
+ end
22
+
23
+
24
+ end
@@ -0,0 +1,20 @@
1
+ require 'lib/rvm/interpreter'
2
+
3
+ describe RVM::Interpreter::Sequence do
4
+ before do
5
+ @part1 = mock('part1 mock')
6
+ @part2 = mock('part2 mock')
7
+ @env = mock('env mock')
8
+ @object = RVM::Interpreter::Sequence.new([@part1,@part2])
9
+ end
10
+
11
+ it "should execute each part after another" do
12
+ @part1.should_receive(:execute).with(@env).once
13
+ @part2.should_receive(:execute).with(@env).once
14
+ @object.execute(@env)
15
+ end
16
+
17
+ it "should have a data_type of :any" do
18
+ @object.data_type.should == :any
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ require 'lib/rvm/interpreter'
2
+
3
+ describe RVM::Interpreter::Variable do
4
+ before do
5
+ @value = mock('value mock')
6
+ @name = mock('name mock')
7
+ @env = mock('env mock')
8
+ @object = RVM::Interpreter::Variable.new(@name)
9
+ end
10
+
11
+ it "should give the values type if name is a constant" do
12
+ @object.data_type.should == :any
13
+ end
14
+
15
+ it "should return the correct value and store it's type" do
16
+ @name.should_receive(:execute).with(@env).once.and_return('name')
17
+ @env.should_receive(:read_var_val).with('name').once.and_return(@value)
18
+ @value.should_receive(:data_type).with(no_args).once.and_return(:type)
19
+ @object.execute(@env).should be(@value)
20
+ @object.data_type.should == :type
21
+ end
22
+
23
+
24
+ end
@@ -0,0 +1,10 @@
1
+ require 'lib/rvm/plugin'
2
+ require File.dirname(__FILE__) + '/helpers/plugin_helper'
3
+
4
+ describe RVM::PluginHost do
5
+ it_should_behave_like 'a plugin host'
6
+ end
7
+
8
+ describe RVM::Plugin do
9
+ it_should_behave_like 'a plugin'
10
+ end
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) +'/../../../lib/rvm/classes'
2
+ require File.dirname(__FILE__) +'/../../../lib/rvm/classes/association'
3
+ require File.dirname(__FILE__) +'/../../../lib/rvm/functions/association/assoc_get'
4
+ require File.dirname(__FILE__) +'/../../../lib/rvm/functions/association/assoc_set'
5
+ require File.dirname(__FILE__) +'/../../../lib/rvm/functions/collection/size'
6
+
7
+ describe RVM::Classes::Association do
8
+
9
+ before(:each) do
10
+ @assoc = RVM::Classes::Association.new
11
+ @assoc['key'] = 'value'
12
+ end
13
+
14
+ it "should craete with no arguments"
15
+
16
+ it "should create with an hash"
17
+
18
+ it "should allow you to read values" do
19
+ @assoc['key'].should == 'value'
20
+ end
21
+
22
+ it "should allow you to write values" do
23
+ @assoc['key'].should == 'value'
24
+ @assoc['key'] = 'another value'
25
+ @assoc['key'].should == 'another value'
26
+ end
27
+
28
+ it "should publish a get method" do
29
+ @assoc.functions.should include('get')
30
+ end
31
+
32
+ it "should publish a set method" do
33
+ @assoc.functions.should include('set')
34
+ end
35
+
36
+ it "should publich a size method" do
37
+ @assoc.functions.should include('size')
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) +'/../../../lib/rvm/classes'
2
+ describe RVM::Classes::Block do
3
+
4
+ it "should create with a value" do
5
+ b = RVM::Classes::Block.new(nil)
6
+ end
7
+
8
+ it "should return true to a execargs call" do
9
+ b = RVM::Classes::Block.new(nil)
10
+ b.execargs.should be_true
11
+ end
12
+
13
+ it "should do nothign on execution" do
14
+ code = mock('code mock')
15
+ env = mock('env mock')
16
+ b = RVM::Classes::Block.new(code)
17
+ b.execute(env).should be_nil
18
+ end
19
+
20
+ it "should execute the given code" do
21
+ code = mock('code mock')
22
+ env = mock('env mock')
23
+ b = RVM::Classes::Block.new(code)
24
+ code.should_receive(:execute).once.with(any_args).and_return(42)
25
+ b.call(env, []).should be(42)
26
+ end
27
+ end