opal 0.3.26 → 0.3.27

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.
Files changed (74) hide show
  1. data/.gitignore +1 -6
  2. data/.travis.yml +1 -4
  3. data/Gemfile +1 -1
  4. data/README.md +3 -8
  5. data/Rakefile +26 -3
  6. data/core/array.rb +33 -91
  7. data/core/boolean.rb +1 -4
  8. data/core/class.rb +25 -71
  9. data/core/error.rb +5 -3
  10. data/core/hash.rb +3 -2
  11. data/core/kernel.rb +40 -1
  12. data/core/load_order +1 -1
  13. data/core/nil_class.rb +1 -3
  14. data/core/runtime.js +4 -44
  15. data/core/template.rb +20 -0
  16. data/core/{opal-spec → test_runner}/runner.js +0 -6
  17. data/lib/opal.rb +1 -12
  18. data/lib/opal/builder.rb +3 -10
  19. data/lib/opal/lexer.rb +1095 -1110
  20. data/lib/opal/parser.rb +229 -26
  21. data/lib/opal/rake_task.rb +3 -3
  22. data/lib/opal/version.rb +1 -1
  23. data/opal.gemspec +2 -2
  24. data/spec/core/array/assoc_spec.rb +2 -3
  25. data/spec/core/array/comparison_spec.rb +16 -0
  26. data/spec/core/array/constructor_spec.rb +0 -9
  27. data/spec/core/array/drop_spec.rb +21 -0
  28. data/spec/core/array/dup_spec.rb +15 -0
  29. data/spec/core/array/{eql_spec.rb → equal_value_spec.rb} +0 -0
  30. data/spec/core/array/index_spec.rb +26 -0
  31. data/spec/core/array/inspect_spec.rb +13 -0
  32. data/spec/core/array/intersection_spec.rb +22 -0
  33. data/spec/core/array/join_spec.rb +9 -0
  34. data/spec/core/array/keep_if_spec.rb +7 -0
  35. data/spec/core/array/minus_spec.rb +19 -9
  36. data/spec/core/array/multiply_spec.rb +13 -0
  37. data/spec/core/array/new_spec.rb +40 -0
  38. data/spec/core/array/rindex_spec.rb +21 -0
  39. data/spec/core/array/select_spec.rb +13 -0
  40. data/spec/core/array/shift_spec.rb +51 -0
  41. data/spec/core/array/slice_spec.rb +37 -0
  42. data/spec/core/array/take_spec.rb +21 -0
  43. data/spec/core/array/take_while_spec.rb +13 -0
  44. data/spec/core/array/to_a_spec.rb +7 -0
  45. data/spec/core/array/unshift_spec.rb +29 -0
  46. data/spec/core/hash/constructor_spec.rb +13 -0
  47. data/spec/core/hash/default_proc_spec.rb +20 -0
  48. data/spec/core/hash/default_spec.rb +8 -0
  49. data/spec/core/hash/delete_spec.rb +11 -0
  50. data/spec/core/hash/dup_spec.rb +10 -0
  51. data/spec/core/hash/reject_spec.rb +18 -0
  52. data/spec/core/hash/to_a_spec.rb +13 -0
  53. data/spec/core/kernel/Array_spec.rb +10 -0
  54. data/spec/core/kernel/class_spec.rb +6 -0
  55. data/spec/core/kernel/equal_spec.rb +12 -0
  56. data/spec/core/kernel/extend_spec.rb +21 -0
  57. data/spec/core/kernel/instance_eval_spec.rb +28 -0
  58. data/spec/core/kernel/instance_variable_get_spec.rb +14 -0
  59. data/spec/core/kernel/instance_variable_set_spec.rb +10 -0
  60. data/spec/core/kernel/match_spec.rb +5 -0
  61. data/spec/core/module/alias_method_spec.rb +10 -0
  62. data/spec/core/module/ancestors_spec.rb +11 -0
  63. data/spec/core/module/append_features_spec.rb +14 -0
  64. data/spec/core/proc/call_spec.rb +21 -0
  65. data/spec/core/proc/proc_tricks_spec.rb +1 -1
  66. data/spec/language/alias_spec.rb +1 -1
  67. data/spec/opal/class/instance_methods_spec.rb +13 -0
  68. data/spec/opal/kernel/attribute_spec.rb +57 -0
  69. data/spec/spec_helper.rb +1 -1
  70. data/spec/test_case.html +13 -0
  71. metadata +88 -12
  72. data/core/erb.rb +0 -32
  73. data/lib/opal/erb_parser.rb +0 -20
  74. data/spec/opal/erb/erb_spec.rb +0 -23
@@ -0,0 +1,7 @@
1
+ describe "Array#to_a" do
2
+ it "returns self" do
3
+ a = [1, 2, 3]
4
+ a.to_a.should == [1, 2, 3]
5
+ a.should equal(a.to_a)
6
+ end
7
+ end
@@ -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
@@ -6,4 +6,12 @@ describe "Hash#default" do
6
6
  Hash.new.default.should == nil
7
7
  Hash.new.default(4).should == nil
8
8
  end
9
+ end
10
+
11
+ describe "Hash#default=" do
12
+ it "sets the default value" do
13
+ h = {}
14
+ h.default = 99
15
+ h.default.should == 99
16
+ end
9
17
  end
@@ -0,0 +1,11 @@
1
+ describe "Hash#delete" do
2
+ it "removes the entry and returns the deleted value" do
3
+ h = {a: 5, b: 2}
4
+ h.delete(:b).should == 2
5
+ h.should == {a: 5}
6
+ end
7
+
8
+ it "returns nil if the key is not found" do
9
+ {}.delete(:a).should == nil
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ describe "Hash#dup" do
2
+ it "copies instance variable but not the objects they refer to" do
3
+ hash = {'key' => 'value'}
4
+
5
+ clone = hash.dup
6
+
7
+ clone.should == hash
8
+ clone.object_id.should_not == hash.object_id
9
+ end
10
+ 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,6 @@
1
+ describe "Kernel#class" do
2
+ it "returns the class of the receiver" do
3
+ Object.new.class.should == Object
4
+ [].class.should == Array
5
+ end
6
+ 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,10 @@
1
+ describe "Kernel#instance_variable_get" do
2
+ before(:each) do
3
+ @obj = Object.new
4
+ end
5
+
6
+ it "sets the value of the referenced variable" do
7
+ @obj.instance_variable_set(:@test, 42)
8
+ @obj.instance_variable_get(:@test).should == 42
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ describe "Kernel#=~" do
2
+ it "should return false" do
3
+ (Object.new =~ 'abc').should be_false
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ class AliasMethodSpec
2
+ def foo; 'foo'; end
3
+ alias_method :bar, :foo
4
+ end
5
+
6
+ describe "Module#alias_method" do
7
+ it "makes a copy of the method" do
8
+ AliasMethodSpec.new.bar.should == 'foo'
9
+ end
10
+ 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
@@ -4,4 +4,4 @@ describe 'Proc' do
4
4
  Proc.new {}.lambda?.should == false
5
5
  end
6
6
  end
7
- end
7
+ end
@@ -1,5 +1,5 @@
1
1
  class AliasObject
2
- attr :foo
2
+ attr_accessor :foo
3
3
  attr_reader :baz
4
4
  attr_accessor :baz
5
5
 
@@ -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
@@ -3,7 +3,7 @@ require 'opal'
3
3
  module Kernel
4
4
  def opal_eval(str)
5
5
  code = Opal::Parser.new.parse str
6
- `eval('(' + code + ')()')`
6
+ `eval(code)`
7
7
  end
8
8
 
9
9
  def opal_parse(str, file='(string)')
@@ -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>