File: test_plugin_helper.rb

package info (click to toggle)
tdiary 5.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,852 kB
  • sloc: ruby: 22,925; xml: 325; makefile: 16; sh: 10
file content (155 lines) | stat: -rw-r--r-- 3,843 bytes parent folder | download | duplicates (5)
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
module TDiary
	module PluginTestStub
		def add_header_proc(*args); end
		def add_body_enter_proc(*args); end
		def add_update_proc(*args); end
		def add_conf_proc(*args); end
		def add_title_proc(*args); end
		def add_subtitle_proc(*args); end
		def add_footer_proc(*args); end
		def feed?; false; end
		def enable_js(*args); end
		def add_js_setting(*args); end
		def to_native( str, charset = nil )
			from = case charset
				when /^utf-8$/i
					'W'
				when /^shift_jis/i
					'S'
				when /^EUC-JP/i
					'E'
				else
					''
			end
			NKF::nkf( "-m0 -#{from}w", str )
		end
	end

	module PluginTestHelper
		def PluginTestHelper.absolute_path_of(plugin_relative_path)
			File.expand_path(File.join(File.dirname(__FILE__), '..', plugin_relative_path))
		end

		def PluginTestHelper.resource_relative_path_of(plugin_relative_path, lang)
			File.join(File.dirname(plugin_relative_path), lang, File.basename(plugin_relative_path))
		end

		def PluginTestHelper.resource_absolute_path_of(plugin_relative_path, lang)
			ppath = PluginTestHelper.absolute_path_of(plugin_relative_path)
			File.join(File.dirname(ppath), lang, File.basename(ppath))
		end
	end

	class ConfStub
		def style; 'tDiary'; end

		def initialize
			@conf = Hash.new
		end

		def []=(k,v)
			@conf[k] = v
		end

		def [](k)
			return @conf[k]
		end
	end

	class StubPlugin
		def self.inherited(child)
			super
			child.module_eval(<<-EOS)
				def context
					binding
				end
			EOS
		end

		include TDiary::PluginTestStub

		def StubPlugin::_load_plugin(plugin_relative_path, lang = 'en')
			plugin_class = Class.new(StubPlugin)
			plugin = plugin_class.new(lang)
			plugin.load(plugin_relative_path)
			return plugin_class, plugin
		end

		def StubPlugin::load_plugin(plugin_relative_path, lang = 'en')
			return StubPlugin::_load_plugin(plugin_relative_path, lang)[1]
		end

		def StubPlugin::new_plugin(plugin_relative_path, lang = 'en')
			return StubPlugin::_load_plugin(plugin_relative_path, lang)[0]
		end

		attr_accessor :conf
		attr_accessor :options

		def initialize(lang = 'en')
			@lang = lang
			reset
		end

		def load(plugin_relative_path)
			reset
			ppath = TDiary::PluginTestHelper.absolute_path_of(plugin_relative_path)
			pl10n = TDiary::PluginTestHelper.resource_absolute_path_of(plugin_relative_path, @lang)
			File.open(pl10n){|f| eval(f.read, context, TDiary::PluginTestHelper.resource_relative_path_of(plugin_relative_path, @lang))}
			File.open(ppath){|f| eval(f.read, context, plugin_relative_path)}
		end

		def reset
			@options = {}
			@conf_genre_label = {}
			@conf = TDiary::ConfStub.new
		end
	end

	class StubPlugin
		def self.inherited(child)
			super
			child.module_eval(<<-EOS)
				def context
					binding
				end
			EOS
		end

		include TDiary::PluginTestStub

		def StubPlugin::_load_plugin(plugin_relative_path, lang = 'en')
			plugin_class = Class.new(StubPlugin)
			plugin = plugin_class.new(lang)
			plugin.load(plugin_relative_path)
			return plugin_class, plugin
		end

		def StubPlugin::load_plugin(plugin_relative_path, lang = 'en')
			return StubPlugin::_load_plugin(plugin_relative_path, lang)[1]
		end

		def StubPlugin::new_plugin(plugin_relative_path, lang = 'en')
			return StubPlugin::_load_plugin(plugin_relative_path, lang)[0]
		end

		def initialize(lang = 'en')
			@lang = lang
			reset
		end

		def load(plugin_relative_path)
			reset
			ppath = TDiary::PluginTestHelper.absolute_path_of(plugin_relative_path)
			pl10n = TDiary::PluginTestHelper.resource_absolute_path_of(plugin_relative_path, @lang)
			File.open(pl10n){|f| eval(f.read, context, TDiary::PluginTestHelper.resource_relative_path_of(plugin_relative_path, @lang))}
			File.open(ppath){|f| eval(f.read, context, plugin_relative_path)}
		end

		def reset
			@options = {}
			@conf_genre_label = {}
			@conf = TDiary::ConfStub.new
		end
	end
end