opal 0.3.41 → 0.3.42
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 +2 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.md +14 -1
- data/Gemfile +2 -5
- data/Rakefile +41 -3
- data/bin/opal +33 -0
- data/lib/opal.rb +2 -12
- data/lib/opal/core_ext.rb +5 -0
- data/lib/opal/grammar.rb +2207 -2138
- data/lib/opal/grammar.y +21 -0
- data/lib/opal/grammar_helpers.rb +360 -0
- data/lib/opal/lexer.rb +55 -401
- data/lib/opal/lexer_scope.rb +28 -0
- data/lib/opal/parser.rb +155 -171
- data/lib/opal/target_scope.rb +257 -0
- data/lib/opal/version.rb +1 -1
- data/opal.gemspec +6 -2
- data/opal/opal-parser.js.erb +3 -2
- data/opal/opal.rb +20 -18
- data/opal/opal/array.rb +21 -12
- data/opal/opal/basic_object.rb +2 -1
- data/opal/opal/boolean.rb +3 -0
- data/opal/opal/browser_loader.js +57 -0
- data/opal/opal/class.rb +51 -13
- data/opal/opal/date.rb +1 -20
- data/opal/opal/enumerable.rb +66 -33
- data/opal/opal/error.rb +2 -0
- data/opal/opal/hash.rb +1 -1
- data/opal/opal/kernel.rb +14 -3
- data/opal/opal/nil_class.rb +4 -0
- data/opal/opal/proc.rb +9 -1
- data/opal/opal/racc.rb +2 -2
- data/opal/opal/regexp.rb +1 -1
- data/opal/opal/runtime.js +14 -4
- data/opal/opal/string.rb +21 -4
- data/opal/opal/time.rb +27 -0
- data/spec/core/array/allocate_spec.rb +7 -1
- data/spec/core/array/append_spec.rb +18 -3
- data/spec/core/array/array_spec.rb +7 -0
- data/spec/core/array/assoc_spec.rb +23 -8
- data/spec/core/array/at_spec.rb +23 -3
- data/spec/core/array/choice_spec.rb +20 -0
- data/spec/core/array/clear_spec.rb +45 -4
- data/spec/core/array/combination_spec.rb +55 -0
- data/spec/core/array/compact_spec.rb +72 -1
- data/spec/core/array/constructor_spec.rb +13 -2
- data/spec/core/array/count_spec.rb +15 -7
- data/spec/core/array/delete_at_spec.rb +44 -1
- data/spec/core/array/delete_if_spec.rb +52 -2
- data/spec/core/array/delete_spec.rb +83 -2
- data/spec/core/array/drop_spec.rb +24 -16
- data/spec/core/array/drop_while_spec.rb +17 -0
- data/spec/core/array/each_index_spec.rb +11 -1
- data/spec/core/array/each_spec.rb +20 -2
- data/spec/core/array/empty_spec.rb +4 -1
- data/spec/core/array/eql_spec.rb +14 -0
- data/spec/core/array/fetch_spec.rb +31 -2
- data/spec/core/array/find_index_spec.rb +8 -0
- data/spec/core/array/first_spec.rb +45 -8
- data/spec/core/array/fixtures/classes.rb +538 -0
- data/spec/core/array/flatten_spec.rb +200 -7
- data/spec/core/array/frozen_spec.rb +32 -0
- data/spec/core/array/include_spec.rb +16 -1
- data/spec/core/array/index_spec.rb +5 -25
- data/spec/core/array/insert_spec.rb +37 -3
- data/spec/core/array/inspect_spec.rb +6 -12
- data/spec/core/array/intersection_spec.rb +55 -4
- data/spec/core/array/join_spec.rb +29 -4
- data/spec/core/array/keep_if_spec.rb +13 -6
- data/spec/core/array/last_spec.rb +35 -1
- data/spec/core/array/length_spec.rb +7 -4
- data/spec/core/array/map_spec.rb +9 -47
- data/spec/core/array/minus_spec.rb +68 -4
- data/spec/core/array/multiply_spec.rb +138 -6
- data/spec/core/array/new_spec.rb +92 -3
- data/spec/core/array/ntimes_spec.rb +26 -0
- data/spec/core/array/plus_spec.rb +48 -2
- data/spec/core/array/pop_spec.rb +159 -39
- data/spec/core/array/push_spec.rb +29 -1
- data/spec/core/array/rassoc_spec.rb +31 -2
- data/spec/core/array/reject_spec.rb +89 -2
- data/spec/core/array/replace_spec.rb +7 -29
- data/spec/core/array/reverse_each_spec.rb +25 -1
- data/spec/core/array/reverse_spec.rb +53 -1
- data/spec/core/array/rindex_spec.rb +55 -5
- data/spec/core/array/select_spec.rb +35 -8
- data/spec/core/array/shared/collect.rb +0 -0
- data/spec/core/array/shared/enumeratorize.rb +12 -0
- data/spec/core/array/shared/eql.rb +95 -0
- data/spec/core/array/shared/index.rb +37 -0
- data/spec/core/array/shared/inspect.rb +3 -0
- data/spec/core/array/shared/join.rb +7 -0
- data/spec/core/array/shared/keep_if.rb +0 -0
- data/spec/core/array/shared/length.rb +0 -0
- data/spec/core/array/shared/replace.rb +0 -0
- data/spec/core/array/shared/slice.rb +0 -0
- data/spec/core/array/shift_spec.rb +132 -23
- data/spec/core/array/shuffle_spec.rb +82 -6
- data/spec/core/array/size_spec.rb +7 -4
- data/spec/core/array/slice_spec.rb +132 -1
- data/spec/core/array/sort_spec.rb +263 -14
- data/spec/core/array/take_spec.rb +24 -16
- data/spec/core/array/take_while_spec.rb +14 -10
- data/spec/core/array/to_a_spec.rb +18 -1
- data/spec/core/array/to_ary_spec.rb +15 -1
- data/spec/core/array/try_convert_spec.rb +39 -2
- data/spec/core/array/uniq_spec.rb +148 -3
- data/spec/core/array/unshift_spec.rb +36 -1
- data/spec/core/array/zip_spec.rb +36 -1
- data/spec/core/class/new_spec.rb +8 -6
- data/spec/core/enumerable/all_spec.rb +37 -9
- data/spec/core/enumerable/any_spec.rb +45 -7
- data/spec/core/enumerable/collect_spec.rb +4 -1
- data/spec/core/enumerable/count_spec.rb +4 -1
- data/spec/core/enumerable/detect_spec.rb +2 -2
- data/spec/core/enumerable/drop_spec.rb +4 -1
- data/spec/core/enumerable/drop_while_spec.rb +4 -1
- data/spec/core/enumerable/each_slice_spec.rb +2 -1
- data/spec/core/enumerable/each_with_index_spec.rb +4 -1
- data/spec/core/enumerable/each_with_object_spec.rb +4 -1
- data/spec/core/enumerable/entries_spec.rb +4 -1
- data/spec/core/enumerable/find_all_spec.rb +4 -1
- data/spec/core/enumerable/find_index_spec.rb +4 -1
- data/spec/core/enumerable/find_spec.rb +5 -2
- data/spec/core/enumerable/first_spec.rb +4 -1
- data/spec/core/enumerable/fixtures/classes.rb +198 -2
- data/spec/core/enumerable/grep_spec.rb +4 -1
- data/spec/core/enumerable/take_spec.rb +4 -1
- data/spec/core/enumerable/to_a_spec.rb +4 -1
- data/spec/core/false/and_spec.rb +11 -0
- data/spec/core/false/inspect_spec.rb +7 -0
- data/spec/core/false/or_spec.rb +11 -0
- data/spec/core/false/to_s_spec.rb +7 -0
- data/spec/core/false/xor_spec.rb +11 -0
- data/spec/core/kernel/rand_spec.rb +5 -5
- data/spec/core/module/const_get_spec.rb +4 -4
- data/spec/core/module/fixtures/classes.rb +434 -0
- data/spec/core/module/method_defined_spec.rb +49 -0
- data/spec/core/module/module_function_spec.rb +28 -0
- data/spec/core/nil/and_spec.rb +3 -1
- data/spec/core/nil/dup_spec.rb +7 -0
- data/spec/core/nil/inspect_spec.rb +3 -1
- data/spec/core/nil/nil_spec.rb +3 -1
- data/spec/core/nil/or_spec.rb +4 -2
- data/spec/core/nil/to_a_spec.rb +3 -1
- data/spec/core/nil/to_f_spec.rb +3 -1
- data/spec/core/nil/to_i_spec.rb +3 -1
- data/spec/core/nil/to_s_spec.rb +3 -1
- data/spec/core/nil/xor_spec.rb +4 -2
- data/spec/core/string/element_reference_spec.rb +14 -1
- data/spec/core/string/fixtures/classes.rb +0 -0
- data/spec/core/true/and_spec.rb +11 -0
- data/spec/core/true/inspect_spec.rb +7 -0
- data/spec/core/true/or_spec.rb +11 -0
- data/spec/core/true/to_s_spec.rb +7 -0
- data/spec/core/true/xor_spec.rb +11 -0
- data/spec/{core → core_ext}/array/element_reference_spec.rb +0 -0
- data/spec/{core → core_ext}/array/equal_value_spec.rb +0 -0
- data/spec/{core → core_ext}/array/fill_spec.rb +0 -0
- data/spec/{core → core_ext}/array/reduce_spec.rb +0 -0
- data/spec/core_ext/basic_object/send_spec.rb +3 -3
- data/spec/{core → core_ext}/boolean/singleton_class_spec.rb +0 -0
- data/spec/{core → core_ext}/boolean/to_json_spec.rb +0 -0
- data/spec/core_ext/class/_inherited_spec.rb +3 -3
- data/spec/core_ext/class/proc_methods_spec.rb +2 -2
- data/spec/core_ext/class/singleton_methods_spec.rb +8 -8
- data/spec/core_ext/method_missing_spec.rb +3 -3
- data/spec/core_ext/native/method_missing_spec.rb +3 -2
- data/spec/core_ext/native/to_native_spec.rb +3 -2
- data/spec/{core → core_ext}/nil/to_json_spec.rb +0 -0
- data/spec/date.rb +0 -0
- data/spec/fileutils.rb +0 -0
- data/spec/filters/ancestors.rb +4 -0
- data/spec/filters/array_delete.rb +3 -0
- data/spec/filters/array_fetch.rb +3 -0
- data/spec/filters/array_first.rb +3 -0
- data/spec/filters/array_flatten.rb +14 -0
- data/spec/filters/array_intersection.rb +5 -0
- data/spec/filters/array_join.rb +6 -0
- data/spec/filters/array_subclasses.rb +4 -0
- data/spec/filters/block_args.rb +3 -0
- data/spec/filters/coerce_integer.rb +9 -0
- data/spec/filters/frozen.rb +4 -0
- data/spec/filters/mocks.rb +3 -0
- data/spec/filters/should_receive.rb +4 -0
- data/spec/filters/tainted.rb +7 -0
- data/spec/fixtures/class.rb +124 -0
- data/spec/fixtures/class_variables.rb +0 -0
- data/spec/fixtures/constants.rb +0 -0
- data/spec/grammar/alias_spec.rb +1 -1
- data/spec/grammar/def_spec.rb +1 -0
- data/spec/grammar/lvar_spec.rb +1 -2
- data/spec/grammar/nth_ref_spec.rb +13 -0
- data/spec/grammar/sclass_spec.rb +6 -7
- data/spec/grammar/str_spec.rb +4 -4
- data/spec/grammar/string_spec.rb +8 -0
- data/spec/grammar/xstr_spec.rb +4 -4
- data/spec/iconv.rb +0 -0
- data/spec/language/alias_spec.rb +140 -3
- data/spec/language/and_spec.rb +14 -7
- data/spec/language/array_spec.rb +57 -5
- data/spec/language/block_spec.rb +466 -49
- data/spec/language/break_spec.rb +294 -44
- data/spec/language/case_spec.rb +151 -3
- data/spec/language/class_spec.rb +196 -0
- data/spec/language/class_variable_spec.rb +56 -0
- data/spec/language/def_spec.rb +507 -4
- data/spec/language/defined_spec.rb +19 -7
- data/spec/language/ensure_spec.rb +26 -39
- data/spec/language/execution_spec.rb +15 -0
- data/spec/language/fixtures/array.rb +11 -0
- data/spec/language/fixtures/block.rb +57 -0
- data/spec/language/fixtures/break.rb +240 -0
- data/spec/language/fixtures/ensure.rb +72 -0
- data/spec/language/fixtures/literal_lambda.rb +7 -0
- data/spec/language/fixtures/metaclass.rb +33 -0
- data/spec/language/fixtures/module.rb +24 -0
- data/spec/language/fixtures/next.rb +78 -12
- data/spec/language/fixtures/return.rb +118 -0
- data/spec/language/fixtures/send.rb +110 -0
- data/spec/language/fixtures/send_1.9.rb +22 -0
- data/spec/language/fixtures/super.rb +308 -0
- data/spec/language/fixtures/variables.rb +58 -0
- data/spec/language/fixtures/yield.rb +5 -0
- data/spec/language/for_spec.rb +192 -0
- data/spec/language/hash_spec.rb +29 -5
- data/spec/language/if_spec.rb +90 -9
- data/spec/language/literal_lambda_spec.rb +1 -47
- data/spec/language/loop_spec.rb +39 -2
- data/spec/language/metaclass_spec.rb +151 -5
- data/spec/language/module_spec.rb +56 -0
- data/spec/language/next_spec.rb +370 -12
- data/spec/language/not_spec.rb +55 -0
- data/spec/language/numbers_spec.rb +56 -0
- data/spec/language/or_spec.rb +31 -3
- data/spec/language/order_spec.rb +79 -0
- data/spec/language/precedence_spec.rb +483 -0
- data/spec/language/proc_spec.rb +249 -21
- data/spec/language/redo_spec.rb +67 -0
- data/spec/language/rescue_spec.rb +121 -0
- data/spec/language/retry_spec.rb +56 -0
- data/spec/language/return_spec.rb +281 -0
- data/spec/language/send_spec.rb +141 -48
- data/spec/language/singleton_class_spec.rb +1 -1
- data/spec/language/string_spec.rb +11 -0
- data/spec/language/super_spec.rb +213 -133
- data/spec/language/symbol_spec.rb +2 -1
- data/spec/language/undef_spec.rb +3 -1
- data/spec/language/unless_spec.rb +6 -2
- data/spec/language/until_spec.rb +102 -3
- data/spec/language/variables_spec.rb +1212 -16
- data/spec/language/versions/array_1.9.rb +39 -0
- data/spec/language/versions/case_1.9.rb +20 -0
- data/spec/language/versions/hash_1.9.rb +18 -0
- data/spec/language/versions/literal_lambda_1.9.rb +143 -0
- data/spec/language/versions/not_1.9.rb +22 -0
- data/spec/language/versions/send_1.9.rb +241 -0
- data/spec/language/versions/symbol_1.9.rb +15 -0
- data/spec/language/versions/variables_1.9.rb +8 -0
- data/spec/language/while_spec.rb +70 -5
- data/spec/language/yield_spec.rb +32 -6
- data/spec/mspec/guards/block_device.rb +0 -0
- data/spec/mspec/guards/endian.rb +0 -0
- data/spec/mspec/helpers/environment.rb +0 -0
- data/spec/mspec/helpers/language_version.rb +0 -0
- data/spec/mspec/helpers/tmp.rb +0 -0
- data/spec/ospec/filter.rb +32 -0
- data/spec/ospec/main.rb.erb +18 -0
- data/spec/ospec/phantom.rb +97 -0
- data/spec/ospec/runner.rb +95 -0
- data/spec/ospec/sprockets.js +40 -0
- data/spec/pp.rb +3 -0
- data/spec/rbconfig.rb +5 -0
- data/spec/spec_helper.rb +53 -26
- data/spec/yaml.rb +0 -0
- metadata +275 -31
- data/config.ru +0 -8
- data/lib/opal/processor.rb +0 -47
- data/lib/opal/scope.rb +0 -236
- data/lib/opal/server.rb +0 -94
- data/spec/core/boolean/and_spec.rb +0 -17
- data/spec/core/boolean/inspect_spec.rb +0 -9
- data/spec/core/boolean/or_spec.rb +0 -17
- data/spec/core/boolean/to_s_spec.rb +0 -9
- data/spec/core/boolean/xor_spec.rb +0 -17
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "Array#combination" do
|
4
|
+
ruby_version_is "1.8.7" do
|
5
|
+
before :each do
|
6
|
+
@array = [1, 2, 3, 4]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns an enumerator when no block is provided" do
|
10
|
+
@array.combination(2).should be_an_instance_of(enumerator_class)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns self when a block is given" do
|
14
|
+
@array.combination(2){}.should equal(@array)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "yields nothing for out of bounds length and return self" do
|
18
|
+
@array.combination(5).to_a.should == []
|
19
|
+
@array.combination(-1).to_a.should == []
|
20
|
+
end
|
21
|
+
|
22
|
+
it "yields the expected combinations" do
|
23
|
+
@array.combination(3).to_a.sort.should == [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "yields nothing if the argument is out of bounds" do
|
27
|
+
@array.combination(-1).to_a.should == []
|
28
|
+
@array.combination(5).to_a.should == []
|
29
|
+
end
|
30
|
+
|
31
|
+
it "yields a copy of self if the argument is the size of the receiver" do
|
32
|
+
r = @array.combination(4).to_a
|
33
|
+
r.should == [@array]
|
34
|
+
r[0].should_not equal(@array)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "yields [] when length is 0" do
|
38
|
+
@array.combination(0).to_a.should == [[]] # one combination of length 0
|
39
|
+
[].combination(0).to_a.should == [[]] # one combination of length 0
|
40
|
+
end
|
41
|
+
|
42
|
+
it "yields a partition consisting of only singletons" do
|
43
|
+
@array.combination(1).to_a.sort.should == [[1],[2],[3],[4]]
|
44
|
+
end
|
45
|
+
|
46
|
+
it "generates from a defensive copy, ignoring mutations" do
|
47
|
+
accum = []
|
48
|
+
@array.combination(2) do |x|
|
49
|
+
accum << x
|
50
|
+
@array[0] = 1
|
51
|
+
end
|
52
|
+
accum.should == [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
+
|
1
4
|
describe "Array#compact" do
|
2
5
|
it "returns a copy of array with all nil elements removed" do
|
3
6
|
a = [1, 2, 4]
|
@@ -14,6 +17,46 @@ describe "Array#compact" do
|
|
14
17
|
a = [1, 2, 3]
|
15
18
|
a.compact.should_not equal(a)
|
16
19
|
end
|
20
|
+
|
21
|
+
ruby_version_is '' ... '1.9.3' do
|
22
|
+
it "keeps tainted status even if all elements are removed" do
|
23
|
+
a = [nil, nil]
|
24
|
+
a.taint
|
25
|
+
a.compact.tainted?.should be_true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
ruby_version_is '1.9' ... '1.9.3' do
|
30
|
+
it "keeps untrusted status even if all elements are removed" do
|
31
|
+
a = [nil, nil]
|
32
|
+
a.untrust
|
33
|
+
a.compact.untrusted?.should be_true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
ruby_version_is '' ... '1.9.3' do
|
38
|
+
it "returns subclass instance for Array subclasses" do
|
39
|
+
ArraySpecs::MyArray[1, 2, 3, nil].compact.should be_kind_of(ArraySpecs::MyArray)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
ruby_version_is '1.9.3' do
|
44
|
+
it "does not return subclass instance for Array subclasses" do
|
45
|
+
ArraySpecs::MyArray[1, 2, 3, nil].compact.should be_kind_of(Array)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "does not keep tainted status even if all elements are removed" do
|
49
|
+
a = [nil, nil]
|
50
|
+
a.taint
|
51
|
+
a.compact.tainted?.should be_false
|
52
|
+
end
|
53
|
+
|
54
|
+
it "does not keep untrusted status even if all elements are removed" do
|
55
|
+
a = [nil, nil]
|
56
|
+
a.untrust
|
57
|
+
a.compact.untrusted?.should be_false
|
58
|
+
end
|
59
|
+
end
|
17
60
|
end
|
18
61
|
|
19
62
|
describe "Array#compact!" do
|
@@ -37,4 +80,32 @@ describe "Array#compact!" do
|
|
37
80
|
it "returns nil if there are no nil elements to remove" do
|
38
81
|
[1, 2, false, 3].compact!.should == nil
|
39
82
|
end
|
40
|
-
|
83
|
+
|
84
|
+
it "keeps tainted status even if all elements are removed" do
|
85
|
+
a = [nil, nil]
|
86
|
+
a.taint
|
87
|
+
a.compact!
|
88
|
+
a.tainted?.should be_true
|
89
|
+
end
|
90
|
+
|
91
|
+
ruby_version_is '1.9' do
|
92
|
+
it "keeps untrusted status even if all elements are removed" do
|
93
|
+
a = [nil, nil]
|
94
|
+
a.untrust
|
95
|
+
a.compact!
|
96
|
+
a.untrusted?.should be_true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
ruby_version_is '' ... '1.9' do
|
101
|
+
it "raises a TypeError on a frozen array" do
|
102
|
+
lambda { ArraySpecs.frozen_array.compact! }.should raise_error(TypeError)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
ruby_version_is '1.9' do
|
107
|
+
it "raises a RuntimeError on a frozen array" do
|
108
|
+
lambda { ArraySpecs.frozen_array.compact! }.should raise_error(RuntimeError)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -1,13 +1,24 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
+
|
1
4
|
describe "Array.[]" do
|
2
5
|
it "returns a new array populated with the given elements" do
|
3
6
|
obj = Object.new
|
4
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
|
5
12
|
end
|
6
13
|
end
|
7
14
|
|
8
15
|
describe "Array[]" do
|
9
16
|
it "is a synonym for .[]" do
|
10
17
|
obj = Object.new
|
11
|
-
Array[5, true, nil, 'a', "Ruby", obj].should == [5, true, nil, "a", "Ruby", obj
|
18
|
+
Array[5, true, nil, 'a', "Ruby", obj].should == Array.[](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
|
12
23
|
end
|
13
|
-
end
|
24
|
+
end
|
@@ -1,9 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
ruby_version_is "1.8.7" do
|
4
|
+
describe "Array#count" do
|
5
|
+
it "returns the number of elements" do
|
6
|
+
[:a, :b, :c].count.should == 3
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns the number of elements that equal the argument" do
|
10
|
+
[:a, :b, :b, :c].count(:b).should == 2
|
11
|
+
end
|
5
12
|
|
6
|
-
|
7
|
-
|
13
|
+
it "returns the number of element for which the block evaluates to true" do
|
14
|
+
[:a, :b, :c].count { |s| s != :b }.should == 2
|
15
|
+
end
|
8
16
|
end
|
9
|
-
end
|
17
|
+
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
+
|
1
4
|
describe "Array#delete_at" do
|
2
5
|
it "removes the element at the specified index" do
|
3
6
|
a = [1, 2, 3, 4]
|
@@ -21,8 +24,48 @@ describe "Array#delete_at" do
|
|
21
24
|
a.should == [1, 2]
|
22
25
|
end
|
23
26
|
|
27
|
+
it "tries to convert the passed argument to an Integer using #to_int" do
|
28
|
+
obj = mock('to_int')
|
29
|
+
obj.should_receive(:to_int).and_return(-1)
|
30
|
+
[1, 2].delete_at(obj).should == 2
|
31
|
+
end
|
32
|
+
|
24
33
|
it "accepts negative indices" do
|
25
34
|
a = [1, 2]
|
26
35
|
a.delete_at(-2).should == 1
|
27
36
|
end
|
28
|
-
|
37
|
+
|
38
|
+
ruby_version_is '' ... '1.9' do
|
39
|
+
it "raises a TypeError on a frozen array" do
|
40
|
+
lambda { [1,2,3].freeze.delete_at(0) }.should raise_error(TypeError)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
ruby_version_is '1.9' do
|
45
|
+
it "raises a RuntimeError on a frozen array" do
|
46
|
+
lambda { [1,2,3].freeze.delete_at(0) }.should raise_error(RuntimeError)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it "keeps tainted status" do
|
51
|
+
ary = [1, 2]
|
52
|
+
ary.taint
|
53
|
+
ary.tainted?.should be_true
|
54
|
+
ary.delete_at(0)
|
55
|
+
ary.tainted?.should be_true
|
56
|
+
ary.delete_at(0) # now empty
|
57
|
+
ary.tainted?.should be_true
|
58
|
+
end
|
59
|
+
|
60
|
+
ruby_version_is '1.9' do
|
61
|
+
it "keeps untrusted status" do
|
62
|
+
ary = [1, 2]
|
63
|
+
ary.untrust
|
64
|
+
ary.untrusted?.should be_true
|
65
|
+
ary.delete_at(0)
|
66
|
+
ary.untrusted?.should be_true
|
67
|
+
ary.delete_at(0) # now empty
|
68
|
+
ary.untrusted?.should be_true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -1,3 +1,7 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
+
require File.expand_path('../shared/enumeratorize', __FILE__)
|
4
|
+
|
1
5
|
describe "Array#delete_if" do
|
2
6
|
before do
|
3
7
|
@a = [ "a", "b", "c" ]
|
@@ -10,12 +14,58 @@ describe "Array#delete_if" do
|
|
10
14
|
end
|
11
15
|
|
12
16
|
it "returns self" do
|
13
|
-
@a.delete_if
|
17
|
+
@a.delete_if{ true }.equal?(@a).should be_true
|
14
18
|
end
|
15
19
|
|
20
|
+
it_behaves_like :enumeratorize, :delete_if
|
21
|
+
|
16
22
|
it "returns self when called on an Array emptied with #shift" do
|
17
23
|
array = [1]
|
18
24
|
array.shift
|
19
25
|
array.delete_if { |x| true }.should equal(array)
|
20
26
|
end
|
21
|
-
|
27
|
+
|
28
|
+
ruby_version_is '1.8.7' do
|
29
|
+
it "returns an Enumerator if no block given, and the enumerator can modify the original array" do
|
30
|
+
enum = @a.delete_if
|
31
|
+
enum.should be_an_instance_of(enumerator_class)
|
32
|
+
@a.should_not be_empty
|
33
|
+
enum.each { true }
|
34
|
+
@a.should be_empty
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
ruby_version_is '' ... '1.9' do
|
39
|
+
it "raises a TypeError on a frozen array" do
|
40
|
+
lambda { ArraySpecs.frozen_array.delete_if {} }.should raise_error(TypeError)
|
41
|
+
end
|
42
|
+
it "raises a TypeError on an empty frozen array" do
|
43
|
+
lambda { ArraySpecs.empty_frozen_array.delete_if {} }.should raise_error(TypeError)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
ruby_version_is '1.9' do
|
48
|
+
it "raises a RuntimeError on a frozen array" do
|
49
|
+
lambda { ArraySpecs.frozen_array.delete_if {} }.should raise_error(RuntimeError)
|
50
|
+
end
|
51
|
+
it "raises a RuntimeError on an empty frozen array" do
|
52
|
+
lambda { ArraySpecs.empty_frozen_array.delete_if {} }.should raise_error(RuntimeError)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "keeps tainted status" do
|
57
|
+
@a.taint
|
58
|
+
@a.tainted?.should be_true
|
59
|
+
@a.delete_if{ true }
|
60
|
+
@a.tainted?.should be_true
|
61
|
+
end
|
62
|
+
|
63
|
+
ruby_version_is '1.9' do
|
64
|
+
it "keeps untrusted status" do
|
65
|
+
@a.untrust
|
66
|
+
@a.untrusted?.should be_true
|
67
|
+
@a.delete_if{ true }
|
68
|
+
@a.untrusted?.should be_true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
+
|
1
4
|
describe "Array#delete" do
|
2
5
|
it "removes elements that are #== to object" do
|
3
6
|
x = mock('delete')
|
@@ -5,19 +8,97 @@ describe "Array#delete" do
|
|
5
8
|
|
6
9
|
a = [1, 2, 3, x, 4, 3, 5, x]
|
7
10
|
a.delete mock('not contained')
|
11
|
+
a.should == [1, 2, 3, x, 4, 3, 5, x]
|
8
12
|
|
9
13
|
a.delete 3
|
10
14
|
a.should == [1, 2, 4, 5]
|
11
15
|
end
|
12
16
|
|
17
|
+
ruby_version_is '1.8.7' ... '1.9' do
|
18
|
+
it "returns the argument" do
|
19
|
+
x = mock('delete')
|
20
|
+
y = mock('delete_more')
|
21
|
+
def x.==(other) 3 == other end
|
22
|
+
def y.==(other) 3 == other end
|
23
|
+
|
24
|
+
a = [1, 2, 3, 4, 3, 5, x]
|
25
|
+
|
26
|
+
ret = a.delete y
|
27
|
+
ret.should equal(y)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
ruby_version_is '1.9' ... '2.0' do
|
32
|
+
it "returns the last element in the array for which object is equal under #==" do
|
33
|
+
x = mock('delete')
|
34
|
+
y = mock('delete_more')
|
35
|
+
def x.==(other) 3 == other end
|
36
|
+
def y.==(other) 3 == other end
|
37
|
+
|
38
|
+
a = [1, 2, 3, y, 4, 3, 5, x]
|
39
|
+
|
40
|
+
ret = a.delete 3
|
41
|
+
ret.should equal(x)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
13
45
|
it "calculates equality correctly for reference values" do
|
14
46
|
a = ["foo", "bar", "foo", "quux", "foo"]
|
15
47
|
a.delete "foo"
|
16
|
-
a.should == ["bar",
|
48
|
+
a.should == ["bar","quux"]
|
17
49
|
end
|
18
50
|
|
19
51
|
it "returns object or nil if no elements match object" do
|
20
52
|
[1, 2, 4, 5].delete(1).should == 1
|
21
53
|
[1, 2, 4, 5].delete(3).should == nil
|
22
54
|
end
|
23
|
-
|
55
|
+
|
56
|
+
it "may be given a block that is executed if no element matches object" do
|
57
|
+
[1].delete(1) {:not_found}.should == 1
|
58
|
+
[].delete('a') {:not_found}.should == :not_found
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns nil if the array is empty due to a shift" do
|
62
|
+
a = [1]
|
63
|
+
a.shift
|
64
|
+
a.delete(nil).should == nil
|
65
|
+
end
|
66
|
+
|
67
|
+
ruby_version_is '' ... '1.9' do
|
68
|
+
it "raises a TypeError on a frozen array if a modification would take place" do
|
69
|
+
lambda { [1, 2, 3].freeze.delete(1) }.should raise_error(TypeError)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "returns false on a frozen array if a modification does not take place" do
|
73
|
+
[1, 2, 3].freeze.delete(0).should == nil
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
ruby_version_is '1.9' do
|
78
|
+
it "raises a RuntimeError on a frozen array" do
|
79
|
+
lambda { [1, 2, 3].freeze.delete(1) }.should raise_error(RuntimeError)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
it "keeps tainted status" do
|
84
|
+
a = [1, 2]
|
85
|
+
a.taint
|
86
|
+
a.tainted?.should be_true
|
87
|
+
a.delete(2)
|
88
|
+
a.tainted?.should be_true
|
89
|
+
a.delete(1) # now empty
|
90
|
+
a.tainted?.should be_true
|
91
|
+
end
|
92
|
+
|
93
|
+
ruby_version_is '1.9' do
|
94
|
+
it "keeps untrusted status" do
|
95
|
+
a = [1, 2]
|
96
|
+
a.untrust
|
97
|
+
a.untrusted?.should be_true
|
98
|
+
a.delete(2)
|
99
|
+
a.untrusted?.should be_true
|
100
|
+
a.delete(1) # now empty
|
101
|
+
a.untrusted?.should be_true
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -1,21 +1,29 @@
|
|
1
|
-
|
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
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
ruby_version_is "1.9" do
|
4
|
+
describe "Array#drop" do
|
5
|
+
it "removes the specified number of elements from the start of the array" do
|
6
|
+
[1, 2, 3, 4, 5].drop(2).should == [3, 4, 5]
|
7
|
+
end
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
it "raises an ArgumentError if the number of elements specified is negative" do
|
10
|
+
lambda { [1, 2].drop(-3) }.should raise_error(ArgumentError)
|
11
|
+
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
it "returns an empty Array if all elements are dropped" do
|
14
|
+
[1, 2].drop(2).should == []
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns an empty Array when called on an empty Array" do
|
18
|
+
[].drop(0).should == []
|
19
|
+
end
|
20
|
+
|
21
|
+
it "does not remove any elements when passed zero" do
|
22
|
+
[1, 2].drop(0).should == [1, 2]
|
23
|
+
end
|
17
24
|
|
18
|
-
|
19
|
-
|
25
|
+
it "returns an empty Array if more elements than exist are dropped" do
|
26
|
+
[1, 2].drop(3).should == []
|
27
|
+
end
|
20
28
|
end
|
21
|
-
end
|
29
|
+
end
|