opal 0.3.41 → 0.3.42
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -6,7 +6,7 @@ describe "Enumerable#all?" do
|
|
6
6
|
before :each do
|
7
7
|
@enum = EnumerableSpecs::Numerous.new
|
8
8
|
@empty = EnumerableSpecs::Empty.new()
|
9
|
-
@enum1 =[0, 1, 2, -1]
|
9
|
+
@enum1 = [0, 1, 2, -1]
|
10
10
|
@enum2 = [nil, false, true]
|
11
11
|
end
|
12
12
|
|
@@ -31,7 +31,7 @@ describe "Enumerable#all?" do
|
|
31
31
|
}.should raise_error(RuntimeError)
|
32
32
|
end
|
33
33
|
|
34
|
-
|
34
|
+
describe "with no block" do
|
35
35
|
it "returns true if no elements are false or nil" do
|
36
36
|
@enum.all?.should == true
|
37
37
|
@enum1.all?.should == true
|
@@ -52,19 +52,25 @@ describe "Enumerable#all?" do
|
|
52
52
|
EnumerableSpecs::Numerous.new(0, "x", false, true).all?.should == false
|
53
53
|
@enum2.all?.should == false
|
54
54
|
end
|
55
|
-
#end
|
56
55
|
|
57
|
-
|
56
|
+
it "gathers whole arrays as elements when each yields multiple" do
|
57
|
+
multi = EnumerableSpecs::YieldsMultiWithFalse.new
|
58
|
+
multi.all?.should be_true
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "with block" do
|
58
64
|
it "returns true if the block never returns false or nil" do
|
59
65
|
@enum.all? { true }.should == true
|
60
|
-
@enum1.all?
|
61
|
-
@enum1.all?
|
66
|
+
@enum1.all?{ |o| o < 5 }.should == true
|
67
|
+
@enum1.all?{ |o| 5 }.should == true
|
62
68
|
end
|
63
69
|
|
64
70
|
it "returns false if the block ever returns false or nil" do
|
65
71
|
@enum.all? { false }.should == false
|
66
72
|
@enum.all? { nil }.should == false
|
67
|
-
@enum1.all?
|
73
|
+
@enum1.all?{ |o| o > 2 }.should == false
|
68
74
|
|
69
75
|
EnumerableSpecs::Numerous.new.all? { |i| i > 5 }.should == false
|
70
76
|
EnumerableSpecs::Numerous.new.all? { |i| i == 3 ? nil : true }.should == false
|
@@ -98,5 +104,27 @@ describe "Enumerable#all?" do
|
|
98
104
|
@enum.all? { raise "from block" }
|
99
105
|
}.should raise_error(RuntimeError)
|
100
106
|
end
|
101
|
-
|
102
|
-
|
107
|
+
|
108
|
+
ruby_version_is "" ... "1.9" do
|
109
|
+
it "gathers whole arrays as elements when each yields multiple" do
|
110
|
+
multi = EnumerableSpecs::YieldsMulti.new
|
111
|
+
multi.all? {|e| Array === e}.should be_true
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
ruby_version_is "1.9" do
|
116
|
+
it "gathers initial args as elements when each yields multiple" do
|
117
|
+
multi = EnumerableSpecs::YieldsMulti.new
|
118
|
+
multi.all? {|e| !(Array === e) }.should be_true
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
it "yields multiple arguments when each yields multiple" do
|
123
|
+
multi = EnumerableSpecs::YieldsMulti.new
|
124
|
+
yielded = []
|
125
|
+
multi.all? {|e, i| yielded << [e, i] }
|
126
|
+
yielded.should == [[1, 2], [3, 4], [6, 7]]
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
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 "Enumerable#any?" do
|
2
5
|
before :each do
|
3
6
|
@enum = EnumerableSpecs::Numerous.new
|
@@ -17,6 +20,14 @@ describe "Enumerable#any?" do
|
|
17
20
|
{}.any? { nil }.should == false
|
18
21
|
end
|
19
22
|
|
23
|
+
it "raises an ArgumentError when any arguments provided" do
|
24
|
+
lambda { @enum.any?(Proc.new {}) }.should raise_error(ArgumentError)
|
25
|
+
lambda { @enum.any?(nil) }.should raise_error(ArgumentError)
|
26
|
+
lambda { @empty.any?(1) }.should raise_error(ArgumentError)
|
27
|
+
lambda { @enum1.any?(1) {} }.should raise_error(ArgumentError)
|
28
|
+
lambda { @enum2.any?(1, 2, 3) {} }.should raise_error(ArgumentError)
|
29
|
+
end
|
30
|
+
|
20
31
|
it "does not hide exceptions out of #each" do
|
21
32
|
lambda {
|
22
33
|
EnumerableSpecs::ThrowingEach.new.any?
|
@@ -27,7 +38,7 @@ describe "Enumerable#any?" do
|
|
27
38
|
}.should raise_error(RuntimeError)
|
28
39
|
end
|
29
40
|
|
30
|
-
|
41
|
+
describe "with no block" do
|
31
42
|
it "returns true if any element is not false or nil" do
|
32
43
|
@enum.any?.should == true
|
33
44
|
@enum1.any?.should == true
|
@@ -41,16 +52,21 @@ describe "Enumerable#any?" do
|
|
41
52
|
EnumerableSpecs::Numerous.new(false, 0, nil).any?.should == true
|
42
53
|
end
|
43
54
|
|
44
|
-
it "returns false if all
|
55
|
+
it "returns false if all elements are false or nil" do
|
45
56
|
EnumerableSpecs::Numerous.new(false).any?.should == false
|
46
57
|
EnumerableSpecs::Numerous.new(false, false).any?.should == false
|
47
58
|
EnumerableSpecs::Numerous.new(nil).any?.should == false
|
48
59
|
EnumerableSpecs::Numerous.new(nil, nil).any?.should == false
|
49
60
|
EnumerableSpecs::Numerous.new(nil, false, nil).any?.should == false
|
50
61
|
end
|
51
|
-
#end
|
52
62
|
|
53
|
-
|
63
|
+
it "gathers whole arrays as elements when each yields multiple" do
|
64
|
+
multi = EnumerableSpecs::YieldsMultiWithFalse.new
|
65
|
+
multi.any?.should be_true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "with block" do
|
54
70
|
it "returns true if the block ever returns other than false or nil" do
|
55
71
|
@enum.any? { true } == true
|
56
72
|
@enum.any? { 0 } == true
|
@@ -65,7 +81,7 @@ describe "Enumerable#any?" do
|
|
65
81
|
|
66
82
|
it "any? should return false if the block never returns other than false or nil" do
|
67
83
|
@enum.any? { false }.should == false
|
68
|
-
@enum.any? {
|
84
|
+
@enum.any? { nil }.should == false
|
69
85
|
|
70
86
|
@enum1.any?{ |o| o < -10 }.should == false
|
71
87
|
@enum1.any?{ |o| nil }.should == false
|
@@ -108,5 +124,27 @@ describe "Enumerable#any?" do
|
|
108
124
|
@enum.any? { raise "from block" }
|
109
125
|
}.should raise_error(RuntimeError)
|
110
126
|
end
|
111
|
-
|
112
|
-
|
127
|
+
|
128
|
+
ruby_version_is "" ... "1.9" do
|
129
|
+
it "gathers whole arrays as elements when each yields multiple" do
|
130
|
+
multi = EnumerableSpecs::YieldsMulti.new
|
131
|
+
multi.any? {|e| e == [1, 2] }.should be_true
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
ruby_version_is "1.9" do
|
136
|
+
it "gathers initial args as elements when each yields multiple" do
|
137
|
+
multi = EnumerableSpecs::YieldsMulti.new
|
138
|
+
multi.any? {|e| e == 1 }.should be_true
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
it "yields multiple arguments when each yields multiple" do
|
143
|
+
multi = EnumerableSpecs::YieldsMulti.new
|
144
|
+
yielded = []
|
145
|
+
multi.any? {|e, i| yielded << [e, i] }
|
146
|
+
yielded.should == [[1, 2]]
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
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 "Enumerable#collect" do
|
2
5
|
before :each do
|
3
6
|
ScratchPad.record []
|
@@ -9,4 +12,4 @@ describe "Enumerable#collect" do
|
|
9
12
|
numerous.collect { |i| i % 2 }.should == [0, 1, 1, 0, 1, 0]
|
10
13
|
numerous.collect { |i| i }.should == entries
|
11
14
|
end
|
12
|
-
end
|
15
|
+
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 "Enumerable#count" do
|
2
5
|
before :each do
|
3
6
|
@elements = [1, 2, 4, 2]
|
@@ -23,4 +26,4 @@ describe "Enumerable#count" do
|
|
23
26
|
it "ignores the block when given an argument" do
|
24
27
|
@numerous.count(4){|x| x%2==0 }.should == 1
|
25
28
|
end
|
26
|
-
end
|
29
|
+
end
|
@@ -21,7 +21,7 @@ describe "Enumerable#detect" do
|
|
21
21
|
|
22
22
|
it "returns the first element for which the block is not false" do
|
23
23
|
@elements.each do |element|
|
24
|
-
|
24
|
+
@numerous.detect {|e| e > element - 1 }.should == element
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -45,4 +45,4 @@ describe "Enumerable#detect" do
|
|
45
45
|
fail_proc = lambda { "yay" }
|
46
46
|
@empty.detect(fail_proc) {|e| true}.should == "yay"
|
47
47
|
end
|
48
|
-
end
|
48
|
+
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 "Enumerable#drop" do
|
2
5
|
before :each do
|
3
6
|
@enum = EnumerableSpecs::Numerous.new(3, 2, 1, :go)
|
@@ -14,4 +17,4 @@ describe "Enumerable#drop" do
|
|
14
17
|
EnumerableSpecs::Numerous.new(3, 2, 1, :go).drop(4).should == []
|
15
18
|
end
|
16
19
|
end
|
17
|
-
end
|
20
|
+
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 "Enumerable#drop_while" do
|
2
5
|
before :each do
|
3
6
|
@enum = EnumerableSpecs::Numerous.new(3, 2, 1, :go)
|
@@ -12,4 +15,4 @@ describe "Enumerable#drop_while" do
|
|
12
15
|
@enum.drop_while{1}.should == []
|
13
16
|
@enum.drop_while{nil}.should == @enum.to_a
|
14
17
|
end
|
15
|
-
end
|
18
|
+
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 "Enumerable#each_with_index" do
|
2
5
|
before :each do
|
3
6
|
@b = EnumerableSpecs::Numerous.new(2, 5, 3, 6, 1, 4)
|
@@ -8,4 +11,4 @@ describe "Enumerable#each_with_index" do
|
|
8
11
|
@b.each_with_index { |o, i| @a << [o, i] }
|
9
12
|
@a.should == [[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]]
|
10
13
|
end
|
11
|
-
end
|
14
|
+
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 "Enumerable#each_with_object" do
|
2
5
|
before :each do
|
3
6
|
@values = [2, 5, 3, 6, 1, 4]
|
@@ -14,4 +17,4 @@ describe "Enumerable#each_with_object" do
|
|
14
17
|
end.should equal(@initial)
|
15
18
|
acc.should == @values
|
16
19
|
end
|
17
|
-
end
|
20
|
+
end
|
@@ -1,6 +1,9 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
+
|
1
4
|
describe "Enumerable#entries" do
|
2
5
|
it "returns an array containing the elements" do
|
3
6
|
numerous = EnumerableSpecs::Numerous.new(1, nil, 'a', 2, false, true)
|
4
7
|
numerous.entries.should == [1, nil, "a", 2, false, true]
|
5
8
|
end
|
6
|
-
end
|
9
|
+
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 "Enumerable#find_all" do
|
2
5
|
before :each do
|
3
6
|
ScratchPad.record []
|
@@ -10,4 +13,4 @@ describe "Enumerable#find_all" do
|
|
10
13
|
@numerous.find_all {|i| true }.should == @elements
|
11
14
|
@numerous.find_all {|i| false }.should == []
|
12
15
|
end
|
13
|
-
end
|
16
|
+
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 "Enumerable#find_index" do
|
2
5
|
before :each do
|
3
6
|
@elements = [2, 4, 6, 8, 10]
|
@@ -38,4 +41,4 @@ describe "Enumerable#find_index" do
|
|
38
41
|
it "ignores the block if an argument is given" do
|
39
42
|
@numerous.find_index(-1) {|e| true }.should == nil
|
40
43
|
end
|
41
|
-
end
|
44
|
+
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 "Enumerable#find" do
|
2
5
|
before :each do
|
3
6
|
ScratchPad.record []
|
@@ -21,7 +24,7 @@ describe "Enumerable#find" do
|
|
21
24
|
|
22
25
|
it "returns the first element for which the block is not false" do
|
23
26
|
@elements.each do |element|
|
24
|
-
|
27
|
+
@numerous.find {|e| e > element - 1 }.should == element
|
25
28
|
end
|
26
29
|
end
|
27
30
|
|
@@ -45,4 +48,4 @@ describe "Enumerable#find" do
|
|
45
48
|
fail_proc = lambda { "yay" }
|
46
49
|
@empty.find(fail_proc) {|e| true}.should == "yay"
|
47
50
|
end
|
48
|
-
end
|
51
|
+
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 "Enumerable#first" do
|
2
5
|
before :each do
|
3
6
|
@values = [4,3,2,1,0,-1]
|
@@ -37,4 +40,4 @@ describe "Enumerable#first" do
|
|
37
40
|
@enum.first(100).should == @values
|
38
41
|
@enum.first(8).should == @values
|
39
42
|
end
|
40
|
-
end
|
43
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
module EnumerableSpecs
|
2
|
+
|
2
3
|
class Numerous
|
3
4
|
include Enumerable
|
4
5
|
def initialize(*list)
|
@@ -10,6 +11,24 @@ module EnumerableSpecs
|
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
14
|
+
class EachCounter < Numerous
|
15
|
+
attr_reader :times_called, :times_yielded, :arguments_passed
|
16
|
+
def initialize(*list)
|
17
|
+
super(*list)
|
18
|
+
@times_yielded = @times_called = 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def each(*arg)
|
22
|
+
@times_called += 1
|
23
|
+
@times_yielded = 0
|
24
|
+
@arguments_passed = arg
|
25
|
+
@list.each do |i|
|
26
|
+
@times_yielded +=1
|
27
|
+
yield i
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
13
32
|
class Empty
|
14
33
|
include Enumerable
|
15
34
|
def each
|
@@ -23,12 +42,18 @@ module EnumerableSpecs
|
|
23
42
|
end
|
24
43
|
end
|
25
44
|
|
45
|
+
class NoEach
|
46
|
+
include Enumerable
|
47
|
+
end
|
48
|
+
|
49
|
+
# (Legacy form rubycon)
|
26
50
|
class EachDefiner
|
51
|
+
|
27
52
|
include Enumerable
|
28
53
|
|
29
54
|
attr_reader :arr
|
30
55
|
|
31
|
-
def initialize(*
|
56
|
+
def initialize(*arr)
|
32
57
|
@arr = arr
|
33
58
|
end
|
34
59
|
|
@@ -40,5 +65,176 @@ module EnumerableSpecs
|
|
40
65
|
i += 1
|
41
66
|
end
|
42
67
|
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
class SortByDummy
|
72
|
+
def initialize(s)
|
73
|
+
@s = s
|
74
|
+
end
|
75
|
+
|
76
|
+
def s
|
77
|
+
@s
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class ComparesByVowelCount
|
82
|
+
|
83
|
+
attr_accessor :value, :vowels
|
84
|
+
|
85
|
+
def self.wrap(*args)
|
86
|
+
args.map {|element| ComparesByVowelCount.new(element)}
|
87
|
+
end
|
88
|
+
|
89
|
+
def initialize(string)
|
90
|
+
self.value = string
|
91
|
+
all_vowels = ['a', 'e' , 'i' , 'o', 'u']
|
92
|
+
self.vowels = string.gsub(/[^aeiou]/,'').size
|
93
|
+
end
|
94
|
+
|
95
|
+
def <=>(other)
|
96
|
+
self.vowels <=> other.vowels
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
class InvalidComparable
|
102
|
+
def <=>(other)
|
103
|
+
"Not Valid"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
class ArrayConvertable
|
108
|
+
attr_accessor :called
|
109
|
+
def initialize(*values)
|
110
|
+
@values = values
|
111
|
+
end
|
112
|
+
|
113
|
+
def to_a
|
114
|
+
self.called = :to_a
|
115
|
+
@values
|
116
|
+
end
|
117
|
+
|
118
|
+
def to_ary
|
119
|
+
self.called = :to_ary
|
120
|
+
@values
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
class EnumConvertable
|
125
|
+
attr_accessor :called
|
126
|
+
attr_accessor :sym
|
127
|
+
def initialize(delegate)
|
128
|
+
@delegate = delegate
|
129
|
+
end
|
130
|
+
|
131
|
+
def to_enum(sym)
|
132
|
+
self.called = :to_enum
|
133
|
+
self.sym = sym
|
134
|
+
@delegate.to_enum(sym)
|
135
|
+
end
|
136
|
+
|
137
|
+
def respond_to_missing?(*args)
|
138
|
+
@delegate.respond_to?(*args)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
class Equals
|
143
|
+
def initialize(obj)
|
144
|
+
@obj = obj
|
145
|
+
end
|
146
|
+
def ==(other)
|
147
|
+
@obj == other
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
class YieldsMulti
|
152
|
+
include Enumerable
|
153
|
+
def each
|
154
|
+
yield 1,2
|
155
|
+
yield 3,4,5
|
156
|
+
yield 6,7,8,9
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
class YieldsMultiWithFalse
|
161
|
+
include Enumerable
|
162
|
+
def each
|
163
|
+
yield false,2
|
164
|
+
yield false,4,5
|
165
|
+
yield false,7,8,9
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
class YieldsMultiWithSingleTrue
|
170
|
+
include Enumerable
|
171
|
+
def each
|
172
|
+
yield false,2
|
173
|
+
yield true,4,5
|
174
|
+
yield false,7,8,9
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
class YieldsMixed
|
179
|
+
include Enumerable
|
180
|
+
def each
|
181
|
+
yield 1
|
182
|
+
yield [2]
|
183
|
+
yield 3,4
|
184
|
+
yield 5,6,7
|
185
|
+
yield [8,9]
|
186
|
+
yield nil
|
187
|
+
yield []
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
class ReverseComparable
|
192
|
+
include Comparable
|
193
|
+
def initialize(num)
|
194
|
+
@num = num
|
195
|
+
end
|
196
|
+
|
197
|
+
attr_accessor :num
|
198
|
+
|
199
|
+
# Reverse comparison
|
200
|
+
def <=>(other)
|
201
|
+
other.num <=> @num
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
class ComparableWithFixnum
|
206
|
+
include Comparable
|
207
|
+
def initialize(num)
|
208
|
+
@num = num
|
209
|
+
end
|
210
|
+
|
211
|
+
def <=>(fixnum)
|
212
|
+
@num <=> fixnum
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
class Uncomparable
|
217
|
+
def <=>(obj)
|
218
|
+
nil
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
class Undupable
|
223
|
+
attr_reader :initialize_called, :initialize_dup_called
|
224
|
+
def dup
|
225
|
+
raise "Can't, sorry"
|
226
|
+
end
|
227
|
+
|
228
|
+
def clone
|
229
|
+
raise "Can't, either, sorry"
|
230
|
+
end
|
231
|
+
|
232
|
+
def initialize
|
233
|
+
@initialize_dup = true
|
234
|
+
end
|
235
|
+
|
236
|
+
def initialize_dup(arg)
|
237
|
+
@initialize_dup_called = true
|
238
|
+
end
|
43
239
|
end
|
44
|
-
end
|
240
|
+
end # EnumerableSpecs utility classes
|