File: handler_tag_test.rb

package info (click to toggle)
ruby-pluggaloid 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 176 kB
  • ctags: 70
  • sloc: ruby: 714; makefile: 2
file content (137 lines) | stat: -rw-r--r-- 3,472 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
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
# -*- coding: utf-8 -*-
require 'minitest/autorun'

require 'pluggaloid'
require_relative 'helper'

describe(Pluggaloid::HandlerTag) do
  include PluggaloidTestHelper

  before do
    Delayer.default = Delayer.generate_class(priority: %i<high normal low>, default: :normal)
    Pluggaloid::Plugin.clear!
    @pluggaloid = Pluggaloid.new(Delayer.default)
  end

  describe 'plugin tag' do
    before do
      a = b = c = d = fd = tag_a = tag_b = tag_c = tag_d = nil
      lst = @lst = []
      @plugin = @pluggaloid.Plugin.create :parent do
        a = on_a{ lst << :a }
        tag_a = handler_tag :tag_a
        tag_b = handler_tag :tag_b
        tag_c = handler_tag :tag_c
        tag_d = handler_tag :tag_d
        b = on_b(tags: tag_a){ lst << :b }
        c = on_c(tags: [tag_a, tag_b]){ lst << :c }
        handler_tag tag_d do
          d = on_d(tags: [tag_b]){ lst << :d }
          handler_tag tag_c do
            fd = filter_d{|&stop| stop.call }
          end
        end
      end
      @a, @b, @c, @d, @fd = a, b, c, d, fd
      @tag_a, @tag_b, @tag_c, @tag_d = tag_a, tag_b, tag_c, tag_d
    end

    it 'plugin has 4 listeners' do
      assert_equal 4, @plugin.listeners.count
    end

    it 'listener a has no tag' do
      assert_empty @a.tags
    end

    it 'listener b has tag_a' do
      assert_equal Set.new([@tag_a]), @b.tags
    end

    it 'listener c has tag_a and tag_b' do
      assert_equal Set.new([@tag_a, @tag_b]), @c.tags
    end

    it 'listener d has tag_b and tag_d' do
      assert_equal Set.new([@tag_b, @tag_d]), @d.tags
    end

    it 'filter fd has tag_c and tag_d' do
      assert_equal Set.new([@tag_c, @tag_d]), @fd.tags
    end

    describe 'detach tag_a' do
      before do
        @plugin.detach(@tag_a)
        @pluggaloid.Event[:a].call
        @pluggaloid.Event[:b].call
        @pluggaloid.Event[:c].call
        eval_all_events(@pluggaloid.Delayer)
      end

      it 'should not call event in tag' do
        assert_equal [:a], @lst
      end
    end

    describe 'detach tag_c' do
      before do
        @pluggaloid.Event[:a].call
        @pluggaloid.Event[:b].call
        @pluggaloid.Event[:c].call
        @pluggaloid.Event[:d].call
      end

      describe 'detach' do
        before do
          @plugin.detach(@tag_c)
          eval_all_events(@pluggaloid.Delayer)
        end

        it 'should not call filter' do
          assert_equal [:a, :b, :c, :d], @lst
        end
      end

      describe 'does not detach' do
        before do
          eval_all_events(@pluggaloid.Delayer)
        end

        it 'should call filter' do
          assert_equal [:a, :b, :c], @lst
        end
      end
    end

    describe 'each' do
      it 'should count of listeners and filters are 2' do
        assert_equal 2, @tag_a.count
      end

      it 'should count of listeners are 2 in tag_a' do
        assert_equal 2, @tag_a.listeners.count
      end

      it 'should there are no filter in tag_a' do
        assert_equal 0, @tag_a.filters.count
      end

      it 'should count of listeners are 2 in tag_b' do
        assert_equal 2, @tag_b.listeners.count
      end

      it 'should there are no filter in tag_b' do
        assert_equal 0, @tag_b.filters.count
      end

      it 'should there are no listener in tag_c' do
        assert_equal 0, @tag_c.listeners.count
      end

      it 'should has one filter in tag_c' do
        assert_equal 1, @tag_c.filters.count
      end
    end
  end
end