require 'RubyExt/require_base' require 'spec' module RubyExt describe "PackageManager" do it "Should load classes only one time" do class CustomObserver attr_reader :list def initialize @list = [] end def update_class klass @list << klass end end obs = CustomObserver.new RubyExt::ClassLoader.add_observer obs ForClassLoader::LoadCount ForClassLoader::LoadCount RubyExt::ClassLoader.delete_observers obs.list.inject(0){|count, klass| klass == ForClassLoader::LoadCount ? count + 1 : count}. should == 1 end it "Should load classes From Multiple directories" do end it "Class has the same name as package" do ForClassLoader::ModuleA::SameName RubyExt::ForClassLoader::ModuleA::SameName::SomeClass end it "Infinity Loop" do lambda{ForClassLoader::ModuleA::InfinityLoop}. should raise_error(/Class Name .+ doesn't correspond to File Name/) end it "Core" do ForClassLoader::ModuleA::ModuleB::ClassC.new end module ForClassLoader::ModuleA class << self def anonymous ClassInsideAnonymousClass end end end it "Should works inside Anonymous Class" do ForClassLoader::ModuleA.anonymous end it "Should raise exception if class isn't defined in scope" do ForClassLoader::Scope::ScopeA1 RubyExt::ClassLoader.error_on_defined_constant = true lambda{ForClassLoader::Scope::ScopeB1}. should raise_error(/Class '.+' is not defined in the '.+' Namespace!/) end it "Class Reloading" do fname = File.join(File.dirname(__FILE__), 'RubyExt/ForClassLoader/ModuleA/ClassReloading.rb') File.delete fname if File.exist? fname File.open(fname, 'w') do |f| f.write %{\ class ClassReloading def test 1 end end} end test = ForClassLoader::ModuleA::ClassReloading.new test.test.should == 1 File.open(fname, 'w') do |f| f.write %{\ class ClassReloading def test 2 end end} end RubyExt::ClassLoader.reload_class(ForClassLoader::ModuleA::ClassReloading.name) test.test.should == 2 File.delete fname if File.exist? fname end end end