PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/spec/ruby/optional/capi/module_spec.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 279 lines | 238 code | 39 blank | 2 comment | 25 complexity | f92c04f56a4cbf74e798c6b67438e443 MD5 | raw file
  1. require File.expand_path('../spec_helper', __FILE__)
  2. require File.expand_path('../fixtures/module', __FILE__)
  3. load_extension('module')
  4. describe "CApiModule" do
  5. before :each do
  6. @m = CApiModuleSpecs.new
  7. end
  8. describe "rb_define_global_const" do
  9. it "defines a constant on Object" do
  10. @m.rb_define_global_const("CApiModuleSpecsGlobalConst", 7)
  11. ::CApiModuleSpecsGlobalConst.should == 7
  12. Object.send :remove_const, :CApiModuleSpecsGlobalConst
  13. end
  14. end
  15. describe "rb_const_set given a symbol name and a value" do
  16. it "sets a new constant on a module" do
  17. @m.rb_const_set(CApiModuleSpecs::C, :W, 7)
  18. CApiModuleSpecs::C::W.should == 7
  19. end
  20. it "sets an existing constant's value" do
  21. @m.rb_const_set(CApiModuleSpecs::C, :Z, 8)
  22. CApiModuleSpecs::C::Z.should == 8
  23. end
  24. end
  25. describe "rb_define_module_under" do
  26. it "creates a new module inside the inner class" do
  27. mod = @m.rb_define_module_under(CApiModuleSpecs, "ModuleSpecsModuleUnder1")
  28. mod.should be_kind_of(Module)
  29. end
  30. it "sets the module name" do
  31. mod = @m.rb_define_module_under(CApiModuleSpecs, "ModuleSpecsModuleUnder2")
  32. mod.name.should == "CApiModuleSpecs::ModuleSpecsModuleUnder2"
  33. end
  34. it "defines a module for an existing Autoload with an extension" do
  35. compile_extension("module_under_autoload")
  36. CApiModuleSpecs::ModuleUnderAutoload.name.should == "CApiModuleSpecs::ModuleUnderAutoload"
  37. end
  38. it "defines a module for an existing Autoload with a ruby object" do
  39. compile_extension("module_under_autoload")
  40. CApiModuleSpecs::RubyUnderAutoload.name.should == "CApiModuleSpecs::RubyUnderAutoload"
  41. end
  42. end
  43. describe "rb_define_const given a String name and a value" do
  44. it "defines a new constant on a module" do
  45. @m.rb_define_const(CApiModuleSpecs::C, "V", 7)
  46. CApiModuleSpecs::C::V.should == 7
  47. end
  48. it "sets an existing constant's value" do
  49. @m.rb_define_const(CApiModuleSpecs::C, "Z", 9)
  50. CApiModuleSpecs::C::Z.should == 9
  51. end
  52. end
  53. describe "rb_const_defined" do
  54. # The fixture converts C boolean test to Ruby 'true' / 'false'
  55. it "returns C non-zero if a constant is defined" do
  56. @m.rb_const_defined(CApiModuleSpecs::A, :X).should be_true
  57. end
  58. it "returns C non-zero if a constant is defined in Object" do
  59. @m.rb_const_defined(CApiModuleSpecs::A, :Module).should be_true
  60. end
  61. end
  62. describe "rb_const_defined_at" do
  63. # The fixture converts C boolean test to Ruby 'true' / 'false'
  64. it "returns C non-zero if a constant is defined" do
  65. @m.rb_const_defined_at(CApiModuleSpecs::A, :X).should be_true
  66. end
  67. it "does not search in ancestors for the constant" do
  68. @m.rb_const_defined_at(CApiModuleSpecs::B, :X).should be_false
  69. end
  70. it "does not search in Object" do
  71. @m.rb_const_defined_at(CApiModuleSpecs::A, :Module).should be_false
  72. end
  73. end
  74. describe "rb_const_get" do
  75. it "returns a constant defined in the module" do
  76. @m.rb_const_get(CApiModuleSpecs::A, :X).should == 1
  77. end
  78. it "returns a constant defined at toplevel" do
  79. @m.rb_const_get(CApiModuleSpecs::A, :Fixnum).should == Fixnum
  80. end
  81. it "returns a constant defined in a superclass" do
  82. @m.rb_const_get_from(CApiModuleSpecs::B, :X).should == 1
  83. end
  84. it "calls #const_missing if the constant is not defined in the class or ancestors" do
  85. CApiModuleSpecs::A.should_receive(:const_missing).with(:CApiModuleSpecsUndefined)
  86. @m.rb_const_get_from(CApiModuleSpecs::A, :CApiModuleSpecsUndefined)
  87. end
  88. end
  89. describe "rb_const_get_from" do
  90. it "returns a constant defined in the module" do
  91. @m.rb_const_get_from(CApiModuleSpecs::B, :Y).should == 2
  92. end
  93. it "returns a constant defined in a superclass" do
  94. @m.rb_const_get_from(CApiModuleSpecs::B, :X).should == 1
  95. end
  96. it "calls #const_missing if the constant is not defined in the class or ancestors" do
  97. CApiModuleSpecs::M.should_receive(:const_missing).with(:Fixnum)
  98. @m.rb_const_get_from(CApiModuleSpecs::M, :Fixnum)
  99. end
  100. end
  101. describe "rb_const_get_at" do
  102. it "returns a constant defined in the module" do
  103. @m.rb_const_get_at(CApiModuleSpecs::B, :Y).should == 2
  104. end
  105. it "calls #const_missing if the constant is not defined in the module" do
  106. CApiModuleSpecs::B.should_receive(:const_missing).with(:X)
  107. @m.rb_const_get_at(CApiModuleSpecs::B, :X)
  108. end
  109. end
  110. describe "rb_define_alias" do
  111. it "defines an alias for an existing method" do
  112. cls = Class.new do
  113. def method_to_be_aliased
  114. :method_to_be_aliased
  115. end
  116. end
  117. @m.rb_define_alias cls, "method_alias", "method_to_be_aliased"
  118. cls.new.method_alias.should == :method_to_be_aliased
  119. end
  120. end
  121. describe "rb_alias" do
  122. it "defines an alias for an existing method" do
  123. cls = Class.new do
  124. def method_to_be_aliased
  125. :method_to_be_aliased
  126. end
  127. end
  128. @m.rb_alias cls, :method_alias, :method_to_be_aliased
  129. cls.new.method_alias.should == :method_to_be_aliased
  130. end
  131. end
  132. describe "rb_define_global_function" do
  133. it "defines a method on Object" do
  134. @m.rb_define_global_function("module_specs_global_function")
  135. Kernel.should have_method(:module_specs_global_function)
  136. module_specs_global_function.should == :test_method
  137. end
  138. end
  139. describe "rb_define_method" do
  140. it "defines a method on a class" do
  141. cls = Class.new
  142. @m.rb_define_method(cls, "test_method")
  143. cls.should have_instance_method(:test_method)
  144. cls.new.test_method.should == :test_method
  145. end
  146. it "defines a method on a module" do
  147. mod = Module.new
  148. @m.rb_define_method(mod, "test_method")
  149. mod.should have_instance_method(:test_method)
  150. end
  151. end
  152. describe "rb_define_module_function" do
  153. before :each do
  154. @mod = Module.new
  155. @m.rb_define_module_function @mod, "test_module_function"
  156. end
  157. it "defines a module function" do
  158. @mod.test_module_function.should == :test_method
  159. end
  160. it "defines a private instance method" do
  161. cls = Class.new
  162. cls.send :include, @mod
  163. cls.should have_private_instance_method(:test_module_function)
  164. end
  165. end
  166. describe "rb_define_private_method" do
  167. it "defines a private method on a class" do
  168. cls = Class.new
  169. @m.rb_define_private_method(cls, "test_method")
  170. cls.should have_private_instance_method(:test_method)
  171. cls.new.send(:test_method).should == :test_method
  172. end
  173. it "defines a private method on a module" do
  174. mod = Module.new
  175. @m.rb_define_private_method(mod, "test_method")
  176. mod.should have_private_instance_method(:test_method)
  177. end
  178. end
  179. describe "rb_define_protected_method" do
  180. it "defines a protected method on a class" do
  181. cls = Class.new
  182. @m.rb_define_protected_method(cls, "test_method")
  183. cls.should have_protected_instance_method(:test_method)
  184. cls.new.send(:test_method).should == :test_method
  185. end
  186. it "defines a protected method on a module" do
  187. mod = Module.new
  188. @m.rb_define_protected_method(mod, "test_method")
  189. mod.should have_protected_instance_method(:test_method)
  190. end
  191. end
  192. describe "rb_define_singleton_method" do
  193. it "defines a method on the singleton class" do
  194. cls = Class.new
  195. a = cls.new
  196. @m.rb_define_singleton_method a, "module_specs_singleton_method"
  197. a.module_specs_singleton_method.should == :test_method
  198. lambda { cls.new.module_specs_singleton_method }.should raise_error(NoMethodError)
  199. end
  200. end
  201. describe "rb_undef_method" do
  202. it "undef'ines a method on a class" do
  203. cls = Class.new do
  204. def ruby_test_method
  205. :ruby_test_method
  206. end
  207. end
  208. cls.new.ruby_test_method.should == :ruby_test_method
  209. @m.rb_undef_method cls, "ruby_test_method"
  210. cls.should_not have_instance_method(:ruby_test_method)
  211. end
  212. end
  213. describe "rb_undef" do
  214. it "undef'ines a method on a class" do
  215. cls = Class.new do
  216. def ruby_test_method
  217. :ruby_test_method
  218. end
  219. end
  220. cls.new.ruby_test_method.should == :ruby_test_method
  221. @m.rb_undef cls, :ruby_test_method
  222. cls.should_not have_instance_method(:ruby_test_method)
  223. end
  224. end
  225. describe "rb_class2name" do
  226. it "returns the module name" do
  227. @m.rb_class2name(CApiModuleSpecs::M).should == "CApiModuleSpecs::M"
  228. end
  229. end
  230. end