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,67 @@
1
+ require "lib/rvm/classes/boolean"
2
+ describe RVM::Classes::Boolean do
3
+
4
+
5
+ it "shoulds creat with a bool value" do
6
+ b = RVM::Classes::Boolean.new(true)
7
+ b.is_true?.should be_true
8
+ b = RVM::Classes::Boolean.new(false)
9
+ b.is_true?.should be_false
10
+ end
11
+
12
+ it "should create with nil" do
13
+ b = RVM::Classes::Boolean.new(nil)
14
+ b.is_true?.should be_false
15
+ end
16
+
17
+ it "should use is_true? if existing" do
18
+ val = mock('value mock')
19
+ val.should_receive(:respond_to?).once.with(:is_true?).and_return(true)
20
+ val.should_receive(:is_true?).once.with(no_args).and_return(false)
21
+ b = RVM::Classes::Boolean.new(val)
22
+ b.is_true?.should be_false
23
+ end
24
+
25
+ it "should have the data type :boolean" do
26
+ b = RVM::Classes::Boolean.new(true)
27
+ b.data_type.should == :boolean
28
+ end
29
+
30
+ def setup
31
+ super
32
+ @Boolean = RVM::Classes::Boolean
33
+ end
34
+
35
+ def test_class
36
+ assert(@Boolean)
37
+ end
38
+
39
+ def test_creation
40
+ assert(@Boolean.new(@Boolean), "Create not nil object bool")
41
+ end
42
+
43
+ def test_compare
44
+ assert_equal(@Boolean.new(true),@Boolean.new(true))
45
+ assert_equal(@Boolean.new(false),@Boolean.new(false))
46
+ assert_equal(@Boolean.new(false),@Boolean.new(nil))
47
+ assert_equal(@Boolean.new(true),@Boolean.new(self))
48
+ end
49
+
50
+ def test_creation_is_true
51
+ assert(@Boolean)
52
+ assert(b1 = @Boolean.new(true), "Create true bool.")
53
+ assert(b2 = @Boolean.new(false), "Create false bool.")
54
+ assert(b3 = @Boolean.new(nil), "Create nil bool.")
55
+ assert(b4 = @Boolean.new(b1), "Create is_true? object bool.")
56
+ assert(b5 = @Boolean.new(b2), "Create not is_true? object bool.")
57
+ assert(b6 = @Boolean.new(@Boolean), "Create not nil object bool.")
58
+
59
+ assert(b1.is_true?, "Test if true is true.")
60
+ assert(!b2.is_true?, "Test if false is false.")
61
+ assert(!b3.is_true?, "Test if nis is false.")
62
+ assert(b4.is_true?, "Test if a is_true? object is true.")
63
+ assert(!b5.is_true?, "Test if a not is_true? object is false.")
64
+ assert(b6.is_true?, "Test if not null object is true.")
65
+ end
66
+
67
+ end
@@ -0,0 +1,43 @@
1
+ require "lib/rvm/classes/error"
2
+ describe RVM::Classes::Error do
3
+
4
+ describe "(creation)" do
5
+
6
+ it "should create with an integer" do
7
+ e = RVM::Classes::Error.new(1,"Test error")
8
+ e.should be
9
+ end
10
+
11
+ it "should create with an integer" do
12
+ e = RVM::Classes::Error.new(1.2,"Test error")
13
+ e.should be
14
+ end
15
+
16
+ it "should create with with odd stuff" do
17
+ e = RVM::Classes::Error.new(nil,"Test error")
18
+ e.should be
19
+ e = RVM::Classes::Error.new("muhaha","Test error")
20
+ e.should be
21
+ end
22
+
23
+ end
24
+
25
+ describe "(usage)" do
26
+ it "shuld convert to a error string" do
27
+ e = RVM::Classes::Error.new(1,"Test error.")
28
+ e.to_s.should == "#1 Test error."
29
+ end
30
+
31
+ it "should prevent double positive error numbers" do
32
+ e = RVM::Classes::Error.new(-3,"Test error.")
33
+ e.to_s.should == "#-3 Test error."
34
+ end
35
+
36
+ it "should return false for is_true?." do
37
+ e = RVM::Classes::Error.new(1,"Test error.")
38
+ e.is_true?.should be_false
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,68 @@
1
+ require "lib/rvm/classes/list"
2
+ describe RVM::Classes::List do
3
+
4
+ describe "(creation)" do
5
+ it "should create empty" do
6
+ l = RVM::Classes::List.new()
7
+ l.size.should be(0)
8
+ end
9
+
10
+ it "should create simple lists with one element" do
11
+ l = RVM::Classes::List.new(true)
12
+ l.size.should be(1)
13
+ end
14
+
15
+
16
+ it "should create from an array" do
17
+ l = RVM::Classes::List.new(['1','2','3','4'])
18
+ l.size.should be(4)
19
+ end
20
+
21
+ it "should split a list of string sepperated items" do
22
+ l = RVM::Classes::List.new("1 2 3 4")
23
+ l.should == ['1','2','3','4']
24
+ l.size.should be(4)
25
+ end
26
+
27
+ it "should split a list of string sepperated items" do
28
+ l = RVM::Classes::List.new("1|2|3|4", '|')
29
+ l.should == ['1','2','3','4']
30
+ l.size.should be(4)
31
+ end
32
+ it "should have the data type :list" do
33
+ l = RVM::Classes::List.new("")
34
+ l.data_type.should == :list
35
+ end
36
+ end
37
+
38
+ describe "(conversion)" do
39
+ it "should rejoin a list, by default with spaces" do
40
+ l = RVM::Classes::List.new([1,2,3])
41
+ l.to_s.should == "1 2 3"
42
+ end
43
+
44
+ it "should rejoin a list by a other sepperator" do
45
+ l = RVM::Classes::List.new([1,2,3], ',')
46
+ l.to_s.should == "1,2,3"
47
+ end
48
+ end
49
+
50
+ describe "(sepperator)" do
51
+ it "should let you switch the sepperator without affecting the elemetns" do
52
+ l = RVM::Classes::List.new("1|a 2|b 3|c 4|d")
53
+ l.size.should be(4)
54
+ l.sepperator = '|'
55
+ l.size.should be(4)
56
+ l.to_s.should == "1|a|2|b|3|c|4|d"
57
+ end
58
+
59
+ it "should let you resplit the list changing it's content" do
60
+ l = RVM::Classes::List.new("1|a 2|b 3|c 4|d")
61
+ l.size.should be(4)
62
+ l.resplit('|')
63
+ l.size.should be(5)
64
+ l.to_s.should == "1|a 2|b 3|c 4|d"
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,132 @@
1
+ require "lib/rvm/classes"
2
+ require "lib/rvm/classes/number"
3
+
4
+ describe RVM::Classes::Number do
5
+
6
+ describe "(creation)" do
7
+
8
+ it "should create from fixnums" do
9
+ n = RVM::Classes::Number.new(1)
10
+ n.should be_instance_of(RVM::Classes::Number)
11
+ n.should == 1
12
+ end
13
+
14
+ it "should create from floats" do
15
+ n = RVM::Classes::Number.new(1.1)
16
+ n.should be_instance_of(RVM::Classes::Number)
17
+ n.should == 1.1
18
+ end
19
+
20
+ it "should create from bignum" do
21
+ n = RVM::Classes::Number.new(10**100)
22
+ n.should be_instance_of(RVM::Classes::Number)
23
+ n.should == 10**100
24
+ end
25
+
26
+ describe "(conversion)" do
27
+ it "should try to call to_f if to_f and to_i is different" do
28
+ value = mock('value mock')
29
+ value.should_receive(:respond_to?).ordered.once.with(:to_f).and_return(true)
30
+ value.should_receive(:to_f).ordered.once.with(no_args).and_return(42)
31
+ value.should_receive(:to_i).ordered.once.with(no_args).and_return(2)
32
+ n = RVM::Classes::Number.new(value)
33
+ n.should be_instance_of(RVM::Classes::Number)
34
+ n.should == 42
35
+ end
36
+
37
+ it "should use to_i if to_f and to_i are the same" do
38
+ value = mock('value mock')
39
+ value.should_receive(:respond_to?).ordered.once.with(:to_f).and_return(true)
40
+ value.should_receive(:to_f).ordered.once.with(no_args).and_return(2)
41
+ value.should_receive(:to_i).ordered.twice.with(no_args).and_return(2)
42
+ value.should_receive(:respond_to?).ordered.once.with(:to_i).and_return(true)
43
+ n = RVM::Classes::Number.new(value)
44
+ n.should be_instance_of(RVM::Classes::Number)
45
+ n.should == 2
46
+ end
47
+
48
+ it "should stuff it in a string if nothing else" do
49
+ value = mock('value mock')
50
+ value.should_receive(:respond_to?).ordered.once.with(:to_f).and_return(false)
51
+ value.should_receive(:respond_to?).ordered.once.with(:to_i).and_return(false)
52
+ value.should_receive(:to_s).ordered.once.and_return('4.2')
53
+ n = RVM::Classes::Number.new(value)
54
+ n.should be_instance_of(RVM::Classes::Number)
55
+ n.should == 4.2
56
+ end
57
+ end
58
+ end
59
+ describe "(runtime)" do
60
+ before do
61
+ @object = RVM::Classes::Number.new 42
62
+ end
63
+
64
+ describe "(convertion)" do
65
+ it "should convert the value to a string" do
66
+ @object.to_s.should == '42'
67
+ end
68
+
69
+ it "should convert the value to integer" do
70
+ @object.to_i.should be_instance_of(Fixnum)
71
+ end
72
+
73
+ it "should convert the value to fliat" do
74
+ @object.to_f.should be_instance_of(Float)
75
+ end
76
+ end
77
+ describe "(compairsont)" do
78
+ it "should be is_true? when not zero" do
79
+ @object.is_true?.should be_true
80
+ end
81
+
82
+ it "should not be is_true? when zero" do
83
+ @object =RVM::Classes::Number.new 0
84
+ @object.is_true?.should_not be_true
85
+ end
86
+
87
+ it "should compare with other Numbers" do
88
+ @object.==(@object).should be_true
89
+ end
90
+
91
+ it "should compare normal ruby number classes" do
92
+ @object.==(42).should be_true
93
+ @object.==(42.0).should be_true
94
+ end
95
+ end
96
+
97
+ describe "(delegation)" do
98
+ before do
99
+ @value = mock('value mock')
100
+ @value.should_receive(:is_a?).once.with(Numeric).and_return(true)
101
+ @value
102
+ @object = RVM::Classes::Number.new @value
103
+ end
104
+
105
+ #it "should delege respond_to to it's value" do
106
+ # @value.should_receive(:respond_to?).once.with(:woggle).and_return('yes god damn it!')
107
+ # @object.respond_to?(:woggle).should == 'yes god damn it!'
108
+ #end
109
+
110
+ it "should delege unknwon methods to it's value and return a Number" do
111
+ @value.should_receive(:woggle).once.with(1).and_return(42)
112
+ res = @object.woggle(1)
113
+ res.should be_instance_of(RVM::Classes::Number)
114
+ res.should == 42
115
+ end
116
+
117
+ it "should delege unknwon methods to it's value and return a Number also work with number arguments" do
118
+ n = RVM::Classes::Number.new(1)
119
+ @value.should_receive(:respond_to?).once.with(:woggle).and_return(true)
120
+ @value.should_receive(:woggle).once.with(n).and_return(42)
121
+ res = @object.woggle(n)
122
+ res.should be_instance_of(RVM::Classes::Number)
123
+ res.should == 42
124
+ end
125
+
126
+ it "should raise a method missing otherwise" do
127
+ @object = RVM::Classes::Number.new 42
128
+ lambda {@object.woggle}.should raise_error
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,175 @@
1
+ require "lib/rvm/classes/string"
2
+
3
+ describe RVM::Classes::String do
4
+
5
+ describe "(creation)" do
6
+ it "should create as an empty string without arguments" do
7
+ str = RVM::Classes::String.new
8
+ str.should be_instance_of(RVM::Classes::String)
9
+ str.empty?.should be_true
10
+ end
11
+
12
+ it "should create with a string parameter" do
13
+ str = RVM::Classes::String.new 'example'
14
+ str.should be_instance_of(RVM::Classes::String)
15
+ str.should == 'example'
16
+ end
17
+
18
+ it "should create with a string .to_s otherwise" do
19
+ value = mock('value mock')
20
+ value.should_receive(:to_s).once.with(no_args).and_return('example2')
21
+ str = RVM::Classes::String.new value
22
+ str.should be_instance_of(RVM::Classes::String)
23
+ str.should == 'example2'
24
+ end
25
+ end
26
+
27
+ describe "(runtime)" do
28
+ before do
29
+ @object = RVM::Classes::String.new "String"
30
+ end
31
+
32
+ it "should be is_true? when non empty" do
33
+ @object.is_true?.should be_true
34
+ end
35
+
36
+ it "should not be is_true? when empty" do
37
+ @object = RVM::Classes::String.new ""
38
+ @object.is_true?.should_not be_true
39
+ end
40
+
41
+ it "should have the data_type :string" do
42
+ @object.data_type.should == :string
43
+ end
44
+
45
+ it "should return a RVM::Classes::String when adding it" do
46
+ str = @object + '!'
47
+ str.should be_instance_of(RVM::Classes::String)
48
+ str.should == 'String!'
49
+ end
50
+
51
+ describe "(center)" do
52
+ it "should center evenly" do
53
+ str = @object.center(8)
54
+ str.should be_instance_of(RVM::Classes::String)
55
+ str.should == ' String '
56
+ end
57
+ it "should align left when uneven" do
58
+ str = @object.center(9)
59
+ str.should be_instance_of(RVM::Classes::String)
60
+ str.should == ' String '
61
+ end
62
+
63
+ it "should not crop the string when too long" do
64
+ str = @object.center(1)
65
+ str.should be_instance_of(RVM::Classes::String)
66
+ str.should == 'String'
67
+ end
68
+
69
+ it "should allow to alter spacing chars" do
70
+ str = @object.center(8,'.')
71
+ str.should be_instance_of(RVM::Classes::String)
72
+ str.should == '.String.'
73
+ end
74
+
75
+ it "should take the first spacing chars" do
76
+ str = @object.center(8,'-.')
77
+ str.should be_instance_of(RVM::Classes::String)
78
+ str.should == '-String-'
79
+ end
80
+ it "should default the spacing char to space" do
81
+ str = @object.center(8,'')
82
+ str.should be_instance_of(RVM::Classes::String)
83
+ str.should == ' String '
84
+ end
85
+ end
86
+
87
+ describe "(ljust)" do
88
+ it "should adjust to the left" do
89
+ str = @object.ljust(7)
90
+ str.should be_instance_of(RVM::Classes::String)
91
+ str.should == 'String '
92
+ end
93
+
94
+ it "should not crop the string when too long" do
95
+ str = @object.ljust(1)
96
+ str.should be_instance_of(RVM::Classes::String)
97
+ str.should == 'String'
98
+ end
99
+
100
+ it "should allow to alter spacing chars" do
101
+ str = @object.ljust(8,'.')
102
+ str.should be_instance_of(RVM::Classes::String)
103
+ str.should == 'String..'
104
+ end
105
+
106
+ it "should take the first spacing chars" do
107
+ str = @object.ljust(8,'-.')
108
+ str.should be_instance_of(RVM::Classes::String)
109
+ str.should == 'String--'
110
+ end
111
+
112
+ it "should default the spacing char to space" do
113
+ str = @object.ljust(8,'')
114
+ str.should be_instance_of(RVM::Classes::String)
115
+ str.should == 'String '
116
+ end
117
+ end
118
+ describe "(rjust)" do
119
+ it "should adjust to the right" do
120
+ str = @object.rjust(7)
121
+ str.should be_instance_of(RVM::Classes::String)
122
+ str.should == ' String'
123
+ end
124
+
125
+ it "should not crop the string when too long" do
126
+ str = @object.rjust(1)
127
+ str.should be_instance_of(RVM::Classes::String)
128
+ str.should == 'String'
129
+ end
130
+
131
+ it "should allow to alter spacing chars" do
132
+ str = @object.rjust(8,'.')
133
+ str.should be_instance_of(RVM::Classes::String)
134
+ str.should == '..String'
135
+ end
136
+
137
+ it "should take the first spacing chars" do
138
+ str = @object.rjust(8,'-.')
139
+ str.should be_instance_of(RVM::Classes::String)
140
+ str.should == '--String'
141
+ end
142
+
143
+ it "should default the spacing char to space" do
144
+ str = @object.rjust(8,'')
145
+ str.should be_instance_of(RVM::Classes::String)
146
+ str.should == ' String'
147
+ end
148
+ end
149
+ describe "(ansi)" do
150
+
151
+ it "should allow you to colorize strings" do
152
+ @object = @object.ansi('hr')
153
+ @object.should == "\e[1;31mString\e[0m"
154
+ end
155
+
156
+ it "should handle embedding of ansi in other ansi" do
157
+ str_left = RVM::Classes::String.new("<")
158
+ str_right = RVM::Classes::String.new(">")
159
+ @object = @object.ansi('hr')
160
+ @object = str_left + @object + str_right
161
+ @object.should == "<\e[1;31mString\e[0m>"
162
+ @object = @object.ansi('g')
163
+ @object.should == "\e[32m<\e[1;31mString\e[32m>\e[0m"
164
+ end
165
+
166
+ it "should handle positioning ansi aware" do
167
+ pending("ANSI Awarenes isn't implemented yet")
168
+ end
169
+
170
+ it "should split ansi aware" do
171
+ pending("ANSI Awarenes isn't implemented yet")
172
+ end
173
+ end
174
+ end
175
+ end