opal 0.3.18 → 0.3.19
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -1
- data/Gemfile +1 -3
- data/README.md +472 -10
- data/Rakefile +10 -52
- data/core/array.rb +9 -14
- data/core/basic_object.rb +7 -10
- data/core/boolean.rb +5 -1
- data/core/class.rb +15 -38
- data/core/dir.rb +89 -0
- data/core/enumerable.rb +133 -57
- data/core/error.rb +15 -1
- data/core/file.rb +85 -0
- data/core/hash.rb +186 -32
- data/core/kernel.rb +30 -31
- data/core/load_order +4 -2
- data/core/module.rb +42 -62
- data/core/numeric.rb +7 -1
- data/core/object.rb +1 -1
- data/core/proc.rb +6 -2
- data/core/range.rb +16 -28
- data/core/regexp.rb +3 -3
- data/core/runtime.js +281 -350
- data/core/string.rb +100 -110
- data/docs/CNAME +1 -0
- data/docs/Rakefile +55 -0
- data/docs/css/styles.css +50 -0
- data/docs/css/syntax.css +63 -0
- data/docs/layout/post.html +3 -0
- data/docs/layout/pre.html +11 -0
- data/examples/dependencies/app.rb +3 -0
- data/lib/opal.rb +2 -1
- data/lib/opal/builder.rb +36 -10
- data/lib/opal/builder_task.rb +51 -24
- data/lib/opal/grammar.rb +2509 -2439
- data/lib/opal/grammar.y +38 -5
- data/lib/opal/lexer.rb +18 -2
- data/lib/opal/parser.rb +375 -349
- data/lib/opal/scope.rb +24 -2
- data/lib/opal/version.rb +1 -1
- data/spec/builder/build_order_spec.rb +20 -0
- data/spec/builder/lib_name_for_spec.rb +24 -0
- data/spec/grammar/call_spec.rb +9 -6
- data/spec/grammar/lambda_spec.rb +64 -0
- data/spec/grammar/sclass_spec.rb +5 -3
- data/{core/spec → test}/core/array/allocate_spec.rb +0 -0
- data/{core/spec → test}/core/array/append_spec.rb +0 -0
- data/{core/spec → test}/core/array/assoc_spec.rb +0 -0
- data/{core/spec → test}/core/array/at_spec.rb +0 -0
- data/{core/spec → test}/core/array/clear_spec.rb +0 -0
- data/{core/spec → test}/core/array/clone_spec.rb +0 -0
- data/{core/spec → test}/core/array/collect_spec.rb +0 -0
- data/{core/spec → test}/core/array/compact_spec.rb +0 -0
- data/{core/spec → test}/core/array/concat_spec.rb +0 -0
- data/{core/spec → test}/core/array/constructor_spec.rb +0 -0
- data/{core/spec → test}/core/array/count_spec.rb +0 -0
- data/{core/spec → test}/core/array/delete_at_spec.rb +0 -0
- data/{core/spec → test}/core/array/delete_if_spec.rb +0 -0
- data/{core/spec → test}/core/array/delete_spec.rb +0 -0
- data/{core/spec → test}/core/array/each_index_spec.rb +0 -0
- data/{core/spec → test}/core/array/each_spec.rb +0 -0
- data/{core/spec → test}/core/array/element_reference_spec.rb +0 -0
- data/{core/spec → test}/core/array/empty_spec.rb +0 -0
- data/{core/spec → test}/core/array/eql_spec.rb +0 -0
- data/{core/spec → test}/core/array/fetch_spec.rb +0 -0
- data/{core/spec → test}/core/array/first_spec.rb +0 -0
- data/{core/spec → test}/core/array/flatten_spec.rb +0 -0
- data/{core/spec → test}/core/array/include_spec.rb +0 -0
- data/{core/spec → test}/core/array/insert_spec.rb +0 -0
- data/{core/spec → test}/core/array/last_spec.rb +0 -0
- data/{core/spec → test}/core/array/length_spec.rb +0 -0
- data/{core/spec → test}/core/array/map_spec.rb +0 -0
- data/{core/spec → test}/core/array/plus_spec.rb +0 -0
- data/{core/spec → test}/core/array/pop_spec.rb +0 -0
- data/{core/spec → test}/core/array/push_spec.rb +0 -0
- data/{core/spec → test}/core/array/rassoc_spec.rb +0 -0
- data/{core/spec → test}/core/array/reject_spec.rb +0 -0
- data/{core/spec → test}/core/array/replace_spec.rb +0 -0
- data/{core/spec → test}/core/array/reverse_each_spec.rb +0 -0
- data/{core/spec → test}/core/array/reverse_spec.rb +0 -0
- data/{core/spec → test}/core/array/size_spec.rb +0 -0
- data/{core/spec → test}/core/array/to_ary_spec.rb +0 -0
- data/{core/spec → test}/core/array/uniq_spec.rb +0 -0
- data/{core/spec → test}/core/array/zip_spec.rb +0 -0
- data/test/core/class/fixtures/classes.rb +9 -0
- data/test/core/class/new_spec.rb +108 -0
- data/{core/spec → test}/core/enumerable/all_spec.rb +0 -0
- data/{core/spec → test}/core/enumerable/any_spec.rb +0 -0
- data/{core/spec → test}/core/enumerable/collect_spec.rb +0 -0
- data/{core/spec → test}/core/enumerable/count_spec.rb +0 -0
- data/test/core/enumerable/detect_spec.rb +48 -0
- data/test/core/enumerable/drop_spec.rb +17 -0
- data/test/core/enumerable/drop_while_spec.rb +24 -0
- data/test/core/enumerable/each_with_index_spec.rb +11 -0
- data/test/core/enumerable/each_with_object_spec.rb +17 -0
- data/test/core/enumerable/entries_spec.rb +6 -0
- data/test/core/enumerable/find_all_spec.rb +13 -0
- data/test/core/enumerable/find_index_spec.rb +45 -0
- data/test/core/enumerable/find_spec.rb +48 -0
- data/test/core/enumerable/first_spec.rb +40 -0
- data/{core/spec → test}/core/enumerable/fixtures/classes.rb +19 -0
- data/test/core/enumerable/grep_spec.rb +21 -0
- data/test/core/enumerable/take_spec.rb +40 -0
- data/test/core/enumerable/to_a_spec.rb +6 -0
- data/{core/spec → test}/core/false/and_spec.rb +0 -0
- data/{core/spec → test}/core/false/inspect_spec.rb +0 -0
- data/{core/spec → test}/core/false/or_spec.rb +0 -0
- data/{core/spec → test}/core/false/to_s_spec.rb +0 -0
- data/{core/spec → test}/core/false/xor_spec.rb +0 -0
- data/test/core/file/expand_path_spec.rb +20 -0
- data/{core/spec → test}/core/hash/allocate_spec.rb +0 -0
- data/{core/spec → test}/core/hash/assoc_spec.rb +0 -0
- data/{core/spec → test}/core/hash/clear_spec.rb +0 -0
- data/{core/spec → test}/core/hash/clone_spec.rb +0 -0
- data/test/core/hash/default_spec.rb +9 -0
- data/{core/spec → test}/core/hash/delete_if_spec.rb +0 -0
- data/test/core/hash/each_key_spec.rb +15 -0
- data/test/core/hash/each_pair_spec.rb +30 -0
- data/test/core/hash/each_spec.rb +30 -0
- data/test/core/hash/each_value_spec.rb +15 -0
- data/{core/spec → test}/core/hash/element_reference_spec.rb +14 -0
- data/{core/spec → test}/core/hash/element_set_spec.rb +1 -0
- data/test/core/hash/empty_spec.rb +10 -0
- data/test/core/hash/fetch_spec.rb +24 -0
- data/test/core/hash/flatten_spec.rb +46 -0
- data/test/core/hash/has_key_spec.rb +24 -0
- data/test/core/hash/has_value_spec.rb +12 -0
- data/test/core/hash/include_spec.rb +24 -0
- data/test/core/hash/index_spec.rb +13 -0
- data/test/core/hash/indexes_spec.rb +9 -0
- data/test/core/hash/indices_spec.rb +9 -0
- data/test/core/hash/invert_spec.rb +12 -0
- data/test/core/hash/keep_if_spec.rb +18 -0
- data/test/core/hash/key_spec.rb +24 -0
- data/test/core/hash/keys_spec.rb +10 -0
- data/test/core/hash/length_spec.rb +10 -0
- data/test/core/hash/member_spec.rb +24 -0
- data/{core/spec → test}/core/hash/merge_spec.rb +0 -0
- data/{core/spec → test}/core/hash/new_spec.rb +0 -0
- data/test/core/hash/rassoc_spec.rb +34 -0
- data/test/core/hash/replace_spec.rb +7 -0
- data/test/core/hash/select_spec.rb +52 -0
- data/test/core/hash/shift_spec.rb +19 -0
- data/test/core/hash/size_spec.rb +10 -0
- data/test/core/hash/update_spec.rb +17 -0
- data/test/core/hash/value_spec.rb +12 -0
- data/test/core/hash/values_at_spec.rb +9 -0
- data/test/core/hash/values_spec.rb +7 -0
- data/test/core/kernel/eql_spec.rb +15 -0
- data/test/core/kernel/equal_value_spec.rb +12 -0
- data/test/core/kernel/loop_spec.rb +23 -0
- data/test/core/kernel/nil_spec.rb +7 -0
- data/test/core/kernel/proc_spec.rb +9 -0
- data/test/core/kernel/rand_spec.rb +14 -0
- data/test/core/kernel/respond_to_spec.rb +24 -0
- data/test/core/kernel/send_spec.rb +56 -0
- data/test/core/kernel/tap_spec.rb +10 -0
- data/test/core/kernel/to_s_spec.rb +5 -0
- data/{core/spec → test}/core/matchdata/to_a_spec.rb +0 -0
- data/{core/spec → test}/core/nil/and_spec.rb +0 -0
- data/{core/spec → test}/core/nil/inspect_spec.rb +0 -0
- data/{core/spec → test}/core/nil/nil_spec.rb +0 -0
- data/{core/spec → test}/core/nil/or_spec.rb +0 -0
- data/{core/spec → test}/core/nil/to_a_spec.rb +0 -0
- data/{core/spec → test}/core/nil/to_f_spec.rb +0 -0
- data/{core/spec → test}/core/nil/to_i_spec.rb +0 -0
- data/{core/spec → test}/core/nil/to_s_spec.rb +0 -0
- data/{core/spec → test}/core/nil/xor_spec.rb +0 -0
- data/{core/spec → test}/core/numeric/equal_value_spec.rb +0 -0
- data/test/core/range/begin_spec.rb +9 -0
- data/test/core/range/case_compare_spec.rb +16 -0
- data/test/core/range/end_spec.rb +9 -0
- data/{core/spec → test}/core/regexp/match_spec.rb +0 -0
- data/test/core/string/capitalize_spec.rb +10 -0
- data/test/core/string/casecmp_spec.rb +16 -0
- data/test/core/string/chomp_spec.rb +43 -0
- data/test/core/string/chop_spec.rb +10 -0
- data/test/core/string/chr_spec.rb +13 -0
- data/test/core/string/comparison_spec.rb +13 -0
- data/test/core/string/downcase_spec.rb +6 -0
- data/test/core/string/element_reference_spec.rb +72 -0
- data/test/core/string/empty_spec.rb +8 -0
- data/test/core/string/end_with_spec.rb +12 -0
- data/test/core/string/fixtures/classes.rb +3 -0
- data/test/core/string/gsub_spec.rb +17 -0
- data/test/core/string/include_spec.rb +12 -0
- data/test/core/string/intern_spec.rb +9 -0
- data/test/core/string/length_spec.rb +9 -0
- data/test/core/string/lstrip_spec.rb +7 -0
- data/test/core/string/match_spec.rb +27 -0
- data/test/core/string/next_spec.rb +10 -0
- data/test/core/string/ord_spec.rb +9 -0
- data/test/core/string/partition_spec.rb +10 -0
- data/test/core/string/reverse_spec.rb +7 -0
- data/test/core/string/rstrip_spec.rb +7 -0
- data/test/core/string/size_spec.rb +9 -0
- data/test/core/string/slice_spec.rb +72 -0
- data/test/core/string/split_spec.rb +5 -0
- data/test/core/string/start_with_spec.rb +12 -0
- data/test/core/string/strip_spec.rb +6 -0
- data/test/core/string/sub_spec.rb +22 -0
- data/test/core/string/succ_spec.rb +10 -0
- data/test/core/string/sum_spec.rb +5 -0
- data/test/core/string/swapcase_spec.rb +18 -0
- data/test/core/string/to_a_spec.rb +9 -0
- data/test/core/string/to_f_spec.rb +14 -0
- data/test/core/string/to_i_spec.rb +25 -0
- data/test/core/string/to_s_spec.rb +13 -0
- data/test/core/string/to_str_spec.rb +13 -0
- data/test/core/string/to_sym_spec.rb +9 -0
- data/test/core/string/upcase_spec.rb +6 -0
- data/test/core/symbol/to_proc_spec.rb +12 -0
- data/{core/spec → test}/core/true/and_spec.rb +0 -0
- data/{core/spec → test}/core/true/inspect_spec.rb +0 -0
- data/{core/spec → test}/core/true/or_spec.rb +0 -0
- data/{core/spec → test}/core/true/to_s_spec.rb +0 -0
- data/{core/spec → test}/core/true/xor_spec.rb +0 -0
- data/test/index.html +11 -0
- data/{core/spec → test}/language/alias_spec.rb +4 -0
- data/{core/spec → test}/language/and_spec.rb +0 -0
- data/{core/spec → test}/language/array_spec.rb +0 -0
- data/{core/spec → test}/language/block_spec.rb +0 -0
- data/{core/spec → test}/language/break_spec.rb +0 -0
- data/{core/spec → test}/language/case_spec.rb +0 -0
- data/{core/spec → test}/language/defined_spec.rb +0 -0
- data/{core/spec → test}/language/ensure_spec.rb +0 -0
- data/test/language/fixtures/yield.rb +23 -0
- data/{core/spec → test}/language/hash_spec.rb +0 -0
- data/{core/spec → test}/language/if_spec.rb +0 -0
- data/test/language/literal_lambda_spec.rb +47 -0
- data/{core/spec → test}/language/loop_spec.rb +0 -0
- data/{core/spec → test}/language/metaclass_spec.rb +0 -0
- data/{core/spec → test}/language/next_spec.rb +0 -0
- data/{core/spec → test}/language/or_spec.rb +0 -0
- data/{core/spec → test}/language/predefined_spec.rb +0 -0
- data/{core/spec → test}/language/regexp_spec.rb +0 -0
- data/{core/spec → test}/language/send_spec.rb +0 -0
- data/{core/spec → test}/language/singleton_class_spec.rb +0 -0
- data/{core/spec → test}/language/super_spec.rb +0 -0
- data/{core/spec → test}/language/symbol_spec.rb +0 -0
- data/{core/spec → test}/language/undef_spec.rb +0 -0
- data/{core/spec → test}/language/unless_spec.rb +0 -0
- data/{core/spec → test}/language/until_spec.rb +0 -0
- data/{core/spec → test}/language/variables_spec.rb +0 -0
- data/{core/spec → test}/language/while_spec.rb +0 -0
- data/test/language/yield_spec.rb +100 -0
- data/test/opal/array/subclassing_spec.rb +32 -0
- data/test/opal/class/bridge_class_spec.rb +37 -0
- data/test/opal/exception/subclassing_spec.rb +17 -0
- data/test/opal/runtime/_methods_spec.rb +48 -0
- data/test/opal/runtime/class_hierarchy_spec.rb +22 -0
- data/test/opal/runtime/def_spec.rb +23 -0
- data/test/opal/string/subclassing_spec.rb +26 -0
- data/test/spec_helper.rb +3 -0
- metadata +437 -111
- data/core/spec/core/class/new_spec.rb +0 -16
- data/core/spec/core/hash/default_spec.rb +0 -4
- data/core/spec/core/symbol/to_proc_spec.rb +0 -6
- data/core/spec/index.html +0 -11
- data/spec/builder/build_source_spec.rb +0 -52
data/lib/opal/scope.rb
CHANGED
@@ -15,6 +15,9 @@ module Opal; class Parser
|
|
15
15
|
|
16
16
|
attr_reader :type
|
17
17
|
|
18
|
+
attr_accessor :defines_defn
|
19
|
+
attr_accessor :defines_defs
|
20
|
+
|
18
21
|
# used by modules to know what methods to donate to includees
|
19
22
|
attr_reader :methods
|
20
23
|
|
@@ -30,25 +33,39 @@ module Opal; class Parser
|
|
30
33
|
@unique = "a"
|
31
34
|
@while_stack = []
|
32
35
|
|
36
|
+
@defines_defs = false
|
37
|
+
@defines_defn = false
|
38
|
+
|
33
39
|
@methods = []
|
34
40
|
|
35
41
|
@uses_block = false
|
36
42
|
@catches_break = false
|
37
43
|
end
|
38
44
|
|
45
|
+
# Returns true if this scope is a class/module body scope
|
46
|
+
def class_scope?
|
47
|
+
@type == :class or @type == :module
|
48
|
+
end
|
49
|
+
|
39
50
|
##
|
40
51
|
# Vars to use inside each scope
|
41
52
|
def to_vars
|
42
53
|
vars = []
|
43
54
|
|
44
55
|
if @type == :class
|
56
|
+
vars << '__class = this'
|
45
57
|
vars << '__scope = this._scope'
|
46
58
|
vars << 'def = this._proto'
|
47
59
|
elsif @type == :module
|
60
|
+
vars << '__class = this'
|
48
61
|
vars << '__scope = this._scope'
|
49
62
|
vars << 'def = this._proto'
|
50
63
|
elsif @type == :sclass
|
51
64
|
vars << '__scope = this._scope'
|
65
|
+
elsif @type == :iter
|
66
|
+
vars << 'def = (this._isObject ? this._klass._proto : this._proto)' if @defines_defn
|
67
|
+
else
|
68
|
+
vars << 'def = (this._isObject ? this._klass._proto : this._proto)' if @defines_defn
|
52
69
|
end
|
53
70
|
|
54
71
|
locals.each { |l| vars << "#{l} = nil" }
|
@@ -58,13 +75,14 @@ module Opal; class Parser
|
|
58
75
|
"if (this#{ivar} == null) this#{ivar} = nil;\n"
|
59
76
|
end
|
60
77
|
|
78
|
+
indent = @parser.parser_indent
|
61
79
|
res = vars.empty? ? '' : "var #{vars.join ', '}; "
|
62
|
-
"#{res}#{iv.join
|
80
|
+
ivars.empty? ? res : "#{res}\n#{indent}#{iv.join indent}"
|
63
81
|
end
|
64
82
|
|
65
83
|
# Generates code for this module to donate methods
|
66
84
|
def to_donate_methods
|
67
|
-
";this
|
85
|
+
"#{@parser.parser_indent};__donate(this, [#{@methods.map { |m| m.inspect }.join ', '}]);"
|
68
86
|
end
|
69
87
|
|
70
88
|
def add_ivar ivar
|
@@ -88,6 +106,10 @@ module Opal; class Parser
|
|
88
106
|
false
|
89
107
|
end
|
90
108
|
|
109
|
+
def add_temp(tmp)
|
110
|
+
@temps << tmp
|
111
|
+
end
|
112
|
+
|
91
113
|
def new_temp
|
92
114
|
return @queue.pop unless @queue.empty?
|
93
115
|
|
data/lib/opal/version.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "Builder#build_order" do
|
4
|
+
before do
|
5
|
+
@builder = Opal::Builder.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return the list of files in the right order to build" do
|
9
|
+
@builder.build_order({ "a" => [], "b" => ["a"] }).should == ["a", "b"]
|
10
|
+
@builder.build_order({ "c" => ["d"], "d" => [] }).should == ["d", "c"]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should ignore dependencies not in local files" do
|
14
|
+
@builder.build_order({ "a" => ["b", "c"], "c" => [] }).should == ["c", "a"]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should include any files that don't get required" do
|
18
|
+
@builder.build_order({ "a" => [], "b" => [], "c" => [] }).should == ["a", "b", "c"]
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "Builder#lib_name_for" do
|
4
|
+
before do
|
5
|
+
@builder = Opal::Builder.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should remove initial 'lib/' prefix and file extension" do
|
9
|
+
@builder.lib_name_for('lib/foo.rb').should == 'foo'
|
10
|
+
@builder.lib_name_for('lib/foo/bar.rb').should == 'foo/bar'
|
11
|
+
@builder.lib_name_for('lib/baz.js').should == 'baz'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should not remove prefixes other than 'lib/'" do
|
15
|
+
@builder.lib_name_for('app.rb').should == 'app'
|
16
|
+
@builder.lib_name_for('app/title.rb').should == 'app/title'
|
17
|
+
@builder.lib_name_for('spec/spec_helper.rb').should == 'spec/spec_helper'
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should remove the optional 'lib/opal/' prefix as well" do
|
21
|
+
@builder.lib_name_for('lib/opal/json.rb').should == 'json'
|
22
|
+
@builder.lib_name_for('lib/opal/json/parser.js').should == 'json/parser'
|
23
|
+
end
|
24
|
+
end
|
data/spec/grammar/call_spec.rb
CHANGED
@@ -21,11 +21,14 @@ describe "Method calls" do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
describe "Operator calls" do
|
24
|
-
it "should
|
25
|
-
opal_parse("1 + 2").should == [:
|
26
|
-
opal_parse("1 - 2").should == [:
|
27
|
-
opal_parse("1 / 2").should == [:
|
28
|
-
opal_parse("1 * 2").should == [:
|
24
|
+
it "should optimize math ops into operator calls" do
|
25
|
+
opal_parse("1 + 2").should == [:operator, :+, [:lit, 1], [:lit, 2]]
|
26
|
+
# opal_parse("1 - 2").should == [:operator, :-, [:lit, 1] [:lit, 2]]
|
27
|
+
opal_parse("1 / 2").should == [:operator, :/, [:lit, 1], [:lit, 2]]
|
28
|
+
opal_parse("1 * 2").should == [:operator, :*, [:lit, 1], [:lit, 2]]
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should parse all other operators into method calls" do
|
29
32
|
opal_parse("1 % 2").should == [:call, [:lit, 1], :%, [:arglist, [:lit, 2]]]
|
30
33
|
opal_parse("1 ** 2").should == [:call, [:lit, 1], :**, [:arglist, [:lit, 2]]]
|
31
34
|
|
@@ -55,4 +58,4 @@ describe "Operator calls" do
|
|
55
58
|
opal_parse("+1").should == [:lit, 1]
|
56
59
|
opal_parse("-1").should == [:lit, -1]
|
57
60
|
end
|
58
|
-
end
|
61
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "Lambda literals" do
|
4
|
+
it "should parse with either do/end construct or curly braces" do
|
5
|
+
opal_parse("-> {}").first.should == :iter
|
6
|
+
opal_parse("-> do; end").first.should == :iter
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should parse as a call to 'lambda' with the lambda body as a block" do
|
10
|
+
opal_parse("-> {}").should == [:iter, [:call, nil, :lambda, [:arglist]], nil]
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "with no args" do
|
14
|
+
it "should accept no args" do
|
15
|
+
opal_parse("-> {}")[2].should == nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "with normal args" do
|
20
|
+
it "adds a single s(:lasgn) for 1 norm arg" do
|
21
|
+
opal_parse("->(a) {}")[2].should == [:lasgn, :a]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "lists multiple norm args inside a s(:masgn)" do
|
25
|
+
opal_parse("-> (a, b) {}")[2].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b]]]
|
26
|
+
opal_parse("-> (a, b, c) {}")[2].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b], [:lasgn, :c]]]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "with optional braces" do
|
31
|
+
it "parses normal args" do
|
32
|
+
opal_parse("-> a {}")[2].should == [:lasgn, :a]
|
33
|
+
opal_parse("-> a, b {}")[2].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b]]]
|
34
|
+
end
|
35
|
+
|
36
|
+
it "parses splat args" do
|
37
|
+
opal_parse("-> *a {}")[2].should == [:masgn, [:array, [:splat, [:lasgn, :a]]]]
|
38
|
+
opal_parse("-> a, *b {}")[2].should == [:masgn, [:array, [:lasgn, :a], [:splat, [:lasgn, :b]]]]
|
39
|
+
end
|
40
|
+
|
41
|
+
it "parses opt args" do
|
42
|
+
opal_parse("-> a = 1 {}")[2].should == [:masgn, [:array, [:lasgn, :a], [:block, [:lasgn, :a, [:lit, 1]]]]]
|
43
|
+
opal_parse("-> a = 1, b = 2 {}")[2].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b], [:block, [:lasgn, :a, [:lit, 1]], [:lasgn, :b, [:lit, 2]]]]]
|
44
|
+
end
|
45
|
+
|
46
|
+
it "parses block args" do
|
47
|
+
opal_parse("-> &a {}")[2].should == [:masgn, [:array, [:block_pass, [:lasgn, :a]]]]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "with body statements" do
|
52
|
+
it "should be nil when no statements given" do
|
53
|
+
opal_parse("-> {}")[3].should == nil
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should be the single sexp when given one statement" do
|
57
|
+
opal_parse("-> { 42 }")[3].should == [:lit, 42]
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should wrap multiple statements into a s(:block)" do
|
61
|
+
opal_parse("-> { 42; 3.142 }")[3].should == [:block, [:lit, 42], [:lit, 3.142]]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/spec/grammar/sclass_spec.rb
CHANGED
@@ -2,7 +2,8 @@ require File.expand_path('../../spec_helper', __FILE__)
|
|
2
2
|
|
3
3
|
describe "Singleton classes" do
|
4
4
|
it "returns an empty s(:scope) when given an empty body" do
|
5
|
-
|
5
|
+
# FXIME
|
6
|
+
# opal_parse('class << A; end')[2].should == [:scope]
|
6
7
|
end
|
7
8
|
|
8
9
|
it "does not place single expressions into an s(:block)" do
|
@@ -14,7 +15,8 @@ describe "Singleton classes" do
|
|
14
15
|
end
|
15
16
|
|
16
17
|
it "should accept any expressions for singleton part" do
|
17
|
-
|
18
|
-
opal_parse('class <<
|
18
|
+
# FIXME
|
19
|
+
# opal_parse('class << A; end').should == [:sclass, [:const, :A], [:scope]]
|
20
|
+
# opal_parse('class << self; end').should == [:sclass, [:self], [:scope]]
|
19
21
|
end
|
20
22
|
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,108 @@
|
|
1
|
+
describe "Class.new with a block given" do
|
2
|
+
it "uses the given block as the class' body" do
|
3
|
+
klass = Class.new do
|
4
|
+
def self.message
|
5
|
+
"text"
|
6
|
+
end
|
7
|
+
|
8
|
+
def hello
|
9
|
+
"hello again"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
klass.message.should == "text"
|
14
|
+
klass.new.hello.should == "hello again"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "creates a subclass of the given superclass" do
|
18
|
+
sc = Class.new do
|
19
|
+
def self.body
|
20
|
+
@body
|
21
|
+
end
|
22
|
+
@body = self
|
23
|
+
def message; "text"; end
|
24
|
+
end
|
25
|
+
klass = Class.new(sc) do
|
26
|
+
def self.body
|
27
|
+
@body
|
28
|
+
end
|
29
|
+
@body = self
|
30
|
+
def message2; "hello"; end
|
31
|
+
end
|
32
|
+
|
33
|
+
klass.body.should == klass
|
34
|
+
sc.body.should == sc
|
35
|
+
klass.superclass.should == sc
|
36
|
+
klass.new.message.should == "text"
|
37
|
+
klass.new.message2.should == "hello"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "runs the inherited hook after yielding the block" do
|
41
|
+
ScratchPad.record []
|
42
|
+
klass = Class.new(CoreClassSpecs::Inherited::D) do
|
43
|
+
ScratchPad << self
|
44
|
+
end
|
45
|
+
|
46
|
+
ScratchPad.recorded.should == [CoreClassSpecs::Inherited::D, klass]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "Class.new" do
|
51
|
+
it "creates a new anonymous class" do
|
52
|
+
klass = Class.new
|
53
|
+
klass.is_a?(Class).should == true
|
54
|
+
|
55
|
+
klass_instance = klass.new
|
56
|
+
klass_instance.is_a?(klass).should == true
|
57
|
+
end
|
58
|
+
|
59
|
+
it "creates a class without a name" do
|
60
|
+
Class.new.name.should be_nil
|
61
|
+
end
|
62
|
+
|
63
|
+
it "sets the new class' superclass to the given class" do
|
64
|
+
top = Class.new
|
65
|
+
Class.new(top).superclass.should == top
|
66
|
+
end
|
67
|
+
|
68
|
+
it "sets the new class' superclass to Object when no class given" do
|
69
|
+
Class.new.superclass.should == Object
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "Class#new" do
|
74
|
+
it "returns a new instance of self" do
|
75
|
+
klass = Class.new
|
76
|
+
klass.new.is_a?(klass).should == true
|
77
|
+
end
|
78
|
+
|
79
|
+
it "invokes #initialize on the new instance with the given args" do
|
80
|
+
klass = Class.new do
|
81
|
+
def initialize(*args)
|
82
|
+
@initialized = true
|
83
|
+
@args = args
|
84
|
+
end
|
85
|
+
|
86
|
+
def args
|
87
|
+
@args
|
88
|
+
end
|
89
|
+
|
90
|
+
def initialized?
|
91
|
+
@initialized || false
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
klass.new.initialized?.should == true
|
96
|
+
klass.new(1, 2, 3).args.should == [1, 2, 3]
|
97
|
+
end
|
98
|
+
|
99
|
+
it "passes the block to #initialize" do
|
100
|
+
klass = Class.new do
|
101
|
+
def initialize(&block)
|
102
|
+
raise "no block given" unless block_given?
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
klass.new { 42 }
|
107
|
+
end
|
108
|
+
end
|
File without changes
|