File: tc_interceptor.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 (92 lines) | stat: -rw-r--r-- 2,457 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
#--
# =============================================================================
# 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/interceptor'
require 'test/unit'

class TC_Interceptor < Test::Unit::TestCase

  def test_empty
    i = Needle::Interceptor.new
    assert_equal [ :priority ], i.options.keys
    assert_equal 0, i.options[:priority]
  end

  def test_missing_action
    i = Needle::Interceptor.new

    assert_raise( Needle::InterceptorConfigurationError ) do
      i.action
    end
  end

  def test_with_redefined
    i = Needle::Interceptor.new

    assert_raise( Needle::InterceptorConfigurationError ) do
      i.with { :a }.with { :a }
    end
  end

  def test_doing_redefined
    i = Needle::Interceptor.new

    assert_raise( Needle::InterceptorConfigurationError ) do
      i.doing { :a }.doing { :a }
    end
  end

  def test_with_doing
    i = Needle::Interceptor.new

    assert_raise( Needle::InterceptorConfigurationError ) do
      i.with { :a }.doing { :a }
    end
  end

  def test_doing_with
    i = Needle::Interceptor.new

    assert_raise( Needle::InterceptorConfigurationError ) do
      i.doing { :a }.with { :a }
    end
  end

  def test_options
    i = Needle::Interceptor.new.
      with_options(
        :priority => 5,
        :exclude => [ /^__/ ],
        :include => [ /^__foo/ ] )

    assert_equal 5, i[:priority]
    assert_equal [ /^__/ ], i[:exclude]
    assert_equal [ /^__foo/ ], i[:include]
  end

  def test_with
    i = Needle::Interceptor.new.with { |c| Hash.new }
    assert_instance_of Hash, i.action.call(nil)
  end

  def test_doing
    i = Needle::Interceptor.new.doing { |ch,ctx| ch.process_next(ctx) }
    assert_instance_of Needle::Interceptor::DynamicInterceptor, i.action.call(nil)
  end

end