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