File: tc_service_point.rb

package info (click to toggle)
libneedle-ruby 1.2.0-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,436 kB
  • ctags: 886
  • sloc: ruby: 4,464; makefile: 52
file content (194 lines) | stat: -rw-r--r-- 4,459 bytes parent folder | download
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
#--
# =============================================================================
# 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'
require 'needle/interceptor'
require 'needle/pipeline/interceptor'
require 'needle/service-point'
require 'test/unit'

class TC_ServicePoint < Test::Unit::TestCase

  class Model
    attr_reader :service_point
    attr_reader :name
    attr_reader :priority
    attr_accessor :succ

    def initialize(point,name,priority,opts={},&callback)
      @point = point
      @priority = priority || 0
    end
    def call( *args )
      succ.call( *args )
    end
    def <=>( item )
      priority <=> item.priority
    end
    def reset!
    end
  end

  class Container
    def initialize( descended=true )
      @descended = descended
    end

    def root
      self
    end

    def non_public_services_exist?
      true
    end

    def non_public_services_exist=( flag )
    end

    def []( name )
      case name
        when :service_models
          { :mock => [ :mock ] }
        when :pipeline_elements
          { :mock => Model, :interceptor => Needle::Pipeline::InterceptorElement }
      end
    end

    def fullname
      "container"
    end

    def descended_from?( c )
      @descended
    end
  end

  class Interceptor
    def initialize( point, opts )
    end
    def process( chain, context )
      chain.process_next( context )
    end
  end

  def setup
    @container = Container.new
  end

  def test_initialize
    point = nil

    assert_nothing_raised do
      point =
        Needle::ServicePoint.new( @container, "test", :model => :mock ) {
          Hash.new }
    end

    assert_equal "test", point.name
    assert_equal "container.test", point.fullname

    assert_nothing_raised do
      point =
        Needle::ServicePoint.new( @container, "test", :pipeline => [] ) {
          Hash.new }
    end
  end

  def test_instance_with_explicit_pipeline_class
    point =
      Needle::ServicePoint.new( @container, "test", :pipeline => [ Model ] ) {
        Hash.new }

    inst = point.instance
    assert_instance_of Hash, inst
  end

  def test_instance
    point =
      Needle::ServicePoint.new( @container, "test", :model => :mock ) {
        Hash.new }

    inst = point.instance
    assert_instance_of Hash, inst
  end

  def test_constructor_parms_single
    point =
      Needle::ServicePoint.new( @container, "test", :model => :mock ) do |c,p|
        assert_equal @container, c
        Hash.new
      end

    point.instance
  end

  def test_constructor_parms_multiple
    point =
      Needle::ServicePoint.new( @container, "test", :model => :mock ) do |c,p|
        assert_equal @container, c
        assert_equal point, p
        Hash.new
      end

    point.instance
  end

  def test_interceptor
    point =
      Needle::ServicePoint.new( @container, "test", :model => :mock ) {
        Hash.new }

    interceptor = Needle::Interceptor.new.with { Interceptor }
    point.interceptor interceptor

    inst = point.instance
    assert_instance_of Needle::InterceptorChainBuilder::InterceptedServiceProxy, inst
  end

  def test_interceptor_after_instance
    reg = Needle::Registry.new
    reg.define.foo { "hello" }

    events = []
    reg.intercept( :foo ).doing do |ch,ctx|
      events << "first"
      ch.process_next ctx
    end
    reg.intercept( :foo ).doing do |ch,ctx|
      events << "second"
      ch.process_next ctx
    end

    obj1 = reg.foo
    obj1.length
    assert_equal [ "first", "second" ], events

    events = []
    reg.intercept( :foo ).doing do |ch,ctx|
      events << "third"
      ch.process_next ctx
    end
        
    obj2 = reg.foo
    obj2.length
    assert_equal [ "first", "second", "third" ], events

    assert_not_same obj1, obj2
  end

end