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
data/opal.gemspec CHANGED
@@ -5,8 +5,8 @@ Gem::Specification.new do |s|
5
5
  s.name = 'opal'
6
6
  s.version = Opal::VERSION
7
7
  s.author = 'Adam Beynon'
8
- s.email = 'adam@adambeynon.com'
9
- s.homepage = 'http://opalrb.org'
8
+ s.email = 'adam.beynon@gmail.com'
9
+ s.homepage = 'http://opal.github.com'
10
10
  s.summary = 'Ruby runtime and core library for javascript'
11
11
  s.description = 'Ruby runtime and core library for javascript.'
12
12
 
@@ -1,7 +1,7 @@
1
1
  describe "Array#assoc" do
2
2
  it "returns the first array whose 1st item is == obj or nil" do
3
3
  s1 = ["colors", "red", "blue", "green"]
4
- s2 = [:letter, "a", "b", "c"]
4
+ s2 = [:letters, "a", "b", "c"]
5
5
  s3 = [4]
6
6
  s4 = ["colors", "cyan", "yellow", "magenda"]
7
7
  s5 = [:letters, "a", "i", "u"]
@@ -11,8 +11,7 @@ describe "Array#assoc" do
11
11
  a.assoc(s2.first).should == s2
12
12
  a.assoc(s3.first).should == s3
13
13
  a.assoc(s4.first).should == s1
14
- # FIXME: WTF?
15
- # a.assoc(s5.first).should == s2
14
+ a.assoc(s5.first).should == s2
16
15
  end
17
16
 
18
17
  it "ignores any non-Array elements" do
@@ -0,0 +1,16 @@
1
+ describe "Array#<=>" do
2
+ it "returns 0 if the arrays are equal" do
3
+ ([] <=> []).should == 0
4
+ ([1, 2, 3, 4, 5, 6] <=> [1, 2, 3, 4, 5, 6]).should == 0
5
+ end
6
+
7
+ it "returns -1 if the array is shorter than the other array" do
8
+ ([] <=> [1]).should == -1
9
+ ([1, 1] <=> [1, 1, 1]).should == -1
10
+ end
11
+
12
+ it "returns +1 if the array is longer than the other array" do
13
+ ([1] <=> []).should == 1
14
+ ([1, 1, 1] <=> [1, 1]).should == 1
15
+ end
16
+ end
@@ -1,12 +1,3 @@
1
- module ArraySpecs
2
- class MyArray < Array
3
- def initialize(a, b)
4
- self << a << b
5
- ScratchPad.record :my_array_initialize
6
- end
7
- end
8
- end
9
-
10
1
  describe "Array.[]" do
11
2
  it "returns a new array populated with the given elements" do
12
3
  obj = Object.new
@@ -0,0 +1,21 @@
1
+ describe "Array#drop" do
2
+ it "removes the specified number of elements from the start of the array" do
3
+ [1, 2, 3, 4, 5].drop(2).should == [3, 4, 5]
4
+ end
5
+
6
+ it "returns an empty Array if all elements are dropped" do
7
+ [1, 2].drop(2).should == []
8
+ end
9
+
10
+ it "returns an empty Array when called on an empty Array" do
11
+ [].drop(0).should == []
12
+ end
13
+
14
+ it "does not drop any elements when passed zero" do
15
+ [1, 2].drop(0).should == [1, 2]
16
+ end
17
+
18
+ it "returns an empty Array if more elements than exist are dropped" do
19
+ [1, 2].drop(3).should == []
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ describe "Array#dup" do
2
+ it "produces a shallow copy where the references are directly copied" do
3
+ a = [mock('1'), mock('2')]
4
+ b = a.dup
5
+ b.first.object_id.should == a.first.object_id
6
+ b.last.object_id.should == a.last.object_id
7
+ end
8
+
9
+ it "creates a new array containing all the elements of the original" do
10
+ a = [1, 2, 3, 4]
11
+ b = a.dup
12
+ b.should == a
13
+ b.object_id.should_not == a.object_id
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ describe "Array#index" do
2
+ it "returns the index of the first element == to object" do
3
+ [1, 2, 3, 4, 5, 6].index(3).should == 2
4
+ [1, 2, 3, 4, 5, 6].index(4).should == 3
5
+ end
6
+
7
+ it "returns 0 if first element == to object" do
8
+ [2, 1, 3, 2, 5].index(2).should == 0
9
+ end
10
+
11
+ it "returns size-1 if only last element == to object" do
12
+ [2, 1, 3, 1, 5].index(5).should == 4
13
+ end
14
+
15
+ it "returns nil if no element == to object" do
16
+ [2, 1, 1, 1, 1].index(3).should == nil
17
+ end
18
+
19
+ it "accepts a block instead of an argument" do
20
+ [4, 2, 1, 5, 1, 3].index { |x| x < 2 }.should == 2
21
+ end
22
+
23
+ it "ignores the block if there is an argument" do
24
+ [4, 2, 1, 5, 1, 3].index(5) { |x| x < 2 }.should == 3
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ describe "Array#inspect" do
2
+ it "returns a String" do
3
+ [1, 2, 3].inspect.should be_kind_of(String)
4
+ end
5
+
6
+ it "returns '[]' for an empty Array" do
7
+ [].inspect.should == "[]"
8
+ end
9
+
10
+ it "calls inspect on its elements and joins the results with commas" do
11
+ [0, 1, 2].inspect.should == "[0, 1, 2]"
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ describe "Array#&" do
2
+ it "creates an array with elements common to both arrays (intersection)" do
3
+ ([] & []).should == []
4
+ ([1, 2] & []).should == []
5
+ ([] & [1, 2]).should == []
6
+ ([1, 3, 5] & [1, 2, 3]).should == [1, 3]
7
+ end
8
+
9
+ it "creates an array with no duplicates" do
10
+ ([1, 1, 3, 5] & [1, 2, 3]).uniq!.should == nil
11
+ end
12
+
13
+ it "creates an array with elements in order they are first encountered" do
14
+ ([1, 2, 3, 2, 5] & [5, 2, 3, 4]).should == [2, 3, 5]
15
+ end
16
+
17
+ it "does not modify the original Array" do
18
+ a = [1, 1, 3, 5]
19
+ a & [1, 2, 3]
20
+ a.should == [1, 1, 3, 5]
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ describe "Array#join" do
2
+ it "returns a string formed by concatenating each element with separator" do
3
+ [1, 2, 3, 4, 5].join(' | ').should == "1 | 2 | 3 | 4 | 5"
4
+ end
5
+
6
+ it "does not separate elements when the passed separator is nil" do
7
+ [1, 2, 3].join(nil).should == '123'
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ describe "Array#keep_if" do
2
+ it "deletes elements for which the block returns a false value" do
3
+ array = [1, 2, 3, 4, 5]
4
+ array.keep_if { |item| item > 3 }.should equal(array)
5
+ array.should == [4, 5]
6
+ end
7
+ end
@@ -1,13 +1,23 @@
1
1
  describe "Array#-" do
2
- it "subtracts two arrays" do
3
- ([ 1, 2, 3 ] - [ 3, 4, 5 ] ).should == [1, 2]
4
- ([ 1, 2, 3 ] - []).should == [1, 2, 3]
5
- ([] - [ 1, 2, 3 ]).should == []
6
- ([] - []).should == []
2
+ it "creates an array minus any items from other array" do
3
+ ([] - [1, 2, 4]).should == []
4
+ ([1, 2, 4] - []).should == [1, 2, 4]
5
+ ([1, 2, 3, 4, 5] - [1, 2, 4]).should == [3, 5]
7
6
  end
8
7
 
9
- it "can subtracts an array from itself" do
10
- ary = [1, 2, 3]
11
- (ary - ary).should == []
8
+ it "removes multiple items on the lhs equal to one on the rhs" do
9
+ ([1, 1, 2, 2, 3, 3, 4, 5] - [1, 2, 4]).should == [3, 3, 5]
12
10
  end
13
- end
11
+
12
+ it "is not destructive" do
13
+ a = [1, 2, 3]
14
+ a - []
15
+ a.should == [1, 2, 3]
16
+ a - [1]
17
+ a.should == [1, 2, 3]
18
+ a - [1, 2, 3]
19
+ a.should == [1, 2, 3]
20
+ a - [:a, :b, :c]
21
+ a.should == [1, 2, 3]
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ describe "Array#* with an integer" do
2
+ it "concatenates n copies of the array when passed an integer" do
3
+ ([1, 2, 3] * 0).should == []
4
+ ([1, 2, 3] * 1).should == [1, 2, 3]
5
+ ([1, 2, 3] * 3).should == [1, 2, 3, 1, 2, 3, 1, 2, 3]
6
+ end
7
+ end
8
+
9
+ describe "Array#* with a string" do
10
+ it "returns a string formed by concatenating each element with separator" do
11
+ ([1, 2, 3, 4, 5] * ' | ').should == "1 | 2 | 3 | 4 | 5"
12
+ end
13
+ end
@@ -0,0 +1,40 @@
1
+ describe "Array.new" do
2
+ it "returns an instance of Array" do
3
+ Array.new.should be_kind_of(Array)
4
+ end
5
+ end
6
+
7
+ describe "Array.new with no arguments" do
8
+ it "returns an empty array" do
9
+ Array.new.empty?.should be_true
10
+ end
11
+ end
12
+
13
+ describe "Array.new with (array)" do
14
+ it "returns an array initialized to the other array" do
15
+ b = [4, 5, 6]
16
+ Array.new(b).should == b
17
+ end
18
+ end
19
+
20
+ describe "Array.new with (size, object=nil)" do
21
+ it "returns an array of size filled with object" do
22
+ obj = [3]
23
+ a = Array.new(2, obj)
24
+ a.should == [obj, obj]
25
+ a[0].should equal(obj)
26
+ a[1].should equal(obj)
27
+ end
28
+
29
+ it "returns an array of size filled with nil when object is omitted" do
30
+ Array.new(3).should == [nil, nil, nil]
31
+ end
32
+
33
+ it "yields the index of the element and sets the element to the value of the block" do
34
+ Array.new(3) { |i| i.to_s }.should == ['0', '1', '2']
35
+ end
36
+
37
+ it "uses the block value instead of using the default value" do
38
+ Array.new(3, :obj) { |i| i.to_s }.should == ['0', '1', '2']
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ describe "Array#rindex" do
2
+ it "returns the first index backwards from the end where element == to object" do
3
+ [2, 1, 3, 5, 1].rindex(3).should == 2
4
+ end
5
+
6
+ it "returns size-1 if last element == to object" do
7
+ [2, 1, 3, 2, 5].rindex(5).should == 4
8
+ end
9
+
10
+ it "returns 0 if only first element == to object" do
11
+ [2, 1, 3, 1, 5].rindex(2).should == 0
12
+ end
13
+
14
+ it "returns nil if no element == to object" do
15
+ [1, 1, 3, 2, 1, 3].rindex(4).should == nil
16
+ end
17
+
18
+ it "accepts a block instead of an argument" do
19
+ [4, 2, 1, 5, 1, 3].rindex { |x| x < 2 }.should == 4
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ describe "Array#select" do
2
+ it "returns a new array of elements for which block is true" do
3
+ [1, 3, 4, 5, 6, 9].select { |i| i.odd? }.should == [1, 3, 5, 9]
4
+ [1, 2, 3, 4, 5, 6].select { true }.should == [1, 2, 3, 4, 5, 6]
5
+ [1, 2, 3, 4, 5, 6].select { false }.should == []
6
+ end
7
+ end
8
+
9
+ describe "Array#select!" do
10
+ it "returns nil if no changes were made in the array" do
11
+ [1, 2, 3].select! { true }.should be_nil
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ describe "Array#shift" do
2
+ it "removes and returns the first element" do
3
+ a = [5, 1, 1, 5, 4]
4
+ a.shift.should == 5
5
+ a.should == [1, 1, 5, 4]
6
+ a.shift.should == 1
7
+ a.should == [1, 5, 4]
8
+ a.shift.should == 1
9
+ a.should == [5, 4]
10
+ a.shift.should == 5
11
+ a.should == [4]
12
+ a.shift.should == 4
13
+ a.should == []
14
+ end
15
+
16
+ it "returns nil whent the array is empty" do
17
+ [].shift.should == nil
18
+ end
19
+
20
+ describe "passed a number n as an argument" do
21
+ it "removes and returns an array with the first n element of the array" do
22
+ a = [1, 2, 3, 4, 5, 6]
23
+
24
+ a.shift(0).should == []
25
+ a.should == [1, 2, 3, 4, 5, 6]
26
+
27
+ a.shift(1).should == [1]
28
+ a.should == [2, 3, 4, 5, 6]
29
+
30
+ a.shift(2).should == [2, 3]
31
+ a.should == [4, 5, 6]
32
+
33
+ a.shift(3).should == [4, 5, 6]
34
+ a.should == []
35
+ end
36
+
37
+ it "does not corrupt the array when shift without arguments is followed by shift with an argument" do
38
+ a = [1, 2, 3, 4, 5]
39
+
40
+ a.shift.should == 1
41
+ a.shift(3).should == [2, 3, 4]
42
+ a.should == [5]
43
+ end
44
+
45
+ it "returns whole elements if n exceeds size of the array" do
46
+ a = [1, 2, 3, 4, 5]
47
+ a.shift(6).should == [1, 2, 3, 4, 5]
48
+ a.should == []
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,37 @@
1
+ describe "Array#slice!" do
2
+ it "removes and return the element at index" do
3
+ a = [1, 2, 3, 4]
4
+ a.slice!(10).should == nil
5
+ a.should == [1, 2, 3, 4]
6
+ a.slice!(-10).should == nil
7
+ a.should == [1, 2, 3, 4]
8
+ a.slice!(2).should == 3
9
+ a.should == [1, 2, 4]
10
+ a.slice!(-1).should == 4
11
+ a.should == [1, 2]
12
+ a.slice!(1).should == 2
13
+ a.should == [1]
14
+ a.slice!(-1).should == 1
15
+ a.should == []
16
+ a.slice!(-1).should == nil
17
+ a.should == []
18
+ a.slice!(0).should == nil
19
+ a.should == []
20
+ end
21
+
22
+ it "removes and returns length elements beginning at start" do
23
+ a = [1, 2, 3, 4, 5, 6]
24
+ a.slice!(2, 3).should == [3, 4, 5]
25
+ a.should == [1, 2, 6]
26
+ a.slice!(1, 1).should == [2]
27
+ a.should == [1, 6]
28
+ a.slice!(1, 0).should == []
29
+ a.should == [1, 6]
30
+ a.slice!(2, 0).should == []
31
+ a.should == [1, 6]
32
+ a.slice!(0, 4).should == [1, 6]
33
+ a.should == []
34
+ a.slice!(0, 4).should == []
35
+ a.should == []
36
+ end
37
+ end
@@ -0,0 +1,21 @@
1
+ describe "Array#take" do
2
+ it "returns the first specified number of elements" do
3
+ [1, 2, 3].take(2).should == [1, 2]
4
+ end
5
+
6
+ it "returns all elements when the argument is greater than the Array size" do
7
+ [1, 2].take(99).should == [1, 2]
8
+ end
9
+
10
+ it "returns all elements when the argument is less than the Array size" do
11
+ [1, 2].take(4).should == [1, 2]
12
+ end
13
+
14
+ it "returns an empty Array when passed zero" do
15
+ [1].take(0).should == []
16
+ end
17
+
18
+ it "returns an empty Array when called on an empty Array" do
19
+ [].take(3).should == []
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ describe "Array#take_while" do
2
+ it "returns all elements until the block returns false" do
3
+ [1, 2, 3].take_while{ |element| element < 3 }.should == [1, 2]
4
+ end
5
+
6
+ it "returns all elements until the block returns nil" do
7
+ [1, 2, nil, 4].take_while{ |element| element }.should == [1, 2]
8
+ end
9
+
10
+ it "returns all elements until the block returns false" do
11
+ [1, 2, false, 4].take_while{ |element| element }.should == [1, 2]
12
+ end
13
+ end