Added v 0.3.1 snapshot.
[twitter4r-core.git] / spec / spec_helper.rb
blob8c1c38eb8822a15895fdbdcb5d5febdddcc927ec
1 $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
3 require 'spec'
5 def require_project_file(file)
6   require(File.join(File.dirname(__FILE__), '..', 'lib', file))  
7 end
9 require_project_file('twitter')
10 require_project_file('twitter/console')
11 require_project_file('twitter/extras')
13 # Add helper methods here if relevant to multiple _spec.rb files
15 # Spec helper that sets attribute <tt>att</tt> for given objects <tt>obj</tt> 
16 # and <tt>other</tt> to given <tt>value</tt>.
17 def equalizer(obj, other, att, value)
18   setter = "#{att}="
19   obj.send(setter, value)
20   other.send(setter, value)
21 end
23 # Spec helper that nil-izes objects passed in
24 def nilize(*objects)
25   objects.each {|obj| obj = nil }
26 end
28 # Returns default <tt>client</tt> context object
29 def client_context(file = 'config/twitter.yml')
30   Twitter::Client.from_config(file)
31 end
33 # Spec helper that returns a mocked <tt>Twitter::Config</tt> object
34 # with stubbed attributes and <tt>attrs</tt> for overriding attribute 
35 # values.
36 def stubbed_twitter_config(config, attrs = {})
37   opts = { 
38     :protocol => :ssl,
39     :host => 'twitter.com',
40     :port => 443,
41     :proxy_host => 'proxy.host',
42     :proxy_port => 8080,
43   }.merge(attrs)
44   config.stub!(:protocol).and_return(opts[:protocol])
45   config.stub!(:host).and_return(opts[:host])
46   config.stub!(:port).and_return(opts[:port])
47   config.stub!(:proxy_host).and_return(opts[:proxy_host])
48   config.stub!(:proxy_port).and_return(opts[:proxy_port])
49   config
50 end
52 def mas_twitter_config(attrs = {})
53   config = mock(Twitter::Config)
54   stubbed_twitter_conf(config, attrs)
55 end
57 def stubbed_twitter_status(status, attrs = {})
58   opts = {
59     :id => 23492343,
60   }.merge(attrs)
61   status.stub!(:id).and_return(opts[:id])
62   status.stub!(:to_i).and_return(opts[:id])
63   (opts.keys - [:id]).each do |att|
64     status.stub!(att).and_return(opts[att])
65   end
66   status.stub!(:bless).and_return(nil)
67   status
68 end
70 def mas_twitter_status(attrs = {})
71   status = mock(Twitter::Status)
72   stubbed_twitter_status(status, attrs)
73 end
75 # Spec helper that returns the project root directory as absolute path string
76 def project_root_dir
77   File.expand_path(File.join(File.dirname(__FILE__), '..'))
78 end
80 # Spec helper that returns stubbed <tt>Net::HTTP</tt> object 
81 # with given <tt>response</tt> and <tt>obj_stubs</tt>.
82 # The <tt>host</tt> and <tt>port</tt> are used to initialize 
83 # the Net::HTTP object.
84 def stubbed_net_http(response, obj_stubs = {}, host = 'twitter.com', port = 80)
85   http = Net::HTTP.new(host, port)
86   Net::HTTP.stub!(:new).and_return(http)
87   http.stub!(:request).and_return(response)
88   http
89 end
91 # Spec helper that returns a mocked <tt>Net::HTTP</tt> object and 
92 # stubs out the <tt>request</tt> method to return the given 
93 # <tt>response</tt>
94 def mas_net_http(response, obj_stubs = {})
95   http = mock(Net::HTTP, obj_stubs)
96   Net::HTTP.stub!(:new).and_return(http)
97   http.stub!(:request).and_return(response)
98   http.stub!(:start).and_yield(http)
99   http.stub!(:use_ssl=)
100   http.stub!(:verify_mode=)
101   http
104 # Spec helper that returns a mocked <tt>Net::HTTP::Get</tt> object and 
105 # stubs relevant class methods and given <tt>obj_stubs</tt> 
106 # for endo-specing
107 def mas_net_http_get(obj_stubs = {})
108   request = Spec::Mocks::Mock.new(Net::HTTP::Get)
109   Net::HTTP::Get.stub!(:new).and_return(request)
110   obj_stubs.each do |method, value|
111     request.stub!(method).and_return(value)
112   end
113   request
116 # Spec helper that returns a mocked <tt>Net::HTTP::Post</tt> object and 
117 # stubs relevant class methods and given <tt>obj_stubs</tt> 
118 # for endo-specing
119 def mas_net_http_post(obj_stubs = {})
120   request = Spec::Mocks::Mock.new(Net::HTTP::Post)
121   Net::HTTP::Post.stub!(:new).and_return(request)
122   obj_stubs.each do |method, value|
123     request.stub!(method).and_return(value)
124   end
125   request
128 # Spec helper that returns a mocked <tt>Net::HTTPResponse</tt> object and 
129 # stubs given <tt>obj_stubs</tt> for endo-specing.
131 def mas_net_http_response(status = :success, 
132                           body = '', 
133                           obj_stubs = {})
134   response = Spec::Mocks::Mock.new(Net::HTTPResponse)
135   response.stub!(:body).and_return(body)
136   case status
137   when :success || 200
138     _create_http_response(response, "200", "OK")
139   when :created || 201
140     _create_http_response(response, "201", "Created")
141   when :redirect || 301
142     _create_http_response(response, "301", "Redirect")
143   when :not_authorized || 401
144     _create_http_response(response, "401", "Not Authorized")
145   when :forbidden || 403
146     _create_http_response(response, "403", "Forbidden")
147   when :file_not_found || 404
148     _create_http_response(response, "404", "File Not Found")
149   when :server_error || 500
150     _create_http_response(response, "500", "Server Error")
151   end
152   response
155 # Local helper method to DRY up code.
156 def _create_http_response(mock_response, code, message)
157   mock_response.stub!(:code).and_return(code)
158   mock_response.stub!(:message).and_return(message)
159   mock_response.stub!(:is_a?).and_return(true) if ["200", "201"].member?(code)