AmberVM 0.0.19
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/README +38 -0
- data/bin/ambervm +278 -0
- data/lib/amber/acts_as_rvm_type.rb +157 -0
- data/lib/amber/classes/association.rb +36 -0
- data/lib/amber/classes/block.rb +52 -0
- data/lib/amber/classes/boolean.rb +40 -0
- data/lib/amber/classes/class.rb +50 -0
- data/lib/amber/classes/error.rb +22 -0
- data/lib/amber/classes/list.rb +96 -0
- data/lib/amber/classes/null.rb +35 -0
- data/lib/amber/classes/number.rb +95 -0
- data/lib/amber/classes/object.rb +56 -0
- data/lib/amber/classes/string.rb +79 -0
- data/lib/amber/classes.rb +113 -0
- data/lib/amber/environment.rb +251 -0
- data/lib/amber/fukubukuro/ecma_core.rb +409 -0
- data/lib/amber/fukubukuro.rb +866 -0
- data/lib/amber/functions/all.rb +3 -0
- data/lib/amber/functions/array/append.rb +50 -0
- data/lib/amber/functions/array/at.rb +50 -0
- data/lib/amber/functions/array/set_at.rb +50 -0
- data/lib/amber/functions/array.rb +30 -0
- data/lib/amber/functions/association/assoc_get.rb +55 -0
- data/lib/amber/functions/association/assoc_set.rb +56 -0
- data/lib/amber/functions/bitwise/bitwise_and.rb +41 -0
- data/lib/amber/functions/bitwise/bitwise_not.rb +41 -0
- data/lib/amber/functions/bitwise/bitwise_or.rb +41 -0
- data/lib/amber/functions/bitwise/bitwise_xor.rb +41 -0
- data/lib/amber/functions/bitwise.rb +3 -0
- data/lib/amber/functions/collection/get.rb +66 -0
- data/lib/amber/functions/collection/set.rb +67 -0
- data/lib/amber/functions/collection/size.rb +54 -0
- data/lib/amber/functions/general/cmp.rb +43 -0
- data/lib/amber/functions/general/eq.rb +45 -0
- data/lib/amber/functions/general/gt.rb +45 -0
- data/lib/amber/functions/general/gte.rb +45 -0
- data/lib/amber/functions/general/lt.rb +45 -0
- data/lib/amber/functions/general/lte.rb +45 -0
- data/lib/amber/functions/general/neq.rb +45 -0
- data/lib/amber/functions/general/type.rb +43 -0
- data/lib/amber/functions/general.rb +3 -0
- data/lib/amber/functions/io/print.rb +45 -0
- data/lib/amber/functions/io.rb +3 -0
- data/lib/amber/functions/list/align.rb +73 -0
- data/lib/amber/functions/list/join.rb +45 -0
- data/lib/amber/functions/list/map.rb +58 -0
- data/lib/amber/functions/list/split.rb +55 -0
- data/lib/amber/functions/list.rb +3 -0
- data/lib/amber/functions/logic/and.rb +55 -0
- data/lib/amber/functions/logic/not.rb +40 -0
- data/lib/amber/functions/logic/or.rb +50 -0
- data/lib/amber/functions/logic.rb +3 -0
- data/lib/amber/functions/math/abs.rb +39 -0
- data/lib/amber/functions/math/acos.rb +39 -0
- data/lib/amber/functions/math/add.rb +40 -0
- data/lib/amber/functions/math/asin.rb +39 -0
- data/lib/amber/functions/math/atan.rb +39 -0
- data/lib/amber/functions/math/ceil.rb +39 -0
- data/lib/amber/functions/math/cos.rb +39 -0
- data/lib/amber/functions/math/dec.rb +39 -0
- data/lib/amber/functions/math/div.rb +44 -0
- data/lib/amber/functions/math/exp.rb +39 -0
- data/lib/amber/functions/math/floor.rb +39 -0
- data/lib/amber/functions/math/inc.rb +39 -0
- data/lib/amber/functions/math/log.rb +39 -0
- data/lib/amber/functions/math/mod.rb +41 -0
- data/lib/amber/functions/math/mul.rb +43 -0
- data/lib/amber/functions/math/neg.rb +43 -0
- data/lib/amber/functions/math/power.rb +43 -0
- data/lib/amber/functions/math/rand.rb +36 -0
- data/lib/amber/functions/math/round.rb +39 -0
- data/lib/amber/functions/math/shl.rb +41 -0
- data/lib/amber/functions/math/shr.rb +41 -0
- data/lib/amber/functions/math/sin.rb +39 -0
- data/lib/amber/functions/math/sub.rb +43 -0
- data/lib/amber/functions/math/tan.rb +39 -0
- data/lib/amber/functions/math.rb +3 -0
- data/lib/amber/functions/objects/send.rb +22 -0
- data/lib/amber/functions/rails/print.rb +44 -0
- data/lib/amber/functions/rails.rb +3 -0
- data/lib/amber/functions/string/ansi.rb +24 -0
- data/lib/amber/functions/string/capstr.rb +23 -0
- data/lib/amber/functions/string/center.rb +25 -0
- data/lib/amber/functions/string/chr.rb +16 -0
- data/lib/amber/functions/string/ljust.rb +26 -0
- data/lib/amber/functions/string/regmatch.rb +34 -0
- data/lib/amber/functions/string/rjust.rb +26 -0
- data/lib/amber/functions/string.rb +3 -0
- data/lib/amber/functions.rb +103 -0
- data/lib/amber/interpreter.rb +1380 -0
- data/lib/amber/languages/brainfuck.rb +153 -0
- data/lib/amber/languages/ecma/compiler.rb +1661 -0
- data/lib/amber/languages/ecma/core-math.js +67 -0
- data/lib/amber/languages/ecma/core-objects.js +57 -0
- data/lib/amber/languages/ecma.rb +9 -0
- data/lib/amber/languages/ecma_fuku/compiler.rb +1622 -0
- data/lib/amber/languages/ecma_fuku/core-math.js +67 -0
- data/lib/amber/languages/ecma_fuku/core-objects.js +56 -0
- data/lib/amber/languages/ecma_fuku.rb +13 -0
- data/lib/amber/languages/math/compiler.rb +70 -0
- data/lib/amber/languages/math/tokenizer.rb +69 -0
- data/lib/amber/languages/math/tree.rb +110 -0
- data/lib/amber/languages/math.rb +26 -0
- data/lib/amber/languages.rb +99 -0
- data/lib/amber/library.rb +79 -0
- data/lib/amber/optimisation.rb +299 -0
- data/lib/amber/plugin.rb +337 -0
- data/lib/amber/rails.rb +90 -0
- data/lib/amber.rb +106 -0
- data/spec/amber/class_spec.rb +27 -0
- data/spec/amber/enviroment_spec.rb +61 -0
- data/spec/amber/function_spec.rb +25 -0
- data/spec/amber/functions/association/assoc_get_spec.rb +41 -0
- data/spec/amber/functions/association/assoc_set_spec.rb +43 -0
- data/spec/amber/functions/collection/get_spec.rb +12 -0
- data/spec/amber/functions/collection/set_spec.rb +10 -0
- data/spec/amber/functions/collection/size_spec.rb +10 -0
- data/spec/amber/functions/list/split_spec.rb +47 -0
- data/spec/amber/functions/string/ansi_spec.rb +44 -0
- data/spec/amber/functions/string/capstr_spec.rb +42 -0
- data/spec/amber/functions/string/center_spec.rb +49 -0
- data/spec/amber/functions/string/ljust_spec.rb +49 -0
- data/spec/amber/functions/string/regmatch_spec.rb +52 -0
- data/spec/amber/functions/string/rjust_spec.rb +49 -0
- data/spec/amber/interpreter/assignment_spec.rb +22 -0
- data/spec/amber/interpreter/condition_spec.rb +103 -0
- data/spec/amber/interpreter/constant_spec.rb +31 -0
- data/spec/amber/interpreter/core_call_spec.rb +72 -0
- data/spec/amber/interpreter/interpreter_spec.rb +11 -0
- data/spec/amber/interpreter/parameter_spec.rb +24 -0
- data/spec/amber/interpreter/sequence_spec.rb +47 -0
- data/spec/amber/interpreter/variable_spec.rb +24 -0
- data/spec/amber/plugin_spec.rb +10 -0
- data/spec/classes/atom/association_spec.rb +39 -0
- data/spec/classes/atom/block_spec.rb +25 -0
- data/spec/classes/atom/boolean_spec.rb +67 -0
- data/spec/classes/atom/error_spec.rb +43 -0
- data/spec/classes/atom/list_spec.rb +68 -0
- data/spec/classes/atom/number_spec.rb +132 -0
- data/spec/classes/atom/string_spec.rb +175 -0
- data/spec/languages/ecma/ecma_array_spec.rb +79 -0
- data/spec/languages/ecma/ecma_closure_spec.rb +38 -0
- data/spec/languages/ecma/ecma_literals_spec.rb +71 -0
- data/spec/languages/ecma/ecma_objects_spec.rb +165 -0
- data/spec/languages/ecma/ecma_old_spec.rb +540 -0
- data/spec/languages/ecma/ecma_spec.rb +64 -0
- data/spec/languages/ecma_fuku/ecma_array_spec.rb +61 -0
- data/spec/languages/ecma_fuku/ecma_closure_spec.rb +33 -0
- data/spec/languages/ecma_fuku/ecma_function_spec.rb +84 -0
- data/spec/languages/ecma_fuku/ecma_literals_spec.rb +55 -0
- data/spec/languages/ecma_fuku/ecma_objects_spec.rb +133 -0
- data/spec/languages/ecma_fuku/ecma_old_spec.rb +415 -0
- data/spec/languages/ecma_fuku/ecma_operator_spec.rb +33 -0
- data/spec/languages/ecma_fuku/ecma_spec.rb +52 -0
- data/spec/languages/math/compiler_spec.rb +49 -0
- data/spec/languages/math/tokenizer_spec.rb +73 -0
- data/spec/languages/math/tree_spec.rb +153 -0
- metadata +225 -0
@@ -0,0 +1,68 @@
|
|
1
|
+
require "amber/classes/list"
|
2
|
+
describe AmberVM::Classes::List do
|
3
|
+
|
4
|
+
describe "(creation)" do
|
5
|
+
it "should create empty" do
|
6
|
+
l = AmberVM::Classes::List.new()
|
7
|
+
l.size.should be(0)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should create simple lists with one element" do
|
11
|
+
l = AmberVM::Classes::List.new(true)
|
12
|
+
l.size.should be(1)
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
it "should create from an array" do
|
17
|
+
l = AmberVM::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 = AmberVM::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 = AmberVM::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 = AmberVM::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 = AmberVM::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 = AmberVM::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 = AmberVM::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 = AmberVM::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 "amber/classes"
|
2
|
+
require "amber/classes/number"
|
3
|
+
|
4
|
+
describe AmberVM::Classes::Number do
|
5
|
+
|
6
|
+
describe "(creation)" do
|
7
|
+
|
8
|
+
it "should create from fixnums" do
|
9
|
+
n = AmberVM::Classes::Number.new(1)
|
10
|
+
n.should be_instance_of(AmberVM::Classes::Number)
|
11
|
+
n.should == 1
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should create from floats" do
|
15
|
+
n = AmberVM::Classes::Number.new(1.1)
|
16
|
+
n.should be_instance_of(AmberVM::Classes::Number)
|
17
|
+
n.should == 1.1
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should create from bignum" do
|
21
|
+
n = AmberVM::Classes::Number.new(10**100)
|
22
|
+
n.should be_instance_of(AmberVM::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 = AmberVM::Classes::Number.new(value)
|
33
|
+
n.should be_instance_of(AmberVM::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 = AmberVM::Classes::Number.new(value)
|
44
|
+
n.should be_instance_of(AmberVM::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 = AmberVM::Classes::Number.new(value)
|
54
|
+
n.should be_instance_of(AmberVM::Classes::Number)
|
55
|
+
n.should == 4.2
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
describe "(runtime)" do
|
60
|
+
before do
|
61
|
+
@object = AmberVM::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 =AmberVM::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 = AmberVM::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(AmberVM::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 = AmberVM::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(AmberVM::Classes::Number)
|
123
|
+
res.should == 42
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should raise a method missing otherwise" do
|
127
|
+
@object = AmberVM::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 "amber/classes/string"
|
2
|
+
|
3
|
+
describe AmberVM::Classes::String do
|
4
|
+
|
5
|
+
describe "(creation)" do
|
6
|
+
it "should create as an empty string without arguments" do
|
7
|
+
str = AmberVM::Classes::String.new
|
8
|
+
str.should be_instance_of(AmberVM::Classes::String)
|
9
|
+
str.empty?.should be_true
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should create with a string parameter" do
|
13
|
+
str = AmberVM::Classes::String.new 'example'
|
14
|
+
str.should be_instance_of(AmberVM::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 = AmberVM::Classes::String.new value
|
22
|
+
str.should be_instance_of(AmberVM::Classes::String)
|
23
|
+
str.should == 'example2'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "(runtime)" do
|
28
|
+
before do
|
29
|
+
@object = AmberVM::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 = AmberVM::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 AmberVM::Classes::String when adding it" do
|
46
|
+
str = @object + '!'
|
47
|
+
str.should be_instance_of(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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(AmberVM::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 = AmberVM::Classes::String.new("<")
|
158
|
+
str_right = AmberVM::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
|
@@ -0,0 +1,79 @@
|
|
1
|
+
$: << 'lib'
|
2
|
+
|
3
|
+
require 'amber'
|
4
|
+
require 'amber/languages'
|
5
|
+
require 'amber/languages/ecma'
|
6
|
+
|
7
|
+
describe AmberVM::Languages::ECMA do
|
8
|
+
before(:each) do
|
9
|
+
@env = AmberVM::Interpreter.env
|
10
|
+
@compiler = AmberVM::Languages::ECMA.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def run code, &tests
|
14
|
+
env = @env.dup
|
15
|
+
yield(@compiler.compile(code).execute(@env), @env)
|
16
|
+
yield(@compiler.compile(code).optimize.execute(env), env)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "arrays" do
|
20
|
+
it "should allow accessing data on an array" do
|
21
|
+
@env['test'] = AmberVM::Classes::List.new([42])
|
22
|
+
code = <<-CODE
|
23
|
+
test[0]
|
24
|
+
CODE
|
25
|
+
run code do |result, env|
|
26
|
+
result.should == 42
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should allow set data on an array" do
|
31
|
+
@env['test'] = AmberVM::Classes::List.new([42])
|
32
|
+
code = <<-CODE
|
33
|
+
test[0] = 23
|
34
|
+
CODE
|
35
|
+
run code do |result, env|
|
36
|
+
result.should == 23
|
37
|
+
env['test'].val[0].val.should == 23
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should allow set data on an array from functions" do
|
42
|
+
@env['test'] = AmberVM::Classes::List.new([42])
|
43
|
+
code = <<-CODE
|
44
|
+
function f(x) {
|
45
|
+
return x + 1;
|
46
|
+
};
|
47
|
+
a = 23;
|
48
|
+
test[0] = f(a);
|
49
|
+
CODE
|
50
|
+
run code do |result, env|
|
51
|
+
result.should == 24
|
52
|
+
env['test'].val[0].val.should == 24
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should allow to concat arrays" do
|
57
|
+
a1 = AmberVM::Classes::List.new([42])
|
58
|
+
@env['test'] = AmberVM::Classes::List.new([a1])
|
59
|
+
code = <<-CODE
|
60
|
+
test[0][0]
|
61
|
+
CODE
|
62
|
+
run code do |result, env|
|
63
|
+
result.should == 42
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should allow to concat arrays when setting values" do
|
68
|
+
a1 = AmberVM::Classes::List.new([42])
|
69
|
+
@env['test'] = AmberVM::Classes::List.new([a1])
|
70
|
+
code = <<-CODE
|
71
|
+
test[0][0] = 23
|
72
|
+
CODE
|
73
|
+
run code do |result, env|
|
74
|
+
result.should == 23
|
75
|
+
a1[0].val.should == 23
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
$: << 'lib'
|
2
|
+
require 'amber'
|
3
|
+
require 'amber/languages'
|
4
|
+
require 'amber/languages/ecma'
|
5
|
+
|
6
|
+
describe AmberVM::Languages::ECMA do
|
7
|
+
before(:each) do
|
8
|
+
@env = AmberVM::Interpreter.env
|
9
|
+
@compiler = AmberVM::Languages::ECMA.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def run code, &tests
|
13
|
+
env = @env.dup
|
14
|
+
yield(@compiler.compile(code).execute(@env), @env)
|
15
|
+
yield(@compiler.compile(code).optimize.execute(env), env)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "closure" do
|
19
|
+
it "should be created" do
|
20
|
+
#pending 'No closures implemented yet.'
|
21
|
+
|
22
|
+
code = <<-CODE
|
23
|
+
function createClosure(x) {
|
24
|
+
return function() {
|
25
|
+
return x;
|
26
|
+
};
|
27
|
+
};
|
28
|
+
closure = createClosure(42);
|
29
|
+
createClosure("wrong!");
|
30
|
+
closure();
|
31
|
+
CODE
|
32
|
+
run code do |result, env|
|
33
|
+
result.should == 42
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
$: << 'lib'
|
2
|
+
|
3
|
+
require 'amber'
|
4
|
+
require 'amber/languages'
|
5
|
+
require 'amber/languages/ecma'
|
6
|
+
|
7
|
+
describe AmberVM::Languages::ECMA do
|
8
|
+
before(:each) do
|
9
|
+
@compiler = AmberVM::Languages::ECMA.new
|
10
|
+
@env = @compiler.env
|
11
|
+
end
|
12
|
+
|
13
|
+
def run code, &tests
|
14
|
+
env = @env.dup
|
15
|
+
yield(@compiler.compile(code).execute(@env), @env)
|
16
|
+
yield(@compiler.compile(code).optimize.execute(env), env)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "literals" do
|
20
|
+
it "should know String literals" do
|
21
|
+
code = <<-CODE
|
22
|
+
"test"
|
23
|
+
CODE
|
24
|
+
run code do |result, env|
|
25
|
+
result.should == "test"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
it "should know Integer literals" do
|
29
|
+
code = <<-CODE
|
30
|
+
42
|
31
|
+
CODE
|
32
|
+
run code do |result, env|
|
33
|
+
result.should == 42
|
34
|
+
end
|
35
|
+
end
|
36
|
+
it "should know Float literals" do
|
37
|
+
code = <<-CODE
|
38
|
+
4.2
|
39
|
+
CODE
|
40
|
+
run code do |result, env|
|
41
|
+
result.should == 4.2
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should know complex Float literals" do
|
46
|
+
code = <<-CODE
|
47
|
+
.2e-10
|
48
|
+
CODE
|
49
|
+
run code do |result, env|
|
50
|
+
result.should == 0.2e-10
|
51
|
+
end
|
52
|
+
end
|
53
|
+
it "should know the false literals" do
|
54
|
+
code = <<-CODE
|
55
|
+
false
|
56
|
+
CODE
|
57
|
+
run code do |result, env|
|
58
|
+
result.should_not be_is_true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
it "should know the true literals" do
|
62
|
+
code = <<-CODE
|
63
|
+
true
|
64
|
+
CODE
|
65
|
+
run code do |result, env|
|
66
|
+
result.should be_is_true
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|