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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
require_relative "spec_helper"
describe Sequel::Model, ".plugin" do
before do
module Sequel::Plugins
module Timestamped
module InstanceMethods
def get_stamp(*args); @values[:stamp] end
def abc; 123; end
end
module ClassMethods
def def; 234; end
end
module DatasetMethods
def ghi; 345; end
end
end
end
@c = Class.new(Sequel::Model(:items))
@t = Sequel::Plugins::Timestamped
end
after do
Sequel::Plugins.send(:remove_const, :Timestamped)
end
it "should raise LoadError if the plugin is not found" do
proc{@c.plugin :something_or_other}.must_raise(LoadError)
end
it "should store the plugin in .plugins" do
@c.plugins.wont_include(@t)
@c.plugin @t
@c.plugins.must_include(@t)
end
it "should be inherited in subclasses" do
@c.plugins.wont_include(@t)
c1 = Class.new(@c)
@c.plugin @t
c2 = Class.new(@c)
@c.plugins.must_include(@t)
c1.plugins.wont_include(@t)
c2.plugins.must_include(@t)
end
it "should accept a symbol and load the module from the Sequel::Plugins namespace" do
@c.plugin :timestamped
@c.plugins.must_include(@t)
end
it "should accept a module" do
m = Module.new
@c.plugin m
@c.plugins.must_include(m)
end
it "should not attempt to load a plugin twice" do
@c.plugins.wont_include(@t)
@c.plugin @t
@c.plugins.reject{|m| m != @t}.length.must_equal 1
@c.plugin @t
@c.plugins.reject{|m| m != @t}.length.must_equal 1
end
it "should call apply and configure if the plugin responds to it, with the args and block used" do
m = Module.new do
def self.args; @args; end
def self.block; @block; end
def self.block_call; @block.call; end
def self.args2; @args2; end
def self.block2; @block2; end
def self.block2_call; @block2.call; end
def self.apply(model, *args, &block)
@args = args
@block = block
model.send(:define_method, :blah){43}
end
def self.configure(model, *args, &block)
@args2 = args
@block2 = block
model.send(:define_method, :blag){44}
end
end
b = lambda{42}
@c.plugin(m, 123, 1=>2, &b)
m.args.must_equal [123, {1=>2}]
m.block.must_equal b
m.block_call.must_equal 42
@c.new.blah.must_equal 43
m.args2.must_equal [123, {1=>2}]
m.block2.must_equal b
m.block2_call.must_equal 42
@c.new.blag.must_equal 44
end
it "should call configure even if the plugin has already been loaded" do
m = Module.new do
@args = []
def self.args; @args; end
def self.configure(model, *args, &block)
@args << [block, *args]
end
end
b = lambda{42}
@c.plugin(m, 123, 1=>2, &b)
m.args.must_equal [[b, 123, {1=>2}]]
b2 = lambda{44}
@c.plugin(m, 234, 2=>3, &b2)
m.args.must_equal [[b, 123, {1=>2}], [b2, 234, {2=>3}]]
end
it "should call things in the following order: apply, ClassMethods, InstanceMethods, DatasetMethods, configure" do
m = Module.new do
@args = []
def self.args; @args; end
def self.apply(model, *args, &block)
@args << :apply
end
def self.configure(model, *args, &block)
@args << :configure
end
self::InstanceMethods = Module.new do
def self.included(model)
model.plugins.last.args << :im
end
end
self::ClassMethods = Module.new do
def self.extended(model)
model.plugins.last.args << :cm
end
end
self::DatasetMethods = Module.new do
def self.extended(dataset)
dataset.model.plugins.last.args << :dm
end
end
end
b = lambda{44}
@c.plugin(m, 123, 1=>2, &b)
m.args.must_equal [:apply, :cm, :im, :dm, :configure]
@c.plugin(m, 234, 2=>3, &b)
m.args.must_equal [:apply, :cm, :im, :dm, :configure, :configure]
end
it "should include an InstanceMethods module in the class if the plugin includes it" do
@c.plugin @t
m = @c.new
m.must_respond_to(:get_stamp)
m.must_respond_to(:abc)
m.abc.must_equal 123
t = Time.now
m[:stamp] = t
m.get_stamp.must_equal t
end
it "should extend the class with a ClassMethods module if the plugin includes it" do
@c.plugin @t
@c.def.must_equal 234
end
it "should extend the class's dataset with a DatasetMethods module if the plugin includes it" do
@c.plugin @t
@c.dataset.ghi.must_equal 345
end
it "should save the DatasetMethods module and apply it later if the class doesn't have a dataset" do
c = Class.new(Sequel::Model)
c.plugin @t
c.dataset = DB[:i]
c.dataset.ghi.must_equal 345
end
it "should save the DatasetMethods module and apply it later if the class has a dataset" do
@c.plugin @t
@c.dataset = DB[:i]
@c.dataset.ghi.must_equal 345
end
it "should not define class methods for private instance methods in DatasetMethod" do
m = Module.new do
self::DatasetMethods = Module.new do
def b; 2; end
private
def a; 1; end
end
end
@c.plugin m
@c.dataset.b.must_equal 2
lambda{@c.dataset.a}.must_raise(NoMethodError)
@c.dataset.send(:a).must_equal 1
lambda{@c.a}.must_raise(NoMethodError)
lambda{@c.send(:a)}.must_raise(NoMethodError)
end
it "should not raise an error if the DatasetMethod module has no public instance methods" do
m = Module.new do
self::DatasetMethods = Module.new do
private
def a; 1; end
end
end
@c.plugin m
end
it "should not raise an error if plugin submodule names exist higher up in the namespace hierarchy" do
class ::ClassMethods; end
@c.plugin(m = Module.new)
Object.send(:remove_const, :ClassMethods)
@c.plugins.must_include(m)
class ::InstanceMethods; end
@c.plugin(m = Module.new)
Object.send(:remove_const, :InstanceMethods)
@c.plugins.must_include(m)
class ::DatasetMethods; end
@c.plugin(m = Module.new)
Object.send(:remove_const, :DatasetMethods)
@c.plugins.must_include(m)
end
end
describe Sequel::Plugins do
before do
@c = Class.new(Sequel::Model(:items))
end
it "should have def_dataset_methods define methods that call methods on the dataset" do
m = Module.new do
module self::ClassMethods
Sequel::Plugins.def_dataset_methods(self, :one)
end
module self::DatasetMethods
def one
1
end
end
end
@c.plugin m
@c.one.must_equal 1
end
it "should have def_dataset_methods accept an array with multiple methods" do
m = Module.new do
module self::ClassMethods
Sequel::Plugins.def_dataset_methods(self, [:one, :two])
end
module self::DatasetMethods
def one
1
end
def two
2
end
end
end
@c.plugin m
@c.one.must_equal 1
@c.two.must_equal 2
end
it "should have inherited_instance_variables add instance variables to copy into the subclass" do
m = Module.new do
def self.apply(model)
model.instance_variable_set(:@one, 1)
end
module self::ClassMethods
attr_reader :one
Sequel::Plugins.inherited_instance_variables(self, :@one=>nil)
end
end
@c.plugin m
Class.new(@c).one.must_equal 1
end
it "should have after_set_dataset add a method to call after set_dataset" do
m = Module.new do
module self::ClassMethods
Sequel::Plugins.after_set_dataset(self, :one)
def foo; dataset.send(:cache_get, :foo) end
private
def one; dataset.send(:cache_set, :foo, 1) end
end
end
@c.plugin m
@c.foo.must_be_nil
@c.set_dataset :blah
@c.foo.must_equal 1
end
end
describe "Sequel::Model.plugin" do
before do
@c = Class.new(Sequel::Model)
end
after do
Sequel::Plugins.send(:remove_const, :SomethingOrOther)
end
it "should try loading plugins from sequel/plugins/:plugin" do
a = []
m = Module.new
@c.define_singleton_method(:require) do |b|
a << b
Sequel::Plugins.const_set(:SomethingOrOther, m)
end
@c.plugin :something_or_other
@c.plugins.must_include m
a.must_equal ['sequel/plugins/something_or_other']
end
end
|