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
data/spec/language/hash_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
1
3
|
describe "Hash literal" do
|
2
4
|
it "{} should return an empty hash" do
|
3
5
|
{}.size.should == 0
|
@@ -7,10 +9,10 @@ describe "Hash literal" do
|
|
7
9
|
it "{} should return a new hash populated with the given elements" do
|
8
10
|
h = {:a => 'a', 'b' => 3, 44 => 2.3}
|
9
11
|
h.size.should == 3
|
10
|
-
h.should == {:a =>
|
12
|
+
h.should == {:a => "a", "b" => 3, 44 => 2.3}
|
11
13
|
end
|
12
14
|
|
13
|
-
it "treats empty expressions
|
15
|
+
it "treats empty expressions as nils" do
|
14
16
|
h = {() => ()}
|
15
17
|
h.keys.should == [nil]
|
16
18
|
h.values.should == [nil]
|
@@ -21,12 +23,22 @@ describe "Hash literal" do
|
|
21
23
|
h.values.should == [:value]
|
22
24
|
h[nil].should == :value
|
23
25
|
|
24
|
-
h = {:
|
25
|
-
h.keys.should == [:
|
26
|
+
h = {:key => ()}
|
27
|
+
h.keys.should == [:key]
|
26
28
|
h.values.should == [nil]
|
27
29
|
h[:key].should == nil
|
28
30
|
end
|
29
31
|
|
32
|
+
pending "freezes string keys on initialization" do
|
33
|
+
key = "foo"
|
34
|
+
h = {key => "bar"}
|
35
|
+
key.reverse!
|
36
|
+
h["foo"].should == "bar"
|
37
|
+
h.keys.first.should == "foo"
|
38
|
+
h.keys.first.frozen?.should == true
|
39
|
+
key.should == "oof"
|
40
|
+
end
|
41
|
+
|
30
42
|
it "checks duplicated keys on initialization" do
|
31
43
|
h = {:foo => :bar, :foo => :foo}
|
32
44
|
h.keys.size.should == 1
|
@@ -38,4 +50,16 @@ describe "Hash literal" do
|
|
38
50
|
h.size.should == 2
|
39
51
|
h.should == {:a => 1, :b => 2}
|
40
52
|
end
|
41
|
-
|
53
|
+
|
54
|
+
it "recognizes '=' at the end of the key" do
|
55
|
+
eval("{:a==>1}").should == {:"a=" => 1}
|
56
|
+
eval("{:a= =>1}").should == {:"a=" => 1}
|
57
|
+
eval("{:a= => 1}").should == {:"a=" => 1}
|
58
|
+
end
|
59
|
+
|
60
|
+
it "with '==>' in the middle raises SyntaxError" do
|
61
|
+
lambda {eval("{:a ==> 1}")}.should raise_error(SyntaxError)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# language_version __FILE__, "hash"
|
data/spec/language/if_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
1
3
|
describe "The if expression" do
|
2
4
|
it "evaluates body if expression is true" do
|
3
5
|
a = []
|
@@ -7,7 +9,7 @@ describe "The if expression" do
|
|
7
9
|
a.should == [123]
|
8
10
|
end
|
9
11
|
|
10
|
-
it "does not evaluate body if expression
|
12
|
+
it "does not evaluate body if expression is false" do
|
11
13
|
a = []
|
12
14
|
if false
|
13
15
|
a << 123
|
@@ -113,7 +115,7 @@ describe "The if expression" do
|
|
113
115
|
end
|
114
116
|
|
115
117
|
it "considers a non-nil and non-boolean object in expression result as true" do
|
116
|
-
if 'x'
|
118
|
+
if mock('x')
|
117
119
|
123
|
118
120
|
else
|
119
121
|
456
|
@@ -135,7 +137,7 @@ describe "The if expression" do
|
|
135
137
|
end.should == 456
|
136
138
|
end
|
137
139
|
|
138
|
-
it "evaluates subsequent elsif statements and
|
140
|
+
it "evaluates subsequent elsif statements and execute body of first matching" do
|
139
141
|
if false
|
140
142
|
123
|
141
143
|
elsif false
|
@@ -210,6 +212,83 @@ describe "The if expression" do
|
|
210
212
|
|
211
213
|
if false then 123; else 456; end.should == 456
|
212
214
|
end
|
215
|
+
|
216
|
+
describe "with a boolean range ('flip-flop' operator)" do
|
217
|
+
before :each do
|
218
|
+
ScratchPad.record []
|
219
|
+
end
|
220
|
+
|
221
|
+
after :each do
|
222
|
+
ScratchPad.clear
|
223
|
+
end
|
224
|
+
|
225
|
+
pending "mimics an awk conditional with a single-element inclusive-end range" do
|
226
|
+
10.times { |i| ScratchPad << i if (i == 4)..(i == 4) }
|
227
|
+
ScratchPad.recorded.should == [4]
|
228
|
+
end
|
229
|
+
|
230
|
+
pending "mimics an awk conditional with a many-element inclusive-end range" do
|
231
|
+
10.times { |i| ScratchPad << i if (i == 4)..(i == 7) }
|
232
|
+
ScratchPad.recorded.should == [4, 5, 6, 7]
|
233
|
+
end
|
234
|
+
|
235
|
+
pending "mimics a sed conditional with a zero-element exclusive-end range" do
|
236
|
+
10.times { |i| ScratchPad << i if (i == 4)...(i == 4) }
|
237
|
+
ScratchPad.recorded.should == [4, 5, 6, 7, 8, 9]
|
238
|
+
end
|
239
|
+
|
240
|
+
pending "mimics a sed conditional with a many-element exclusive-end range" do
|
241
|
+
10.times { |i| ScratchPad << i if (i == 4)...(i == 5) }
|
242
|
+
ScratchPad.recorded.should == [4, 5]
|
243
|
+
end
|
244
|
+
|
245
|
+
pending "allows combining two flip-flops" do
|
246
|
+
10.times { |i| ScratchPad << i if (i == 4)...(i == 5) or (i == 7)...(i == 8) }
|
247
|
+
ScratchPad.recorded.should == [4, 5, 7, 8]
|
248
|
+
end
|
249
|
+
|
250
|
+
pending "evaluates the first conditions lazily with inclusive-end range" do
|
251
|
+
collector = proc { |i| ScratchPad << i }
|
252
|
+
10.times { |i| i if collector[i]...false }
|
253
|
+
ScratchPad.recorded.should == [0]
|
254
|
+
end
|
255
|
+
|
256
|
+
pending "evaluates the first conditions lazily with exclusive-end range" do
|
257
|
+
collector = proc { |i| ScratchPad << i }
|
258
|
+
10.times { |i| i if collector[i]..false }
|
259
|
+
ScratchPad.recorded.should == [0]
|
260
|
+
end
|
261
|
+
|
262
|
+
pending "evaluates the second conditions lazily with inclusive-end range" do
|
263
|
+
collector = proc { |i| ScratchPad << i }
|
264
|
+
10.times { |i| i if (i == 4)...collector[i] }
|
265
|
+
ScratchPad.recorded.should == [5]
|
266
|
+
end
|
267
|
+
|
268
|
+
pending "evaluates the second conditions lazily with exclusive-end range" do
|
269
|
+
collector = proc { |i| ScratchPad << i }
|
270
|
+
10.times { |i| i if (i == 4)..collector[i] }
|
271
|
+
ScratchPad.recorded.should == [4]
|
272
|
+
end
|
273
|
+
|
274
|
+
pending "scopes state by flip-flop" do
|
275
|
+
store_me = proc { |i| ScratchPad << i if (i == 4)..(i == 7) }
|
276
|
+
store_me[1]
|
277
|
+
store_me[4]
|
278
|
+
proc { store_me[1] }.call
|
279
|
+
store_me[7]
|
280
|
+
store_me[5]
|
281
|
+
ScratchPad.recorded.should == [4, 1, 7]
|
282
|
+
end
|
283
|
+
|
284
|
+
pending "keeps flip-flops from interfering" do
|
285
|
+
a = proc { |i| ScratchPad << i if (i == 4)..(i == 7) }
|
286
|
+
b = proc { |i| ScratchPad << i if (i == 4)..(i == 7) }
|
287
|
+
6.times(&a)
|
288
|
+
6.times(&b)
|
289
|
+
ScratchPad.recorded.should == [4, 5, 4, 5]
|
290
|
+
end
|
291
|
+
end
|
213
292
|
end
|
214
293
|
|
215
294
|
describe "The postfix if form" do
|
@@ -219,7 +298,7 @@ describe "The postfix if form" do
|
|
219
298
|
a.should == [123]
|
220
299
|
end
|
221
300
|
|
222
|
-
it "does not evaluate statement if expression
|
301
|
+
it "does not evaluate statement if expression is false" do
|
223
302
|
a = []
|
224
303
|
a << 123 if false
|
225
304
|
a.should == []
|
@@ -229,7 +308,7 @@ describe "The postfix if form" do
|
|
229
308
|
(123 if true).should == 123
|
230
309
|
end
|
231
310
|
|
232
|
-
it "returns nil if
|
311
|
+
it "returns nil if expression is false" do
|
233
312
|
(123 if false).should == nil
|
234
313
|
end
|
235
314
|
|
@@ -238,7 +317,7 @@ describe "The postfix if form" do
|
|
238
317
|
end
|
239
318
|
|
240
319
|
it "considers a non-nil object as true" do
|
241
|
-
(123 if 'x').should == 123
|
320
|
+
(123 if mock('x')).should == 123
|
242
321
|
end
|
243
322
|
|
244
323
|
it "evaluates then-body in containing scope" do
|
@@ -249,12 +328,12 @@ describe "The postfix if form" do
|
|
249
328
|
b.should == 124
|
250
329
|
end
|
251
330
|
|
252
|
-
it "evaluates else-body
|
331
|
+
it "evaluates else-body in containing scope" do
|
253
332
|
a = 123
|
254
333
|
if false
|
255
334
|
b = a+1
|
256
335
|
else
|
257
|
-
b= a+2
|
336
|
+
b = a+2
|
258
337
|
end
|
259
338
|
b.should == 125
|
260
339
|
end
|
@@ -272,4 +351,6 @@ describe "The postfix if form" do
|
|
272
351
|
end
|
273
352
|
b.should == 126
|
274
353
|
end
|
275
|
-
end
|
354
|
+
end
|
355
|
+
|
356
|
+
# language_version __FILE__, "if"
|
@@ -1,47 +1 @@
|
|
1
|
-
|
2
|
-
it "can be specified as a literal" do
|
3
|
-
lambda { ->(){} }.call
|
4
|
-
end
|
5
|
-
|
6
|
-
it "returns a Proc object" do
|
7
|
-
->(){}.should be_kind_of(Proc)
|
8
|
-
end
|
9
|
-
|
10
|
-
it "returns a lambda" do
|
11
|
-
->(){}.lambda?.should be_true
|
12
|
-
end
|
13
|
-
|
14
|
-
it "can be assigned to a variable" do
|
15
|
-
var = ->(){}
|
16
|
-
var.lambda?.should be_true
|
17
|
-
end
|
18
|
-
|
19
|
-
it "understands a do/end block in place of {}" do
|
20
|
-
lambda do
|
21
|
-
->() do
|
22
|
-
end
|
23
|
-
end.call
|
24
|
-
end
|
25
|
-
|
26
|
-
it "can be interpolated into a String" do
|
27
|
-
"1+2=#{->{ 1 + 2 }.call}".should == "1+2=3"
|
28
|
-
end
|
29
|
-
|
30
|
-
it "can be used as a Hash key" do
|
31
|
-
h = {}
|
32
|
-
h[->(){ 1 + 2 }.call] = :value
|
33
|
-
h.key?(3).should be_true
|
34
|
-
end
|
35
|
-
|
36
|
-
it "can be used in method parameter lists" do
|
37
|
-
def glark7654(a=-> { :foo })
|
38
|
-
a.call
|
39
|
-
end
|
40
|
-
glark7654.should == :foo
|
41
|
-
end
|
42
|
-
|
43
|
-
it "accepts an paramter list between the paranthesis" do
|
44
|
-
lambda { ->(a) {} }.call
|
45
|
-
lambda { ->(a,b) {} }.call
|
46
|
-
end
|
47
|
-
end
|
1
|
+
# language_version __FILE__, "literal_lambda"
|
data/spec/language/loop_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
1
3
|
describe "The loop expression" do
|
2
4
|
it "repeats the given block until a break is called" do
|
3
5
|
outer_loop = 0
|
@@ -8,7 +10,7 @@ describe "The loop expression" do
|
|
8
10
|
outer_loop.should == 10
|
9
11
|
end
|
10
12
|
|
11
|
-
|
13
|
+
pending "executes code in its own scope" do
|
12
14
|
loop do
|
13
15
|
inner_loop = 123
|
14
16
|
break
|
@@ -27,4 +29,39 @@ describe "The loop expression" do
|
|
27
29
|
break
|
28
30
|
end.should == nil
|
29
31
|
end
|
30
|
-
|
32
|
+
|
33
|
+
it "skips to end of body with next" do
|
34
|
+
a = []
|
35
|
+
i = 0
|
36
|
+
loop do
|
37
|
+
break if (i+=1) >= 5
|
38
|
+
next if i == 3
|
39
|
+
a << i
|
40
|
+
end
|
41
|
+
a.should == [1, 2, 4]
|
42
|
+
end
|
43
|
+
|
44
|
+
pending "restarts the current iteration with redo" do
|
45
|
+
a = []
|
46
|
+
loop do
|
47
|
+
a << 1
|
48
|
+
redo if a.size < 2
|
49
|
+
a << 2
|
50
|
+
break if a.size == 3
|
51
|
+
end
|
52
|
+
a.should == [1, 1, 2]
|
53
|
+
end
|
54
|
+
|
55
|
+
pending "uses a spaghetti nightmare of redo, next and break" do
|
56
|
+
a = []
|
57
|
+
loop do
|
58
|
+
a << 1
|
59
|
+
redo if a.size == 1
|
60
|
+
a << 2
|
61
|
+
next if a.size == 3
|
62
|
+
a << 3
|
63
|
+
break if a.size > 6
|
64
|
+
end
|
65
|
+
a.should == [1, 1, 2, 1, 2, 3, 1, 2, 3]
|
66
|
+
end
|
67
|
+
end
|
@@ -1,13 +1,159 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../../fixtures/class', __FILE__)
|
3
|
+
require File.expand_path('../fixtures/metaclass', __FILE__)
|
4
|
+
|
1
5
|
describe "self in a metaclass body (class << obj)" do
|
2
|
-
it "is
|
3
|
-
class << true; self; end.should ==
|
6
|
+
it "is TrueClass for true" do
|
7
|
+
class << true; self; end.should == TrueClass
|
4
8
|
end
|
5
9
|
|
6
|
-
it "is
|
7
|
-
class << false; self; end.should ==
|
10
|
+
it "is FalseClass for false" do
|
11
|
+
class << false; self; end.should == FalseClass
|
8
12
|
end
|
9
13
|
|
10
14
|
it "is NilClass for nil" do
|
11
15
|
class << nil; self; end.should == NilClass
|
12
16
|
end
|
13
|
-
|
17
|
+
|
18
|
+
pending "raises a TypeError for numbers" do
|
19
|
+
lambda { class << 1; self; end }.should raise_error(TypeError)
|
20
|
+
end
|
21
|
+
|
22
|
+
pending "raises a TypeError for symbols" do
|
23
|
+
lambda { class << :symbol; self; end }.should raise_error(TypeError)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "is a singleton Class instance" do
|
27
|
+
cls = class << mock('x'); self; end
|
28
|
+
cls.is_a?(Class).should == true
|
29
|
+
cls.should_not equal(Object)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "A constant on a metaclass" do
|
34
|
+
before(:each) do
|
35
|
+
@object = Object.new
|
36
|
+
class << @object
|
37
|
+
CONST = self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
pending "can be accessed after the metaclass body is reopened" do
|
42
|
+
class << @object
|
43
|
+
CONST.should == self
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
pending "can be accessed via self::CONST" do
|
48
|
+
class << @object
|
49
|
+
self::CONST.should == self
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
pending "can be accessed via const_get" do
|
54
|
+
class << @object
|
55
|
+
const_get(:CONST).should == self
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
pending "is not defined on the object's class" do
|
60
|
+
@object.class.const_defined?(:CONST).should be_false
|
61
|
+
end
|
62
|
+
|
63
|
+
pending "is not defined in the metaclass opener's scope" do
|
64
|
+
class << @object
|
65
|
+
CONST
|
66
|
+
end
|
67
|
+
lambda { CONST }.should raise_error(NameError)
|
68
|
+
end
|
69
|
+
|
70
|
+
pending "cannot be accessed via object::CONST" do
|
71
|
+
lambda do
|
72
|
+
@object::CONST
|
73
|
+
end.should raise_error(TypeError)
|
74
|
+
end
|
75
|
+
|
76
|
+
pending "raises a NameError for anonymous_module::CONST" do
|
77
|
+
@object = Class.new
|
78
|
+
class << @object
|
79
|
+
CONST = 100
|
80
|
+
end
|
81
|
+
|
82
|
+
lambda do
|
83
|
+
@object::CONST
|
84
|
+
end.should raise_error(NameError)
|
85
|
+
end
|
86
|
+
|
87
|
+
ruby_version_is ""..."1.9" do
|
88
|
+
it "appears in the metaclass constant list" do
|
89
|
+
constants = class << @object; constants; end
|
90
|
+
constants.should include("CONST")
|
91
|
+
end
|
92
|
+
|
93
|
+
it "does not appear in the object's class constant list" do
|
94
|
+
@object.class.constants.should_not include("CONST")
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
ruby_version_is "1.9" do
|
99
|
+
pending "appears in the metaclass constant list" do
|
100
|
+
constants = class << @object; constants; end
|
101
|
+
constants.should include(:CONST)
|
102
|
+
end
|
103
|
+
|
104
|
+
pending "does not appear in the object's class constant list" do
|
105
|
+
@object.class.constants.should_not include(:CONST)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
pending "is not preserved when the object is duped" do
|
110
|
+
@object = @object.dup
|
111
|
+
|
112
|
+
lambda do
|
113
|
+
class << @object; CONST; end
|
114
|
+
end.should raise_error(NameError)
|
115
|
+
end
|
116
|
+
|
117
|
+
pending "is preserved when the object is cloned" do
|
118
|
+
@object = @object.clone
|
119
|
+
|
120
|
+
class << @object
|
121
|
+
CONST.should_not be_nil
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe "calling methods on the metaclass" do
|
127
|
+
|
128
|
+
it "calls a method on the metaclass" do
|
129
|
+
MetaClassSpecs::A.cheese.should == 'edam'
|
130
|
+
MetaClassSpecs::B.cheese.should == 'stilton'
|
131
|
+
end
|
132
|
+
|
133
|
+
it "calls a method on the instance's metaclass" do
|
134
|
+
b = MetaClassSpecs::B.new
|
135
|
+
b_meta = MetaClassSpecs.metaclass_of b
|
136
|
+
b_meta.send(:define_method, :cheese) {'cheshire'}
|
137
|
+
b.cheese.should == 'cheshire'
|
138
|
+
end
|
139
|
+
|
140
|
+
pending "calls a method in deeper chains of metaclasses" do
|
141
|
+
b = MetaClassSpecs::B.new
|
142
|
+
b_meta = MetaClassSpecs.metaclass_of b
|
143
|
+
b_meta_meta = MetaClassSpecs.metaclass_of b_meta
|
144
|
+
b_meta_meta.send(:define_method, :cheese) {'gouda'}
|
145
|
+
b_meta.cheese.should == 'gouda'
|
146
|
+
|
147
|
+
b_meta_meta_meta = MetaClassSpecs.metaclass_of b_meta_meta
|
148
|
+
b_meta_meta_meta.send(:define_method, :cheese) {'wensleydale'}
|
149
|
+
b_meta_meta.cheese.should == 'wensleydale'
|
150
|
+
end
|
151
|
+
|
152
|
+
ruby_version_is "1.9" do
|
153
|
+
it "calls a method defined on the metaclass of the metaclass" do
|
154
|
+
d_meta = MetaClassSpecs::D.singleton_class
|
155
|
+
d_meta.ham.should == 'iberico'
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|