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
data/spec/language/proc_spec.rb
CHANGED
@@ -1,37 +1,265 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "A Proc" do
|
4
|
+
it "captures locals from the surrounding scope" do
|
5
|
+
var = 1
|
6
|
+
lambda { var }.call.should == 1
|
4
7
|
end
|
5
8
|
|
6
|
-
|
7
|
-
|
9
|
+
ruby_version_is ""..."1.9" do
|
10
|
+
it "overwrites a captured local when used as an argument" do
|
11
|
+
var = 1
|
12
|
+
lambda { |var| var }.call(2).should == 2
|
13
|
+
var.should == 2
|
14
|
+
end
|
8
15
|
end
|
9
16
|
|
10
|
-
|
11
|
-
|
17
|
+
ruby_version_is "1.9" do
|
18
|
+
it "does not capture a local when an argument has the same name" do
|
19
|
+
var = 1
|
20
|
+
lambda { |var| var }.call(2).should == 2
|
21
|
+
var.should == 1
|
22
|
+
end
|
12
23
|
end
|
13
|
-
end
|
14
24
|
|
15
|
-
describe "
|
16
|
-
|
17
|
-
|
25
|
+
describe "taking zero arguments" do
|
26
|
+
before :each do
|
27
|
+
@l = lambda { 1 }
|
28
|
+
end
|
29
|
+
|
30
|
+
it "does not raise an exception if no values are passed" do
|
31
|
+
@l.call.should == 1
|
32
|
+
end
|
33
|
+
|
34
|
+
ruby_version_is ""..."1.9" do
|
35
|
+
it "does not raise an exception if a value is passed" do
|
36
|
+
@l.call(0).should == 1
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
ruby_version_is "1.9" do
|
41
|
+
pending "raises an ArgumentErro if a value is passed" do
|
42
|
+
lambda { @l.call(0) }.should raise_error(ArgumentError)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "taking || arguments" do
|
48
|
+
before :each do
|
49
|
+
@l = lambda { || 1 }
|
50
|
+
end
|
51
|
+
|
52
|
+
it "does not raise an exception when passed no values" do
|
53
|
+
@l.call.should == 1
|
54
|
+
end
|
55
|
+
|
56
|
+
pending "raises an ArgumentError if a value is passed" do
|
57
|
+
lambda { @l.call(0) }.should raise_error(ArgumentError)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "taking |a| arguments" do
|
62
|
+
before :each do
|
63
|
+
@l = lambda { |a| a }
|
64
|
+
end
|
65
|
+
|
66
|
+
pending "assigns the value passed to the argument" do
|
67
|
+
@l.call(2).should == 2
|
68
|
+
end
|
69
|
+
|
70
|
+
pending "does not destructure a single Array value" do
|
71
|
+
@l.call([1, 2]).should == [1, 2]
|
72
|
+
end
|
73
|
+
|
74
|
+
pending "does not call #to_ary to convert a single passed object to an Array" do
|
75
|
+
obj = mock("block yield to_ary")
|
76
|
+
obj.should_not_receive(:to_ary)
|
77
|
+
|
78
|
+
@l.call(obj).should equal(obj)
|
79
|
+
end
|
80
|
+
|
81
|
+
ruby_version_is ""..."1.9" do
|
82
|
+
it "assigns nil to the argument if no value is passed" do
|
83
|
+
@l.call.should be_nil
|
84
|
+
end
|
85
|
+
|
86
|
+
it "assigns all the values passed to the argument as an Array" do
|
87
|
+
@l.call(1, 2).should == [1, 2]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
ruby_version_is "1.9" do
|
92
|
+
pending "raises an ArgumentError if no value is passed" do
|
93
|
+
lambda { @l.call }.should raise_error(ArgumentError)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "taking |a, b| arguments" do
|
99
|
+
before :each do
|
100
|
+
@l = lambda { |a, b| [a, b] }
|
101
|
+
end
|
102
|
+
|
103
|
+
pending "raises an ArgumentError if passed no values" do
|
104
|
+
lambda { @l.call }.should raise_error(ArgumentError)
|
105
|
+
end
|
106
|
+
|
107
|
+
pending "raises an ArgumentError if passed one value" do
|
108
|
+
lambda { @l.call(0) }.should raise_error(ArgumentError)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "assigns the values passed to the arguments" do
|
112
|
+
@l.call(1, 2).should == [1, 2]
|
113
|
+
end
|
114
|
+
|
115
|
+
pending "does not call #to_ary to convert a single passed object to an Array" do
|
116
|
+
obj = mock("proc call to_ary")
|
117
|
+
obj.should_not_receive(:to_ary)
|
118
|
+
|
119
|
+
lambda { @l.call(obj) }.should raise_error(ArgumentError)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe "taking |a, *b| arguments" do
|
124
|
+
before :each do
|
125
|
+
@l = lambda { |a, *b| [a, b] }
|
126
|
+
end
|
127
|
+
|
128
|
+
pending "raises an ArgumentError if passed no values" do
|
129
|
+
lambda { @l.call }.should raise_error(ArgumentError)
|
130
|
+
end
|
131
|
+
|
132
|
+
pending "does not destructure a single Array value yielded" do
|
133
|
+
@l.call([1, 2, 3]).should == [[1, 2, 3], []]
|
134
|
+
end
|
135
|
+
|
136
|
+
pending "assigns all passed values after the first to the rest argument" do
|
137
|
+
@l.call(1, 2, 3).should == [1, [2, 3]]
|
138
|
+
end
|
139
|
+
|
140
|
+
pending "does not call #to_ary to convert a single passed object to an Array" do
|
141
|
+
obj = mock("block yield to_ary")
|
142
|
+
obj.should_not_receive(:to_ary)
|
143
|
+
|
144
|
+
@l.call(obj).should == [obj, []]
|
145
|
+
end
|
18
146
|
end
|
19
147
|
|
20
|
-
|
21
|
-
|
148
|
+
describe "taking |*| arguments" do
|
149
|
+
before :each do
|
150
|
+
# @l = lambda { |*| 1 }
|
151
|
+
end
|
152
|
+
|
153
|
+
pending "does not raise an exception when passed no values" do
|
154
|
+
@l.call.should == 1
|
155
|
+
end
|
156
|
+
|
157
|
+
pending "does not raise an exception when passed multiple values" do
|
158
|
+
@l.call(2, 3, 4).should == 1
|
159
|
+
end
|
160
|
+
|
161
|
+
pending "does not call #to_ary to convert a single passed object to an Array" do
|
162
|
+
obj = mock("block yield to_ary")
|
163
|
+
obj.should_not_receive(:to_ary)
|
164
|
+
|
165
|
+
@l.call(obj).should == 1
|
166
|
+
end
|
22
167
|
end
|
23
168
|
|
24
|
-
|
25
|
-
|
26
|
-
|
169
|
+
describe "taking |*a| arguments" do
|
170
|
+
before :each do
|
171
|
+
@l = lambda { |*a| a }
|
172
|
+
end
|
173
|
+
|
174
|
+
pending "assigns [] to the argument when passed no values" do
|
175
|
+
@l.call.should == []
|
176
|
+
end
|
177
|
+
|
178
|
+
pending "assigns the argument an Array wrapping one passed value" do
|
179
|
+
@l.call(1).should == [1]
|
180
|
+
end
|
181
|
+
|
182
|
+
pending "assigns the argument an Array wrapping all values passed" do
|
183
|
+
@l.call(1, 2, 3).should == [1, 2, 3]
|
184
|
+
end
|
185
|
+
|
186
|
+
pending "does not call #to_ary to convert a single passed object to an Array" do
|
187
|
+
obj = mock("block yield to_ary")
|
188
|
+
obj.should_not_receive(:to_ary)
|
189
|
+
|
190
|
+
@l.call(obj).should == [obj]
|
191
|
+
end
|
27
192
|
end
|
28
193
|
|
29
|
-
|
30
|
-
|
194
|
+
describe "taking |a, | arguments" do
|
195
|
+
before :each do
|
196
|
+
# @l = lambda { |a, | a }
|
197
|
+
end
|
198
|
+
|
199
|
+
pending "raises an ArgumentError when passed no values" do
|
200
|
+
lambda { @l.call }.should raise_error(ArgumentError)
|
201
|
+
end
|
202
|
+
|
203
|
+
pending "raises an ArgumentError when passed more than one value" do
|
204
|
+
lambda { @l.call(1, 2) }.should raise_error(ArgumentError)
|
205
|
+
end
|
206
|
+
|
207
|
+
pending "assigns the argument the value passed" do
|
208
|
+
@l.call(1).should == 1
|
209
|
+
end
|
210
|
+
|
211
|
+
pending "does not destructure when passed a single Array" do
|
212
|
+
@l.call([1,2]).should == [1, 2]
|
213
|
+
end
|
214
|
+
|
215
|
+
pending "does not call #to_ary to convert a single passed object to an Array" do
|
216
|
+
obj = mock("block yield to_ary")
|
217
|
+
obj.should_not_receive(:to_ary)
|
218
|
+
|
219
|
+
@l.call(obj).should == obj
|
220
|
+
end
|
31
221
|
end
|
32
222
|
|
33
|
-
|
34
|
-
|
35
|
-
|
223
|
+
describe "taking |(a, b)| arguments" do
|
224
|
+
before :each do
|
225
|
+
# @l = lambda { |(a, b)| [a, b] }
|
226
|
+
end
|
227
|
+
|
228
|
+
pending "raises an ArgumentError when passed no values" do
|
229
|
+
lambda { @l.call }.should raise_error(ArgumentError)
|
230
|
+
end
|
231
|
+
|
232
|
+
ruby_version_is ""..."1.9" do
|
233
|
+
it "raises an ArgumentError when passed a single Array" do
|
234
|
+
lambda { @l.call([1, 2]) }.should raise_error(ArgumentError)
|
235
|
+
end
|
236
|
+
|
237
|
+
it "raises an ArgumentError when passed a single object" do
|
238
|
+
obj = mock("block yield to_ary")
|
239
|
+
obj.should_not_receive(:to_ary)
|
240
|
+
|
241
|
+
lambda { @l.call(obj) }.should raise_error(ArgumentError)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
ruby_version_is "1.9" do
|
246
|
+
pending "destructures a single Array value yielded" do
|
247
|
+
@l.call([1, 2]).should == [1, 2]
|
248
|
+
end
|
249
|
+
|
250
|
+
pending "calls #to_ary to convert a single passed object to an Array" do
|
251
|
+
obj = mock("block yield to_ary")
|
252
|
+
obj.should_receive(:to_ary).and_return([1, 2])
|
253
|
+
|
254
|
+
@l.call(obj).should == [1, 2]
|
255
|
+
end
|
256
|
+
|
257
|
+
pending "raises an TypeError if #to_ary does not return an Array" do
|
258
|
+
obj = mock("block yield to_ary invalid")
|
259
|
+
obj.should_receive(:to_ary).and_return(1)
|
260
|
+
|
261
|
+
lambda { @l.call(obj) }.should raise_error(TypeError)
|
262
|
+
end
|
263
|
+
end
|
36
264
|
end
|
37
265
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "The redo statement" do
|
4
|
+
pending "restarts block execution if used within block" do
|
5
|
+
a = []
|
6
|
+
lambda {
|
7
|
+
a << 1
|
8
|
+
redo if a.size < 2
|
9
|
+
a << 2
|
10
|
+
}.call
|
11
|
+
a.should == [1, 1, 2]
|
12
|
+
end
|
13
|
+
|
14
|
+
pending "re-executes the closest loop" do
|
15
|
+
exist = [2,3]
|
16
|
+
processed = []
|
17
|
+
order = []
|
18
|
+
[1,2,3,4].each do |x|
|
19
|
+
order << x
|
20
|
+
begin
|
21
|
+
processed << x
|
22
|
+
if(exist.include?(x))
|
23
|
+
raise StandardError, "included"
|
24
|
+
end
|
25
|
+
rescue StandardError => e
|
26
|
+
exist.delete(x)
|
27
|
+
redo
|
28
|
+
end
|
29
|
+
end
|
30
|
+
processed.should == [1,2,2,3,3,4]
|
31
|
+
exist.should == []
|
32
|
+
order.should == [1,2,2,3,3,4]
|
33
|
+
end
|
34
|
+
|
35
|
+
pending "re-executes the last step in enumeration" do
|
36
|
+
list = []
|
37
|
+
[1,2,3].each do |x|
|
38
|
+
list << x
|
39
|
+
break if list.size == 6
|
40
|
+
redo if x == 3
|
41
|
+
end
|
42
|
+
list.should == [1,2,3,3,3,3]
|
43
|
+
end
|
44
|
+
|
45
|
+
# The #count method is on 1.9, but this causes SyntaxError,
|
46
|
+
# Invalid redo in 1.9
|
47
|
+
pending do
|
48
|
+
quarantine! do
|
49
|
+
it "triggers ensure block when re-executing a block" do
|
50
|
+
list = []
|
51
|
+
[1,2,3].each do |x|
|
52
|
+
list << x
|
53
|
+
begin
|
54
|
+
list << 10*x
|
55
|
+
# causes SyntaxError in 1.9
|
56
|
+
# redo if list.count(1) == 1
|
57
|
+
ensure
|
58
|
+
list << 100*x
|
59
|
+
end
|
60
|
+
end
|
61
|
+
list.should == [1,10,100,1,10,100,2,20,200,3,30,300]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# language_version __FILE__, "redo"
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
class SpecificExampleException < StandardError
|
3
|
+
end
|
4
|
+
class OtherCustomException < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
def exception_list
|
8
|
+
[SpecificExampleException, ZeroDivisionError]
|
9
|
+
end
|
10
|
+
describe "The rescue keyword" do
|
11
|
+
before :each do
|
12
|
+
ScratchPad.record []
|
13
|
+
end
|
14
|
+
|
15
|
+
it "can be used to handle a specific exception" do
|
16
|
+
lambda do
|
17
|
+
begin
|
18
|
+
raise SpecificExampleException, "Raising this to be handled below"
|
19
|
+
rescue SpecificExampleException
|
20
|
+
end
|
21
|
+
end.should_not raise_error
|
22
|
+
end
|
23
|
+
|
24
|
+
it "can capture the raised exception in a local variable" do
|
25
|
+
begin
|
26
|
+
raise SpecificExampleException, "some text"
|
27
|
+
rescue SpecificExampleException => e
|
28
|
+
e.message.should == "some text"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
pending "can rescue multiple raised exceptions with a single rescue block" do
|
33
|
+
lambda do
|
34
|
+
[lambda{1/0}, lambda{raise SpecificExampleException}].each do |block|
|
35
|
+
begin
|
36
|
+
block.call
|
37
|
+
rescue SpecificExampleException, ZeroDivisionError
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end.should_not raise_error
|
41
|
+
end
|
42
|
+
|
43
|
+
pending "can rescue a splatted list of exceptions" do
|
44
|
+
caught_it = false
|
45
|
+
begin
|
46
|
+
raise SpecificExampleException, "not important"
|
47
|
+
rescue *exception_list
|
48
|
+
caught_it = true
|
49
|
+
end
|
50
|
+
caught_it.should be_true
|
51
|
+
caught = []
|
52
|
+
lambda do
|
53
|
+
[lambda{1/0}, lambda{raise SpecificExampleException}].each do |block|
|
54
|
+
begin
|
55
|
+
block.call
|
56
|
+
rescue *exception_list
|
57
|
+
caught << $!
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end.should_not raise_error
|
61
|
+
caught.size.should == 2
|
62
|
+
exception_list.each do |exception_class|
|
63
|
+
caught.map{|e| e.class}.include?(exception_class).should be_true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
pending "will only rescue the specified exceptions when doing a splat rescue" do
|
68
|
+
lambda do
|
69
|
+
begin
|
70
|
+
raise OtherCustomException, "not rescued!"
|
71
|
+
rescue *exception_list
|
72
|
+
end
|
73
|
+
end.should raise_error(OtherCustomException)
|
74
|
+
end
|
75
|
+
|
76
|
+
pending "will execute an else block only if no exceptions were raised" do
|
77
|
+
# begin
|
78
|
+
# ScratchPad << :one
|
79
|
+
# rescue
|
80
|
+
# ScratchPad << :does_not_run
|
81
|
+
# else
|
82
|
+
# ScratchPad << :two
|
83
|
+
# end
|
84
|
+
# ScratchPad.recorded.should == [:one, :two]
|
85
|
+
end
|
86
|
+
|
87
|
+
pending "will not execute an else block if an exception was raised" do
|
88
|
+
# begin
|
89
|
+
# ScratchPad << :one
|
90
|
+
# raise "an error occurred"
|
91
|
+
# rescue
|
92
|
+
# ScratchPad << :two
|
93
|
+
# else
|
94
|
+
# ScratchPad << :does_not_run
|
95
|
+
# end
|
96
|
+
# ScratchPad.recorded.should == [:one, :two]
|
97
|
+
end
|
98
|
+
|
99
|
+
pending "will not rescue errors raised in an else block in the rescue block above it" do
|
100
|
+
# lambda do
|
101
|
+
# begin
|
102
|
+
# ScratchPad << :one
|
103
|
+
# rescue Exception => e
|
104
|
+
# ScratchPad << :does_not_run
|
105
|
+
# else
|
106
|
+
# ScratchPad << :two
|
107
|
+
# raise SpecificExampleException, "an error from else"
|
108
|
+
# end
|
109
|
+
# end.should raise_error(SpecificExampleException)
|
110
|
+
# ScratchPad.recorded.should == [:one, :two]
|
111
|
+
end
|
112
|
+
|
113
|
+
ruby_version_is "1.9" do
|
114
|
+
pending "parses 'a += b rescue c' as 'a += (b rescue c)'" do
|
115
|
+
a = 'a'
|
116
|
+
c = 'c'
|
117
|
+
# a += b rescue c
|
118
|
+
a.should == 'ac'
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|