aboutsummaryrefslogtreecommitdiffstats
path: root/test/core/test_plugin.rb
blob: 390821117084f9f5a0509416843d30ccdece44b4 (plain)
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
# -*- coding: utf-8 -*-

require 'test/unit'
require 'rubygems'
require 'mocha'
require File.expand_path(File.dirname(__FILE__) + '/../helper')

miquire :core, 'plugin'

class TC_Plugin < Test::Unit::TestCase
  def setup
    Plugin.clear!
  end

  must "basic plugin" do
    sum = 0
    Plugin.create(:event) do
      on_increase do |v|
        sum += v end

      filter_increase do |v|
        [v * 2]
      end
    end
    Event[:increase].call(1)
    Delayer.run while not Delayer.empty?
    assert_equal(2, sum)
  end

  must "uninstall" do
    sum = 0
    Plugin.create(:event) do
      on_increase do |v|
        sum += v end
      filter_increase do |v|
        [v * 2]
      end
    end
    Plugin.create(:event).uninstall
    Event[:increase].call(1)
    Delayer.run while not Delayer.empty?
    assert_equal(0, sum)
  end

  must "detach" do
    sum = 0
    event = filter = nil
    Plugin.create(:event) do
      event = on_increase do |v|
        sum += v end
      filter = filter_increase do |v|
        [v * 2]
      end
    end
    Event[:increase].call(1)
    Delayer.run while not Delayer.empty?
    assert_equal(2, sum)

    Plugin.create(:event).detach filter
    Event[:increase].call(1)
    Delayer.run while not Delayer.empty?
    assert_equal(3, sum)

    Plugin.create(:event).detach event
    Event[:increase].call(1)
    Delayer.run while not Delayer.empty?
    assert_equal(3, sum)
  end

  must "get plugin list" do
    assert_equal([], Plugin.plugin_list)
    Plugin.create(:plugin_0)
    assert_equal([:plugin_0], Plugin.plugin_list)
    Plugin.create(:plugin_1)
    assert_equal([:plugin_0, :plugin_1], Plugin.plugin_list)
  end

  must "dsl method defevent" do
    Plugin.create :defevent do
      defevent :increase, prototype: [Integer] end
    assert_equal([Integer], Event[:increase].options[:prototype])
    assert_equal(Plugin[:defevent], Event[:increase].options[:plugin])
  end

  must "unload hook" do
    value = 0
    Plugin.create(:unload) {
      on_unload {
        value += 2 }
      on_unload {
        value += 1 } }
    assert_equal(value, 0)
    Plugin.create(:unload).uninstall
    assert_equal(value, 3)
  end

  must "simple dsl" do
    Plugin.create :dsl_def do
      defdsl :twice do |number|
        number * 2
      end
    end

    dsl_use = Plugin.create(:dsl_use)
    assert_equal(4, dsl_use.twice(2))
    assert_equal(0, dsl_use.twice(0))
    assert_equal(-26, dsl_use.twice(-13))
  end

  must "callback dsl" do
    Plugin.create :dsl_def do
      defdsl :rejector do |value, &condition|
        value.reject(&condition)
      end
    end

    dsl_use = Plugin.create(:dsl_use)
    assert_equal([2, 4, 6], dsl_use.rejector(1..6){ |d| 0 != (d & 1) })
  end
end