opal 0.3.11 → 0.3.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +13 -0
- data/Gemfile +10 -0
- data/LICENSE +20 -0
- data/README.md +11 -116
- data/Rakefile +126 -0
- data/bin/opal +1 -2
- data/docs/spec_runner.html +16 -0
- data/index.html +434 -0
- data/lib/opal.rb +14 -15
- data/lib/opal/builder.rb +46 -148
- data/lib/opal/command.rb +45 -115
- data/lib/opal/context.rb +139 -78
- data/lib/opal/dependency_builder.rb +34 -0
- data/lib/opal/environment.rb +92 -0
- data/lib/opal/parser/grammar.rb +4915 -0
- data/lib/opal/{parser.y → parser/grammar.y} +430 -284
- data/lib/opal/parser/lexer.rb +1329 -0
- data/lib/opal/parser/parser.rb +1460 -0
- data/lib/opal/parser/scope.rb +140 -0
- data/lib/opal/parser/sexp.rb +17 -0
- data/lib/opal/version.rb +2 -1
- data/opal.gemspec +23 -0
- data/opal.js +3149 -4162
- data/runtime/README.md +25 -0
- data/runtime/corelib/alpha.rb +10 -0
- data/runtime/corelib/array.rb +962 -0
- data/runtime/corelib/basic_object.rb +66 -0
- data/runtime/corelib/boolean.rb +44 -0
- data/runtime/corelib/class.rb +43 -0
- data/runtime/corelib/comparable.rb +25 -0
- data/runtime/corelib/complex.rb +2 -0
- data/runtime/corelib/dir.rb +29 -0
- data/runtime/corelib/enumerable.rb +316 -0
- data/runtime/corelib/enumerator.rb +80 -0
- data/runtime/corelib/error.rb +25 -0
- data/runtime/corelib/file.rb +80 -0
- data/runtime/corelib/hash.rb +503 -0
- data/runtime/corelib/io.rb +44 -0
- data/runtime/corelib/kernel.rb +237 -0
- data/runtime/corelib/load_order +29 -0
- data/runtime/corelib/match_data.rb +37 -0
- data/runtime/corelib/module.rb +171 -0
- data/runtime/corelib/native.rb +50 -0
- data/runtime/corelib/nil_class.rb +47 -0
- data/runtime/corelib/numeric.rb +219 -0
- data/runtime/corelib/object.rb +21 -0
- data/runtime/corelib/proc.rb +42 -0
- data/runtime/corelib/range.rb +38 -0
- data/runtime/corelib/rational.rb +16 -0
- data/runtime/corelib/regexp.rb +63 -0
- data/runtime/corelib/string.rb +185 -0
- data/runtime/corelib/struct.rb +97 -0
- data/runtime/corelib/time.rb +196 -0
- data/runtime/corelib/top_self.rb +7 -0
- data/runtime/gemlib/alpha.rb +5 -0
- data/runtime/gemlib/kernel.rb +17 -0
- data/runtime/gemlib/load_order +2 -0
- data/runtime/kernel/class.js +256 -0
- data/runtime/kernel/debug.js +42 -0
- data/runtime/kernel/init.js +114 -0
- data/runtime/kernel/load_order +5 -0
- data/runtime/kernel/loader.js +151 -0
- data/runtime/kernel/runtime.js +414 -0
- data/runtime/spec/README.md +34 -0
- data/runtime/spec/core/array/allocate_spec.rb +15 -0
- data/runtime/spec/core/array/append_spec.rb +31 -0
- data/runtime/spec/core/array/assoc_spec.rb +29 -0
- data/runtime/spec/core/array/at_spec.rb +38 -0
- data/runtime/spec/core/array/clear_spec.rb +22 -0
- data/runtime/spec/core/array/collect_spec.rb +3 -0
- data/runtime/spec/core/array/compact_spec.rb +42 -0
- data/runtime/spec/core/array/concat_spec.rb +21 -0
- data/runtime/spec/core/array/constructor_spec.rb +24 -0
- data/runtime/spec/core/array/count_spec.rb +11 -0
- data/runtime/spec/core/array/delete_at_spec.rb +31 -0
- data/runtime/spec/core/array/delete_if_spec.rb +24 -0
- data/runtime/spec/core/array/delete_spec.rb +26 -0
- data/runtime/spec/core/array/each_index_spec.rb +33 -0
- data/runtime/spec/core/array/each_spec.rb +11 -0
- data/runtime/spec/core/array/element_reference_spec.rb +136 -0
- data/runtime/spec/core/array/element_set_spec.rb +7 -0
- data/runtime/spec/core/array/empty_spec.rb +10 -0
- data/runtime/spec/core/array/eql_spec.rb +3 -0
- data/runtime/spec/core/array/equal_value_spec.rb +3 -0
- data/runtime/spec/core/array/fetch_spec.rb +26 -0
- data/runtime/spec/core/array/first_spec.rb +54 -0
- data/runtime/spec/core/array/fixtures/classes.rb +8 -0
- data/runtime/spec/core/array/flatten_spec.rb +41 -0
- data/runtime/spec/core/array/include_spec.rb +20 -0
- data/runtime/spec/core/array/insert_spec.rb +59 -0
- data/runtime/spec/core/array/last_spec.rb +57 -0
- data/runtime/spec/core/array/length_spec.rb +3 -0
- data/runtime/spec/core/array/map_spec.rb +3 -0
- data/runtime/spec/core/array/plus_spec.rb +16 -0
- data/runtime/spec/core/array/pop_spec.rb +79 -0
- data/runtime/spec/core/array/push_spec.rb +19 -0
- data/runtime/spec/core/array/rassoc_spec.rb +12 -0
- data/runtime/spec/core/array/reject_spec.rb +54 -0
- data/runtime/spec/core/array/replace_spec.rb +3 -0
- data/runtime/spec/core/array/reverse_each_spec.rb +18 -0
- data/runtime/spec/core/array/reverse_spec.rb +9 -0
- data/runtime/spec/core/array/shared/collect.rb +53 -0
- data/runtime/spec/core/array/shared/eql.rb +19 -0
- data/runtime/spec/core/array/shared/length.rb +6 -0
- data/runtime/spec/core/array/shared/replace.rb +31 -0
- data/runtime/spec/core/class/new_spec.rb +19 -0
- data/runtime/spec/core/enumerable/all_spec.rb +102 -0
- data/runtime/spec/core/enumerable/any_spec.rb +115 -0
- data/runtime/spec/core/enumerable/collect_spec.rb +3 -0
- data/runtime/spec/core/enumerable/count_spec.rb +29 -0
- data/runtime/spec/core/enumerable/detect_spec.rb +3 -0
- data/runtime/spec/core/enumerable/find_spec.rb +3 -0
- data/runtime/spec/core/enumerable/fixtures/classes.rb +26 -0
- data/runtime/spec/core/enumerable/shared/collect.rb +12 -0
- data/runtime/spec/core/enumerable/shared/entries.rb +7 -0
- data/runtime/spec/core/enumerable/shared/find.rb +49 -0
- data/runtime/spec/core/enumerable/to_a_spec.rb +7 -0
- data/runtime/spec/core/false/and_spec.rb +11 -0
- data/runtime/spec/core/false/inspect_spec.rb +7 -0
- data/runtime/spec/core/false/or_spec.rb +11 -0
- data/runtime/spec/core/false/to_s_spec.rb +7 -0
- data/runtime/spec/core/false/xor_spec.rb +11 -0
- data/runtime/spec/core/hash/allocate_spec.rb +15 -0
- data/runtime/spec/core/hash/assoc_spec.rb +29 -0
- data/runtime/spec/core/hash/clear_spec.rb +21 -0
- data/runtime/spec/core/hash/clone_spec.rb +12 -0
- data/runtime/spec/core/hash/default_spec.rb +6 -0
- data/runtime/spec/core/hash/delete_if_spec.rb +15 -0
- data/runtime/spec/core/hash/element_reference_spec.rb +16 -0
- data/runtime/spec/core/hash/element_set_spec.rb +8 -0
- data/runtime/spec/core/hash/new_spec.rb +13 -0
- data/runtime/spec/core/matchdata/to_a_spec.rb +7 -0
- data/runtime/spec/core/nil/and_spec.rb +12 -0
- data/runtime/spec/core/nil/inspect_spec.rb +8 -0
- data/runtime/spec/core/nil/nil_spec.rb +8 -0
- data/runtime/spec/core/nil/or_spec.rb +12 -0
- data/runtime/spec/core/nil/to_a_spec.rb +8 -0
- data/runtime/spec/core/nil/to_f_spec.rb +12 -0
- data/runtime/spec/core/nil/to_i_spec.rb +12 -0
- data/runtime/spec/core/nil/to_s_spec.rb +8 -0
- data/runtime/spec/core/nil/xor_spec.rb +12 -0
- data/runtime/spec/core/numeric/equal_value_spec.rb +11 -0
- data/runtime/spec/core/object/is_a_spec.rb +2 -0
- data/runtime/spec/core/object/shared/kind_of.rb +0 -0
- data/runtime/spec/core/regexp/match_spec.rb +23 -0
- data/runtime/spec/core/regexp/shared/match.rb +11 -0
- data/runtime/spec/core/symbol/to_proc_spec.rb +8 -0
- data/runtime/spec/core/true/and_spec.rb +11 -0
- data/runtime/spec/core/true/inspect_spec.rb +7 -0
- data/runtime/spec/core/true/or_spec.rb +11 -0
- data/runtime/spec/core/true/to_s_spec.rb +7 -0
- data/runtime/spec/core/true/xor_spec.rb +11 -0
- data/runtime/spec/language/alias_spec.rb +25 -0
- data/runtime/spec/language/and_spec.rb +62 -0
- data/runtime/spec/language/array_spec.rb +68 -0
- data/runtime/spec/language/block_spec.rb +105 -0
- data/runtime/spec/language/break_spec.rb +49 -0
- data/runtime/spec/language/case_spec.rb +165 -0
- data/runtime/spec/language/defined_spec.rb +80 -0
- data/runtime/spec/language/ensure_spec.rb +82 -0
- data/runtime/spec/language/fixtures/block.rb +19 -0
- data/runtime/spec/language/fixtures/break.rb +39 -0
- data/runtime/spec/language/fixtures/defined.rb +9 -0
- data/runtime/spec/language/fixtures/ensure.rb +37 -0
- data/runtime/spec/language/fixtures/next.rb +46 -0
- data/runtime/spec/language/fixtures/send.rb +36 -0
- data/runtime/spec/language/fixtures/super.rb +43 -0
- data/runtime/spec/language/hash_spec.rb +43 -0
- data/runtime/spec/language/if_spec.rb +278 -0
- data/runtime/spec/language/loop_spec.rb +32 -0
- data/runtime/spec/language/next_spec.rb +128 -0
- data/runtime/spec/language/or_spec.rb +65 -0
- data/runtime/spec/language/predefined_spec.rb +21 -0
- data/runtime/spec/language/regexp/interpolation_spec.rb +9 -0
- data/runtime/spec/language/regexp_spec.rb +7 -0
- data/runtime/spec/language/send_spec.rb +105 -0
- data/runtime/spec/language/string_spec.rb +4 -0
- data/runtime/spec/language/super_spec.rb +18 -0
- data/runtime/spec/language/symbol_spec.rb +41 -0
- data/runtime/spec/language/undef_spec.rb +16 -0
- data/runtime/spec/language/unless_spec.rb +44 -0
- data/runtime/spec/language/until_spec.rb +137 -0
- data/runtime/spec/language/variables_spec.rb +28 -0
- data/runtime/spec/language/versions/hash_1.9.rb +20 -0
- data/runtime/spec/language/while_spec.rb +175 -0
- data/runtime/spec/library/stringscanner/scan_spec.rb +36 -0
- data/runtime/spec/opal/forwardable/def_instance_delegator_spec.rb +49 -0
- data/runtime/spec/opal/opal/defined_spec.rb +15 -0
- data/runtime/spec/opal/opal/function_spec.rb +11 -0
- data/runtime/spec/opal/opal/native_spec.rb +16 -0
- data/runtime/spec/opal/opal/null_spec.rb +10 -0
- data/runtime/spec/opal/opal/number_spec.rb +11 -0
- data/runtime/spec/opal/opal/object_spec.rb +16 -0
- data/runtime/spec/opal/opal/string_spec.rb +11 -0
- data/runtime/spec/opal/opal/typeof_spec.rb +9 -0
- data/runtime/spec/opal/opal/undefined_spec.rb +10 -0
- data/runtime/spec/opal/true/case_compare_spec.rb +12 -0
- data/runtime/spec/opal/true/class_spec.rb +10 -0
- data/runtime/spec/spec_helper.rb +25 -0
- data/runtime/stdlib/base64.rb +91 -0
- data/runtime/stdlib/date.rb +4 -0
- data/{stdlib → runtime/stdlib}/dev.rb +0 -0
- data/runtime/stdlib/forwardable.rb +33 -0
- data/runtime/stdlib/optparse.rb +0 -0
- data/runtime/stdlib/pp.rb +6 -0
- data/{stdlib → runtime/stdlib}/racc/parser.rb +0 -0
- data/runtime/stdlib/rbconfig.rb +0 -0
- data/runtime/stdlib/si.rb +17 -0
- data/runtime/stdlib/strscan.rb +53 -0
- data/runtime/stdlib/uri.rb +111 -0
- data/runtime/stdlib/uri/common.rb +1014 -0
- data/runtime/stdlib/uri/ftp.rb +261 -0
- data/runtime/stdlib/uri/generic.rb +1599 -0
- data/runtime/stdlib/uri/http.rb +106 -0
- data/runtime/stdlib/uri/https.rb +22 -0
- data/runtime/stdlib/uri/ldap.rb +260 -0
- data/runtime/stdlib/uri/ldaps.rb +20 -0
- data/runtime/stdlib/uri/mailto.rb +280 -0
- data/spec/builder/build_source_spec.rb +52 -0
- data/spec/builder/fixtures/build_source/adam.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/a.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/wow/b.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/wow/cow/c.rb +0 -0
- data/spec/builder/fixtures/build_source/beynon.rb +0 -0
- data/spec/builder/fixtures/build_source/charles.js +0 -0
- data/spec/builder/fixtures/build_source/foo/a.rb +0 -0
- data/spec/builder/fixtures/build_source/foo/b.rb +0 -0
- data/spec/builder/fixtures/build_source/foo/x.js +0 -0
- data/spec/builder/fixtures/build_source/foo/y.js +0 -0
- data/spec/builder/output_path_spec.rb +50 -0
- data/spec/grammar/alias_spec.rb +26 -0
- data/spec/grammar/and_spec.rb +13 -0
- data/spec/grammar/array_spec.rb +22 -0
- data/spec/grammar/attrasgn_spec.rb +28 -0
- data/spec/grammar/begin_spec.rb +38 -0
- data/spec/grammar/block_spec.rb +12 -0
- data/spec/grammar/break_spec.rb +17 -0
- data/spec/grammar/call_spec.rb +58 -0
- data/spec/grammar/class_spec.rb +35 -0
- data/spec/grammar/const_spec.rb +13 -0
- data/spec/grammar/cvar_spec.rb +11 -0
- data/spec/grammar/def_spec.rb +60 -0
- data/spec/grammar/false_spec.rb +17 -0
- data/spec/grammar/file_spec.rb +7 -0
- data/spec/grammar/gvar_spec.rb +13 -0
- data/spec/grammar/hash_spec.rb +17 -0
- data/spec/grammar/iasgn_spec.rb +9 -0
- data/spec/grammar/if_spec.rb +26 -0
- data/spec/grammar/iter_spec.rb +59 -0
- data/spec/grammar/ivar_spec.rb +9 -0
- data/spec/grammar/lasgn_spec.rb +8 -0
- data/spec/grammar/line_spec.rb +8 -0
- data/spec/grammar/lvar_spec.rb +38 -0
- data/spec/grammar/module_spec.rb +27 -0
- data/spec/grammar/nil_spec.rb +17 -0
- data/spec/grammar/not_spec.rb +27 -0
- data/spec/grammar/op_asgn1_spec.rb +23 -0
- data/spec/grammar/op_asgn2_spec.rb +23 -0
- data/spec/grammar/or_spec.rb +13 -0
- data/spec/grammar/return_spec.rb +17 -0
- data/spec/grammar/sclass_spec.rb +20 -0
- data/spec/grammar/self_spec.rb +17 -0
- data/spec/grammar/str_spec.rb +96 -0
- data/spec/grammar/super_spec.rb +20 -0
- data/spec/grammar/true_spec.rb +17 -0
- data/spec/grammar/undef_spec.rb +15 -0
- data/spec/grammar/unless_spec.rb +13 -0
- data/spec/grammar/while_spec.rb +15 -0
- data/spec/grammar/xstr_spec.rb +116 -0
- data/spec/grammar/yield_spec.rb +20 -0
- data/spec/spec_helper.rb +9 -0
- metadata +346 -21
- data/lib/opal/bundle.rb +0 -34
- data/lib/opal/lexer.rb +0 -902
- data/lib/opal/nodes.rb +0 -2150
- data/lib/opal/parser.rb +0 -4894
- data/lib/opal/rake/bundle_task.rb +0 -63
- data/opal-parser.js +0 -8343
- data/stdlib/strscan.rb +0 -52
- data/templates/init/Rakefile +0 -7
- data/templates/init/index.html +0 -17
- data/templates/init/lib/__NAME__.rb +0 -2
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe "The 'case'-construct" do
|
|
4
|
+
it "evaluates the body of the when clause matching the case target expression" do
|
|
5
|
+
case 1
|
|
6
|
+
when 2; false
|
|
7
|
+
when 1; true
|
|
8
|
+
end.should == true
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "evaluates the body of the when clause whose array expression includes the case target expression" do
|
|
12
|
+
case 2
|
|
13
|
+
when 3, 4; false
|
|
14
|
+
when 1, 2; true
|
|
15
|
+
end.should == true
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "evaluates the body of the when clause in left-to-right order if it's an array expression" do
|
|
19
|
+
@calls = []
|
|
20
|
+
def foo; @calls << :foo; end
|
|
21
|
+
def bar; @calls << :bar; end
|
|
22
|
+
|
|
23
|
+
case true
|
|
24
|
+
when foo, bar;
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
@calls.should == [:foo, :bar]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "evaluates the body of the when clause whose range expression includes the case target expression" do
|
|
31
|
+
case 5
|
|
32
|
+
when 21..30; false
|
|
33
|
+
when 1..20; true
|
|
34
|
+
end.should == true
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "returns nil when no 'then'-bodies are given" do
|
|
38
|
+
case "a"
|
|
39
|
+
when "a"
|
|
40
|
+
when "b"
|
|
41
|
+
end.should == nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "evaluates the 'else'-body when no other expression matches" do
|
|
45
|
+
case "c"
|
|
46
|
+
when "a"; 'foo'
|
|
47
|
+
when "b"; 'bar'
|
|
48
|
+
else 'zzz'
|
|
49
|
+
end.should == 'zzz'
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "returns nil when no expression matches and 'else'-body is empty" do
|
|
53
|
+
case "c"
|
|
54
|
+
when "a"; "a"
|
|
55
|
+
when "b"; "b"
|
|
56
|
+
else
|
|
57
|
+
end.should == nil
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it "returns 2 when a then body is empty" do
|
|
61
|
+
case Object.new
|
|
62
|
+
when Numeric then
|
|
63
|
+
1
|
|
64
|
+
when String then
|
|
65
|
+
# ok
|
|
66
|
+
else
|
|
67
|
+
2
|
|
68
|
+
end.should == 2
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "returns the statement following 'then'" do
|
|
72
|
+
case "a"
|
|
73
|
+
when "a" then 'foo'
|
|
74
|
+
when "b" then 'bar'
|
|
75
|
+
end.should == 'foo'
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "tests classes with case equality" do
|
|
79
|
+
case "a"
|
|
80
|
+
when String
|
|
81
|
+
'foo'
|
|
82
|
+
when Symbol
|
|
83
|
+
'bar'
|
|
84
|
+
end.should == 'foo'
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "tests with matching regexps" do
|
|
88
|
+
case "hello"
|
|
89
|
+
when /abc/; false
|
|
90
|
+
when /^hell/; true
|
|
91
|
+
end.should == true
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it "does not test with equality when given classes" do
|
|
95
|
+
case :symbol.class
|
|
96
|
+
when Symbol
|
|
97
|
+
"bar"
|
|
98
|
+
when String
|
|
99
|
+
"bar"
|
|
100
|
+
else
|
|
101
|
+
"foo"
|
|
102
|
+
end.should == "foo"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it "takes lists of values" do
|
|
106
|
+
case 'z'
|
|
107
|
+
when 'a', 'b', 'c', 'd'
|
|
108
|
+
"foo"
|
|
109
|
+
when 'x', 'y', 'z'
|
|
110
|
+
"bar"
|
|
111
|
+
end.should == "bar"
|
|
112
|
+
|
|
113
|
+
case 'b'
|
|
114
|
+
when 'a', 'b', 'c', 'd'
|
|
115
|
+
"foo"
|
|
116
|
+
when 'x', 'y', 'z'
|
|
117
|
+
"bar"
|
|
118
|
+
end.should == "foo"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
it "expands arrays to lists of values" do
|
|
122
|
+
case 'z'
|
|
123
|
+
when *['a', 'b', 'c', 'd']
|
|
124
|
+
"foo"
|
|
125
|
+
when *['x', 'y', 'z']
|
|
126
|
+
"bar"
|
|
127
|
+
end.should == "bar"
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
it "takes an expanded array in addition to a list of values" do
|
|
131
|
+
case 'f'
|
|
132
|
+
when 'f', *['a', 'b', 'c', 'd']
|
|
133
|
+
"foo"
|
|
134
|
+
when *['x', 'y', 'z']
|
|
135
|
+
"bar"
|
|
136
|
+
end.should == "foo"
|
|
137
|
+
|
|
138
|
+
case 'b'
|
|
139
|
+
when 'f', *['a', 'b', 'c', 'd']
|
|
140
|
+
"foo"
|
|
141
|
+
when *['x', 'y', 'z']
|
|
142
|
+
"bar"
|
|
143
|
+
end.should == "foo"
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
it "concats arrays before expanding them" do
|
|
147
|
+
a = ['a', 'b', 'c', 'd']
|
|
148
|
+
b = ['f']
|
|
149
|
+
|
|
150
|
+
case 'f'
|
|
151
|
+
when 'f', *a|b
|
|
152
|
+
"foo"
|
|
153
|
+
when *['x', 'y', 'z']
|
|
154
|
+
"bar"
|
|
155
|
+
end.should == "foo"
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
it "never matchers when clauses with no values" do
|
|
159
|
+
case nil
|
|
160
|
+
when *[]
|
|
161
|
+
"foo"
|
|
162
|
+
end.should == nil
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
|
2
|
+
require File.expand_path('../fixtures/defined', __FILE__)
|
|
3
|
+
|
|
4
|
+
describe "The defined? keyword for literals" do
|
|
5
|
+
it "returns 'self' for self" do
|
|
6
|
+
ret = defined?(self)
|
|
7
|
+
ret.should == "self"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "returns 'nil' for nil" do
|
|
11
|
+
ret = defined?(nil)
|
|
12
|
+
ret.should == "nil"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "returns 'true' for true" do
|
|
16
|
+
ret = defined?(true)
|
|
17
|
+
ret.should == "true"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "returns 'false' for false" do
|
|
21
|
+
ret = defined?(false)
|
|
22
|
+
ret.should == "false"
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe "The defined? keyword when called with a method name" do
|
|
27
|
+
describe "without a receiver" do
|
|
28
|
+
it "returns 'method' if the method is defined" do
|
|
29
|
+
defined?(puts).should == "method"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "returns nil if the method is not defined" do
|
|
33
|
+
defined?(defined_specs_undefined_method).should be_nil
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
describe "having a module as a receiver" do
|
|
38
|
+
it "returns 'method' if the method is defined" do
|
|
39
|
+
defined?(Kernel.puts).should == "method"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "returns nil if the method is private" do
|
|
43
|
+
# FIXME: should opal-spec have some special not_opal_compatible error that is similar to skip?
|
|
44
|
+
#defined?(Object.print).should be_nil
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "returns nil if the method is protected" do
|
|
48
|
+
# FIXME: see above
|
|
49
|
+
# defined?(DefinedSpecs::Basic.new.protected_method).should be_nil
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "returns nil if the method is not defined" do
|
|
53
|
+
defined?(Kernel.defined_specs_undefined_method).should be_nil
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
describe "having a local variable as receiver" do
|
|
58
|
+
it "returns 'method' if the method is defined" do
|
|
59
|
+
obj = DefinedSpecs::Basic.new
|
|
60
|
+
defined?(obj.a_defined_method).should == "method"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "returns nil if the method is not defined" do
|
|
64
|
+
obj = DefinedSpecs::Basic.new
|
|
65
|
+
defined?(obj.an_undefined_method).should be_nil
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
describe "having an instance variable as receiver" do
|
|
70
|
+
it "returns 'method' if the method is defined" do
|
|
71
|
+
@defined_specs_obj = DefinedSpecs::Basic.new
|
|
72
|
+
defined?(@defined_specs_obj.a_defined_method).should == "method"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it "returns nil if the method is not defined" do
|
|
76
|
+
@defined_specs_obj = DefinedSpecs::Basic.new
|
|
77
|
+
defined?(@defined_specs_obj.an_undefined_method).should be_nil
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
|
2
|
+
require File.expand_path('../fixtures/ensure', __FILE__)
|
|
3
|
+
|
|
4
|
+
describe "An ensure block inside a begin block" do
|
|
5
|
+
before :each do
|
|
6
|
+
ScratchPad.record []
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "is executed when an exception is raised in it's corresponding begin block" do
|
|
10
|
+
begin
|
|
11
|
+
lambda {
|
|
12
|
+
begin
|
|
13
|
+
ScratchPad << :begin
|
|
14
|
+
raise "An exception occured!"
|
|
15
|
+
ensure
|
|
16
|
+
ScratchPad << :ensure
|
|
17
|
+
end
|
|
18
|
+
}.should raise_error(RuntimeError)
|
|
19
|
+
|
|
20
|
+
ScratchPad.recorded.should == [:begin, :ensure]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "is executed when an exception is raised and rescued in it's corresponding begin block" do
|
|
25
|
+
begin
|
|
26
|
+
begin
|
|
27
|
+
ScratchPad << :begin
|
|
28
|
+
raise "An exception occured!"
|
|
29
|
+
rescue
|
|
30
|
+
ScratchPad << :rescue
|
|
31
|
+
ensure
|
|
32
|
+
ScratchPad << :ensure
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
ScratchPad.recorded.should == [:begin, :rescue, :ensure]
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "is executed when nothing is raised or thrown in it's corresponding begin block" do
|
|
40
|
+
begin
|
|
41
|
+
ScratchPad << :begin
|
|
42
|
+
rescue
|
|
43
|
+
ScratchPad << :rescue
|
|
44
|
+
ensure
|
|
45
|
+
ScratchPad << :ensure
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
ScratchPad.recorded.should == [:begin, :ensure]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "has no return value" do
|
|
52
|
+
begin
|
|
53
|
+
:begin
|
|
54
|
+
ensure
|
|
55
|
+
:ensure
|
|
56
|
+
end.should == :begin
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe "An ensure block inside a method" do
|
|
61
|
+
before(:each) do
|
|
62
|
+
@obj = EnsureSpec::Container.new
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "is executed when an exception is raised in the method" do
|
|
66
|
+
lambda { @obj.raise_in_method_with_ensure }.should raise_error(RuntimeError)
|
|
67
|
+
@obj.executed.should == [:method, :ensure]
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "is executed when an exception is raised and rescued in the method" do
|
|
71
|
+
@obj.raise_and_rescue_in_method_with_ensure
|
|
72
|
+
@obj.executed.should == [:method, :rescue, :ensure]
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it "has no impact on the method's implicit return value" do
|
|
76
|
+
@obj.implicit_return_in_method_with_ensure.should == :method
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "has an impact on the method's explicit return value" do
|
|
80
|
+
@obj.explicit_return_in_method_with_ensure.should == :ensure
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module BreakSpecs
|
|
2
|
+
class Driver
|
|
3
|
+
def initialize(ensures=false)
|
|
4
|
+
@ensures = ensures
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def note(value)
|
|
8
|
+
ScratchPad << value
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class Block < Driver
|
|
13
|
+
def break_nil
|
|
14
|
+
note :a
|
|
15
|
+
note yielding {
|
|
16
|
+
note :b
|
|
17
|
+
break
|
|
18
|
+
note :c
|
|
19
|
+
}
|
|
20
|
+
note :d
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def break_value
|
|
24
|
+
note :a
|
|
25
|
+
note yielding {
|
|
26
|
+
note :b
|
|
27
|
+
break :break
|
|
28
|
+
note :c
|
|
29
|
+
}
|
|
30
|
+
note :d
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def yielding
|
|
34
|
+
note :aa
|
|
35
|
+
note yield
|
|
36
|
+
note :bb
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module EnsureSpec
|
|
2
|
+
class Container
|
|
3
|
+
attr_reader :executed
|
|
4
|
+
|
|
5
|
+
def initialize
|
|
6
|
+
@executed = []
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def raise_in_method_with_ensure
|
|
10
|
+
@executed << :method
|
|
11
|
+
raise "An Exception"
|
|
12
|
+
ensure
|
|
13
|
+
@executed << :ensure
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def raise_and_rescue_in_method_with_ensure
|
|
17
|
+
@executed << :method
|
|
18
|
+
raise "An Exception"
|
|
19
|
+
rescue
|
|
20
|
+
@executed << :rescue
|
|
21
|
+
ensure
|
|
22
|
+
@executed << :ensure
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def implicit_return_in_method_with_ensure
|
|
26
|
+
:method
|
|
27
|
+
ensure
|
|
28
|
+
:ensure
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def explicit_return_in_method_with_ensure
|
|
32
|
+
return :method
|
|
33
|
+
ensure
|
|
34
|
+
return :ensure
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
class NextSpecs
|
|
2
|
+
def self.yielding_method(expected)
|
|
3
|
+
yield.should == expected
|
|
4
|
+
:method_return_value
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def self.yielding
|
|
8
|
+
yield
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.while_next(arg)
|
|
12
|
+
x = true
|
|
13
|
+
while x
|
|
14
|
+
begin
|
|
15
|
+
ScratchPad << :begin
|
|
16
|
+
x = false
|
|
17
|
+
if arg
|
|
18
|
+
next 42
|
|
19
|
+
else
|
|
20
|
+
next
|
|
21
|
+
end
|
|
22
|
+
ensure
|
|
23
|
+
ScratchPad << :ensure
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.while_within_iter(arg)
|
|
29
|
+
yielding do
|
|
30
|
+
x = true
|
|
31
|
+
while x
|
|
32
|
+
begin
|
|
33
|
+
ScratchPad << :begin
|
|
34
|
+
x = false
|
|
35
|
+
if arg
|
|
36
|
+
next 42
|
|
37
|
+
else
|
|
38
|
+
next
|
|
39
|
+
end
|
|
40
|
+
ensure
|
|
41
|
+
ScratchPad << :ensure
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|