opal 0.3.41 → 0.3.42

Sign up to get free protection for your applications and to get access to all the features.
Files changed (285) hide show
  1. data/.gitignore +2 -0
  2. data/.travis.yml +3 -0
  3. data/CHANGELOG.md +14 -1
  4. data/Gemfile +2 -5
  5. data/Rakefile +41 -3
  6. data/bin/opal +33 -0
  7. data/lib/opal.rb +2 -12
  8. data/lib/opal/core_ext.rb +5 -0
  9. data/lib/opal/grammar.rb +2207 -2138
  10. data/lib/opal/grammar.y +21 -0
  11. data/lib/opal/grammar_helpers.rb +360 -0
  12. data/lib/opal/lexer.rb +55 -401
  13. data/lib/opal/lexer_scope.rb +28 -0
  14. data/lib/opal/parser.rb +155 -171
  15. data/lib/opal/target_scope.rb +257 -0
  16. data/lib/opal/version.rb +1 -1
  17. data/opal.gemspec +6 -2
  18. data/opal/opal-parser.js.erb +3 -2
  19. data/opal/opal.rb +20 -18
  20. data/opal/opal/array.rb +21 -12
  21. data/opal/opal/basic_object.rb +2 -1
  22. data/opal/opal/boolean.rb +3 -0
  23. data/opal/opal/browser_loader.js +57 -0
  24. data/opal/opal/class.rb +51 -13
  25. data/opal/opal/date.rb +1 -20
  26. data/opal/opal/enumerable.rb +66 -33
  27. data/opal/opal/error.rb +2 -0
  28. data/opal/opal/hash.rb +1 -1
  29. data/opal/opal/kernel.rb +14 -3
  30. data/opal/opal/nil_class.rb +4 -0
  31. data/opal/opal/proc.rb +9 -1
  32. data/opal/opal/racc.rb +2 -2
  33. data/opal/opal/regexp.rb +1 -1
  34. data/opal/opal/runtime.js +14 -4
  35. data/opal/opal/string.rb +21 -4
  36. data/opal/opal/time.rb +27 -0
  37. data/spec/core/array/allocate_spec.rb +7 -1
  38. data/spec/core/array/append_spec.rb +18 -3
  39. data/spec/core/array/array_spec.rb +7 -0
  40. data/spec/core/array/assoc_spec.rb +23 -8
  41. data/spec/core/array/at_spec.rb +23 -3
  42. data/spec/core/array/choice_spec.rb +20 -0
  43. data/spec/core/array/clear_spec.rb +45 -4
  44. data/spec/core/array/combination_spec.rb +55 -0
  45. data/spec/core/array/compact_spec.rb +72 -1
  46. data/spec/core/array/constructor_spec.rb +13 -2
  47. data/spec/core/array/count_spec.rb +15 -7
  48. data/spec/core/array/delete_at_spec.rb +44 -1
  49. data/spec/core/array/delete_if_spec.rb +52 -2
  50. data/spec/core/array/delete_spec.rb +83 -2
  51. data/spec/core/array/drop_spec.rb +24 -16
  52. data/spec/core/array/drop_while_spec.rb +17 -0
  53. data/spec/core/array/each_index_spec.rb +11 -1
  54. data/spec/core/array/each_spec.rb +20 -2
  55. data/spec/core/array/empty_spec.rb +4 -1
  56. data/spec/core/array/eql_spec.rb +14 -0
  57. data/spec/core/array/fetch_spec.rb +31 -2
  58. data/spec/core/array/find_index_spec.rb +8 -0
  59. data/spec/core/array/first_spec.rb +45 -8
  60. data/spec/core/array/fixtures/classes.rb +538 -0
  61. data/spec/core/array/flatten_spec.rb +200 -7
  62. data/spec/core/array/frozen_spec.rb +32 -0
  63. data/spec/core/array/include_spec.rb +16 -1
  64. data/spec/core/array/index_spec.rb +5 -25
  65. data/spec/core/array/insert_spec.rb +37 -3
  66. data/spec/core/array/inspect_spec.rb +6 -12
  67. data/spec/core/array/intersection_spec.rb +55 -4
  68. data/spec/core/array/join_spec.rb +29 -4
  69. data/spec/core/array/keep_if_spec.rb +13 -6
  70. data/spec/core/array/last_spec.rb +35 -1
  71. data/spec/core/array/length_spec.rb +7 -4
  72. data/spec/core/array/map_spec.rb +9 -47
  73. data/spec/core/array/minus_spec.rb +68 -4
  74. data/spec/core/array/multiply_spec.rb +138 -6
  75. data/spec/core/array/new_spec.rb +92 -3
  76. data/spec/core/array/ntimes_spec.rb +26 -0
  77. data/spec/core/array/plus_spec.rb +48 -2
  78. data/spec/core/array/pop_spec.rb +159 -39
  79. data/spec/core/array/push_spec.rb +29 -1
  80. data/spec/core/array/rassoc_spec.rb +31 -2
  81. data/spec/core/array/reject_spec.rb +89 -2
  82. data/spec/core/array/replace_spec.rb +7 -29
  83. data/spec/core/array/reverse_each_spec.rb +25 -1
  84. data/spec/core/array/reverse_spec.rb +53 -1
  85. data/spec/core/array/rindex_spec.rb +55 -5
  86. data/spec/core/array/select_spec.rb +35 -8
  87. data/spec/core/array/shared/collect.rb +0 -0
  88. data/spec/core/array/shared/enumeratorize.rb +12 -0
  89. data/spec/core/array/shared/eql.rb +95 -0
  90. data/spec/core/array/shared/index.rb +37 -0
  91. data/spec/core/array/shared/inspect.rb +3 -0
  92. data/spec/core/array/shared/join.rb +7 -0
  93. data/spec/core/array/shared/keep_if.rb +0 -0
  94. data/spec/core/array/shared/length.rb +0 -0
  95. data/spec/core/array/shared/replace.rb +0 -0
  96. data/spec/core/array/shared/slice.rb +0 -0
  97. data/spec/core/array/shift_spec.rb +132 -23
  98. data/spec/core/array/shuffle_spec.rb +82 -6
  99. data/spec/core/array/size_spec.rb +7 -4
  100. data/spec/core/array/slice_spec.rb +132 -1
  101. data/spec/core/array/sort_spec.rb +263 -14
  102. data/spec/core/array/take_spec.rb +24 -16
  103. data/spec/core/array/take_while_spec.rb +14 -10
  104. data/spec/core/array/to_a_spec.rb +18 -1
  105. data/spec/core/array/to_ary_spec.rb +15 -1
  106. data/spec/core/array/try_convert_spec.rb +39 -2
  107. data/spec/core/array/uniq_spec.rb +148 -3
  108. data/spec/core/array/unshift_spec.rb +36 -1
  109. data/spec/core/array/zip_spec.rb +36 -1
  110. data/spec/core/class/new_spec.rb +8 -6
  111. data/spec/core/enumerable/all_spec.rb +37 -9
  112. data/spec/core/enumerable/any_spec.rb +45 -7
  113. data/spec/core/enumerable/collect_spec.rb +4 -1
  114. data/spec/core/enumerable/count_spec.rb +4 -1
  115. data/spec/core/enumerable/detect_spec.rb +2 -2
  116. data/spec/core/enumerable/drop_spec.rb +4 -1
  117. data/spec/core/enumerable/drop_while_spec.rb +4 -1
  118. data/spec/core/enumerable/each_slice_spec.rb +2 -1
  119. data/spec/core/enumerable/each_with_index_spec.rb +4 -1
  120. data/spec/core/enumerable/each_with_object_spec.rb +4 -1
  121. data/spec/core/enumerable/entries_spec.rb +4 -1
  122. data/spec/core/enumerable/find_all_spec.rb +4 -1
  123. data/spec/core/enumerable/find_index_spec.rb +4 -1
  124. data/spec/core/enumerable/find_spec.rb +5 -2
  125. data/spec/core/enumerable/first_spec.rb +4 -1
  126. data/spec/core/enumerable/fixtures/classes.rb +198 -2
  127. data/spec/core/enumerable/grep_spec.rb +4 -1
  128. data/spec/core/enumerable/take_spec.rb +4 -1
  129. data/spec/core/enumerable/to_a_spec.rb +4 -1
  130. data/spec/core/false/and_spec.rb +11 -0
  131. data/spec/core/false/inspect_spec.rb +7 -0
  132. data/spec/core/false/or_spec.rb +11 -0
  133. data/spec/core/false/to_s_spec.rb +7 -0
  134. data/spec/core/false/xor_spec.rb +11 -0
  135. data/spec/core/kernel/rand_spec.rb +5 -5
  136. data/spec/core/module/const_get_spec.rb +4 -4
  137. data/spec/core/module/fixtures/classes.rb +434 -0
  138. data/spec/core/module/method_defined_spec.rb +49 -0
  139. data/spec/core/module/module_function_spec.rb +28 -0
  140. data/spec/core/nil/and_spec.rb +3 -1
  141. data/spec/core/nil/dup_spec.rb +7 -0
  142. data/spec/core/nil/inspect_spec.rb +3 -1
  143. data/spec/core/nil/nil_spec.rb +3 -1
  144. data/spec/core/nil/or_spec.rb +4 -2
  145. data/spec/core/nil/to_a_spec.rb +3 -1
  146. data/spec/core/nil/to_f_spec.rb +3 -1
  147. data/spec/core/nil/to_i_spec.rb +3 -1
  148. data/spec/core/nil/to_s_spec.rb +3 -1
  149. data/spec/core/nil/xor_spec.rb +4 -2
  150. data/spec/core/string/element_reference_spec.rb +14 -1
  151. data/spec/core/string/fixtures/classes.rb +0 -0
  152. data/spec/core/true/and_spec.rb +11 -0
  153. data/spec/core/true/inspect_spec.rb +7 -0
  154. data/spec/core/true/or_spec.rb +11 -0
  155. data/spec/core/true/to_s_spec.rb +7 -0
  156. data/spec/core/true/xor_spec.rb +11 -0
  157. data/spec/{core → core_ext}/array/element_reference_spec.rb +0 -0
  158. data/spec/{core → core_ext}/array/equal_value_spec.rb +0 -0
  159. data/spec/{core → core_ext}/array/fill_spec.rb +0 -0
  160. data/spec/{core → core_ext}/array/reduce_spec.rb +0 -0
  161. data/spec/core_ext/basic_object/send_spec.rb +3 -3
  162. data/spec/{core → core_ext}/boolean/singleton_class_spec.rb +0 -0
  163. data/spec/{core → core_ext}/boolean/to_json_spec.rb +0 -0
  164. data/spec/core_ext/class/_inherited_spec.rb +3 -3
  165. data/spec/core_ext/class/proc_methods_spec.rb +2 -2
  166. data/spec/core_ext/class/singleton_methods_spec.rb +8 -8
  167. data/spec/core_ext/method_missing_spec.rb +3 -3
  168. data/spec/core_ext/native/method_missing_spec.rb +3 -2
  169. data/spec/core_ext/native/to_native_spec.rb +3 -2
  170. data/spec/{core → core_ext}/nil/to_json_spec.rb +0 -0
  171. data/spec/date.rb +0 -0
  172. data/spec/fileutils.rb +0 -0
  173. data/spec/filters/ancestors.rb +4 -0
  174. data/spec/filters/array_delete.rb +3 -0
  175. data/spec/filters/array_fetch.rb +3 -0
  176. data/spec/filters/array_first.rb +3 -0
  177. data/spec/filters/array_flatten.rb +14 -0
  178. data/spec/filters/array_intersection.rb +5 -0
  179. data/spec/filters/array_join.rb +6 -0
  180. data/spec/filters/array_subclasses.rb +4 -0
  181. data/spec/filters/block_args.rb +3 -0
  182. data/spec/filters/coerce_integer.rb +9 -0
  183. data/spec/filters/frozen.rb +4 -0
  184. data/spec/filters/mocks.rb +3 -0
  185. data/spec/filters/should_receive.rb +4 -0
  186. data/spec/filters/tainted.rb +7 -0
  187. data/spec/fixtures/class.rb +124 -0
  188. data/spec/fixtures/class_variables.rb +0 -0
  189. data/spec/fixtures/constants.rb +0 -0
  190. data/spec/grammar/alias_spec.rb +1 -1
  191. data/spec/grammar/def_spec.rb +1 -0
  192. data/spec/grammar/lvar_spec.rb +1 -2
  193. data/spec/grammar/nth_ref_spec.rb +13 -0
  194. data/spec/grammar/sclass_spec.rb +6 -7
  195. data/spec/grammar/str_spec.rb +4 -4
  196. data/spec/grammar/string_spec.rb +8 -0
  197. data/spec/grammar/xstr_spec.rb +4 -4
  198. data/spec/iconv.rb +0 -0
  199. data/spec/language/alias_spec.rb +140 -3
  200. data/spec/language/and_spec.rb +14 -7
  201. data/spec/language/array_spec.rb +57 -5
  202. data/spec/language/block_spec.rb +466 -49
  203. data/spec/language/break_spec.rb +294 -44
  204. data/spec/language/case_spec.rb +151 -3
  205. data/spec/language/class_spec.rb +196 -0
  206. data/spec/language/class_variable_spec.rb +56 -0
  207. data/spec/language/def_spec.rb +507 -4
  208. data/spec/language/defined_spec.rb +19 -7
  209. data/spec/language/ensure_spec.rb +26 -39
  210. data/spec/language/execution_spec.rb +15 -0
  211. data/spec/language/fixtures/array.rb +11 -0
  212. data/spec/language/fixtures/block.rb +57 -0
  213. data/spec/language/fixtures/break.rb +240 -0
  214. data/spec/language/fixtures/ensure.rb +72 -0
  215. data/spec/language/fixtures/literal_lambda.rb +7 -0
  216. data/spec/language/fixtures/metaclass.rb +33 -0
  217. data/spec/language/fixtures/module.rb +24 -0
  218. data/spec/language/fixtures/next.rb +78 -12
  219. data/spec/language/fixtures/return.rb +118 -0
  220. data/spec/language/fixtures/send.rb +110 -0
  221. data/spec/language/fixtures/send_1.9.rb +22 -0
  222. data/spec/language/fixtures/super.rb +308 -0
  223. data/spec/language/fixtures/variables.rb +58 -0
  224. data/spec/language/fixtures/yield.rb +5 -0
  225. data/spec/language/for_spec.rb +192 -0
  226. data/spec/language/hash_spec.rb +29 -5
  227. data/spec/language/if_spec.rb +90 -9
  228. data/spec/language/literal_lambda_spec.rb +1 -47
  229. data/spec/language/loop_spec.rb +39 -2
  230. data/spec/language/metaclass_spec.rb +151 -5
  231. data/spec/language/module_spec.rb +56 -0
  232. data/spec/language/next_spec.rb +370 -12
  233. data/spec/language/not_spec.rb +55 -0
  234. data/spec/language/numbers_spec.rb +56 -0
  235. data/spec/language/or_spec.rb +31 -3
  236. data/spec/language/order_spec.rb +79 -0
  237. data/spec/language/precedence_spec.rb +483 -0
  238. data/spec/language/proc_spec.rb +249 -21
  239. data/spec/language/redo_spec.rb +67 -0
  240. data/spec/language/rescue_spec.rb +121 -0
  241. data/spec/language/retry_spec.rb +56 -0
  242. data/spec/language/return_spec.rb +281 -0
  243. data/spec/language/send_spec.rb +141 -48
  244. data/spec/language/singleton_class_spec.rb +1 -1
  245. data/spec/language/string_spec.rb +11 -0
  246. data/spec/language/super_spec.rb +213 -133
  247. data/spec/language/symbol_spec.rb +2 -1
  248. data/spec/language/undef_spec.rb +3 -1
  249. data/spec/language/unless_spec.rb +6 -2
  250. data/spec/language/until_spec.rb +102 -3
  251. data/spec/language/variables_spec.rb +1212 -16
  252. data/spec/language/versions/array_1.9.rb +39 -0
  253. data/spec/language/versions/case_1.9.rb +20 -0
  254. data/spec/language/versions/hash_1.9.rb +18 -0
  255. data/spec/language/versions/literal_lambda_1.9.rb +143 -0
  256. data/spec/language/versions/not_1.9.rb +22 -0
  257. data/spec/language/versions/send_1.9.rb +241 -0
  258. data/spec/language/versions/symbol_1.9.rb +15 -0
  259. data/spec/language/versions/variables_1.9.rb +8 -0
  260. data/spec/language/while_spec.rb +70 -5
  261. data/spec/language/yield_spec.rb +32 -6
  262. data/spec/mspec/guards/block_device.rb +0 -0
  263. data/spec/mspec/guards/endian.rb +0 -0
  264. data/spec/mspec/helpers/environment.rb +0 -0
  265. data/spec/mspec/helpers/language_version.rb +0 -0
  266. data/spec/mspec/helpers/tmp.rb +0 -0
  267. data/spec/ospec/filter.rb +32 -0
  268. data/spec/ospec/main.rb.erb +18 -0
  269. data/spec/ospec/phantom.rb +97 -0
  270. data/spec/ospec/runner.rb +95 -0
  271. data/spec/ospec/sprockets.js +40 -0
  272. data/spec/pp.rb +3 -0
  273. data/spec/rbconfig.rb +5 -0
  274. data/spec/spec_helper.rb +53 -26
  275. data/spec/yaml.rb +0 -0
  276. metadata +275 -31
  277. data/config.ru +0 -8
  278. data/lib/opal/processor.rb +0 -47
  279. data/lib/opal/scope.rb +0 -236
  280. data/lib/opal/server.rb +0 -94
  281. data/spec/core/boolean/and_spec.rb +0 -17
  282. data/spec/core/boolean/inspect_spec.rb +0 -9
  283. data/spec/core/boolean/or_spec.rb +0 -17
  284. data/spec/core/boolean/to_s_spec.rb +0 -9
  285. data/spec/core/boolean/xor_spec.rb +0 -17
@@ -0,0 +1,196 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+ require File.expand_path('../../fixtures/class', __FILE__)
3
+
4
+ ClassSpecsNumber = 12
5
+
6
+ module ClassSpecs
7
+ Number = 12
8
+ end
9
+
10
+ describe "A class definition" do
11
+ it "creates a new class" do
12
+ ClassSpecs::A.should be_kind_of(Class)
13
+ ClassSpecs::A.new.should be_kind_of(ClassSpecs::A)
14
+ end
15
+
16
+ pending "has no class variables" do
17
+ ClassSpecs::A.class_variables.should == []
18
+ end
19
+
20
+ pending "raises TypeError if constant given as class name exists and is not a Module" do
21
+ # 1.9 needs the constant defined here because of it's scoping rules
22
+ ClassSpecsNumber = 12
23
+ lambda {
24
+ class ClassSpecsNumber
25
+ end
26
+ }.should raise_error(TypeError)
27
+ end
28
+
29
+ # test case known to be detecting bugs (JRuby, MRI 1.9)
30
+ pending "raises TypeError if the constant qualifying the class is nil" do
31
+ lambda {
32
+ class nil::Foo
33
+ end
34
+ }.should raise_error(TypeError)
35
+ end
36
+
37
+ pending "raises TypeError if any constant qualifying the class is not a Module" do
38
+ lambda {
39
+ class ClassSpecs::Number::MyClass
40
+ end
41
+ }.should raise_error(TypeError)
42
+
43
+ lambda {
44
+ class ClassSpecsNumber::MyClass
45
+ end
46
+ }.should raise_error(TypeError)
47
+ end
48
+
49
+ pending "allows using self as the superclass if self is a class" do
50
+ ClassSpecs::I::J.superclass.should == ClassSpecs::I
51
+
52
+ lambda {
53
+ class ShouldNotWork < self; end
54
+ }.should raise_error(TypeError)
55
+ end
56
+
57
+ pending "raises a TypeError if inheriting from a metaclass" do
58
+ obj = mock("metaclass super")
59
+ meta = obj.singleton_class
60
+ lambda { class ClassSpecs::MetaclassSuper < meta; end }.should raise_error(TypeError)
61
+ end
62
+
63
+ # # I do not think this is a valid spec -- rue
64
+ # it "has no class-level instance variables" do
65
+ # ClassSpecs::A.instance_variables.should == []
66
+ # end
67
+
68
+ pending "allows the declaration of class variables in the body" do
69
+ ClassSpecs.string_class_variables(ClassSpecs::B).should == ["@@cvar"]
70
+ ClassSpecs::B.send(:class_variable_get, :@@cvar).should == :cvar
71
+ end
72
+
73
+ pending "stores instance variables defined in the class body in the class object" do
74
+ ClassSpecs.string_instance_variables(ClassSpecs::B).should include("@ivar")
75
+ ClassSpecs::B.instance_variable_get(:@ivar).should == :ivar
76
+ end
77
+
78
+ pending "allows the declaration of class variables in a class method" do
79
+ ClassSpecs::C.class_variables.should == []
80
+ ClassSpecs::C.make_class_variable
81
+ ClassSpecs.string_class_variables(ClassSpecs::C).should == ["@@cvar"]
82
+ end
83
+
84
+ pending "allows the definition of class-level instance variables in a class method" do
85
+ ClassSpecs.string_instance_variables(ClassSpecs::C).should_not include("@civ")
86
+ ClassSpecs::C.make_class_instance_variable
87
+ ClassSpecs.string_instance_variables(ClassSpecs::C).should include("@civ")
88
+ end
89
+
90
+ pending "allows the declaration of class variables in an instance method" do
91
+ ClassSpecs::D.class_variables.should == []
92
+ ClassSpecs::D.new.make_class_variable
93
+ ClassSpecs.string_class_variables(ClassSpecs::D).should == ["@@cvar"]
94
+ end
95
+
96
+ it "allows the definition of instance methods" do
97
+ ClassSpecs::E.new.meth.should == :meth
98
+ end
99
+
100
+ it "allows the definition of class methods" do
101
+ ClassSpecs::E.cmeth.should == :cmeth
102
+ end
103
+
104
+ it "allows the definition of class methods using class << self" do
105
+ ClassSpecs::E.smeth.should == :smeth
106
+ end
107
+
108
+ pending "allows the definition of Constants" do
109
+ Object.const_defined?('CONSTANT').should == false
110
+ ClassSpecs::E.const_defined?('CONSTANT').should == true
111
+ ClassSpecs::E::CONSTANT.should == :constant!
112
+ end
113
+
114
+ it "returns the value of the last statement in the body" do
115
+ class ClassSpecs::Empty; end.should == nil
116
+ class ClassSpecs::Twenty; 20; end.should == 20
117
+ class ClassSpecs::Plus; 10 + 20; end.should == 30
118
+ class ClassSpecs::Singleton; class << self; :singleton; end; end.should == :singleton
119
+ end
120
+ end
121
+
122
+ describe "An outer class definition" do
123
+ ruby_version_is ""..."1.9" do
124
+ it "contains the inner classes" do
125
+ ClassSpecs::Container.constants.should include('A', 'B')
126
+ end
127
+ end
128
+
129
+ ruby_version_is "1.9" do
130
+ pending "contains the inner classes" do
131
+ ClassSpecs::Container.constants.should include(:A, :B)
132
+ end
133
+ end
134
+ end
135
+
136
+ describe "A class definition extending an object (sclass)" do
137
+ it "allows adding methods" do
138
+ ClassSpecs::O.smeth.should == :smeth
139
+ end
140
+
141
+ pending "raises a TypeError when trying to extend numbers" do
142
+ lambda {
143
+ eval <<-CODE
144
+ class << 1
145
+ def xyz
146
+ self
147
+ end
148
+ end
149
+ CODE
150
+ }.should raise_error(TypeError)
151
+ end
152
+
153
+ pending "allows accessing the block of the original scope" do
154
+ ClassSpecs.sclass_with_block { 123 }.should == 123
155
+ end
156
+
157
+ pending do
158
+ not_compliant_on :rubinius do
159
+ it "can use return to cause the enclosing method to return" do
160
+ ClassSpecs.sclass_with_return.should == :inner
161
+ end
162
+ end
163
+ end
164
+ end
165
+
166
+ describe "Reopening a class" do
167
+ it "extends the previous definitions" do
168
+ c = ClassSpecs::F.new
169
+ c.meth.should == :meth
170
+ c.another.should == :another
171
+ end
172
+
173
+ it "overwrites existing methods" do
174
+ ClassSpecs::G.new.override.should == :override
175
+ end
176
+
177
+ pending "raises a TypeError when superclasses mismatch" do
178
+ lambda { class ClassSpecs::A < Array; end }.should raise_error(TypeError)
179
+ end
180
+
181
+ it "adds new methods to subclasses" do
182
+ lambda { ClassSpecs::M.m }.should raise_error(NoMethodError)
183
+ class ClassSpecs::L
184
+ def self.m
185
+ 1
186
+ end
187
+ end
188
+ ClassSpecs::M.m.should == 1
189
+ end
190
+ end
191
+
192
+ describe "class provides hooks" do
193
+ it "calls inherited when a class is created" do
194
+ ClassSpecs::H.track_inherited.should == [ClassSpecs::K]
195
+ end
196
+ end
@@ -0,0 +1,56 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+ require File.expand_path('../../fixtures/class_variables', __FILE__)
3
+
4
+ describe "A class variable" do
5
+ pending "can be accessed from a subclass" do
6
+ ClassVariablesSpec::ClassB.new.cvar_a.should == :cvar_a
7
+ end
8
+
9
+ pending "is set in the superclass" do
10
+ a = ClassVariablesSpec::ClassA.new
11
+ b = ClassVariablesSpec::ClassB.new
12
+ b.cvar_a = :new_val
13
+
14
+ a.cvar_a.should == :new_val
15
+ end
16
+ end
17
+
18
+ describe "A class variable defined in a module" do
19
+ pending "can be accessed from classes that extend the module" do
20
+ ClassVariablesSpec::ClassC.cvar_m.should == :value
21
+ end
22
+
23
+ pending "is not defined in these classes" do
24
+ ClassVariablesSpec::ClassC.cvar_defined?.should be_false
25
+ end
26
+
27
+ pending "is only updated in the module a method defined in the module is used" do
28
+ ClassVariablesSpec::ClassC.cvar_m = "new value"
29
+ ClassVariablesSpec::ClassC.cvar_m.should == "new value"
30
+
31
+ ClassVariablesSpec::ClassC.cvar_defined?.should be_false
32
+ end
33
+
34
+ pending "is updated in the class when a Method defined in the class is used" do
35
+ ClassVariablesSpec::ClassC.cvar_c = "new value"
36
+ ClassVariablesSpec::ClassC.cvar_defined?.should be_true
37
+ end
38
+
39
+ pending "can be accessed inside the class using the module methods" do
40
+ ClassVariablesSpec::ClassC.cvar_c = "new value"
41
+
42
+ ClassVariablesSpec::ClassC.cvar_m.should == "new value"
43
+ end
44
+
45
+ pending "can be accessed from modules that extend the module" do
46
+ ClassVariablesSpec::ModuleO.cvar_n.should == :value
47
+ end
48
+
49
+ pending "is defined in the extended module" do
50
+ ClassVariablesSpec::ModuleN.class_variable_defined?(:@@cvar_n).should be_true
51
+ end
52
+
53
+ pending "is not defined in the extending module" do
54
+ ClassVariablesSpec::ModuleO.class_variable_defined?(:@@cvar_n).should be_false
55
+ end
56
+ end
@@ -1,11 +1,318 @@
1
- class DefSpecSingleton
2
- class << self
3
- def a_class_method;self;end
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ # Language-level method behaviour
4
+ describe "Redefining a method" do
5
+ it "replaces the original method" do
6
+ def barfoo; 100; end
7
+ barfoo.should == 100
8
+
9
+ def barfoo; 200; end
10
+ barfoo.should == 200
11
+ end
12
+ end
13
+
14
+ describe "Defining an 'initialize' method" do
15
+ pending "sets the method's visibility to private" do
16
+ class DefInitializeSpec
17
+ def initialize
18
+ end
19
+ end
20
+ DefInitializeSpec.should have_private_instance_method(:initialize, false)
21
+ end
22
+ end
23
+
24
+ describe "Defining an 'initialize_copy' method" do
25
+ pending "sets the method's visibility to private" do
26
+ class DefInitializeCopySpec
27
+ def initialize_copy
28
+ end
29
+ end
30
+ DefInitializeCopySpec.should have_private_instance_method(:initialize_copy, false)
31
+ end
32
+ end
33
+
34
+ describe "An instance method definition with a splat" do
35
+ it "accepts an unnamed '*' argument" do
36
+ def foo(*); end;
37
+
38
+ foo.should == nil
39
+ foo(1, 2).should == nil
40
+ foo(1, 2, 3, 4, :a, :b, 'c', 'd').should == nil
41
+ end
42
+
43
+ it "accepts a named * argument" do
44
+ def foo(*a); a; end;
45
+ foo.should == []
46
+ foo(1, 2).should == [1, 2]
47
+ foo([:a]).should == [[:a]]
48
+ end
49
+
50
+ it "accepts non-* arguments before the * argument" do
51
+ def foo(a, b, c, d, e, *f); [a, b, c, d, e, f]; end
52
+ foo(1, 2, 3, 4, 5, 6, 7, 8).should == [1, 2, 3, 4, 5, [6, 7, 8]]
53
+ end
54
+
55
+ it "allows only a single * argument" do
56
+ lambda { eval 'def foo(a, *b, *c); end' }.should raise_error(SyntaxError)
57
+ end
58
+
59
+ it "requires the presence of any arguments that precede the *" do
60
+ def foo(a, b, *c); end
61
+ lambda { foo 1 }.should raise_error(ArgumentError)
62
+ end
63
+ end
64
+
65
+ describe "An instance method with a default argument" do
66
+ it "evaluates the default when no arguments are passed" do
67
+ def foo(a = 1)
68
+ a
69
+ end
70
+ foo.should == 1
71
+ foo(2).should == 2
72
+ end
73
+
74
+ it "evaluates the default empty expression when no arguments are passed" do
75
+ def foo(a = ())
76
+ a
77
+ end
78
+ foo.should == nil
79
+ foo(2).should == 2
80
+ end
81
+
82
+ it "assigns an empty Array to an unused splat argument" do
83
+ def foo(a = 1, *b)
84
+ [a,b]
85
+ end
86
+ foo.should == [1, []]
87
+ foo(2).should == [2, []]
88
+ end
89
+
90
+ it "evaluates the default when required arguments precede it" do
91
+ def foo(a, b = 2)
92
+ [a,b]
93
+ end
94
+ lambda { foo }.should raise_error(ArgumentError)
95
+ foo(1).should == [1, 2]
96
+ end
97
+
98
+ it "prefers to assign to a default argument before a splat argument" do
99
+ def foo(a, b = 2, *c)
100
+ [a,b,c]
101
+ end
102
+ lambda { foo }.should raise_error(ArgumentError)
103
+ foo(1).should == [1,2,[]]
104
+ end
105
+
106
+ it "prefers to assign to a default argument when there are no required arguments" do
107
+ def foo(a = 1, *args)
108
+ [a,args]
109
+ end
110
+ foo(2,2).should == [2,[2]]
111
+ end
112
+
113
+ it "does not evaluate the default when passed a value and a * argument" do
114
+ def foo(a, b = 2, *args)
115
+ [a,b,args]
116
+ end
117
+ foo(2,3,3).should == [2,3,[3]]
118
+ end
119
+ end
120
+
121
+ describe "A singleton method definition" do
122
+ after :all do
123
+ Object.remove_class_variable :@@a rescue nil
124
+ end
125
+
126
+ pending "can be declared for a local variable" do
127
+ a = "hi"
128
+ def a.foo
129
+ 5
130
+ end
131
+ a.foo.should == 5
132
+ end
133
+
134
+ pending "can be declared for an instance variable" do
135
+ @a = "hi"
136
+ def @a.foo
137
+ 6
138
+ end
139
+ @a.foo.should == 6
140
+ end
141
+
142
+ pending "can be declared for a global variable" do
143
+ $__a__ = "hi"
144
+ def $__a__.foo
145
+ 7
146
+ end
147
+ $__a__.foo.should == 7
148
+ end
149
+
150
+ pending "can be declared for a class variable" do
151
+ @@a = "hi"
152
+ def @@a.foo
153
+ 8
154
+ end
155
+ @@a.foo.should == 8
156
+ end
157
+
158
+ pending "can be declared with an empty method body" do
159
+ class DefSpec
160
+ def self.foo;end
161
+ end
162
+ DefSpec.foo.should == nil
163
+ end
164
+
165
+ pending "can be redefined" do
166
+ obj = Object.new
167
+ def obj.==(other)
168
+ 1
169
+ end
170
+ (obj==1).should == 1
171
+ def obj.==(other)
172
+ 2
173
+ end
174
+ (obj==2).should == 2
175
+ end
176
+
177
+ ruby_version_is ""..."1.9" do
178
+ it "raises TypeError if frozen" do
179
+ obj = Object.new
180
+ obj.freeze
181
+ lambda { def obj.foo; end }.should raise_error(TypeError)
182
+ end
183
+ end
184
+
185
+ ruby_version_is "1.9" do
186
+ pending "raises RuntimeError if frozen" do
187
+ obj = Object.new
188
+ obj.freeze
189
+ lambda { def obj.foo; end }.should raise_error(RuntimeError)
190
+ end
191
+ end
192
+ end
193
+
194
+ describe "Redefining a singleton method" do
195
+ pending "does not inherit a previously set visibility " do
196
+ o = Object.new
197
+
198
+ class << o; private; def foo; end; end;
199
+
200
+ class << o; should have_private_instance_method(:foo); end
201
+
202
+ class << o; def foo; end; end;
203
+
204
+ class << o; should_not have_private_instance_method(:foo); end
205
+ class << o; should have_instance_method(:foo); end
206
+
207
+ end
208
+ end
209
+
210
+ describe "Redefining a singleton method" do
211
+ pending "does not inherit a previously set visibility " do
212
+ o = Object.new
213
+
214
+ class << o; private; def foo; end; end;
215
+
216
+ class << o; should have_private_instance_method(:foo); end
217
+
218
+ class << o; def foo; end; end;
219
+
220
+ class << o; should_not have_private_instance_method(:foo); end
221
+ class << o; should have_instance_method(:foo); end
222
+
223
+ end
224
+ end
225
+
226
+ describe "A method defined with extreme default arguments" do
227
+ pending "can redefine itself when the default is evaluated" do
228
+ class DefSpecs
229
+ def foo(x = (def foo; "hello"; end;1));x;end
230
+ end
231
+
232
+ d = DefSpecs.new
233
+ d.foo(42).should == 42
234
+ d.foo.should == 1
235
+ d.foo.should == 'hello'
236
+ end
237
+
238
+ pending "may use an fcall as a default" do
239
+ def foo(x = caller())
240
+ x
241
+ end
242
+ foo.shift.should be_kind_of(String)
243
+ end
244
+
245
+ it "evaluates the defaults in the method's scope" do
246
+ def foo(x = ($foo_self = self; nil)); end
247
+ foo
248
+ $foo_self.should == self
249
+ end
250
+
251
+ it "may use preceding arguments as defaults" do
252
+ def foo(obj, width=obj.length)
253
+ width
254
+ end
255
+ foo('abcde').should == 5
256
+ end
257
+
258
+ it "may use a lambda as a default" do
259
+ def foo(output = 'a', prc = lambda {|n| output * n})
260
+ prc.call(5)
261
+ end
262
+ foo.should == 'aaaaa'
263
+ end
264
+ end
265
+
266
+ describe "A singleton method defined with extreme default arguments" do
267
+ pending "may use a method definition as a default" do
268
+ $__a = "hi"
269
+ def $__a.foo(x = (def $__a.foo; "hello"; end;1));x;end
270
+
271
+ $__a.foo(42).should == 42
272
+ $__a.foo.should == 1
273
+ $__a.foo.should == 'hello'
274
+ end
275
+
276
+ pending "may use an fcall as a default" do
277
+ a = "hi"
278
+ def a.foo(x = caller())
279
+ x
280
+ end
281
+ a.foo.shift.should be_kind_of(String)
282
+ end
283
+
284
+ pending "evaluates the defaults in the singleton scope" do
285
+ a = "hi"
286
+ def a.foo(x = ($foo_self = self; nil)); 5 ;end
287
+ a.foo
288
+ $foo_self.should == a
289
+ end
290
+
291
+ pending "may use preceding arguments as defaults" do
292
+ a = 'hi'
293
+ def a.foo(obj, width=obj.length)
294
+ width
295
+ end
296
+ a.foo('abcde').should == 5
297
+ end
298
+
299
+ pending "may use a lambda as a default" do
300
+ a = 'hi'
301
+ def a.foo(output = 'a', prc = lambda {|n| output * n})
302
+ prc.call(5)
303
+ end
304
+ a.foo.should == 'aaaaa'
4
305
  end
5
306
  end
6
307
 
7
308
  describe "A method definition inside a metaclass scope" do
8
- it "can create a class method" do
309
+ pending "can create a class method" do
310
+ class DefSpecSingleton
311
+ class << self
312
+ def a_class_method;self;end
313
+ end
314
+ end
315
+
9
316
  DefSpecSingleton.a_class_method.should == DefSpecSingleton
10
317
  lambda { Object.a_class_method }.should raise_error(NoMethodError)
11
318
  end
@@ -19,6 +326,83 @@ describe "A method definition inside a metaclass scope" do
19
326
  obj.a_singleton_method.should == obj
20
327
  lambda { Object.new.a_singleton_method }.should raise_error(NoMethodError)
21
328
  end
329
+
330
+ ruby_version_is ""..."1.9" do
331
+ it "raises TypeError if frozen" do
332
+ obj = Object.new
333
+ obj.freeze
334
+
335
+ class << obj
336
+ lambda { def foo; end }.should raise_error(TypeError)
337
+ end
338
+ end
339
+ end
340
+
341
+ ruby_version_is "1.9" do
342
+ pending "raises RuntimeError if frozen" do
343
+ obj = Object.new
344
+ obj.freeze
345
+
346
+ class << obj
347
+ lambda { def foo; end }.should raise_error(RuntimeError)
348
+ end
349
+ end
350
+ end
351
+ end
352
+
353
+ describe "A nested method definition" do
354
+ pending "creates an instance method when evaluated in an instance method" do
355
+ class DefSpecNested
356
+ def create_instance_method
357
+ def an_instance_method;self;end
358
+ an_instance_method
359
+ end
360
+ end
361
+
362
+ obj = DefSpecNested.new
363
+ obj.create_instance_method.should == obj
364
+ obj.an_instance_method.should == obj
365
+
366
+ other = DefSpecNested.new
367
+ other.an_instance_method.should == other
368
+
369
+ DefSpecNested.should have_instance_method(:an_instance_method)
370
+ end
371
+
372
+ pending "creates a class method when evaluated in a class method" do
373
+ class DefSpecNested
374
+ class << self
375
+ def create_class_method
376
+ def a_class_method;self;end
377
+ a_class_method
378
+ end
379
+ end
380
+ end
381
+
382
+ lambda { DefSpecNested.a_class_method }.should raise_error(NoMethodError)
383
+ DefSpecNested.create_class_method.should == DefSpecNested
384
+ DefSpecNested.a_class_method.should == DefSpecNested
385
+ lambda { Object.a_class_method }.should raise_error(NoMethodError)
386
+ lambda { DefSpecNested.new.a_class_method }.should raise_error(NoMethodError)
387
+ end
388
+
389
+ pending "creates a singleton method when evaluated in the metaclass of an instance" do
390
+ class DefSpecNested
391
+ def create_singleton_method
392
+ class << self
393
+ def a_singleton_method;self;end
394
+ end
395
+ a_singleton_method
396
+ end
397
+ end
398
+
399
+ obj = DefSpecNested.new
400
+ obj.create_singleton_method.should == obj
401
+ obj.a_singleton_method.should == obj
402
+
403
+ other = DefSpecNested.new
404
+ lambda { other.a_singleton_method }.should raise_error(NoMethodError)
405
+ end
22
406
  end
23
407
 
24
408
  describe "A method definition inside an instance_eval" do
@@ -45,4 +429,123 @@ describe "A method definition inside an instance_eval" do
45
429
  other = Object.new
46
430
  lambda { other.a_metaclass_eval_method }.should raise_error(NoMethodError)
47
431
  end
432
+
433
+ pending "creates a class method when the receiver is a class" do
434
+ DefSpecNested.instance_eval do
435
+ def an_instance_eval_class_method;self;end
436
+ end
437
+
438
+ DefSpecNested.an_instance_eval_class_method.should == DefSpecNested
439
+ lambda { Object.an_instance_eval_class_method }.should raise_error(NoMethodError)
440
+ end
48
441
  end
442
+
443
+ describe "A method definition in an eval" do
444
+ pending "creates an instance method" do
445
+ class DefSpecNested
446
+ def eval_instance_method
447
+ eval "def an_eval_instance_method;self;end", binding
448
+ an_eval_instance_method
449
+ end
450
+ end
451
+
452
+ obj = DefSpecNested.new
453
+ obj.eval_instance_method.should == obj
454
+ obj.an_eval_instance_method.should == obj
455
+
456
+ other = DefSpecNested.new
457
+ other.an_eval_instance_method.should == other
458
+
459
+ lambda { Object.new.an_eval_instance_method }.should raise_error(NoMethodError)
460
+ end
461
+
462
+ pending "creates a class method" do
463
+ class DefSpecNestedB
464
+ class << self
465
+ def eval_class_method
466
+ eval "def an_eval_class_method;self;end" #, binding
467
+ an_eval_class_method
468
+ end
469
+ end
470
+ end
471
+
472
+ DefSpecNestedB.eval_class_method.should == DefSpecNestedB
473
+ DefSpecNestedB.an_eval_class_method.should == DefSpecNestedB
474
+
475
+ lambda { Object.an_eval_class_method }.should raise_error(NoMethodError)
476
+ lambda { DefSpecNestedB.new.an_eval_class_method}.should raise_error(NoMethodError)
477
+ end
478
+
479
+ pending "creates a singleton method" do
480
+ class DefSpecNested
481
+ def eval_singleton_method
482
+ class << self
483
+ eval "def an_eval_singleton_method;self;end", binding
484
+ end
485
+ an_eval_singleton_method
486
+ end
487
+ end
488
+
489
+ obj = DefSpecNested.new
490
+ obj.eval_singleton_method.should == obj
491
+ obj.an_eval_singleton_method.should == obj
492
+
493
+ other = DefSpecNested.new
494
+ lambda { other.an_eval_singleton_method }.should raise_error(NoMethodError)
495
+ end
496
+ end
497
+
498
+ describe "a method definition that sets more than one default parameter all to the same value" do
499
+ def foo(a=b=c={})
500
+ [a,b,c]
501
+ end
502
+ pending "assigns them all the same object by default" do
503
+ foo.should == [{},{},{}]
504
+ a, b, c = foo
505
+ a.should eql(b)
506
+ a.should eql(c)
507
+ end
508
+
509
+ it "allows the first argument to be given, and sets the rest to null" do
510
+ foo(1).should == [1,nil,nil]
511
+ end
512
+
513
+ it "assigns the parameters different objects across different default calls" do
514
+ a, b, c = foo
515
+ d, e, f = foo
516
+ a.should_not equal(d)
517
+ end
518
+
519
+ pending "only allows overriding the default value of the first such parameter in each set" do
520
+ lambda { foo(1,2) }.should raise_error(ArgumentError)
521
+ end
522
+
523
+ def bar(a=b=c=1,d=2)
524
+ [a,b,c,d]
525
+ end
526
+
527
+ pending "treats the argument after the multi-parameter normally" do
528
+ bar.should == [1,1,1,2]
529
+ bar(3).should == [3,nil,nil,2]
530
+ bar(3,4).should == [3,nil,nil,4]
531
+ lambda { bar(3,4,5) }.should raise_error(ArgumentError)
532
+ end
533
+ end
534
+
535
+ describe "The def keyword" do
536
+ describe "within a closure" do
537
+ pending "looks outside the closure for the visibility" do
538
+ module DefSpecsLambdaVisibility
539
+ private
540
+
541
+ lambda {
542
+ def some_method; end
543
+ }.call
544
+ end
545
+
546
+ DefSpecsLambdaVisibility.should have_private_instance_method("some_method")
547
+ end
548
+ end
549
+ end
550
+
551
+ # language_version __FILE__, "def"