#-- # ============================================================================= # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu) # All rights reserved. # # This source file is distributed as part of the Needle dependency injection # library for Ruby. This file (and the library as a whole) may be used only as # allowed by either the BSD license, or the Ruby license (or, by association # with the Ruby license, the GPL). See the "doc" subdirectory of the Needle # distribution for the texts of these licenses. # ----------------------------------------------------------------------------- # needle website : http://needle.rubyforge.org # project website: http://rubyforge.org/projects/needle # ============================================================================= #++ $:.unshift "../lib" require 'needle/container' require 'needle/registry' require 'test/unit' class TC_Container < Test::Unit::TestCase class TC_DefinitionContext < Test::Unit::TestCase class MockContainer attr_reader :events def initialize; @events = []; end def method_missing(s,*a,&b) @events << { :name => s, :args => a, :block => b } end end def setup @container = MockContainer.new @ctx = Needle::Container::DefinitionContext.new( @container ) end def test_register assert_nothing_raised do @ctx.hello { "world" } end assert_equal :register, @container.events[0][:name] assert_equal [ :hello ], @container.events[0][:args] assert_not_nil @container.events[0][:block] end def test_reference_bad assert_raise( NoMethodError ) do @ctx.hello( :arg ) end end def test_register_good assert_nothing_raised do @ctx.hello end assert_equal :[], @container.events[0][:name] assert_equal [ :hello ], @container.events[0][:args] assert_nil @container.events[0][:block] end def test_intercept assert_nothing_raised do @ctx.intercept( :foo ) end assert_equal :intercept, @container.events[0][:name] assert_equal [ :foo ], @container.events[0][:args] assert_nil @container.events[0][:block] end def test_this_container assert_equal @container, @ctx.this_container end end def test_default container = Needle::Container.new assert_nil container.parent assert_nil container.name assert_equal container, container.root assert_equal "", container.fullname end def test_named container = Needle::Container.new( nil, "name" ) assert_nil container.parent assert_equal "name", container.name assert_equal container, container.root assert_equal "name", container.fullname end def test_nested outer = Needle::Container.new inner = Needle::Container.new( outer ) assert_same outer, inner.parent assert_equal outer, inner.root end def test_root outer = Needle::Container.new middle = Needle::Container.new( outer ) inner = Needle::Container.new( middle ) assert_same middle, inner.parent assert_equal outer, inner.root end def test_nested_named outer = Needle::Container.new( nil, "outer" ) inner = Needle::Container.new( outer, "inner" ) assert_equal "inner", inner.name assert_equal "outer.inner", inner.fullname end def test_service_not_found container = Needle::Container.new assert_raise( Needle::ServiceNotFound ) do container[:test] end end def test_register container = Needle::Container.new container.register( :test, :pipeline=>[] ) { Hash.new } assert_nothing_raised { container[:test] } assert_nothing_raised { container.test } assert_instance_of Hash, container[:test] assert_instance_of Hash, container.test assert container.respond_to?(:test) end def test_builder container = Needle::Container.new b1 = container.builder b2 = container.builder assert_same b1.__id__, b2.__id__ end def test_define_block container = Needle::Container.new container.define do |b| b.test( :pipeline=>[] ) { Hash.new } b.namespace_define :subitem, :pipeline=>[] do |b2| b2.test2( :pipeline=>[] ) { Hash.new } end end assert container.has_key?( :test ) assert_instance_of Hash, container.test assert container.subitem.has_key?( :test2 ) assert_instance_of Hash, container.subitem.test2 end def test_define_noblock container = Needle::Container.new container.define.test( :pipeline=>[] ) { Hash.new } assert container.has_key?( :test ) assert_instance_of Hash, container.test end def test_define! container = Needle::Container.new container.define! do test( :pipeline=>[] ) { Hash.new } namespace! :subitem, :pipeline=>[] do test2( :pipeline=>[] ) { Hash.new } end end assert container.has_key?( :test ) assert_instance_of Hash, container.test assert container.subitem.has_key?( :test2 ) assert_instance_of Hash, container.subitem.test2 end def test_namespace container = Needle::Container.new container.namespace( :test, :pipeline=>[] ) assert_instance_of Needle::Container, container.test container.namespace( :test2, :pipeline=>[] ) do |ns| assert_instance_of Needle::Container, ns end assert_instance_of Needle::Container, container.test2 end def test_namespace_define container = Needle::Container.new container.namespace_define( :test, :pipeline=>[] ) do |b| b.item( :pipeline=>[] ) { Hash.new } end assert container.has_key?( :test ) assert container.test.has_key?( :item ) end def test_namespace_define! container = Needle::Container.new container.namespace_define!( :test, :pipeline=>[] ) do item( :pipeline=>[] ) { Hash.new } end assert container.has_key?( :test ) assert container.test.has_key?( :item ) end def test_namespace! container = Needle::Container.new container.namespace!( :test, :pipeline=>[] ) do item( :pipeline=>[] ) { Hash.new } end assert container.has_key?( :test ) assert container.test.has_key?( :item ) end def test_has_key container = Needle::Container.new assert !container.has_key?(:test) container.register( :test, :pipeline=>[] ) { Hash.new } assert container.has_key?(:test) end def test_knows_key container = Needle::Container.new assert !container.knows_key?(:test) container.register( :test, :pipeline=>[] ) { Hash.new } assert container.knows_key?(:test) end def test_parent_knows_key outer = Needle::Container.new inner = Needle::Container.new( outer ) outer.register( :test, :pipeline=>[] ) { Hash.new } assert !inner.has_key?(:test) assert inner.knows_key?(:test) end def test_service_in_parent outer = Needle::Container.new inner = Needle::Container.new( outer ) outer.register( :test, :pipeline=>[] ) { Hash.new } assert_nothing_raised do inner[:test] end end def test_service_not_in_parent outer = Needle::Container.new inner = Needle::Container.new( outer ) assert_raise( Needle::ServiceNotFound ) do inner[:test] end end def test_intercept_not_found container = Needle::Container.new assert_raise( Needle::ServiceNotFound ) do container.intercept( :test ) end end def test_intercept container = Needle::Registry.new container.register( :test ) { Hash.new } filtered = false container.intercept( :test ).doing { |chain,ctx| filtered = true; chain.process_next(ctx) } assert !filtered svc = container.test svc[:hello] = :world assert filtered end def test_find_definition_missing container = Needle::Container.new assert_nil container.find_definition( :bogus ) end def test_find_definition_found_local container = Needle::Container.new container.register( :test, :pipeline=>[] ) { Object.new } assert_not_nil container.find_definition( :test ) end def test_find_definition_found_ancestor outer = Needle::Container.new inner = Needle::Container.new( outer ) outer.register( :test, :pipeline=>[] ) { Object.new } assert_not_nil inner.find_definition( :test ) end def test_pipeline container = Needle::Container.new container.register( :test, :pipeline=>[] ) { Object.new } assert_instance_of Needle::Pipeline::Collection, container.pipeline( :test ) p1 = container.pipeline(:test) p2 = container.pipeline(:test) assert_same p1, p2 end end