opal 0.3.11 → 0.3.15
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.
- data/.gitignore +13 -0
- data/Gemfile +10 -0
- data/LICENSE +20 -0
- data/README.md +11 -116
- data/Rakefile +126 -0
- data/bin/opal +1 -2
- data/docs/spec_runner.html +16 -0
- data/index.html +434 -0
- data/lib/opal.rb +14 -15
- data/lib/opal/builder.rb +46 -148
- data/lib/opal/command.rb +45 -115
- data/lib/opal/context.rb +139 -78
- data/lib/opal/dependency_builder.rb +34 -0
- data/lib/opal/environment.rb +92 -0
- data/lib/opal/parser/grammar.rb +4915 -0
- data/lib/opal/{parser.y → parser/grammar.y} +430 -284
- data/lib/opal/parser/lexer.rb +1329 -0
- data/lib/opal/parser/parser.rb +1460 -0
- data/lib/opal/parser/scope.rb +140 -0
- data/lib/opal/parser/sexp.rb +17 -0
- data/lib/opal/version.rb +2 -1
- data/opal.gemspec +23 -0
- data/opal.js +3149 -4162
- data/runtime/README.md +25 -0
- data/runtime/corelib/alpha.rb +10 -0
- data/runtime/corelib/array.rb +962 -0
- data/runtime/corelib/basic_object.rb +66 -0
- data/runtime/corelib/boolean.rb +44 -0
- data/runtime/corelib/class.rb +43 -0
- data/runtime/corelib/comparable.rb +25 -0
- data/runtime/corelib/complex.rb +2 -0
- data/runtime/corelib/dir.rb +29 -0
- data/runtime/corelib/enumerable.rb +316 -0
- data/runtime/corelib/enumerator.rb +80 -0
- data/runtime/corelib/error.rb +25 -0
- data/runtime/corelib/file.rb +80 -0
- data/runtime/corelib/hash.rb +503 -0
- data/runtime/corelib/io.rb +44 -0
- data/runtime/corelib/kernel.rb +237 -0
- data/runtime/corelib/load_order +29 -0
- data/runtime/corelib/match_data.rb +37 -0
- data/runtime/corelib/module.rb +171 -0
- data/runtime/corelib/native.rb +50 -0
- data/runtime/corelib/nil_class.rb +47 -0
- data/runtime/corelib/numeric.rb +219 -0
- data/runtime/corelib/object.rb +21 -0
- data/runtime/corelib/proc.rb +42 -0
- data/runtime/corelib/range.rb +38 -0
- data/runtime/corelib/rational.rb +16 -0
- data/runtime/corelib/regexp.rb +63 -0
- data/runtime/corelib/string.rb +185 -0
- data/runtime/corelib/struct.rb +97 -0
- data/runtime/corelib/time.rb +196 -0
- data/runtime/corelib/top_self.rb +7 -0
- data/runtime/gemlib/alpha.rb +5 -0
- data/runtime/gemlib/kernel.rb +17 -0
- data/runtime/gemlib/load_order +2 -0
- data/runtime/kernel/class.js +256 -0
- data/runtime/kernel/debug.js +42 -0
- data/runtime/kernel/init.js +114 -0
- data/runtime/kernel/load_order +5 -0
- data/runtime/kernel/loader.js +151 -0
- data/runtime/kernel/runtime.js +414 -0
- data/runtime/spec/README.md +34 -0
- data/runtime/spec/core/array/allocate_spec.rb +15 -0
- data/runtime/spec/core/array/append_spec.rb +31 -0
- data/runtime/spec/core/array/assoc_spec.rb +29 -0
- data/runtime/spec/core/array/at_spec.rb +38 -0
- data/runtime/spec/core/array/clear_spec.rb +22 -0
- data/runtime/spec/core/array/collect_spec.rb +3 -0
- data/runtime/spec/core/array/compact_spec.rb +42 -0
- data/runtime/spec/core/array/concat_spec.rb +21 -0
- data/runtime/spec/core/array/constructor_spec.rb +24 -0
- data/runtime/spec/core/array/count_spec.rb +11 -0
- data/runtime/spec/core/array/delete_at_spec.rb +31 -0
- data/runtime/spec/core/array/delete_if_spec.rb +24 -0
- data/runtime/spec/core/array/delete_spec.rb +26 -0
- data/runtime/spec/core/array/each_index_spec.rb +33 -0
- data/runtime/spec/core/array/each_spec.rb +11 -0
- data/runtime/spec/core/array/element_reference_spec.rb +136 -0
- data/runtime/spec/core/array/element_set_spec.rb +7 -0
- data/runtime/spec/core/array/empty_spec.rb +10 -0
- data/runtime/spec/core/array/eql_spec.rb +3 -0
- data/runtime/spec/core/array/equal_value_spec.rb +3 -0
- data/runtime/spec/core/array/fetch_spec.rb +26 -0
- data/runtime/spec/core/array/first_spec.rb +54 -0
- data/runtime/spec/core/array/fixtures/classes.rb +8 -0
- data/runtime/spec/core/array/flatten_spec.rb +41 -0
- data/runtime/spec/core/array/include_spec.rb +20 -0
- data/runtime/spec/core/array/insert_spec.rb +59 -0
- data/runtime/spec/core/array/last_spec.rb +57 -0
- data/runtime/spec/core/array/length_spec.rb +3 -0
- data/runtime/spec/core/array/map_spec.rb +3 -0
- data/runtime/spec/core/array/plus_spec.rb +16 -0
- data/runtime/spec/core/array/pop_spec.rb +79 -0
- data/runtime/spec/core/array/push_spec.rb +19 -0
- data/runtime/spec/core/array/rassoc_spec.rb +12 -0
- data/runtime/spec/core/array/reject_spec.rb +54 -0
- data/runtime/spec/core/array/replace_spec.rb +3 -0
- data/runtime/spec/core/array/reverse_each_spec.rb +18 -0
- data/runtime/spec/core/array/reverse_spec.rb +9 -0
- data/runtime/spec/core/array/shared/collect.rb +53 -0
- data/runtime/spec/core/array/shared/eql.rb +19 -0
- data/runtime/spec/core/array/shared/length.rb +6 -0
- data/runtime/spec/core/array/shared/replace.rb +31 -0
- data/runtime/spec/core/class/new_spec.rb +19 -0
- data/runtime/spec/core/enumerable/all_spec.rb +102 -0
- data/runtime/spec/core/enumerable/any_spec.rb +115 -0
- data/runtime/spec/core/enumerable/collect_spec.rb +3 -0
- data/runtime/spec/core/enumerable/count_spec.rb +29 -0
- data/runtime/spec/core/enumerable/detect_spec.rb +3 -0
- data/runtime/spec/core/enumerable/find_spec.rb +3 -0
- data/runtime/spec/core/enumerable/fixtures/classes.rb +26 -0
- data/runtime/spec/core/enumerable/shared/collect.rb +12 -0
- data/runtime/spec/core/enumerable/shared/entries.rb +7 -0
- data/runtime/spec/core/enumerable/shared/find.rb +49 -0
- data/runtime/spec/core/enumerable/to_a_spec.rb +7 -0
- data/runtime/spec/core/false/and_spec.rb +11 -0
- data/runtime/spec/core/false/inspect_spec.rb +7 -0
- data/runtime/spec/core/false/or_spec.rb +11 -0
- data/runtime/spec/core/false/to_s_spec.rb +7 -0
- data/runtime/spec/core/false/xor_spec.rb +11 -0
- data/runtime/spec/core/hash/allocate_spec.rb +15 -0
- data/runtime/spec/core/hash/assoc_spec.rb +29 -0
- data/runtime/spec/core/hash/clear_spec.rb +21 -0
- data/runtime/spec/core/hash/clone_spec.rb +12 -0
- data/runtime/spec/core/hash/default_spec.rb +6 -0
- data/runtime/spec/core/hash/delete_if_spec.rb +15 -0
- data/runtime/spec/core/hash/element_reference_spec.rb +16 -0
- data/runtime/spec/core/hash/element_set_spec.rb +8 -0
- data/runtime/spec/core/hash/new_spec.rb +13 -0
- data/runtime/spec/core/matchdata/to_a_spec.rb +7 -0
- data/runtime/spec/core/nil/and_spec.rb +12 -0
- data/runtime/spec/core/nil/inspect_spec.rb +8 -0
- data/runtime/spec/core/nil/nil_spec.rb +8 -0
- data/runtime/spec/core/nil/or_spec.rb +12 -0
- data/runtime/spec/core/nil/to_a_spec.rb +8 -0
- data/runtime/spec/core/nil/to_f_spec.rb +12 -0
- data/runtime/spec/core/nil/to_i_spec.rb +12 -0
- data/runtime/spec/core/nil/to_s_spec.rb +8 -0
- data/runtime/spec/core/nil/xor_spec.rb +12 -0
- data/runtime/spec/core/numeric/equal_value_spec.rb +11 -0
- data/runtime/spec/core/object/is_a_spec.rb +2 -0
- data/runtime/spec/core/object/shared/kind_of.rb +0 -0
- data/runtime/spec/core/regexp/match_spec.rb +23 -0
- data/runtime/spec/core/regexp/shared/match.rb +11 -0
- data/runtime/spec/core/symbol/to_proc_spec.rb +8 -0
- data/runtime/spec/core/true/and_spec.rb +11 -0
- data/runtime/spec/core/true/inspect_spec.rb +7 -0
- data/runtime/spec/core/true/or_spec.rb +11 -0
- data/runtime/spec/core/true/to_s_spec.rb +7 -0
- data/runtime/spec/core/true/xor_spec.rb +11 -0
- data/runtime/spec/language/alias_spec.rb +25 -0
- data/runtime/spec/language/and_spec.rb +62 -0
- data/runtime/spec/language/array_spec.rb +68 -0
- data/runtime/spec/language/block_spec.rb +105 -0
- data/runtime/spec/language/break_spec.rb +49 -0
- data/runtime/spec/language/case_spec.rb +165 -0
- data/runtime/spec/language/defined_spec.rb +80 -0
- data/runtime/spec/language/ensure_spec.rb +82 -0
- data/runtime/spec/language/fixtures/block.rb +19 -0
- data/runtime/spec/language/fixtures/break.rb +39 -0
- data/runtime/spec/language/fixtures/defined.rb +9 -0
- data/runtime/spec/language/fixtures/ensure.rb +37 -0
- data/runtime/spec/language/fixtures/next.rb +46 -0
- data/runtime/spec/language/fixtures/send.rb +36 -0
- data/runtime/spec/language/fixtures/super.rb +43 -0
- data/runtime/spec/language/hash_spec.rb +43 -0
- data/runtime/spec/language/if_spec.rb +278 -0
- data/runtime/spec/language/loop_spec.rb +32 -0
- data/runtime/spec/language/next_spec.rb +128 -0
- data/runtime/spec/language/or_spec.rb +65 -0
- data/runtime/spec/language/predefined_spec.rb +21 -0
- data/runtime/spec/language/regexp/interpolation_spec.rb +9 -0
- data/runtime/spec/language/regexp_spec.rb +7 -0
- data/runtime/spec/language/send_spec.rb +105 -0
- data/runtime/spec/language/string_spec.rb +4 -0
- data/runtime/spec/language/super_spec.rb +18 -0
- data/runtime/spec/language/symbol_spec.rb +41 -0
- data/runtime/spec/language/undef_spec.rb +16 -0
- data/runtime/spec/language/unless_spec.rb +44 -0
- data/runtime/spec/language/until_spec.rb +137 -0
- data/runtime/spec/language/variables_spec.rb +28 -0
- data/runtime/spec/language/versions/hash_1.9.rb +20 -0
- data/runtime/spec/language/while_spec.rb +175 -0
- data/runtime/spec/library/stringscanner/scan_spec.rb +36 -0
- data/runtime/spec/opal/forwardable/def_instance_delegator_spec.rb +49 -0
- data/runtime/spec/opal/opal/defined_spec.rb +15 -0
- data/runtime/spec/opal/opal/function_spec.rb +11 -0
- data/runtime/spec/opal/opal/native_spec.rb +16 -0
- data/runtime/spec/opal/opal/null_spec.rb +10 -0
- data/runtime/spec/opal/opal/number_spec.rb +11 -0
- data/runtime/spec/opal/opal/object_spec.rb +16 -0
- data/runtime/spec/opal/opal/string_spec.rb +11 -0
- data/runtime/spec/opal/opal/typeof_spec.rb +9 -0
- data/runtime/spec/opal/opal/undefined_spec.rb +10 -0
- data/runtime/spec/opal/true/case_compare_spec.rb +12 -0
- data/runtime/spec/opal/true/class_spec.rb +10 -0
- data/runtime/spec/spec_helper.rb +25 -0
- data/runtime/stdlib/base64.rb +91 -0
- data/runtime/stdlib/date.rb +4 -0
- data/{stdlib → runtime/stdlib}/dev.rb +0 -0
- data/runtime/stdlib/forwardable.rb +33 -0
- data/runtime/stdlib/optparse.rb +0 -0
- data/runtime/stdlib/pp.rb +6 -0
- data/{stdlib → runtime/stdlib}/racc/parser.rb +0 -0
- data/runtime/stdlib/rbconfig.rb +0 -0
- data/runtime/stdlib/si.rb +17 -0
- data/runtime/stdlib/strscan.rb +53 -0
- data/runtime/stdlib/uri.rb +111 -0
- data/runtime/stdlib/uri/common.rb +1014 -0
- data/runtime/stdlib/uri/ftp.rb +261 -0
- data/runtime/stdlib/uri/generic.rb +1599 -0
- data/runtime/stdlib/uri/http.rb +106 -0
- data/runtime/stdlib/uri/https.rb +22 -0
- data/runtime/stdlib/uri/ldap.rb +260 -0
- data/runtime/stdlib/uri/ldaps.rb +20 -0
- data/runtime/stdlib/uri/mailto.rb +280 -0
- data/spec/builder/build_source_spec.rb +52 -0
- data/spec/builder/fixtures/build_source/adam.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/a.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/wow/b.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/wow/cow/c.rb +0 -0
- data/spec/builder/fixtures/build_source/beynon.rb +0 -0
- data/spec/builder/fixtures/build_source/charles.js +0 -0
- data/spec/builder/fixtures/build_source/foo/a.rb +0 -0
- data/spec/builder/fixtures/build_source/foo/b.rb +0 -0
- data/spec/builder/fixtures/build_source/foo/x.js +0 -0
- data/spec/builder/fixtures/build_source/foo/y.js +0 -0
- data/spec/builder/output_path_spec.rb +50 -0
- data/spec/grammar/alias_spec.rb +26 -0
- data/spec/grammar/and_spec.rb +13 -0
- data/spec/grammar/array_spec.rb +22 -0
- data/spec/grammar/attrasgn_spec.rb +28 -0
- data/spec/grammar/begin_spec.rb +38 -0
- data/spec/grammar/block_spec.rb +12 -0
- data/spec/grammar/break_spec.rb +17 -0
- data/spec/grammar/call_spec.rb +58 -0
- data/spec/grammar/class_spec.rb +35 -0
- data/spec/grammar/const_spec.rb +13 -0
- data/spec/grammar/cvar_spec.rb +11 -0
- data/spec/grammar/def_spec.rb +60 -0
- data/spec/grammar/false_spec.rb +17 -0
- data/spec/grammar/file_spec.rb +7 -0
- data/spec/grammar/gvar_spec.rb +13 -0
- data/spec/grammar/hash_spec.rb +17 -0
- data/spec/grammar/iasgn_spec.rb +9 -0
- data/spec/grammar/if_spec.rb +26 -0
- data/spec/grammar/iter_spec.rb +59 -0
- data/spec/grammar/ivar_spec.rb +9 -0
- data/spec/grammar/lasgn_spec.rb +8 -0
- data/spec/grammar/line_spec.rb +8 -0
- data/spec/grammar/lvar_spec.rb +38 -0
- data/spec/grammar/module_spec.rb +27 -0
- data/spec/grammar/nil_spec.rb +17 -0
- data/spec/grammar/not_spec.rb +27 -0
- data/spec/grammar/op_asgn1_spec.rb +23 -0
- data/spec/grammar/op_asgn2_spec.rb +23 -0
- data/spec/grammar/or_spec.rb +13 -0
- data/spec/grammar/return_spec.rb +17 -0
- data/spec/grammar/sclass_spec.rb +20 -0
- data/spec/grammar/self_spec.rb +17 -0
- data/spec/grammar/str_spec.rb +96 -0
- data/spec/grammar/super_spec.rb +20 -0
- data/spec/grammar/true_spec.rb +17 -0
- data/spec/grammar/undef_spec.rb +15 -0
- data/spec/grammar/unless_spec.rb +13 -0
- data/spec/grammar/while_spec.rb +15 -0
- data/spec/grammar/xstr_spec.rb +116 -0
- data/spec/grammar/yield_spec.rb +20 -0
- data/spec/spec_helper.rb +9 -0
- metadata +346 -21
- data/lib/opal/bundle.rb +0 -34
- data/lib/opal/lexer.rb +0 -902
- data/lib/opal/nodes.rb +0 -2150
- data/lib/opal/parser.rb +0 -4894
- data/lib/opal/rake/bundle_task.rb +0 -63
- data/opal-parser.js +0 -8343
- data/stdlib/strscan.rb +0 -52
- data/templates/init/Rakefile +0 -7
- data/templates/init/index.html +0 -17
- data/templates/init/lib/__NAME__.rb +0 -2
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Opal Tests
|
|
2
|
+
==========
|
|
3
|
+
|
|
4
|
+
All tests/specs in this folder should be run in an opal context, either
|
|
5
|
+
with the bundled buidl tools, or directly in the browser.
|
|
6
|
+
|
|
7
|
+
License
|
|
8
|
+
=======
|
|
9
|
+
|
|
10
|
+
All specs/tests are taken from rubyspec. Original license:
|
|
11
|
+
|
|
12
|
+
Copyright (c) 2008 Engine Yard, Inc. All rights reserved.
|
|
13
|
+
|
|
14
|
+
Permission is hereby granted, free of charge, to any person
|
|
15
|
+
obtaining a copy of this software and associated documentation
|
|
16
|
+
files (the "Software"), to deal in the Software without
|
|
17
|
+
restriction, including without limitation the rights to use,
|
|
18
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
19
|
+
copies of the Software, and to permit persons to whom the
|
|
20
|
+
Software is furnished to do so, subject to the following
|
|
21
|
+
conditions:
|
|
22
|
+
|
|
23
|
+
The above copyright notice and this permission notice shall be
|
|
24
|
+
included in all copies or substantial portions of the Software.
|
|
25
|
+
|
|
26
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
27
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
28
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
29
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
30
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
31
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
32
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
33
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
34
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe "Array.allocate" do
|
|
4
|
+
it "returns an instance of Array" do
|
|
5
|
+
ary = Array.allocate
|
|
6
|
+
ary.should be_kind_of(Array)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "returns a fully-formed instance of Array" do
|
|
10
|
+
ary = Array.allocate
|
|
11
|
+
ary.size.should == 0
|
|
12
|
+
ary << 1
|
|
13
|
+
ary.should == [1]
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe "Array#<<" do
|
|
4
|
+
it "pushes the object onto the end of the array" do
|
|
5
|
+
([ 1, 2 ] << "c" << "d" << [ 3, 4 ]).should == [1, 2, "c", "d", [3, 4]]
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "returns self to allow chaining" do
|
|
9
|
+
a = []
|
|
10
|
+
b = a
|
|
11
|
+
(a << 1).should == b
|
|
12
|
+
(a << 2 << 3).should == b
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "correctly resizes the Array" do
|
|
16
|
+
a = []
|
|
17
|
+
a.size.should == 0
|
|
18
|
+
a << :foo
|
|
19
|
+
a.size.should == 1
|
|
20
|
+
a << :bar << :baz
|
|
21
|
+
a.size.should == 3
|
|
22
|
+
|
|
23
|
+
a = [1, 2, 3]
|
|
24
|
+
a.shift
|
|
25
|
+
a.shift
|
|
26
|
+
a.shift
|
|
27
|
+
a << :foo
|
|
28
|
+
a.should == [:foo]
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe "Array#assoc" do
|
|
4
|
+
it "returns the first array whose 1st item is == obj or nil" do
|
|
5
|
+
s1 = ["colors", "red", "blue", "green"]
|
|
6
|
+
s2 = [:letter, "a", "b", "c"]
|
|
7
|
+
s3 = [4]
|
|
8
|
+
s4 = ["colors", "cyan", "yellow", "magenda"]
|
|
9
|
+
s5 = [:letters, "a", "i", "u"]
|
|
10
|
+
s_nil = [nil, nil]
|
|
11
|
+
a = [s1, s2, s3, s4, s5, s_nil]
|
|
12
|
+
a.assoc(s1.first).should == s1
|
|
13
|
+
a.assoc(s2.first).should == s2
|
|
14
|
+
a.assoc(s3.first).should == s3
|
|
15
|
+
a.assoc(s4.first).should == s1
|
|
16
|
+
# FIXME: WTF?
|
|
17
|
+
# a.assoc(s5.first).should == s2
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "ignores any non-Array elements" do
|
|
21
|
+
[1, 2, 3].assoc(2).should be_nil
|
|
22
|
+
s1 = [4]
|
|
23
|
+
s2 = [5, 4, 3]
|
|
24
|
+
a = ["foo", [], s1, s2, nil, []]
|
|
25
|
+
a.assoc(s1.first).should == s1
|
|
26
|
+
a.assoc(s2.first).should == s2
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe "Array#at" do
|
|
4
|
+
it "returns the (n+1)'th element for the passed index n" do
|
|
5
|
+
a = [1, 2, 3, 4, 5, 6]
|
|
6
|
+
a.at(0).should == 1
|
|
7
|
+
a.at(1).should == 2
|
|
8
|
+
a.at(5).should == 6
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "returns nil if the given index is greater than or equal to the array's length" do
|
|
12
|
+
a = [1, 2, 3, 4, 5, 6]
|
|
13
|
+
a.at(6).should == nil
|
|
14
|
+
a.at(7).should == nil
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "returns the (-n)'th element from the last, for the given negative index n" do
|
|
18
|
+
a = [1, 2, 3, 4, 5, 6]
|
|
19
|
+
a.at(-1).should == 6
|
|
20
|
+
a.at(-2).should == 5
|
|
21
|
+
a.at(-6).should == 1
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "returns nil if the given index is less than -len, where len is length of the array" do
|
|
25
|
+
a = [1, 2, 3, 4, 5, 6]
|
|
26
|
+
a.at(-7).should == nil
|
|
27
|
+
a.at(-8).should == nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "does not extend the array unless the given index is out of range" do
|
|
31
|
+
a = [1, 2, 3, 4, 5, 6]
|
|
32
|
+
a.length.should == 6
|
|
33
|
+
a.at(100)
|
|
34
|
+
a.length.should == 6
|
|
35
|
+
a.at(-100)
|
|
36
|
+
a.length.should == 6
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe "Array#clear" do
|
|
4
|
+
it "removes all elements" do
|
|
5
|
+
a = [1, 2, 3, 4]
|
|
6
|
+
a.clear
|
|
7
|
+
a.should == []
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "returns self" do
|
|
11
|
+
a = [1]
|
|
12
|
+
old = a.object_id
|
|
13
|
+
a.clear.object_id.should == old
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "leaves the Array empty" do
|
|
17
|
+
a = [1]
|
|
18
|
+
a.clear
|
|
19
|
+
a.empty?.should == true
|
|
20
|
+
a.size.should == 0
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe "Array#compact" do
|
|
4
|
+
it "returns a copy of array with all nil elements removed" do
|
|
5
|
+
a = [1, 2, 4]
|
|
6
|
+
a.compact.should == [1, 2, 4]
|
|
7
|
+
a = [1, nil, 2, 4]
|
|
8
|
+
a.compact.should == [1, 2, 4]
|
|
9
|
+
a = [1, 2, 4, nil]
|
|
10
|
+
a.compact.should == [1, 2, 4]
|
|
11
|
+
a = [nil, 1, 2, 4]
|
|
12
|
+
a.compact.should == [1, 2, 4]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "does not return self" do
|
|
16
|
+
a = [1, 2, 3]
|
|
17
|
+
a.compact.should_not equal(a)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
describe "Array#compact!" do
|
|
22
|
+
it "removes all nil elements" do
|
|
23
|
+
a = ['a', nil, 'b', false, 'c']
|
|
24
|
+
a.compact!.should equal(a)
|
|
25
|
+
a.should == ["a", "b", false, "c"]
|
|
26
|
+
a = [nil, 'a', 'b', false, 'c']
|
|
27
|
+
a.compact!.should equal(a)
|
|
28
|
+
a.should == ["a", "b", false, "c"]
|
|
29
|
+
a = ['a', 'b', false, 'c', nil]
|
|
30
|
+
a.compact!.should equal(a)
|
|
31
|
+
a.should == ["a", "b", false, "c"]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "returns self if some nil elements are removed" do
|
|
35
|
+
a = ['a', nil, 'b', false, 'c']
|
|
36
|
+
a.compact!.object_id.should == a.object_id
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "returns nil if there are no nil elements to remove" do
|
|
40
|
+
[1, 2, false, 3].compact!.should == nil
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe "Array#concat" do
|
|
4
|
+
it "returns the array itself" do
|
|
5
|
+
ary = [1,2,3]
|
|
6
|
+
ary.concat([4,5,6]).equal?(ary).should be_true
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "appends the elements in the other array" do
|
|
10
|
+
ary = [1, 2, 3]
|
|
11
|
+
ary.concat([9, 10, 11]).should equal(ary)
|
|
12
|
+
ary.should == [1, 2, 3, 9, 10, 11]
|
|
13
|
+
ary.concat([])
|
|
14
|
+
ary.should == [1, 2, 3, 9, 10, 11]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "does not loop endlessly when argument is self" do
|
|
18
|
+
ary = ["x", "y"]
|
|
19
|
+
ary.concat(ary).should == ["x", "y", "x", "y"]
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
|
3
|
+
|
|
4
|
+
describe "Array.[]" do
|
|
5
|
+
it "returns a new array populated with the given elements" do
|
|
6
|
+
obj = Object.new
|
|
7
|
+
Array.[](5, true, nil, 'a', "Ruby", obj).should == [5, true, nil, "a", "Ruby", obj]
|
|
8
|
+
|
|
9
|
+
a = ArraySpecs::MyArray.[](5, true, nil, 'a', "Ruby", obj)
|
|
10
|
+
a.should be_kind_of(ArraySpecs::MyArray)
|
|
11
|
+
a.inspect.should == [5, true, nil, "a", "Ruby", obj].inspect
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
describe "Array[]" do
|
|
16
|
+
it "is a synonym for .[]" do
|
|
17
|
+
obj = Object.new
|
|
18
|
+
Array[5, true, nil, 'a', "Ruby", obj].should == [5, true, nil, "a", "Ruby", obj]
|
|
19
|
+
|
|
20
|
+
a = ArraySpecs::MyArray[5, true, nil, 'a', "Ruby", obj]
|
|
21
|
+
a.should be_kind_of(ArraySpecs::MyArray)
|
|
22
|
+
a.inspect.should == [5, true, nil, "a", "Ruby", obj].inspect
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe "Array#count" do
|
|
4
|
+
it "returns the count of elements" do
|
|
5
|
+
[1, :two, 'three'].count.should == 3
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "returns count of elements that equals given object" do
|
|
9
|
+
[1, 'some text', 'other text', 2, 1].count(1).should == 2
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
|
3
|
+
|
|
4
|
+
describe "Array#delete_at" do
|
|
5
|
+
it "removes the element at the specified index" do
|
|
6
|
+
a = [1, 2, 3, 4]
|
|
7
|
+
a.delete_at(2)
|
|
8
|
+
a.should == [1, 2, 4]
|
|
9
|
+
a.delete_at(-1)
|
|
10
|
+
a.should == [1, 2]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "returns the removed element at the specified index" do
|
|
14
|
+
a = [1, 2, 3, 4]
|
|
15
|
+
a.delete_at(2).should == 3
|
|
16
|
+
a.delete_at(-1).should == 4
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "returns nil and makes no modification if the index is out of range" do
|
|
20
|
+
a = [1, 2]
|
|
21
|
+
a.delete_at(3).should == nil
|
|
22
|
+
a.should == [1, 2]
|
|
23
|
+
a.delete_at(-3).should == nil
|
|
24
|
+
a.should == [1, 2]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "accepts negative indices" do
|
|
28
|
+
a = [1, 2]
|
|
29
|
+
a.delete_at(-2).should == 1
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
|
3
|
+
|
|
4
|
+
describe "Array#delete_if" do
|
|
5
|
+
before do
|
|
6
|
+
@a = [ "a", "b", "c" ]
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "removes each element for which block returns true" do
|
|
10
|
+
@a = [ "a", "b", "c" ]
|
|
11
|
+
@a.delete_if { |x| x >= "b" }
|
|
12
|
+
@a.should == ["a"]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "returns self" do
|
|
16
|
+
@a.delete_if { true }.equal?(@a).should be_true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "returns self when called on an Array emptied with #shift" do
|
|
20
|
+
array = [1]
|
|
21
|
+
array.shift
|
|
22
|
+
array.delete_if { |x| true }.should equal(array)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
|
3
|
+
|
|
4
|
+
describe "Array#delete" do
|
|
5
|
+
it "removes elements that are #== to object" do
|
|
6
|
+
x = mock('delete')
|
|
7
|
+
def x.==(other) 3 == other end
|
|
8
|
+
|
|
9
|
+
a = [1, 2, 3, x, 4, 3, 5, x]
|
|
10
|
+
a.delete mock('not contained')
|
|
11
|
+
|
|
12
|
+
a.delete 3
|
|
13
|
+
a.should == [1, 2, 4, 5]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "calculates equality correctly for reference values" do
|
|
17
|
+
a = ["foo", "bar", "foo", "quux", "foo"]
|
|
18
|
+
a.delete "foo"
|
|
19
|
+
a.should == ["bar", "quux"]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "returns object or nil if no elements match object" do
|
|
23
|
+
[1, 2, 4, 5].delete(1).should == 1
|
|
24
|
+
[1, 2, 4, 5].delete(3).should == nil
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
|
3
|
+
|
|
4
|
+
describe "Array#each_index" do
|
|
5
|
+
before :each do
|
|
6
|
+
ScratchPad.record []
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "passes the index of each element to the block" do
|
|
10
|
+
a = ['a', 'b', 'c', 'd']
|
|
11
|
+
a.each_index { |i| ScratchPad << i }
|
|
12
|
+
ScratchPad.recorded.should == [0, 1, 2, 3]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "returns self" do
|
|
16
|
+
a = [:a, :b, :c]
|
|
17
|
+
a.each_index { |i| }.should equal(a)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "is not confused by removing elements from the front" do
|
|
21
|
+
a = [1, 2, 3]
|
|
22
|
+
|
|
23
|
+
a.shift
|
|
24
|
+
ScratchPad.record []
|
|
25
|
+
a.each_index { |i| ScratchPad << i }
|
|
26
|
+
ScratchPad.recorded.should == [0, 1]
|
|
27
|
+
|
|
28
|
+
a.shift
|
|
29
|
+
ScratchPad.record []
|
|
30
|
+
a.each_index { |i| ScratchPad << i }
|
|
31
|
+
ScratchPad.recorded.should == [0]
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
|
3
|
+
|
|
4
|
+
describe "Array#each" do
|
|
5
|
+
it "yields each element to the block" do
|
|
6
|
+
a = []
|
|
7
|
+
x = [1, 2, 3]
|
|
8
|
+
x.each { |item| a << item }.should equal(x)
|
|
9
|
+
a.should == [1, 2, 3]
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
|
3
|
+
|
|
4
|
+
describe "Array#[]" do
|
|
5
|
+
it "returns the element at index with [index]" do
|
|
6
|
+
[ "a", "b", "c", "d", "e" ][1].should == "b"
|
|
7
|
+
|
|
8
|
+
a = [1, 2, 3, 4]
|
|
9
|
+
a[0].should == 1
|
|
10
|
+
a[1].should == 2
|
|
11
|
+
a[2].should == 3
|
|
12
|
+
a[3].should == 4
|
|
13
|
+
a[4].should == nil
|
|
14
|
+
a[10].should == nil
|
|
15
|
+
|
|
16
|
+
a.should == [1, 2, 3, 4]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "returns the element at index from the end of the array with [-index]" do
|
|
20
|
+
[ "a", "b", "c", "d", "e" ][-2].should == "d"
|
|
21
|
+
|
|
22
|
+
a = [1, 2, 3, 4]
|
|
23
|
+
a[-1].should == 4
|
|
24
|
+
a[-2].should == 3
|
|
25
|
+
a[-3].should == 2
|
|
26
|
+
a[-4].should == 1
|
|
27
|
+
a[-5].should == nil
|
|
28
|
+
a[-10].should == nil
|
|
29
|
+
|
|
30
|
+
a.should == [1, 2, 3, 4]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "returns count elements starting from index with [index, count]" do
|
|
34
|
+
[ "a", "b", "c", "d", "e" ][2, 3].should == ["c", "d", "e"]
|
|
35
|
+
|
|
36
|
+
a = [1, 2, 3, 4]
|
|
37
|
+
|
|
38
|
+
a[0, 0].should == []
|
|
39
|
+
a[0, 1].should == [1]
|
|
40
|
+
a[0, 2].should == [1, 2]
|
|
41
|
+
a[0, 4].should == [1, 2, 3, 4]
|
|
42
|
+
a[0, 6].should == [1, 2, 3, 4]
|
|
43
|
+
a[0, -1].should == nil
|
|
44
|
+
a[0, -2].should == nil
|
|
45
|
+
a[0, -4].should == nil
|
|
46
|
+
|
|
47
|
+
a[2, 0].should == []
|
|
48
|
+
a[2, 1].should == [3]
|
|
49
|
+
a[2, 2].should == [3, 4]
|
|
50
|
+
a[2, 4].should == [3, 4]
|
|
51
|
+
a[2, -1].should == nil
|
|
52
|
+
|
|
53
|
+
a[4, 0].should == []
|
|
54
|
+
a[4, 2].should == []
|
|
55
|
+
a[4, -1].should == nil
|
|
56
|
+
|
|
57
|
+
a[5, 0].should == nil
|
|
58
|
+
a[5, 2].should == nil
|
|
59
|
+
a[5, -1].should == nil
|
|
60
|
+
|
|
61
|
+
a[6, 0].should == nil
|
|
62
|
+
a[6, 2].should == nil
|
|
63
|
+
a[6, -1].should == nil
|
|
64
|
+
|
|
65
|
+
a.should == [1, 2, 3, 4]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "returns count elements starting at index from the end of array with [-index, count]" do
|
|
69
|
+
[ "a", "b", "c", "d", "e" ][-2, 2].should == ["d", "e"]
|
|
70
|
+
|
|
71
|
+
a = [1, 2, 3, 4]
|
|
72
|
+
a[-1, 0].should == []
|
|
73
|
+
a[-1, 1].should == [4]
|
|
74
|
+
a[-1, 2].should == [4]
|
|
75
|
+
a[-1, -1].should == nil
|
|
76
|
+
|
|
77
|
+
a[-2, 0].should == []
|
|
78
|
+
a[-2, 1].should == [3]
|
|
79
|
+
a[-2, 2].should == [3, 4]
|
|
80
|
+
a[-2, 4].should == [3, 4]
|
|
81
|
+
a[-2, -1].should == nil
|
|
82
|
+
|
|
83
|
+
a[-4, 0].should == []
|
|
84
|
+
a[-4, 1].should == [1]
|
|
85
|
+
a[-4, 2].should == [1, 2]
|
|
86
|
+
a[-4, 4].should == [1, 2, 3, 4]
|
|
87
|
+
a[-4, 6].should == [1, 2, 3, 4]
|
|
88
|
+
a[-4, -1].should == nil
|
|
89
|
+
|
|
90
|
+
a[-5, 0].should == nil
|
|
91
|
+
a[-5, 1].should == nil
|
|
92
|
+
a[-5, 10].should == nil
|
|
93
|
+
a[-5, -1].should == nil
|
|
94
|
+
|
|
95
|
+
a.should == [1, 2, 3, 4]
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it "returns the first count elements with [0, count]" do
|
|
99
|
+
[ "a", "b", "c", "d", "e" ][0, 3].should == ["a", "b", "c"]
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it "returns the subarray which is independent to self with [index,count]" do
|
|
103
|
+
a = [1, 2, 3]
|
|
104
|
+
sub = a[1,2]
|
|
105
|
+
sub.replace([:a, :b])
|
|
106
|
+
a.should == [1, 2, 3]
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it "returns nil for a requested index not in the array with [index]" do
|
|
110
|
+
[ "a", "b", "c", "d", "e" ][5].should == nil
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it "returns [] of the index is valid but length is zero with [index, length]" do
|
|
114
|
+
[ "a", "b", "c", "d", "e" ][0, 0].should == []
|
|
115
|
+
[ "a", "b", "c", "d", "e" ][2, 0].should == []
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
it "returns nil if length is zero but index is invalid with [index, length]" do
|
|
119
|
+
[ "a", "b", "c", "d", "e" ][100, 0].should == nil
|
|
120
|
+
[ "a", "b", "c", "d", "e" ][-50, 0].should == nil
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it "returns [] if index == array.size with [index, length]" do
|
|
124
|
+
%w|a b c d e|[5, 2].should == []
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
it "returns nil if index > array.size with [index, length]" do
|
|
128
|
+
%w|a b c d e|[6, 2].should == nil
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
it "returns nil if length is negative with [index, length]" do
|
|
132
|
+
%w|a b c d e|[3, -1].should == nil
|
|
133
|
+
%w|a b c d e|[2, -2].should == nil
|
|
134
|
+
%w|a b c d e|[1, -100].should == nil
|
|
135
|
+
end
|
|
136
|
+
end
|