File: collect_test.rb

package info (click to toggle)
ruby-pluggaloid 1.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: ruby: 1,752; sh: 4; makefile: 2
file content (135 lines) | stat: -rw-r--r-- 3,558 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
# frozen_string_literal: true

require 'bundler/setup'
require 'minitest/autorun'

require 'pluggaloid'
require_relative 'helper'

describe(Pluggaloid::Plugin) do
  include PluggaloidTestHelper

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

  it 'collect' do
    Pluggaloid::Plugin.create(:event) do
      defevent :list, prototype: [Integer, Pluggaloid::COLLECT]

      filter_list do |i, yielder|
        i.times(&yielder.method(:<<))
        [i, yielder]
      end
    end

    assert_equal([0, 1, 2], Pluggaloid::Event[:list].collect(3).to_a)
  end

  it 'fail when collect with undefined' do
    Pluggaloid::Plugin.create(:event) do
      filter_list do |i, yielder|
        i.times(&yielder.method(:<<))
        [i, yielder]
      end
    end

    assert_raises(Pluggaloid::UndefinedCollectionIndexError) do
      Pluggaloid::Event[:list].collect(3)
    end
  end

  it 'success caller arguments include Pluggaloid::COLLECT when collect with undefined' do
    Pluggaloid::Plugin.create(:event) do
      filter_list do |i, yielder|
        i.times(&yielder.method(:<<))
        [i, yielder]
      end
    end

    assert_equal([0, 1, 2], Pluggaloid::Event[:list].collect(3, Pluggaloid::COLLECT).to_a)
  end

  it 'success different caller argument specific and prototype definition' do
    Pluggaloid::Plugin.create(:event) do
      defevent :list, prototype: [Integer, Pluggaloid::COLLECT]

      filter_list do |i, yielder|
        i.times(&yielder.method(:<<))
        [i, yielder]
      end
    end

    assert_equal([0, 1, 2], Pluggaloid::Event[:list].collect(Pluggaloid::COLLECT, 3).to_a)
  end

  describe 'collection' do
    it 'add' do
      Pluggaloid::Plugin.create(:event) do
        defevent :list, prototype: [Integer, Pluggaloid::COLLECT]
        defevent :insert, prototype: [Pluggaloid::STREAM]

        collection(:list, 3) do |collector|
          subscribe(:insert).each do |i|
            collector.add(i * 3)
          end
        end
      end
      eval_all_events do
        Pluggaloid::Event[:insert].call([2, 5])
      end
      assert_equal([6, 15], Pluggaloid::Event[:list].collect(3).to_a)
    end

    it 'delete' do
      Pluggaloid::Plugin.create(:event) do
        defevent :list, prototype: [Integer, Pluggaloid::COLLECT]
        defevent :destroy, prototype: [Pluggaloid::STREAM]

        collection(:list, 3) do |collector|
          collector << 1 << 2 << 3
          subscribe(:destroy).each do |i|
            collector.delete(i)
          end
        end
      end
      eval_all_events do
        Pluggaloid::Event[:destroy].call([2, 4])
      end
      assert_equal([1, 3], Pluggaloid::Event[:list].collect(3).to_a)
    end

    it 'rewind' do
      added = []
      deleted = []
      Pluggaloid::Plugin.create(:event) do
        defevent :list, prototype: [Integer, Pluggaloid::COLLECT]

        collection(:list, 3) do |collector|
          collector << 1 << 2 << 3
          on_rewind do |e|
            collector.rewind do |lst|
              e
            end
          end
        end

        subscribe(:list__add, 3) do |elm|
          added += elm
        end

        subscribe(:list__delete, 3) do |elm|
          deleted += elm
        end
      end
      eval_all_events do
        Pluggaloid::Event[:rewind].call([2, 3, 4])
      end
      assert_equal([2, 3, 4], Pluggaloid::Event[:list].collect(3).to_a)
      assert_equal([1, 2, 3, 4], added)
      assert_equal([1], deleted)
    end
  end

end