File: wrap_action_call.rb

package info (click to toggle)
libinnate-ruby 2010.07-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 812 kB
  • ctags: 621
  • sloc: ruby: 4,242; makefile: 2
file content (83 lines) | stat: -rw-r--r-- 1,794 bytes parent folder | download | duplicates (3)
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
require File.expand_path('../../../helper', __FILE__)

SPEC_WRAP_LOG = []

class SpecWrapActionCall
  Innate.node '/'

  def first; end
  def second; end
  def third; end

  private

  def wrap_before(action)
    SPEC_WRAP_LOG << [:before, action.name]
    yield
  end

  def wrap_after(action)
    SPEC_WRAP_LOG << [:after, action.name]
    yield
  end
end

class SpecWrapActionCallStop
  Innate.node '/stop'

  def index; 'Hello'; end

  def wrap_pass(action)
    yield
  end

  def wrap_stop(action)
    'No Hello'
  end
end


describe 'Node#wrap_action_call' do
  behaves_like :rack_test

  it 'executes our wrapper' do
    SPEC_WRAP_LOG.clear
    SpecWrapActionCall.add_action_wrapper(2.0, :wrap_after)

    get('/first')
    SPEC_WRAP_LOG.should == [[:after, 'first']]

    get('/second')
    SPEC_WRAP_LOG.should == [[:after, 'first'], [:after, 'second']]

    get('/third')
    SPEC_WRAP_LOG.should == [[:after, 'first'], [:after, 'second'], [:after, 'third']]
  end

  it 'executes wrappers in correct order' do
    SPEC_WRAP_LOG.clear
    SpecWrapActionCall.add_action_wrapper(1.0, :wrap_before)

    get('/first')
    SPEC_WRAP_LOG.should == [[:before, 'first'], [:after, 'first']]

    get('/second')
    SPEC_WRAP_LOG.should == [
      [:before, 'first'], [:after, 'first'],
      [:before, 'second'], [:after, 'second']]

    get('/third')
    SPEC_WRAP_LOG.should == [
      [:before, 'first'], [:after, 'first'],
      [:before, 'second'], [:after, 'second'],
      [:before, 'third'], [:after, 'third']]
  end

  it 'stops in the chain when not yielded' do
    SpecWrapActionCallStop.add_action_wrapper(1.0, :wrap_pass)
    get('/stop').body.should == 'Hello'

    SpecWrapActionCallStop.add_action_wrapper(2.0, :wrap_stop)
    get('/stop').body.should == 'No Hello'
  end
end