File: plugin_spec.rb

package info (click to toggle)
tdiary 5.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,088 kB
  • sloc: ruby: 23,031; javascript: 1,029; xml: 325; makefile: 26; sh: 4
file content (394 lines) | stat: -rw-r--r-- 12,780 bytes parent folder | download | duplicates (4)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
require 'spec_helper'

require File.dirname(__FILE__) + "/../plugin/plugin_helper"
require 'tdiary/plugin'

describe TDiary::Plugin do
	before do
		config = PluginFake::Config.new
		config.plugin_path = 'spec/fixtures/plugin'
		@plugin = TDiary::Plugin.new({ conf: config, debug: true })
	end

	describe '#load_plugin' do
		before { @plugin.load_plugin('spec/fixtures/plugin/sample.rb') }
		subject { @plugin }

		it '読み込まれたプラグインのメソッドを呼び出せること' do
			expect(subject.sample).to eq 'sample plugin'
		end

		it 'プラグイン一覧が @plugin_files で取得できること' do
			# TODO: 実際にはPlugin.newした時点でload_pluginが呼ばれている
			# @plugin_filesの追加もinitializeメソッド内で実行されている
			expect(subject.instance_variable_get(:@plugin_files)).to include('spec/fixtures/plugin/sample.rb')
		end

		context 'リソースファイルが存在する場合' do
			before do
				@plugin.instance_variable_get(:@conf).lang = 'ja'
				@plugin.load_plugin('spec/fixtures/plugin/sample.rb')
			end

			it 'Confファイルで指定した言語に対応するリソースが読み込まれること' do
				expect(@plugin.sample_ja).to eq 'サンプルプラグイン'
			end
		end
	end

	describe '#eval_src' do
		before do
			@src = ERB::new('hello <%= sample %><%= undefined_method %>').src
			@plugin.instance_variable_set(:@debug, false)
		end
		subject { @plugin.eval_src(@src) }

		it 'Pluginオブジェクト内でソースが実行されること' do
			is_expected.to eq 'hello sample plugin'
		end

		context 'debugモードがONの場合' do
			before { @plugin.instance_variable_set(:@debug, true) }

			it 'Plugin内のエラーが通知されること' do
				expect { subject }.to raise_error(NameError)
			end
		end
	end

	describe '#header_proc' do
		before do
			@plugin.__send__(:add_header_proc, ){ 'header1 ' }
			@plugin.__send__(:add_header_proc, ){ 'header2' }
		end
		subject { @plugin.__send__(:header_proc) }

		it 'add_header_procで登録したブロックが実行されること' do
			is_expected.to eq 'header1 header2'
		end
	end

	describe '#footer_proc' do
		before do
			@plugin.__send__(:add_footer_proc, ){ 'footer1 ' }
			@plugin.__send__(:add_footer_proc, ){ 'footer2' }
		end
		subject { @plugin.__send__(:footer_proc) }

		it 'add_footer_procで登録したブロックが実行されること' do
			is_expected.to eq 'footer1 footer2'
		end
	end

	describe '#update_proc' do
		let (:proc1) { lambda {} }
		let (:proc2) { lambda {} }
		before do
			@plugin.__send__(:add_update_proc, &proc1)
			@plugin.__send__(:add_update_proc, &proc2)
		end
		subject { @plugin.__send__(:update_proc) }

		it 'add_update_procで登録したブロックが実行されること' do
			expect(proc1).to receive(:call)
			expect(proc2).to receive(:call)
			# should_receiveの場合はsubjectが使えないため明示的に実行
			@plugin.__send__(:update_proc)
		end

		it '空の文字列を返すこと' do
			is_expected.to eq ''
		end
	end

	describe '#title_proc' do
		let (:proc1) { lambda {|date, title| "title1" } }
		let (:proc2) { lambda {|date, title| "title2" } }
		let (:date) { Time.local(2012, 1, 2) }
		before do
			@plugin.__send__(:add_title_proc, &proc1)
			@plugin.__send__(:add_title_proc, &proc2)
		end
		subject { @plugin.__send__(:title_proc, date, 'title') }

		it 'add_title_procで登録したブロックを実行し、最後の結果を返すこと' do
			is_expected.to eq 'title2'
		end

		it '前のprocの結果が次のprocに渡されること' do
			expect(proc1).to receive(:call).with(date, 'title').and_return('title1')
			expect(proc2).to receive(:call).with(date, 'title1')
			@plugin.__send__(:title_proc, date, 'title')
		end

		it 'apply_pluginメソッドを呼び出すこと' do
			expect(@plugin).to receive(:apply_plugin)
			# should_receiveの場合はsubjectが使えないため明示的に実行
			@plugin.__send__(:title_proc, date, 'title')
		end
	end

	describe '#body_enter_proc' do
		let (:date) { Time.local(2012, 1, 2) }
		before do
			@plugin.__send__(:add_body_enter_proc, ){|date| 'body1 ' }
			@plugin.__send__(:add_body_enter_proc, ){|date| 'body2' }
		end
		subject { @plugin.__send__(:body_enter_proc, date) }

		it 'add_body_enter_procで登録したブロックが実行されること' do
			is_expected.to eq 'body1 body2'
		end
	end

	describe '#body_leave_proc' do
		before do
			@plugin.__send__(:add_body_leave_proc, ){|date| 'body1 ' }
			@plugin.__send__(:add_body_leave_proc, ){|date| 'body2' }
		end
		subject { @plugin.__send__(:body_leave_proc, @date) }

		it 'add_body_leave_procで登録したブロックが実行されること' do
			is_expected.to eq 'body1 body2'
		end
	end

	describe '#section_enter_proc' do
		let (:proc1) { lambda {|date, index| 'section1 ' } }
		let (:proc2) { lambda {|date, index| 'section2' } }
		let (:date1) { Time.local(2012, 1, 2) }
		let (:date2) { Time.local(2012, 1, 3) }
		before do
			@plugin.__send__(:add_section_enter_proc, &proc1)
			@plugin.__send__(:add_section_enter_proc, &proc2)
		end
		subject { @plugin.__send__(:section_enter_proc, date1) }

		it 'add_section_enter_procで登録したブロックを実行し、結果を連結して返すこと' do
			is_expected.to eq 'section1 section2'
		end

		it '呼ばれた回数に応じてセクション番号の数が増加すること (日付単位)' do
			expect(proc1).to receive(:call).with(date1, 1)
			@plugin.__send__(:section_enter_proc, date1)
			expect(proc1).to receive(:call).with(date1, 2)
			@plugin.__send__(:section_enter_proc, date1)
			expect(proc1).to receive(:call).with(date2, 1)
			@plugin.__send__(:section_enter_proc, date2)
		end
	end

	describe '#subtitle_proc' do
		let (:proc1) { lambda {|date, index, subtitle| "subtitle1" } }
		let (:proc2) { lambda {|date, index, subtitle| "subtitle2" } }
		let (:date1) { Time.local(2012, 1, 2) }
		let (:date2) { Time.local(2012, 1, 3) }
		before do
			@plugin.__send__(:add_subtitle_proc, &proc1)
			@plugin.__send__(:add_subtitle_proc, &proc2)
		end
		subject { @plugin.__send__(:subtitle_proc, date1, 'subtitle') }

		it 'add_subtitle_procで登録したブロックを実行し、最後の結果を返すこと' do
			is_expected.to eq 'subtitle2'
		end

		it '前のprocの結果が次のprocに渡されること' do
			expect(proc1).to receive(:call).with(date1, 1, 'subtitle').and_return('subtitle1')
			expect(proc2).to receive(:call).with(date1, 1, 'subtitle1')
			@plugin.__send__(:section_enter_proc, date1)
			@plugin.__send__(:subtitle_proc, date1, 'subtitle')
		end

		it 'apply_pluginメソッドを呼び出すこと' do
			expect(@plugin).to receive(:apply_plugin)
			# should_receiveの場合はsubjectが使えないため明示的に実行
			@plugin.__send__(:subtitle_proc, date1, 'title')
		end
	end

	describe '#section_leave_proc' do
		let (:proc1) { lambda {|date, index| 'section1 ' } }
		let (:proc2) { lambda {|date, index| 'section2' } }
		let (:date1) { Time.local(2012, 1, 2) }
		let (:date2) { Time.local(2012, 1, 3) }
		before do
			@plugin.__send__(:add_section_leave_proc, &proc1)
			@plugin.__send__(:add_section_leave_proc, &proc2)
		end
		subject { @plugin.__send__(:section_leave_proc, date1) }

		it 'add_section_leave_procで登録したブロックを実行し、結果を連結して返すこと' do
			is_expected.to eq 'section1 section2'
		end

		it '呼ばれた回数に応じてセクション番号の数が増加すること (日付単位)' do
			# セクション番号はsection_enter_procの呼び出し回数で決定する
			expect(proc1).to receive(:call).with(date1, 1)
			@plugin.__send__(:section_enter_proc, date1)
			@plugin.__send__(:section_leave_proc, date1)
			expect(proc1).to receive(:call).with(date1, 2)
			@plugin.__send__(:section_enter_proc, date1)
			@plugin.__send__(:section_leave_proc, date1)
			expect(proc1).to receive(:call).with(date2, 1)
			@plugin.__send__(:section_enter_proc, date2)
			@plugin.__send__(:section_leave_proc, date2)
		end
	end

	describe '#comment_leave_proc' do
		let (:date) { Time.local(2012, 1, 2) }
		before do
			@plugin.__send__(:add_comment_leave_proc ){|date| 'comment1 ' }
			@plugin.__send__(:add_comment_leave_proc ){|date| 'comment2' }
		end
		subject { @plugin.__send__(:comment_leave_proc, date) }

		it 'add_comment_leave_procで登録したブロックが実行されること' do
			is_expected.to eq 'comment1 comment2'
		end
	end

	describe '#edit_proc' do
		let (:date) { Time.local(2012, 1, 2) }
		before do
			@plugin.__send__(:add_edit_proc){|date| 'edit1 ' }
			@plugin.__send__(:add_edit_proc){|date| 'edit2' }
		end
		subject { @plugin.__send__(:edit_proc, date) }

		it 'add_edit_procで登録したブロックが実行されること' do
			is_expected.to eq 'edit1 edit2'
		end
	end

	describe '#form_proc' do
		let (:date) { Time.local(2012, 1, 2) }
		before do
			@plugin.__send__(:add_form_proc){|date| 'form1 ' }
			@plugin.__send__(:add_form_proc){|date| 'form2' }
		end
		subject { @plugin.__send__(:form_proc, date) }

		it 'add_form_procで登録したブロックが実行されること' do
			is_expected.to eq 'form1 form2'
		end
	end

	describe '#add_conf_proc' do
		let(:proc) { lambda { 'conf' } }
		subject { @plugin.__send__(:add_conf_proc, 'key1', 'label1', nil, &proc) }

		context '@modeがconfの場合' do
			before { @plugin.instance_variable_set(:@mode, 'conf') }
			it 'コールバックを登録すること' do
				is_expected.to include('label1')
			end
		end

		context '@modeがsaveconfの場合' do
			before { @plugin.instance_variable_set(:@mode, 'saveconf') }
			it 'コールバックを登録すること' do
				is_expected.to include('label1')
			end
		end

		context '@modeがconf, saveconf以外の場合' do
			before { @plugin.instance_variable_set(:@mode, 'edit') }
			it 'コールバックの登録を無視すること' do
				is_expected.to be_nil
			end
		end
	end

	describe '#conf_proc' do
		let (:date) { Time.local(2012, 1, 2) }
		before do
			@plugin.instance_variable_set(:@mode, 'conf')
			@plugin.__send__(:add_conf_proc, 'key1', 'label1', nil){ 'conf1' }
			@plugin.__send__(:add_conf_proc, 'key2', 'label2', nil){ 'conf2' }
		end
		subject { @plugin.__send__(:conf_proc, 'key1') }

		it 'add_conf_procで登録したブロックのうち、keyが一致するものを実行して結果を返すこと' do
			is_expected.to eq 'conf1'
		end
	end

	describe '#remove_tag' do
		before { @string = 'test <a href="http://example.com/">example.<b>com</b></a>' }
		subject { @plugin.__send__(:remove_tag, @string) }

		it '文字列からタグが除去されること' do
			is_expected.to eq 'test example.com'
		end
	end

	describe '#apply_plugin' do
		before do
			@plugin.instance_variable_get(:@conf).options['apply_plugin'] = true
		end

		subject { @plugin.__send__(:apply_plugin, '<%= sample %>') }
		it 'プラグインが再実行されること' do
			is_expected.to eq 'sample plugin'
		end

		context '解釈できない文字列を渡された場合' do
			subject { @plugin.__send__(:apply_plugin, '<%= undefined_method %>') }
			it { is_expected.to include 'Invalid Text' }
		end

		context '文字列がnilの場合' do
			subject { @plugin.__send__(:apply_plugin, nil) }
			it { is_expected.to eq '' }
		end

		context 'remove_tagがtrueの場合' do
			it 'remove_tagメソッドを呼び出すこと' do
				expect(@plugin).to receive(:remove_tag)
				@plugin.__send__(:apply_plugin, '', true)
			end
		end
	end

	describe '#content_proc' do
		let (:proc1) { lambda {|date| "contents1" } }
		let (:proc2) { lambda {|date| "contents2" } }
		let (:date) { Time.local(2012, 1, 2) }
		before do
			@plugin.__send__(:add_content_proc, 'key1', &proc1)
			@plugin.__send__(:add_content_proc, 'key2', &proc2)
		end
		subject { @plugin.__send__(:content_proc, 'key1', date) }

		it 'add_content_procで登録したブロックのうち、keyに相当するものを実行すること' do
			is_expected.to eq 'contents1'
		end

		context 'keyに相当するブロックが存在しない場合' do
			subject { @plugin.__send__(:content_proc, 'unregistered_key', date) }
			it { expect { subject }.to raise_error(TDiary::PluginError) }
		end
	end

	describe '#startup_proc' do
		let (:proc) { lambda { "some plugin" } }
		let (:app) { lambda { "tdiary application" } }
		before do
			@plugin.__send__(:add_startup_proc, &proc)
		end

		it 'add_startup_procで登録したブロックが実行されること' do
			expect(proc).to receive(:call).with(app)
			@plugin.__send__(:startup_proc, app)
		end
	end
end

# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End: