opal 0.3.26 → 0.3.27
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -6
- data/.travis.yml +1 -4
- data/Gemfile +1 -1
- data/README.md +3 -8
- data/Rakefile +26 -3
- data/core/array.rb +33 -91
- data/core/boolean.rb +1 -4
- data/core/class.rb +25 -71
- data/core/error.rb +5 -3
- data/core/hash.rb +3 -2
- data/core/kernel.rb +40 -1
- data/core/load_order +1 -1
- data/core/nil_class.rb +1 -3
- data/core/runtime.js +4 -44
- data/core/template.rb +20 -0
- data/core/{opal-spec → test_runner}/runner.js +0 -6
- data/lib/opal.rb +1 -12
- data/lib/opal/builder.rb +3 -10
- data/lib/opal/lexer.rb +1095 -1110
- data/lib/opal/parser.rb +229 -26
- data/lib/opal/rake_task.rb +3 -3
- data/lib/opal/version.rb +1 -1
- data/opal.gemspec +2 -2
- data/spec/core/array/assoc_spec.rb +2 -3
- data/spec/core/array/comparison_spec.rb +16 -0
- data/spec/core/array/constructor_spec.rb +0 -9
- data/spec/core/array/drop_spec.rb +21 -0
- data/spec/core/array/dup_spec.rb +15 -0
- data/spec/core/array/{eql_spec.rb → equal_value_spec.rb} +0 -0
- data/spec/core/array/index_spec.rb +26 -0
- data/spec/core/array/inspect_spec.rb +13 -0
- data/spec/core/array/intersection_spec.rb +22 -0
- data/spec/core/array/join_spec.rb +9 -0
- data/spec/core/array/keep_if_spec.rb +7 -0
- data/spec/core/array/minus_spec.rb +19 -9
- data/spec/core/array/multiply_spec.rb +13 -0
- data/spec/core/array/new_spec.rb +40 -0
- data/spec/core/array/rindex_spec.rb +21 -0
- data/spec/core/array/select_spec.rb +13 -0
- data/spec/core/array/shift_spec.rb +51 -0
- data/spec/core/array/slice_spec.rb +37 -0
- data/spec/core/array/take_spec.rb +21 -0
- data/spec/core/array/take_while_spec.rb +13 -0
- data/spec/core/array/to_a_spec.rb +7 -0
- data/spec/core/array/unshift_spec.rb +29 -0
- data/spec/core/hash/constructor_spec.rb +13 -0
- data/spec/core/hash/default_proc_spec.rb +20 -0
- data/spec/core/hash/default_spec.rb +8 -0
- data/spec/core/hash/delete_spec.rb +11 -0
- data/spec/core/hash/dup_spec.rb +10 -0
- data/spec/core/hash/reject_spec.rb +18 -0
- data/spec/core/hash/to_a_spec.rb +13 -0
- data/spec/core/kernel/Array_spec.rb +10 -0
- data/spec/core/kernel/class_spec.rb +6 -0
- data/spec/core/kernel/equal_spec.rb +12 -0
- data/spec/core/kernel/extend_spec.rb +21 -0
- data/spec/core/kernel/instance_eval_spec.rb +28 -0
- data/spec/core/kernel/instance_variable_get_spec.rb +14 -0
- data/spec/core/kernel/instance_variable_set_spec.rb +10 -0
- data/spec/core/kernel/match_spec.rb +5 -0
- data/spec/core/module/alias_method_spec.rb +10 -0
- data/spec/core/module/ancestors_spec.rb +11 -0
- data/spec/core/module/append_features_spec.rb +14 -0
- data/spec/core/proc/call_spec.rb +21 -0
- data/spec/core/proc/proc_tricks_spec.rb +1 -1
- data/spec/language/alias_spec.rb +1 -1
- data/spec/opal/class/instance_methods_spec.rb +13 -0
- data/spec/opal/kernel/attribute_spec.rb +57 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/test_case.html +13 -0
- metadata +88 -12
- data/core/erb.rb +0 -32
- data/lib/opal/erb_parser.rb +0 -20
- data/spec/opal/erb/erb_spec.rb +0 -23
@@ -0,0 +1,29 @@
|
|
1
|
+
describe "Array#unshift" do
|
2
|
+
it "prepends object to the original array" do
|
3
|
+
a = [1, 2, 3]
|
4
|
+
a.unshift("a").should equal(a)
|
5
|
+
a.should == ['a', 1, 2, 3]
|
6
|
+
a.unshift().should equal(a)
|
7
|
+
a.should == ['a', 1, 2, 3]
|
8
|
+
a.unshift(5, 4, 3)
|
9
|
+
a.should == [5, 4, 3, 'a', 1, 2, 3]
|
10
|
+
|
11
|
+
# shift all but one element
|
12
|
+
a = [1, 2]
|
13
|
+
a.shift
|
14
|
+
a.unshift(3, 4)
|
15
|
+
a.should == [3, 4, 2]
|
16
|
+
|
17
|
+
# now shift all elements
|
18
|
+
a.shift
|
19
|
+
a.shift
|
20
|
+
a.shift
|
21
|
+
a.unshift(3, 4)
|
22
|
+
a.should == [3, 4]
|
23
|
+
end
|
24
|
+
|
25
|
+
it "quietly ignores unshifting nothing" do
|
26
|
+
[].unshift().should == []
|
27
|
+
[].unshift(*[]).should == []
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
describe "Hash.[]" do
|
2
|
+
describe "passed zero arguments" do
|
3
|
+
it "returns an empty hash" do
|
4
|
+
Hash[].should == {}
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
it "creates a Hash; values can be provided as the argument list" do
|
9
|
+
Hash[:a, 1, :b, 2].should == { :a => 1, :b => 2 }
|
10
|
+
Hash[].should == {}
|
11
|
+
Hash[:a, 1, :b, {:c => 2}].should == {:a => 1, :b => {:c => 2}}
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
describe "Hash#default_proc" do
|
2
|
+
it "returns the block passed to Hash.new" do
|
3
|
+
h = Hash.new { |i| 'Paris' }
|
4
|
+
p = h.default_proc
|
5
|
+
p.call(1).should == 'Paris'
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns nil if no block was passed to proc" do
|
9
|
+
{}.default_proc.should == nil
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "Hash#default_proc=" do
|
14
|
+
it "replaces the block passed to Hash.new" do
|
15
|
+
h = Hash.new { |i| 'Paris' }
|
16
|
+
h.default_proc = Proc.new { 'Montreal' }
|
17
|
+
p = h.default_proc
|
18
|
+
p.call(1).should == 'Montreal'
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
describe "Hash#reject" do
|
2
|
+
it "returns a new hash removing keys for which the block yields true" do
|
3
|
+
h = {1=>false, 2=>true, 3=>false, 4=>true}
|
4
|
+
h.reject { |k,v| v }.keys.should == [1,3]
|
5
|
+
end
|
6
|
+
|
7
|
+
it "is equivalent to hsh.dup.delete_if" do
|
8
|
+
h = {:a => 'a', :b => 'b', :c => 'd'}
|
9
|
+
h.reject { |k,v| k == 'd' }.should == (h.dup.delete_if { |k, v| k == 'd' })
|
10
|
+
|
11
|
+
all_args_reject = []
|
12
|
+
all_args_delete_if = []
|
13
|
+
h = {1 => 2, 3 => 4}
|
14
|
+
h.reject { |*args| all_args_reject << args }
|
15
|
+
h.delete_if { |*args| all_args_delete_if << args }
|
16
|
+
all_args_reject.should == all_args_delete_if
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
describe "Hash#to_a" do
|
2
|
+
it "returns a list of [key, value] pairs with same order as each()" do
|
3
|
+
h = {:a => 1, 1 => :a, 3 => :b, :b => 5}
|
4
|
+
pairs = []
|
5
|
+
|
6
|
+
h.each_pair do |key, value|
|
7
|
+
pairs << [key, value]
|
8
|
+
end
|
9
|
+
|
10
|
+
h.to_a.should be_kind_of(Array)
|
11
|
+
h.to_a.should == pairs
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
describe "Kernel#Array" do
|
2
|
+
it "returns an Array containing the argument if it responds to neither #to_ary nor #to_a" do
|
3
|
+
obj = mock('obj')
|
4
|
+
Array(obj).should == [obj]
|
5
|
+
end
|
6
|
+
|
7
|
+
it "returns an empty Array when passed nil" do
|
8
|
+
Array(nil).should == []
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
describe "Kernel#equal?" do
|
2
|
+
it "returns true only if obj and other are the same object" do
|
3
|
+
o1 = Object.new
|
4
|
+
o2 = Object.new
|
5
|
+
o1.equal?(o1).should == true
|
6
|
+
o2.equal?(o2).should == true
|
7
|
+
o1.equal?(o2).should == false
|
8
|
+
nil.equal?(nil).should == true
|
9
|
+
o1.equal?(nil).should == false
|
10
|
+
nil.equal?(o2).should == false
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module KernelExtendSpecs
|
2
|
+
module Mod
|
3
|
+
def foo; 3.142; end
|
4
|
+
end
|
5
|
+
|
6
|
+
class A
|
7
|
+
extend Mod
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "Kernel#extend" do
|
12
|
+
it "extends the class/module with the module" do
|
13
|
+
KernelExtendSpecs::A.foo.should == 3.142
|
14
|
+
end
|
15
|
+
|
16
|
+
it "extends the object with the module" do
|
17
|
+
obj = Object.new
|
18
|
+
obj.extend KernelExtendSpecs::Mod
|
19
|
+
obj.foo.should == 3.142
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
describe "Kernel#instance_eval" do
|
2
|
+
before :each do
|
3
|
+
ScratchPad.clear
|
4
|
+
end
|
5
|
+
|
6
|
+
it "yields the object to the block" do
|
7
|
+
"hola".instance_eval { |o| ScratchPad.record o }
|
8
|
+
ScratchPad.recorded.should == "hola"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns the result of the block" do
|
12
|
+
"hola".instance_eval { :result }.should == :result
|
13
|
+
end
|
14
|
+
|
15
|
+
it "binds self to the receiver" do
|
16
|
+
s = "hola"
|
17
|
+
(s == s.instance_eval { self }).should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "executes in the context of the receiver" do
|
21
|
+
"Ruby-fu".instance_eval { size }.should == 7
|
22
|
+
end
|
23
|
+
|
24
|
+
it "has access to receiver's instance variables" do
|
25
|
+
@foo_bar = 42
|
26
|
+
instance_eval { @foo_bar }.should == 42
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
describe "Kernel#instance_variable_get" do
|
2
|
+
before(:each) do
|
3
|
+
@obj = Object.new
|
4
|
+
@obj.instance_variable_set("@test", :test)
|
5
|
+
end
|
6
|
+
|
7
|
+
it "returns nil when the referred instance variable does not exist" do
|
8
|
+
@obj.instance_variable_get(:@does_not_exist).should be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns the value of the passed instance variable" do
|
12
|
+
@obj.instance_variable_get(:@test).should == :test
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module ModuleAncestorsSpec
|
2
|
+
class A; end
|
3
|
+
class B < A; end
|
4
|
+
end
|
5
|
+
|
6
|
+
describe "Module#ancestors" do
|
7
|
+
it "returns a list of modules in self (including self)" do
|
8
|
+
ModuleAncestorsSpec::B.ancestors.include?(ModuleAncestorsSpec::B).should == true
|
9
|
+
ModuleAncestorsSpec::B.ancestors.include?(ModuleAncestorsSpec::A).should == true
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module AppendFeaturesSpec
|
2
|
+
class Klass; end
|
3
|
+
|
4
|
+
def self.append_features(mod)
|
5
|
+
ScratchPad.record mod
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "Module#append_features" do
|
10
|
+
it "gets called when self is included in another module/class" do
|
11
|
+
AppendFeaturesSpec::Klass.include AppendFeaturesSpec
|
12
|
+
ScratchPad.recorded.should == AppendFeaturesSpec::Klass
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
describe "Proc#call" do
|
2
|
+
it "invokes self" do
|
3
|
+
Proc.new { "test!" }.call.should == "test!"
|
4
|
+
lambda { "test!" }.call.should == "test!"
|
5
|
+
proc { "test!" }.call.should == "test!"
|
6
|
+
end
|
7
|
+
|
8
|
+
it "sets self's parameters to the given values" do
|
9
|
+
Proc.new { |a, b| a + b }.call(1, 2).should == 3
|
10
|
+
Proc.new { |*args| args }.call(1, 2, 3, 4).should == [1, 2, 3, 4]
|
11
|
+
Proc.new { |_, *args| args }.call(1, 2, 3).should == [2, 3]
|
12
|
+
|
13
|
+
lambda { |a, b| a + b }.call(1, 2).should == 3
|
14
|
+
lambda { |*args| args }.call(1, 2, 3, 4).should == [1, 2, 3, 4]
|
15
|
+
lambda { |_, *args| args }.call(1, 2, 3).should == [2, 3]
|
16
|
+
|
17
|
+
proc { |a, b| a + b }.call(1, 2).should == 3
|
18
|
+
proc { |*args| args }.call(1, 2, 3, 4).should == [1, 2, 3, 4]
|
19
|
+
proc { |_, *args| args }.call(1, 2, 3).should == [2, 3]
|
20
|
+
end
|
21
|
+
end
|
data/spec/language/alias_spec.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
class InstanceMethodsSpec
|
2
|
+
def foo; end
|
3
|
+
def bar; end
|
4
|
+
def baz; end
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Class do
|
8
|
+
describe '#instance_methods' do
|
9
|
+
it 'returns an array of the instance method names for this class' do
|
10
|
+
InstanceMethodsSpec.instance_methods.should == [:foo, :bar, :baz]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
class AttributeSpec
|
2
|
+
attr_accessor :foo, :bar
|
3
|
+
attr_reader :first_name, :baz
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@foo = 'adam'
|
7
|
+
@bar = 42
|
8
|
+
@baz = 3.142
|
9
|
+
@buz = 'omg'
|
10
|
+
end
|
11
|
+
|
12
|
+
def baz?
|
13
|
+
'should not return this one'
|
14
|
+
end
|
15
|
+
|
16
|
+
def buz?
|
17
|
+
@buz
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Kernel do
|
22
|
+
before do
|
23
|
+
@obj = AttributeSpec.new
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#attribute_get" do
|
27
|
+
it "returns attribute values for simple keys" do
|
28
|
+
@obj.attribute_get(:foo).should == 'adam'
|
29
|
+
@obj.attribute_get(:bar).should == 42
|
30
|
+
end
|
31
|
+
|
32
|
+
it "checks for boolean (foo?) accessors after normal getters" do
|
33
|
+
@obj.attribute_get(:baz).should == 3.142
|
34
|
+
@obj.attribute_get(:buz).should == 'omg'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns nil for unknown attributes" do
|
38
|
+
@obj.attribute_get(:fullname).should be_nil
|
39
|
+
@obj.attribute_get(:pingpong).should be_nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#attribute_set" do
|
44
|
+
it "uses the setter for the given attribute" do
|
45
|
+
@obj.attribute_set(:foo, 42)
|
46
|
+
@obj.foo.should == 42
|
47
|
+
|
48
|
+
@obj.attribute_set(:foo, 3.142)
|
49
|
+
@obj.foo.should == 3.142
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns nil when setting an attribute with no setter method" do
|
53
|
+
@obj.attribute_set(:baz, 'this should not be set')
|
54
|
+
@obj.baz.should == 3.142
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/test_case.html
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
<!doctype html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Opal Test Case</title>
|
5
|
+
<script type="text/javascript" src="../../opal.js"></script>
|
6
|
+
<script type="text/javascript" src="../../opal-parser.js"></script>
|
7
|
+
<script type="text/javascript" src="../../opal-spec.js"></script>
|
8
|
+
<script type="text/javascript" src="specs.js"></script>
|
9
|
+
<script type="text/javascript" src="../runner.js"></script>
|
10
|
+
</head>
|
11
|
+
<body>
|
12
|
+
</body>
|
13
|
+
</html>
|