opal 0.3.16 → 0.3.17
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile +8 -7
- data/README.md +21 -13
- data/Rakefile +64 -78
- data/bin/opal +18 -29
- data/core/alpha.rb +2 -9
- data/core/array.rb +106 -50
- data/core/basic_object.rb +10 -8
- data/core/boolean.rb +2 -0
- data/core/class.rb +25 -13
- data/core/comparable.rb +6 -6
- data/core/enumerable.rb +90 -94
- data/core/enumerator.rb +3 -3
- data/core/hash.rb +86 -46
- data/core/kernel.rb +55 -39
- data/core/load_order +2 -5
- data/core/match_data.rb +1 -1
- data/core/module.rb +45 -20
- data/core/nil_class.rb +6 -2
- data/core/numeric.rb +20 -10
- data/core/proc.rb +2 -2
- data/core/range.rb +72 -13
- data/core/regexp.rb +5 -3
- data/core/runtime.js +228 -287
- data/core/string.rb +345 -37
- data/core/top_self.rb +1 -1
- data/lib/opal.rb +13 -91
- data/lib/opal/builder.rb +47 -120
- data/lib/opal/builder_task.rb +74 -0
- data/lib/opal/{parser/grammar.rb → grammar.rb} +1094 -1083
- data/lib/opal/{parser/grammar.y → grammar.y} +7 -0
- data/lib/opal/{parser/lexer.rb → lexer.rb} +32 -11
- data/lib/opal/{parser/parser.rb → parser.rb} +232 -238
- data/lib/opal/{parser/scope.rb → scope.rb} +72 -9
- data/lib/opal/version.rb +2 -3
- data/opal.gemspec +1 -2
- data/{core_spec → spec}/core/array/allocate_spec.rb +1 -3
- data/{core_spec → spec}/core/array/append_spec.rb +1 -4
- data/{core_spec → spec}/core/array/assoc_spec.rb +1 -4
- data/{core_spec → spec}/core/array/at_spec.rb +1 -3
- data/{core_spec → spec}/core/array/clear_spec.rb +1 -3
- data/spec/core/array/clone_spec.rb +15 -0
- data/{core_spec/core/array/shared/collect.rb → spec/core/array/collect_spec.rb} +1 -1
- data/{core_spec → spec}/core/array/compact_spec.rb +1 -3
- data/{core_spec → spec}/core/array/concat_spec.rb +1 -3
- data/{core_spec → spec}/core/array/constructor_spec.rb +9 -3
- data/{core_spec → spec}/core/array/count_spec.rb +1 -3
- data/{core_spec → spec}/core/array/delete_at_spec.rb +1 -4
- data/{core_spec → spec}/core/array/delete_if_spec.rb +1 -4
- data/{core_spec → spec}/core/array/delete_spec.rb +1 -4
- data/{core_spec → spec}/core/array/each_index_spec.rb +1 -4
- data/{core_spec → spec}/core/array/each_spec.rb +1 -4
- data/{core_spec → spec}/core/array/element_reference_spec.rb +74 -4
- data/{core_spec → spec}/core/array/empty_spec.rb +1 -4
- data/{core_spec/core/array/shared/eql.rb → spec/core/array/eql_spec.rb} +1 -1
- data/{core_spec → spec}/core/array/fetch_spec.rb +1 -4
- data/{core_spec → spec}/core/array/first_spec.rb +1 -3
- data/{core_spec → spec}/core/array/flatten_spec.rb +1 -3
- data/{core_spec → spec}/core/array/include_spec.rb +1 -3
- data/{core_spec → spec}/core/array/insert_spec.rb +1 -4
- data/{core_spec → spec}/core/array/last_spec.rb +1 -4
- data/{core_spec/core/array/shared/length.rb → spec/core/array/length_spec.rb} +1 -1
- data/spec/core/array/map_spec.rb +53 -0
- data/{core_spec → spec}/core/array/plus_spec.rb +1 -4
- data/{core_spec → spec}/core/array/pop_spec.rb +1 -4
- data/{core_spec → spec}/core/array/push_spec.rb +1 -4
- data/{core_spec → spec}/core/array/rassoc_spec.rb +1 -4
- data/{core_spec → spec}/core/array/reject_spec.rb +1 -4
- data/{core_spec/core/array/shared/replace.rb → spec/core/array/replace_spec.rb} +1 -1
- data/{core_spec → spec}/core/array/reverse_each_spec.rb +1 -4
- data/{core_spec → spec}/core/array/reverse_spec.rb +1 -4
- data/spec/core/array/size_spec.rb +6 -0
- data/spec/core/array/to_ary_spec.rb +6 -0
- data/spec/core/array/uniq_spec.rb +22 -0
- data/spec/core/array/zip_spec.rb +20 -0
- data/{core_spec → spec}/core/class/new_spec.rb +1 -4
- data/{core_spec → spec}/core/enumerable/all_spec.rb +1 -4
- data/{core_spec → spec}/core/enumerable/any_spec.rb +1 -4
- data/{core_spec/core/enumerable/shared/collect.rb → spec/core/enumerable/collect_spec.rb} +1 -1
- data/{core_spec → spec}/core/enumerable/count_spec.rb +2 -5
- data/{core_spec → spec}/core/enumerable/fixtures/classes.rb +1 -2
- data/{core_spec → spec}/core/false/and_spec.rb +1 -3
- data/{core_spec → spec}/core/false/inspect_spec.rb +1 -3
- data/{core_spec → spec}/core/false/or_spec.rb +1 -3
- data/{core_spec → spec}/core/false/to_s_spec.rb +1 -3
- data/{core_spec → spec}/core/false/xor_spec.rb +1 -3
- data/{core_spec → spec}/core/hash/allocate_spec.rb +1 -3
- data/spec/core/hash/assoc_spec.rb +25 -0
- data/{core_spec → spec}/core/hash/clear_spec.rb +1 -3
- data/{core_spec → spec}/core/hash/clone_spec.rb +1 -3
- data/{core_spec → spec}/core/hash/default_spec.rb +1 -3
- data/{core_spec → spec}/core/hash/delete_if_spec.rb +1 -3
- data/{core_spec → spec}/core/hash/element_reference_spec.rb +1 -3
- data/{core_spec → spec}/core/hash/element_set_spec.rb +1 -3
- data/spec/core/hash/merge_spec.rb +37 -0
- data/{core_spec → spec}/core/hash/new_spec.rb +1 -3
- data/{core_spec → spec}/core/matchdata/to_a_spec.rb +1 -3
- data/{core_spec → spec}/core/nil/and_spec.rb +1 -4
- data/{core_spec → spec}/core/nil/inspect_spec.rb +1 -4
- data/{core_spec → spec}/core/nil/nil_spec.rb +1 -4
- data/{core_spec → spec}/core/nil/or_spec.rb +1 -4
- data/{core_spec → spec}/core/nil/to_a_spec.rb +1 -4
- data/{core_spec → spec}/core/nil/to_f_spec.rb +1 -4
- data/{core_spec → spec}/core/nil/to_i_spec.rb +1 -4
- data/{core_spec → spec}/core/nil/to_s_spec.rb +1 -4
- data/{core_spec → spec}/core/nil/xor_spec.rb +1 -4
- data/{core_spec → spec}/core/numeric/equal_value_spec.rb +1 -3
- data/{core_spec → spec}/core/regexp/match_spec.rb +10 -3
- data/{core_spec → spec}/core/symbol/to_proc_spec.rb +1 -3
- data/{core_spec → spec}/core/true/and_spec.rb +1 -3
- data/{core_spec → spec}/core/true/inspect_spec.rb +1 -3
- data/{core_spec → spec}/core/true/or_spec.rb +1 -3
- data/{core_spec → spec}/core/true/to_s_spec.rb +1 -3
- data/{core_spec → spec}/core/true/xor_spec.rb +1 -3
- data/spec/index.html +11 -0
- data/{core_spec → spec}/language/alias_spec.rb +1 -3
- data/{core_spec → spec}/language/and_spec.rb +1 -4
- data/{core_spec → spec}/language/array_spec.rb +1 -4
- data/{core_spec → spec}/language/block_spec.rb +20 -3
- data/{core_spec → spec}/language/break_spec.rb +40 -3
- data/{core_spec → spec}/language/case_spec.rb +1 -4
- data/{core_spec → spec}/language/defined_spec.rb +9 -3
- data/{core_spec → spec}/language/ensure_spec.rb +38 -3
- data/{core_spec → spec}/language/hash_spec.rb +1 -3
- data/{core_spec → spec}/language/if_spec.rb +1 -4
- data/{core_spec → spec}/language/loop_spec.rb +1 -3
- data/spec/language/metaclass_spec.rb +13 -0
- data/{core_spec → spec}/language/next_spec.rb +47 -3
- data/{core_spec → spec}/language/or_spec.rb +1 -4
- data/{core_spec → spec}/language/predefined_spec.rb +1 -3
- data/{core_spec/language/regexp/interpolation_spec.rb → spec/language/regexp_spec.rb} +6 -3
- data/{core_spec → spec}/language/send_spec.rb +38 -4
- data/spec/language/singleton_class_spec.rb +31 -0
- data/spec/language/super_spec.rb +188 -0
- data/{core_spec → spec}/language/symbol_spec.rb +1 -3
- data/{core_spec → spec}/language/undef_spec.rb +1 -3
- data/{core_spec → spec}/language/unless_spec.rb +1 -4
- data/{core_spec → spec}/language/until_spec.rb +1 -3
- data/{core_spec → spec}/language/variables_spec.rb +1 -3
- data/{core_spec → spec}/language/while_spec.rb +1 -3
- data/{spec → test}/builder/build_source_spec.rb +0 -0
- data/{spec → test}/builder/fixtures/build_source/adam.rb +0 -0
- data/{spec → test}/builder/fixtures/build_source/bar/a.rb +0 -0
- data/{spec → test}/builder/fixtures/build_source/bar/wow/b.rb +0 -0
- data/{spec → test}/builder/fixtures/build_source/bar/wow/cow/c.rb +0 -0
- data/{spec → test}/builder/fixtures/build_source/beynon.rb +0 -0
- data/{spec → test}/builder/fixtures/build_source/charles.js +0 -0
- data/{spec → test}/builder/fixtures/build_source/foo/a.rb +0 -0
- data/{spec → test}/builder/fixtures/build_source/foo/b.rb +0 -0
- data/{spec → test}/builder/fixtures/build_source/foo/x.js +0 -0
- data/{spec → test}/builder/fixtures/build_source/foo/y.js +0 -0
- data/{spec → test}/grammar/alias_spec.rb +0 -0
- data/{spec → test}/grammar/and_spec.rb +0 -0
- data/{spec → test}/grammar/array_spec.rb +0 -0
- data/{spec → test}/grammar/attrasgn_spec.rb +0 -0
- data/{spec → test}/grammar/begin_spec.rb +0 -0
- data/{spec → test}/grammar/block_spec.rb +0 -0
- data/{spec → test}/grammar/break_spec.rb +0 -0
- data/{spec → test}/grammar/call_spec.rb +0 -0
- data/{spec → test}/grammar/class_spec.rb +0 -0
- data/{spec → test}/grammar/const_spec.rb +0 -0
- data/{spec → test}/grammar/cvar_spec.rb +0 -0
- data/{spec → test}/grammar/def_spec.rb +0 -0
- data/{spec → test}/grammar/false_spec.rb +0 -0
- data/{spec → test}/grammar/file_spec.rb +0 -0
- data/{spec → test}/grammar/gvar_spec.rb +0 -0
- data/{spec → test}/grammar/hash_spec.rb +0 -0
- data/{spec → test}/grammar/iasgn_spec.rb +0 -0
- data/{spec → test}/grammar/if_spec.rb +0 -0
- data/{spec → test}/grammar/iter_spec.rb +0 -0
- data/{spec → test}/grammar/ivar_spec.rb +0 -0
- data/{spec → test}/grammar/lasgn_spec.rb +0 -0
- data/{spec → test}/grammar/line_spec.rb +0 -0
- data/{spec → test}/grammar/lvar_spec.rb +0 -0
- data/{spec → test}/grammar/masgn_spec.rb +0 -0
- data/{spec → test}/grammar/module_spec.rb +0 -0
- data/{spec → test}/grammar/nil_spec.rb +0 -0
- data/{spec → test}/grammar/not_spec.rb +0 -0
- data/{spec → test}/grammar/op_asgn1_spec.rb +0 -0
- data/{spec → test}/grammar/op_asgn2_spec.rb +0 -0
- data/{spec → test}/grammar/or_spec.rb +0 -0
- data/{spec → test}/grammar/return_spec.rb +0 -0
- data/{spec → test}/grammar/sclass_spec.rb +0 -0
- data/{spec → test}/grammar/self_spec.rb +0 -0
- data/{spec → test}/grammar/str_spec.rb +0 -0
- data/{spec → test}/grammar/super_spec.rb +0 -0
- data/{spec → test}/grammar/true_spec.rb +0 -0
- data/{spec → test}/grammar/undef_spec.rb +0 -0
- data/{spec → test}/grammar/unless_spec.rb +0 -0
- data/{spec → test}/grammar/while_spec.rb +0 -0
- data/{spec → test}/grammar/xstr_spec.rb +0 -0
- data/{spec → test}/grammar/yield_spec.rb +0 -0
- data/{spec → test}/spec_helper.rb +0 -0
- metadata +330 -264
- data/core/debug.js +0 -59
- data/core/debug.rb +0 -35
- data/core/dir.rb +0 -90
- data/core/file.rb +0 -83
- data/core/gemlib.rb +0 -30
- data/core/io.rb +0 -44
- data/core_spec/README.md +0 -34
- data/core_spec/core/array/collect_spec.rb +0 -3
- data/core_spec/core/array/element_set_spec.rb +0 -7
- data/core_spec/core/array/eql_spec.rb +0 -3
- data/core_spec/core/array/equal_value_spec.rb +0 -3
- data/core_spec/core/array/fixtures/classes.rb +0 -8
- data/core_spec/core/array/length_spec.rb +0 -3
- data/core_spec/core/array/map_spec.rb +0 -3
- data/core_spec/core/array/replace_spec.rb +0 -3
- data/core_spec/core/enumerable/collect_spec.rb +0 -3
- data/core_spec/core/enumerable/detect_spec.rb +0 -3
- data/core_spec/core/enumerable/find_spec.rb +0 -3
- data/core_spec/core/enumerable/first_spec.rb +0 -3
- data/core_spec/core/enumerable/shared/entries.rb +0 -7
- data/core_spec/core/enumerable/shared/find.rb +0 -49
- data/core_spec/core/enumerable/shared/take.rb +0 -31
- data/core_spec/core/enumerable/to_a_spec.rb +0 -7
- data/core_spec/core/hash/assoc_spec.rb +0 -29
- data/core_spec/core/object/is_a_spec.rb +0 -2
- data/core_spec/core/object/shared/kind_of.rb +0 -0
- data/core_spec/core/regexp/shared/match.rb +0 -11
- data/core_spec/language/fixtures/block.rb +0 -19
- data/core_spec/language/fixtures/break.rb +0 -39
- data/core_spec/language/fixtures/defined.rb +0 -9
- data/core_spec/language/fixtures/ensure.rb +0 -37
- data/core_spec/language/fixtures/next.rb +0 -46
- data/core_spec/language/fixtures/send.rb +0 -36
- data/core_spec/language/fixtures/super.rb +0 -43
- data/core_spec/language/regexp_spec.rb +0 -7
- data/core_spec/language/string_spec.rb +0 -4
- data/core_spec/language/super_spec.rb +0 -18
- data/core_spec/language/versions/hash_1.9.rb +0 -20
- data/core_spec/opal/opal/defined_spec.rb +0 -15
- data/core_spec/opal/opal/function_spec.rb +0 -11
- data/core_spec/opal/opal/native_spec.rb +0 -16
- data/core_spec/opal/opal/null_spec.rb +0 -10
- data/core_spec/opal/opal/number_spec.rb +0 -11
- data/core_spec/opal/opal/object_spec.rb +0 -16
- data/core_spec/opal/opal/string_spec.rb +0 -11
- data/core_spec/opal/opal/typeof_spec.rb +0 -9
- data/core_spec/opal/opal/undefined_spec.rb +0 -10
- data/core_spec/opal/true/case_compare_spec.rb +0 -12
- data/core_spec/opal/true/class_spec.rb +0 -10
- data/core_spec/release_runner.html +0 -17
- data/core_spec/runner.html +0 -16
- data/core_spec/spec_helper.rb +0 -23
- data/lib/opal/context.rb +0 -269
- data/lib/opal/dependency_builder.rb +0 -133
- data/lib/opal/environment.rb +0 -87
- data/lib/opal/parser/sexp.rb +0 -17
@@ -6,9 +6,11 @@ module Opal; class Parser
|
|
6
6
|
|
7
7
|
attr_accessor :name
|
8
8
|
|
9
|
+
attr_accessor :block_name
|
10
|
+
|
9
11
|
attr_reader :scope_name
|
10
12
|
attr_reader :ivars
|
11
|
-
|
13
|
+
|
12
14
|
attr_accessor :donates_methods
|
13
15
|
|
14
16
|
attr_reader :type
|
@@ -16,7 +18,8 @@ module Opal; class Parser
|
|
16
18
|
# used by modules to know what methods to donate to includees
|
17
19
|
attr_reader :methods
|
18
20
|
|
19
|
-
def initialize(type)
|
21
|
+
def initialize(type, parser)
|
22
|
+
@parser = parser
|
20
23
|
@type = type
|
21
24
|
@locals = []
|
22
25
|
@temps = []
|
@@ -39,20 +42,20 @@ module Opal; class Parser
|
|
39
42
|
vars = []
|
40
43
|
|
41
44
|
if @type == :class
|
42
|
-
vars << '
|
43
|
-
vars << 'def = this
|
45
|
+
vars << '__scope = this._scope'
|
46
|
+
vars << 'def = this._proto'
|
44
47
|
elsif @type == :module
|
45
|
-
vars << '
|
46
|
-
vars << 'def = this
|
48
|
+
vars << '__scope = this._scope'
|
49
|
+
vars << 'def = this._proto'
|
47
50
|
elsif @type == :sclass
|
48
|
-
vars << '
|
51
|
+
vars << '__scope = this._scope'
|
49
52
|
end
|
50
53
|
|
51
54
|
locals.each { |l| vars << "#{l} = nil" }
|
52
55
|
temps.each { |t| vars << t }
|
53
56
|
|
54
57
|
iv = ivars.map do |ivar|
|
55
|
-
|
58
|
+
"if (this#{ivar} == null) this#{ivar} = nil;\n"
|
56
59
|
end
|
57
60
|
|
58
61
|
res = vars.empty? ? '' : "var #{vars.join ', '}; "
|
@@ -88,7 +91,7 @@ module Opal; class Parser
|
|
88
91
|
def new_temp
|
89
92
|
return @queue.pop unless @queue.empty?
|
90
93
|
|
91
|
-
tmp = "
|
94
|
+
tmp = "__#{@unique}"
|
92
95
|
@unique = @unique.succ
|
93
96
|
@temps << tmp
|
94
97
|
tmp
|
@@ -117,6 +120,48 @@ module Opal; class Parser
|
|
117
120
|
@parent.uses_block!
|
118
121
|
else
|
119
122
|
@uses_block = true
|
123
|
+
identify!
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def identify!
|
128
|
+
@identity ||= @parser.unique_temp
|
129
|
+
end
|
130
|
+
|
131
|
+
def identity
|
132
|
+
@identity
|
133
|
+
end
|
134
|
+
|
135
|
+
def get_super_chain
|
136
|
+
chain = []
|
137
|
+
scope = self
|
138
|
+
defn = 'null'
|
139
|
+
mid = 'null'
|
140
|
+
|
141
|
+
while scope
|
142
|
+
if scope.type == :iter
|
143
|
+
chain << scope.identify!
|
144
|
+
scope = scope.parent if scope.parent
|
145
|
+
|
146
|
+
elsif scope.type == :def
|
147
|
+
defn = scope.identify!
|
148
|
+
mid = "'#{scope.mid}'"
|
149
|
+
break
|
150
|
+
|
151
|
+
else
|
152
|
+
break
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
[chain, defn, mid]
|
157
|
+
end
|
158
|
+
|
159
|
+
def identify_def
|
160
|
+
if @type == :iter && @parent
|
161
|
+
identify!
|
162
|
+
@parent.identify_def
|
163
|
+
elsif @type == :def
|
164
|
+
identify!
|
120
165
|
end
|
121
166
|
end
|
122
167
|
|
@@ -135,6 +180,24 @@ module Opal; class Parser
|
|
135
180
|
def catches_break?
|
136
181
|
@catches_break
|
137
182
|
end
|
183
|
+
|
184
|
+
def mid=(mid)
|
185
|
+
@mid = mid
|
186
|
+
end
|
187
|
+
|
188
|
+
# Gets the method id (as a jsid) of the current scope, which is assumed
|
189
|
+
# to be a method (:def). If not, and this scope is a :iter, then the
|
190
|
+
# parent scope will be checked. As a fallback, nil is returned. This is
|
191
|
+
# used by super() to find out what method super() should be sent to.
|
192
|
+
def mid
|
193
|
+
if @type == :def
|
194
|
+
@mid
|
195
|
+
elsif @type == :iter && @parent
|
196
|
+
@parent.mid
|
197
|
+
else
|
198
|
+
nil
|
199
|
+
end
|
200
|
+
end
|
138
201
|
end
|
139
202
|
|
140
203
|
end; end
|
data/lib/opal/version.rb
CHANGED
data/opal.gemspec
CHANGED
@@ -11,8 +11,7 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.description = 'Ruby runtime and core library for javascript.'
|
12
12
|
|
13
13
|
s.files = `git ls-files`.split("\n")
|
14
|
-
s.files += %w[opal.js opal.debug.js]
|
15
14
|
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
16
15
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
16
|
s.require_paths = ['lib']
|
18
|
-
end
|
17
|
+
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
|
3
1
|
describe "Array#<<" do
|
4
2
|
it "pushes the object onto the end of the array" do
|
5
3
|
([ 1, 2 ] << "c" << "d" << [ 3, 4 ]).should == [1, 2, "c", "d", [3, 4]]
|
@@ -27,5 +25,4 @@ describe "Array#<<" do
|
|
27
25
|
a << :foo
|
28
26
|
a.should == [:foo]
|
29
27
|
end
|
30
|
-
end
|
31
|
-
|
28
|
+
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
|
3
1
|
describe "Array#assoc" do
|
4
2
|
it "returns the first array whose 1st item is == obj or nil" do
|
5
3
|
s1 = ["colors", "red", "blue", "green"]
|
@@ -25,5 +23,4 @@ describe "Array#assoc" do
|
|
25
23
|
a.assoc(s1.first).should == s1
|
26
24
|
a.assoc(s2.first).should == s2
|
27
25
|
end
|
28
|
-
end
|
29
|
-
|
26
|
+
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
|
3
1
|
describe "Array#at" do
|
4
2
|
it "returns the (n+1)'th element for the passed index n" do
|
5
3
|
a = [1, 2, 3, 4, 5, 6]
|
@@ -35,4 +33,4 @@ describe "Array#at" do
|
|
35
33
|
a.at(-100)
|
36
34
|
a.length.should == 6
|
37
35
|
end
|
38
|
-
end
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
describe "Array#clone" do
|
2
|
+
it "produces a shallow copy where the references are directly copied" do
|
3
|
+
a = [mock('1'), mock('2')]
|
4
|
+
b = a.clone
|
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.clone
|
12
|
+
b.should == a
|
13
|
+
b.object_id.should_not == a.object_id
|
14
|
+
end
|
15
|
+
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
|
3
1
|
describe "Array#compact" do
|
4
2
|
it "returns a copy of array with all nil elements removed" do
|
5
3
|
a = [1, 2, 4]
|
@@ -39,4 +37,4 @@ describe "Array#compact!" do
|
|
39
37
|
it "returns nil if there are no nil elements to remove" do
|
40
38
|
[1, 2, false, 3].compact!.should == nil
|
41
39
|
end
|
42
|
-
end
|
40
|
+
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
|
3
1
|
describe "Array#concat" do
|
4
2
|
it "returns the array itself" do
|
5
3
|
ary = [1,2,3]
|
@@ -18,4 +16,4 @@ describe "Array#concat" do
|
|
18
16
|
ary = ["x", "y"]
|
19
17
|
ary.concat(ary).should == ["x", "y", "x", "y"]
|
20
18
|
end
|
21
|
-
end
|
19
|
+
end
|
@@ -1,5 +1,11 @@
|
|
1
|
-
|
2
|
-
|
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
|
3
9
|
|
4
10
|
describe "Array.[]" do
|
5
11
|
it "returns a new array populated with the given elements" do
|
@@ -21,4 +27,4 @@ describe "Array[]" do
|
|
21
27
|
a.should be_kind_of(ArraySpecs::MyArray)
|
22
28
|
a.inspect.should == [5, true, nil, "a", "Ruby", obj].inspect
|
23
29
|
end
|
24
|
-
end
|
30
|
+
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
|
3
1
|
describe "Array#count" do
|
4
2
|
it "returns the count of elements" do
|
5
3
|
[1, :two, 'three'].count.should == 3
|
@@ -8,4 +6,4 @@ describe "Array#count" do
|
|
8
6
|
it "returns count of elements that equals given object" do
|
9
7
|
[1, 'some text', 'other text', 2, 1].count(1).should == 2
|
10
8
|
end
|
11
|
-
end
|
9
|
+
end
|
@@ -1,6 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
-
|
4
1
|
describe "Array#delete_at" do
|
5
2
|
it "removes the element at the specified index" do
|
6
3
|
a = [1, 2, 3, 4]
|
@@ -28,4 +25,4 @@ describe "Array#delete_at" do
|
|
28
25
|
a = [1, 2]
|
29
26
|
a.delete_at(-2).should == 1
|
30
27
|
end
|
31
|
-
end
|
28
|
+
end
|
@@ -1,6 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
-
|
4
1
|
describe "Array#delete_if" do
|
5
2
|
before do
|
6
3
|
@a = [ "a", "b", "c" ]
|
@@ -21,4 +18,4 @@ describe "Array#delete_if" do
|
|
21
18
|
array.shift
|
22
19
|
array.delete_if { |x| true }.should equal(array)
|
23
20
|
end
|
24
|
-
end
|
21
|
+
end
|
@@ -1,6 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
-
|
4
1
|
describe "Array#delete" do
|
5
2
|
it "removes elements that are #== to object" do
|
6
3
|
x = mock('delete')
|
@@ -23,4 +20,4 @@ describe "Array#delete" do
|
|
23
20
|
[1, 2, 4, 5].delete(1).should == 1
|
24
21
|
[1, 2, 4, 5].delete(3).should == nil
|
25
22
|
end
|
26
|
-
end
|
23
|
+
end
|
@@ -1,6 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
-
|
4
1
|
describe "Array#each_index" do
|
5
2
|
before :each do
|
6
3
|
ScratchPad.record []
|
@@ -30,4 +27,4 @@ describe "Array#each_index" do
|
|
30
27
|
a.each_index { |i| ScratchPad << i }
|
31
28
|
ScratchPad.recorded.should == [0]
|
32
29
|
end
|
33
|
-
end
|
30
|
+
end
|
@@ -1,6 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
-
|
4
1
|
describe "Array#each" do
|
5
2
|
it "yields each element to the block" do
|
6
3
|
a = []
|
@@ -8,4 +5,4 @@ describe "Array#each" do
|
|
8
5
|
x.each { |item| a << item }.should equal(x)
|
9
6
|
a.should == [1, 2, 3]
|
10
7
|
end
|
11
|
-
end
|
8
|
+
end
|
@@ -1,6 +1,3 @@
|
|
1
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
-
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
-
|
4
1
|
describe "Array#[]" do
|
5
2
|
it "returns the element at index with [index]" do
|
6
3
|
[ "a", "b", "c", "d", "e" ][1].should == "b"
|
@@ -106,6 +103,79 @@ describe "Array#[]" do
|
|
106
103
|
a.should == [1, 2, 3]
|
107
104
|
end
|
108
105
|
|
106
|
+
it "returns the elements specified by Range indexes with [m..n]" do
|
107
|
+
[ "a", "b", "c", "d", "e" ][1..3].should == ["b", "c", "d"]
|
108
|
+
[ "a", "b", "c", "d", "e" ][4..-1].should == ['e']
|
109
|
+
[ "a", "b", "c", "d", "e" ][3..3].should == ['d']
|
110
|
+
[ "a", "b", "c", "d", "e" ][3..-2].should == ['d']
|
111
|
+
['a'][0..-1].should == ['a']
|
112
|
+
|
113
|
+
|
114
|
+
a = [1, 2, 3, 4]
|
115
|
+
|
116
|
+
a[0..-10].should == []
|
117
|
+
a[0..0].should == [1]
|
118
|
+
a[0..1].should == [1, 2]
|
119
|
+
a[0..2].should == [1, 2, 3]
|
120
|
+
a[0..3].should == [1, 2, 3, 4]
|
121
|
+
a[0..4].should == [1, 2, 3, 4]
|
122
|
+
a[0..10].should == [1, 2, 3, 4]
|
123
|
+
|
124
|
+
a[2..-10].should == []
|
125
|
+
a[2..0].should == []
|
126
|
+
a[2..2].should == [3]
|
127
|
+
a[2..3].should == [3, 4]
|
128
|
+
a[2..4].should == [3, 4]
|
129
|
+
|
130
|
+
a[3..0].should == []
|
131
|
+
a[3..3].should == [4]
|
132
|
+
a[3..4].should == [4]
|
133
|
+
|
134
|
+
a[4..0].should == []
|
135
|
+
a[4..4].should == []
|
136
|
+
a[4..5].should == []
|
137
|
+
|
138
|
+
a[5..0].should == nil
|
139
|
+
a[5..5].should == nil
|
140
|
+
a[5..6].should == nil
|
141
|
+
|
142
|
+
a.should == [1, 2, 3, 4]
|
143
|
+
end
|
144
|
+
|
145
|
+
it "returns elements specified by Range indexes except the lement at index n with [m...n]" do
|
146
|
+
[ "a", "b", "c", "d", "e" ][1...3].should == ["b", "c"]
|
147
|
+
|
148
|
+
a = [1, 2, 3, 4]
|
149
|
+
|
150
|
+
a[0...-10].should == []
|
151
|
+
a[0...0].should == []
|
152
|
+
a[0...1].should == [1]
|
153
|
+
a[0...2].should == [1, 2]
|
154
|
+
a[0...3].should == [1, 2, 3]
|
155
|
+
a[0...4].should == [1, 2, 3, 4]
|
156
|
+
a[0...10].should == [1, 2, 3, 4]
|
157
|
+
|
158
|
+
a[2...-10].should == []
|
159
|
+
a[2...0].should == []
|
160
|
+
a[2...2].should == []
|
161
|
+
a[2...3].should == [3]
|
162
|
+
a[2...4].should == [3, 4]
|
163
|
+
|
164
|
+
a[3...0].should == []
|
165
|
+
a[3...3].should == []
|
166
|
+
a[3...4].should == [4]
|
167
|
+
|
168
|
+
a[4...0].should == []
|
169
|
+
a[4...4].should == []
|
170
|
+
a[4...5].should == []
|
171
|
+
|
172
|
+
a[5...0].should == nil
|
173
|
+
a[5...5].should == nil
|
174
|
+
a[5...6].should == nil
|
175
|
+
|
176
|
+
a.should == [1, 2, 3, 4]
|
177
|
+
end
|
178
|
+
|
109
179
|
it "returns nil for a requested index not in the array with [index]" do
|
110
180
|
[ "a", "b", "c", "d", "e" ][5].should == nil
|
111
181
|
end
|
@@ -133,4 +203,4 @@ describe "Array#[]" do
|
|
133
203
|
%w|a b c d e|[2, -2].should == nil
|
134
204
|
%w|a b c d e|[1, -100].should == nil
|
135
205
|
end
|
136
|
-
end
|
206
|
+
end
|