root/platform/tdiary/spec/spec_helper.rb

Revision 12243, 3.4 kB (checked in by hiraku, 6 months ago)

Fix "@account_ad_list => nil" in add_conf_proc.

Line 
1$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "plugin")))
2require 'rubygems'
3require 'spec'
4require 'spec/fixture'
5require 'erb'
6
7# FIXME PluginFake in under construction.
8class PluginFake
9        include ERB::Util
10
11        attr_reader :conf
12        attr_accessor :mode, :date
13
14        def initialize
15                @conf = Config.new
16                @mode = ""
17                @date = nil
18                @header_procs = []
19                @footer_procs = []
20                @update_procs = []
21                @conf_procs = []
22                @body_enter_procs = []
23                @body_leave_procs = []
24        end
25
26        def add_conf_proc( key, label, genre=nil, &block )
27                @conf_procs << block
28        end
29
30        def add_header_proc( block = Proc::new )
31                @header_procs << block
32        end
33
34        def add_footer_proc( block = Proc::new )
35                @footer_procs << block
36        end
37
38        def add_update_proc( block = Proc::new )
39                @update_procs << block
40        end
41
42        def conf_proc
43                r = []
44                @conf_procs.each do |proc|
45                        r << proc.call
46                end
47                r.join.chomp
48        end
49
50        def header_proc
51                r = []
52                @header_procs.each do |proc|
53                        r << proc.call
54                end
55                r.join.chomp
56        end
57       
58        def footer_proc
59                r = []
60                @footer_procs.each do |proc|
61                        r << proc.call
62                end
63                r.join.chomp
64        end
65
66        def add_body_enter_proc( block = Proc::new )
67                @body_enter_procs << block
68        end
69       
70        def body_enter_proc( date )
71                r = []
72                @body_enter_procs.each do |proc|
73                        r << proc.call( date )
74                end
75                r.join.chomp
76        end
77
78        def add_body_leave_proc( block = Proc::new )
79                @body_leave_procs << block
80        end
81       
82        def body_leave_proc( date )
83                r = []
84                @body_leave_procs.each do |proc|
85                        r << proc.call( date )
86                end
87                r.join.chomp
88        end
89
90        class Config
91
92                attr_accessor :index, :html_title, :cgi
93
94                def initialize
95                        @cgi = CGIFake.new
96                        @options = {}
97                        @options2 = {}
98                        @index = './'
99                        @html_title = ''
100
101                        bot = ["bot", "spider", "antenna", "crawler", "moget", "slurp"]
102                        bot += @options['bot'] || []
103                        @bot = Regexp::new( "(#{bot.uniq.join( '|' )})", true )
104                end
105
106                def []( key )
107                        @options[key]
108                end
109
110                def []=( key, val )
111                        @options2[key] = @options[key] = val
112                end
113
114                def delete( key )
115                        @options.delete( key )
116                        @options2.delete( key )
117                end
118
119                def base_url
120                        begin
121                                if @options['base_url'].length > 0 then
122                                        return @options['base_url']
123                                end
124                        rescue
125                        end
126                end
127
128                def mobile_agent?
129                        @cgi.mobile_agent?
130                end
131
132                def bot?
133                        @bot =~ @cgi.user_agent
134                end
135        end
136
137        def iphone?
138                @conf.cgi.iphone?
139        end
140        alias ipod? iphone?
141end
142
143class CGIFake
144        attr_accessor :user_agent
145
146        def initialize
147                @user_agent = ""
148        end
149
150        def mobile_agent?
151                self.user_agent =~ %r[
152                        ^DoCoMo|
153                        ^(?:KDDI|UP\.Browser)|
154                        ^(?:J-(?:PHONE|EMULATOR)|Vodafone|SoftBank|MOT-|[VS]emulator)|
155                        WILLCOM|DDIPOCKET|
156                        PDXGW|ASTEL|Palmscape|Xiino|sharp\ pda\ browser|Windows\ CE|L-mode
157                ]x
158        end
159
160        def iphone?
161                self.user_agent =~ /iP(?:hone|od)/
162        end
163end
164
165
166def fake_plugin( name_sym, cgi=nil, base=nil, &block )
167        plugin = PluginFake.new
168        yield plugin if block_given?
169
170        file_path = plugin_path( name_sym, base )
171        plugin_name = File.basename( file_path, ".rb" )
172
173        plugin.instance_eval do
174                eval( File.read( file_path ), binding,
175                        "(#{File.basename(file_path)})", 1 )
176        end
177        plugin_sym = plugin_name.to_sym
178        if plugin.class.private_method_defined?( plugin_sym )
179                plugin.__send__( :public, plugin_sym )
180        end
181
182        plugin
183end
184
185def plugin_path( plugin_sym, base=nil )
186        paths = []
187        paths << ( base ? base : "plugin" )
188        paths << "#{plugin_sym.to_s}.rb"
189        File.expand_path( File.join( paths ))
190end
191
192def anchor( s )
193        if /^([\-\d]+)#?([pct]\d*)?$/ =~ s then
194                if $2 then
195                        "?date=#$1##$2"
196                else
197                        "?date=#$1"
198                end
199        else
200                ""
201        end
202end
Note: See TracBrowser for help on using the browser.