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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
# -*- coding: utf-8 -*-
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 "fire and filtering event, receive a plugin" do
sum = 0
Pluggaloid::Plugin.create(:event) do
on_increase do |v|
sum += v end
filter_increase do |v|
[v * 2] end end
eval_all_events do
Pluggaloid::Event[:increase].call(1) end
assert_equal(2, sum)
end
it "filter in another thread" do
success = filter_thread = nil
Pluggaloid::Plugin.create(:event) do
on_thread do
end
on_unhandled do
success = true end
filter_thread do
filter_thread = Thread.current
[] end end
eval_all_events do
Pluggaloid::Event[:thread].call end
assert filter_thread
assert_equal Thread.current, filter_thread
Pluggaloid::Event.filter_another_thread = true
filter_thread = nil
eval_all_events do
Pluggaloid::Event[:thread].call
Pluggaloid::Event[:unhandled].call end
assert filter_thread, "The filter doesn't run."
assert success, "Event :unhandled doesn't run."
refute_equal Thread.current, filter_thread, 'The filter should execute in not a main thread'
end
it "uninstall" do
sum = 0
Pluggaloid::Plugin.create(:event) do
on_increase do |v|
sum += v end
filter_increase do |v|
[v * 2] end end
eval_all_events do
Pluggaloid::Plugin.create(:event).uninstall
Pluggaloid::Event[:increase].call(1) end
assert_equal(0, sum)
end
it "detach" do
sum = 0
event = filter = nil
Pluggaloid::Plugin.create(:event) do
event = on_increase do |v|
sum += v end
filter = filter_increase do |v|
[v * 2] end end
eval_all_events do
Pluggaloid::Event[:increase].call(1) end
assert_equal(2, sum, "It should execute filter when event called")
eval_all_events do
Pluggaloid::Plugin[:event].detach filter
Pluggaloid::Event[:increase].call(1) end
assert_equal(3, sum, "It should not execute detached filter when event called")
eval_all_events do
Pluggaloid::Plugin.create(:event).detach event
Pluggaloid::Event[:increase].call(1) end
assert_equal(3, sum, "It should not executed detached event")
end
it "get plugin list" do
assert_empty(Pluggaloid::Plugin.plugin_list, "Plugin list must empty in first time")
Pluggaloid::Plugin.create(:plugin_0)
assert_equal(%i<plugin_0>, Pluggaloid::Plugin.plugin_list, "The new plugin should appear plugin list")
Pluggaloid::Plugin.create(:plugin_1)
assert_equal(%i<plugin_0 plugin_1>, Pluggaloid::Plugin.plugin_list, "The new plugin should appear plugin list")
end
it "dsl method defevent" do
Pluggaloid::Plugin.create :defevent do
defevent :increase, prototype: [Integer] end
assert_equal([Integer], Pluggaloid::Event[:increase].options[:prototype])
assert_equal(Pluggaloid::Plugin[:defevent], Pluggaloid::Event[:increase].options[:plugin])
end
describe "unload hook" do
before do
@value = value = []
Pluggaloid::Plugin.create(:temporary) {
on_unload {
value << 2 }
on_unload {
value << 1 } }
Pluggaloid::Plugin.create(:eternal) {
on_unload {
raise "try to unload eternal plugin" } }
end
it 'should not call unload event it is not unload' do
assert_empty(@value)
end
describe 'unload temporary plugin' do
before do
eval_all_events do
Pluggaloid::Plugin.create(:temporary).uninstall
end
end
it 'was called unload hooks' do
assert_equal([1, 2].sort, @value.sort)
end
end
end
describe "defdsl" do
it "simple dsl" do
Pluggaloid::Plugin.create :dsl_def do
defdsl :twice do |number|
number * 2 end end
dsl_use = Pluggaloid::Plugin[:dsl_use]
assert_equal(4, dsl_use.twice(2))
assert_equal(0, dsl_use.twice(0))
assert_equal(-26, dsl_use.twice(-13))
end
it "callback dsl" do
Pluggaloid::Plugin.create :dsl_def do
defdsl :rejector do |value, &condition|
value.reject(&condition) end end
dsl_use = Pluggaloid::Plugin.create(:dsl_use)
assert_equal([2, 4, 6], dsl_use.rejector(1..6){ |d| 0 != (d & 1) })
end
end
it 'raises NoDefaultDelayerError if Delayer do not have default delayer' do
Delayer.default = nil
Pluggaloid::Plugin.clear!
assert_raises(Pluggaloid::NoDefaultDelayerError) do
Pluggaloid::Plugin.create(:raises) do
on_no_default_delayer_error do; end end end
end
describe "named listener" do
it 'raises when duplicate slug registered to equally events' do
assert_raises(Pluggaloid::DuplicateListenerSlugError) do
Pluggaloid::Plugin.create :duplicate_slug do
on_a(slug: :duplicate){}
on_a(slug: :duplicate){}
end
end
end
it 'was not raises when duplicate slug registered to another events' do
Pluggaloid::Plugin.create :duplicate_slug do
on_a(slug: :duplicate){}
on_b(slug: :duplicate){}
end
end
it 'successful when dupliacte name registered to another events' do
a = b = nil
Pluggaloid::Plugin.create :duplicate_name do
a = on_a(name: "duplicate"){}
b = on_b(name: "duplicate"){}
end
assert_equal("duplicate", a.name, 'a.name should be "duplicate"')
assert_equal("duplicate", b.name, 'b.name should be "duplicate"')
end
it 'include listener slug and name in Pluggaloid::Listener#inspect' do
a = nil
Pluggaloid::Plugin.create :inspect do
a = on_a(slug: :inspect_slug, name: "inspect name"){}
end
assert(a.inspect.include?("inspect_slug"), 'Pluggaloid::Listener.inspect does not include slug')
assert(a.inspect.include?("inspect name"), 'Pluggaloid::Listener.inspect does not include name')
end
end
it 'call undefined method in plugin context' do
assert_raises(NameError) do
Pluggaloid::Plugin.create(:raises) do
undefined_call
end
end
end
end
|