bbq 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,33 +1,2 @@
1
- require 'bbq'
2
- require 'active_support'
3
- require 'test/unit'
4
- require 'bbq/test_user'
5
- require 'test/unit/assertions'
6
-
7
- module Bbq
8
- class TestCase < ActiveSupport::TestCase
9
- class << self
10
- alias :scenario :test
11
- alias :background :setup
12
- end
13
-
14
- alias :background :setup
15
- end
16
-
17
- # test/unit specific methods for test_user
18
- class TestUser
19
- include Test::Unit::Assertions
20
-
21
- def see!(*args)
22
- args.each do |arg|
23
- assert has_content?(arg), "Expecting to see \"#{arg}\", text not found."
24
- end
25
- end
26
-
27
- def not_see!(*args)
28
- args.each do |arg|
29
- assert has_no_content?(arg), "Found \"#{arg}\", which was unexpected."
30
- end
31
- end
32
- end
33
- end
1
+ warn "[DEPRECATION] require 'bbq/test' is deprecated. Please replace it with 'bbq/test_unit'"
2
+ require "bbq/test_unit"
@@ -0,0 +1,94 @@
1
+ require 'bbq/roles'
2
+
3
+ module Bbq
4
+ class TestClient
5
+ class UnsupportedMethodError < StandardError; end
6
+
7
+ include Bbq::Roles
8
+
9
+ def initialize(options = {})
10
+ @options = options
11
+ end
12
+
13
+ HTTP_METHODS = %w(get post put delete head options patch)
14
+
15
+ HTTP_METHODS.each do |method|
16
+ class_eval <<-RUBY
17
+ def #{method}(path, params = {}, headers = {})
18
+ response = driver.send(:#{method}, path, params, default_headers.merge(headers))
19
+ parsed_response = parse_response(response)
20
+ yield parsed_response if block_given?
21
+ parsed_response
22
+ rescue NoMethodError
23
+ raise UnsupportedMethodError, "Your driver does not support #{method.upcase} method"
24
+ end
25
+ RUBY
26
+ end
27
+
28
+ protected
29
+
30
+ def app
31
+ @options[:app] || Bbq.app
32
+ end
33
+
34
+ def default_headers
35
+ @options[:headers] || {}
36
+ end
37
+
38
+ def driver
39
+ @driver ||= RackTest.new(app)
40
+ end
41
+
42
+ def parse_response(response)
43
+ case response.headers["Content-Type"]
44
+ when /^application\/(.*\+)?json/
45
+ response.extend(JsonBody)
46
+ when /^application\/(.*\+)?x-yaml/
47
+ response.extend(YamlBody)
48
+ else
49
+ response
50
+ end
51
+ end
52
+
53
+ class RackTest
54
+ attr_accessor :app
55
+
56
+ def initialize(app)
57
+ self.app = app
58
+ end
59
+
60
+ module ConvertHeaders
61
+ HTTP_METHODS.each do |method|
62
+ class_eval <<-RUBY
63
+ def #{method}(path, params = {}, headers = {})
64
+ super(path, params, to_env_headers(headers))
65
+ end
66
+ RUBY
67
+ end
68
+
69
+ def to_env_headers(http_headers)
70
+ http_headers.map do |k, v|
71
+ k = k.upcase.gsub("-", "_")
72
+ k = "HTTP_#{k}" unless ["CONTENT_TYPE", "CONTENT_LENGTH"].include?(k)
73
+ { k => v }
74
+ end.inject(:merge)
75
+ end
76
+ end
77
+
78
+ include Rack::Test::Methods
79
+ include ConvertHeaders
80
+ end
81
+
82
+ module JsonBody
83
+ def body
84
+ @parsed_body ||= super.empty?? super : JSON.parse(super)
85
+ end
86
+ end
87
+
88
+ module YamlBody
89
+ def body
90
+ @parsed_body ||= YAML.load(super)
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,37 @@
1
+ require 'bbq'
2
+ require 'bbq/session'
3
+ require 'active_support/test_case'
4
+ require 'test/unit'
5
+ require 'test/unit/assertions'
6
+
7
+ module Bbq
8
+ class TestCase < ActiveSupport::TestCase
9
+ class << self
10
+ alias :scenario :test
11
+ alias :background :setup
12
+ end
13
+
14
+ alias :background :setup
15
+
16
+ teardown do
17
+ Bbq::Session.pool.release
18
+ end
19
+ end
20
+
21
+ # test/unit specific methods for test_user
22
+ class TestUser
23
+ include Test::Unit::Assertions
24
+
25
+ def see!(*args)
26
+ args.each do |arg|
27
+ assert has_content?(arg), "Expecting to see \"#{arg}\", text not found."
28
+ end
29
+ end
30
+
31
+ def not_see!(*args)
32
+ args.each do |arg|
33
+ assert has_no_content?(arg), "Found \"#{arg}\", which was unexpected."
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,52 +1,33 @@
1
+ require 'bbq'
1
2
  require 'capybara/rails' if Bbq.rails?
2
- require 'capybara/dsl'
3
- require 'securerandom'
4
- require 'bbq/util'
3
+ require 'bbq/session'
4
+ require 'bbq/roles'
5
+ require 'bbq/test_user/capybara_dsl'
5
6
  require 'bbq/test_user/eyes'
6
7
  require 'bbq/test_user/within'
7
8
 
8
9
  module Bbq
9
10
  class TestUser
10
- if Bbq.rails?
11
- include ActionDispatch::Routing::UrlFor
12
- include Rails.application.routes.url_helpers
13
- include ActionDispatch::Routing::RouteSet::MountedHelpers unless Rails.version < "3.1"
14
- end
15
- include Capybara::DSL
11
+ include Bbq::TestUser::CapybaraDsl
16
12
  include Bbq::TestUser::Eyes
17
13
  include Bbq::TestUser::Within
14
+ include Bbq::Roles
18
15
 
19
16
  attr_reader :options
20
17
 
21
18
  def initialize(options = {})
22
- @session_name = options.delete(:session_name)
23
- @current_driver = options.delete(:driver)
24
- @options = options
25
- end
26
-
27
- def page
28
- Capybara.using_driver(current_driver) do
29
- Capybara.using_session(session_name) do
30
- Capybara.current_session
31
- end
32
- end
19
+ @options = default_options.merge(options)
33
20
  end
34
21
 
35
- # Discuss: Shall we freeze ?
36
- def session_name
37
- @session_name ||= SecureRandom.hex(8)
22
+ def default_options
23
+ {
24
+ :pool => Bbq::Session.pool,
25
+ :driver => ::Capybara.default_driver
26
+ }
38
27
  end
39
28
 
40
- # Discuss: Shall we freeze ?
41
- def current_driver
42
- @current_driver
43
- end
44
-
45
- def roles(*names)
46
- names.each do |name|
47
- module_obj = Bbq::Util.find_module(name, self)
48
- self.extend(module_obj)
49
- end
29
+ def page
30
+ @page ||= options[:session] || Bbq::Session.next(:driver => options[:driver], :pool => options[:pool])
50
31
  end
51
32
  end
52
33
  end
@@ -0,0 +1,15 @@
1
+ require 'capybara/dsl'
2
+
3
+ module Bbq
4
+ class TestUser
5
+ module CapybaraDsl
6
+ ::Capybara::Session::DSL_METHODS.each do |method|
7
+ class_eval <<-RUBY, __FILE__, __LINE__+1
8
+ def #{method}(*args, &block)
9
+ page.#{method}(*args, &block)
10
+ end
11
+ RUBY
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Bbq
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require 'bbq'
2
2
 
3
3
  module Bbq
4
- class InstallGenerator < Rails::Generators::Base
4
+ class InstallGenerator < ::Rails::Generators::Base
5
5
  hook_for :test_framework, :aliases => "-t", :as => "bbq_install"
6
6
  end
7
7
  end
@@ -1,7 +1,7 @@
1
1
  require 'bbq'
2
2
 
3
3
  module Bbq
4
- class TestGenerator < Rails::Generators::NamedBase
4
+ class TestGenerator < ::Rails::Generators::NamedBase
5
5
  hook_for :test_framework, :aliases => "-t", :as => "bbq_test"
6
6
  end
7
7
  end
@@ -1,3 +1,5 @@
1
+ require "bbq/test_user"
2
+
1
3
  class TestUser < Bbq::TestUser
2
4
  # FIXME: explain here how to extend TestUser
3
5
  end
@@ -1,7 +1,7 @@
1
1
 
2
2
  To finish setup, place in your test/test_helper.rb the following:
3
3
 
4
- require 'bbq/test'
4
+ require 'bbq/test_unit'
5
5
 
6
6
  Also make sure that you require there support files:
7
7
 
@@ -1,3 +1,5 @@
1
+ require "bbq/test_user"
2
+
1
3
  class TestUser < Bbq::TestUser
2
4
  # FIXME: explain here how to extend TestUser
3
5
  end
@@ -1,3 +1,5 @@
1
+ Mime::Type.register "application/vnd.magic+json; version=2", :vnd_json_v2
2
+
1
3
  class HomeController < ApplicationController
2
4
  before_filter :authenticate_user!, :only => :index
3
5
 
@@ -11,4 +13,13 @@ class HomeController < ApplicationController
11
13
 
12
14
  def ponycorns
13
15
  end
16
+
17
+ def rainbow
18
+ @rainbow = { "wonderful" => true, "colors" => 7 }
19
+ respond_to do |format|
20
+ format.json { render :json => @rainbow }
21
+ format.vnd_json_v2 { render :json => @rainbow }
22
+ format.yaml { render :text => @rainbow.to_yaml }
23
+ end
24
+ end
14
25
  end
@@ -4,4 +4,5 @@ Dummy::Application.routes.draw do
4
4
 
5
5
  match "/miracle" => "home#miracle"
6
6
  match "/ponycorns" => "home#ponycorns"
7
+ match "/rainbow" => "home#rainbow"
7
8
  end
@@ -0,0 +1,46 @@
1
+ class DriverFactory
2
+
3
+ attr_reader :drivers_count
4
+
5
+ def initialize(log_path)
6
+ @log = File.open(log_path, 'w')
7
+ @drivers = []
8
+ end
9
+
10
+ def get_driver(app)
11
+ @log.puts "Driver created"
12
+ driver = TestDriver.new(app)
13
+ @drivers << driver
14
+ driver
15
+ end
16
+
17
+ def drivers_clean?
18
+ @drivers.all?(&:clean?)
19
+ end
20
+
21
+ def drivers_count
22
+ @drivers.size
23
+ end
24
+
25
+ class TestDriver
26
+
27
+ def initialize(app)
28
+ @app = app
29
+ @clean = true
30
+ end
31
+
32
+ def visit(path)
33
+ @clean = false
34
+ end
35
+
36
+ def reset!
37
+ @clean = true
38
+ end
39
+
40
+ def clean?
41
+ @clean
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -3,83 +3,221 @@ require 'test_helper'
3
3
  class BbqRspecTest < Test::Unit::TestCase
4
4
  include CommandHelper
5
5
 
6
+ def setup
7
+ @log_path = 'test/dummy/log/test_driver.log'
8
+ end
9
+
6
10
  def test_dsl
7
11
  create_file 'test/dummy/spec/acceptance/dsl_spec.rb', <<-RSPEC
8
- require 'spec_helper'
9
- require 'bbq/rspec'
10
-
11
- feature 'dsl' do
12
- background do
13
- @a = 1
14
- end
15
-
16
- scenario 'valid' do
17
- @a.should == 1
18
- end
19
- end
12
+ require 'spec_helper'
13
+ require 'bbq/rspec'
14
+
15
+ feature 'dsl' do
16
+ background do
17
+ @a = 1
18
+ end
19
+
20
+ scenario 'valid' do
21
+ @a.should == 1
22
+ end
23
+ end
20
24
  RSPEC
21
25
 
22
- run_cmd 'rspec -Itest/dummy/spec test/dummy/spec/acceptance/dsl_spec.rb'
26
+ run_cmd 'bundle exec rspec -Itest/dummy/spec test/dummy/spec/acceptance/dsl_spec.rb'
23
27
  assert_match /1 example, 0 failures/, output
24
28
  end
25
29
 
26
30
  def test_capybara_matchers
27
31
  create_file 'test/dummy/spec/acceptance/capybara_matchers_spec.rb', <<-RSPEC
28
- require 'spec_helper'
29
- require 'bbq/rspec'
30
-
31
- feature 'capybara matchers' do
32
- scenario 'should see welcome text' do
33
- user = Bbq::TestUser.new
34
- user.visit "/miracle"
35
- user.page.should have_content("MIRACLE")
36
- user.should have_no_content("BBQ")
37
- end
38
- end
32
+ require 'spec_helper'
33
+ require 'bbq/rspec'
34
+ require 'bbq/test_user'
35
+
36
+ feature 'capybara matchers' do
37
+ scenario 'should see welcome text' do
38
+ user = Bbq::TestUser.new
39
+ user.visit "/miracle"
40
+ user.page.should have_content("MIRACLE")
41
+ user.should have_no_content("BBQ")
42
+ end
43
+ end
39
44
  RSPEC
40
45
 
41
- run_cmd 'rspec -Itest/dummy/spec test/dummy/spec/acceptance/capybara_matchers_spec.rb'
46
+ run_cmd 'bundle exec rspec -Itest/dummy/spec test/dummy/spec/acceptance/capybara_matchers_spec.rb'
42
47
  assert_match /1 example, 0 failures/, output
43
48
  end
44
49
 
45
50
  def test_bbq_matchers
46
51
  create_file 'test/dummy/spec/acceptance/bbq_matchers_spec.rb', <<-RSPEC
47
- require 'spec_helper'
48
- require 'bbq/rspec'
49
-
50
- feature 'bbq matchers' do
51
- scenario 'should see welcome text' do
52
- user = Bbq::TestUser.new
53
- user.visit "/miracle"
54
- user.should see("MIRACLE")
55
- user.should not_see("BBQ")
56
- end
57
- end
52
+ require 'spec_helper'
53
+ require 'bbq/rspec'
54
+ require 'bbq/test_user'
55
+
56
+ feature 'bbq matchers' do
57
+ scenario 'should see welcome text' do
58
+ user = Bbq::TestUser.new
59
+ user.visit "/miracle"
60
+ expect { user.should see("MIRACLE") }.to_not raise_error(RSpec::Expectations::ExpectationNotMetError)
61
+ expect { user.should_not see("MIRACLE") }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected not to see MIRACLE in/)
62
+ expect { user.should_not see("BBQ") }.to_not raise_error(RSpec::Expectations::ExpectationNotMetError)
63
+ expect { user.should see("BBQ") }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected to see BBQ in/)
64
+ end
65
+
66
+ scenario 'should allow to chain matcher with within scope' do
67
+ user = Bbq::TestUser.new
68
+ user.visit '/ponycorns'
69
+
70
+ expect { user.should see('Pink').within('#unicorns') }.to_not raise_error(RSpec::Expectations::ExpectationNotMetError)
71
+ expect { user.should_not see('Pink').within('#unicorns') }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected not to see Pink in Pink/)
72
+ expect { user.should_not see('Violet').within('#unicorns') }.to_not raise_error(RSpec::Expectations::ExpectationNotMetError)
73
+ expect { user.should see('Violet').within('#unicorns') }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected to see Violet in Pink/)
74
+ end
75
+ end
58
76
  RSPEC
59
77
 
60
- run_cmd 'rspec -Itest/dummy/spec test/dummy/spec/acceptance/bbq_matchers_spec.rb'
61
- assert_match /1 example, 0 failures/, output
78
+ run_cmd 'bundle exec rspec -Itest/dummy/spec test/dummy/spec/acceptance/bbq_matchers_spec.rb'
79
+ assert_match /2 examples, 0 failures/, output
62
80
  end
63
81
 
64
82
  def test_implicit_user_eyes
65
83
  create_file 'test/dummy/spec/acceptance/implicit_user_eyes_spec.rb', <<-RSPEC
66
- require 'spec_helper'
67
- require 'bbq/rspec'
68
-
69
- feature 'implicit user eyes' do
70
- scenario 'should see welcome text' do
71
- user = Bbq::TestUser.new
72
- user.visit "/miracle"
73
- user.see!("MIRACLE")
74
- user.not_see!("BBQ")
75
-
76
- lambda { user.see!("BBQ") }.should raise_error
77
- lambda { user.not_see!("MIRACLE") }.should raise_error
78
- end
79
- end
84
+ require 'spec_helper'
85
+ require 'bbq/rspec'
86
+ require 'bbq/test_user'
87
+
88
+ feature 'implicit user eyes' do
89
+ scenario 'should see welcome text' do
90
+ user = Bbq::TestUser.new
91
+ user.visit "/miracle"
92
+ user.see!("MIRACLE")
93
+ user.not_see!("BBQ")
94
+
95
+ expect { user.see!("BBQ") }.to raise_error
96
+ expect { user.not_see!("MIRACLE") }.to raise_error
97
+ end
98
+
99
+ scenario 'should work with within option' do
100
+ user = Bbq::TestUser.new
101
+ user.visit '/ponycorns'
102
+ user.see! 'Pink', :within => '#unicorns'
103
+ user.not_see! 'Violet', :within => '#unicorns'
104
+
105
+ expect { user.see! 'Violet', :within => '#unicorns' }.to raise_error
106
+ expect { user.not_see! 'Pink', :within => '#unicorns' }.to raise_error
107
+ end
108
+ end
80
109
  RSPEC
81
110
 
82
- run_cmd 'rspec -Itest/dummy/spec test/dummy/spec/acceptance/implicit_user_eyes_spec.rb'
111
+ run_cmd 'bundle exec rspec -Itest/dummy/spec test/dummy/spec/acceptance/implicit_user_eyes_spec.rb'
112
+ assert_match /2 examples, 0 failures/, output
113
+ end
114
+
115
+ def test_api_client
116
+ create_file 'test/dummy/spec/acceptance/api_spec.rb', <<-RSPEC
117
+ require 'spec_helper'
118
+ require 'bbq/rspec'
119
+ require 'bbq/test_client'
120
+
121
+ feature 'application API' do
122
+ scenario 'client fetches the rainbow as JSON' do
123
+ client = Bbq::TestClient.new(:headers => { 'Accept' => 'application/json' })
124
+ client.get "/rainbow" do |response|
125
+ response.status.should == 200
126
+ response.headers["Content-Type"].should match "application/json"
127
+ response.body["colors"].should == 7
128
+ response.body["wonderful"].should == true
129
+ end
130
+ end
131
+ end
132
+ RSPEC
133
+
134
+ run_cmd 'bundle exec rspec -Itest/dummy/spec test/dummy/spec/acceptance/api_spec.rb'
83
135
  assert_match /1 example, 0 failures/, output
84
136
  end
137
+
138
+ def test_session_pool
139
+ create_file 'test/dummy/spec/acceptance/session_pool_spec.rb', <<-RSPEC
140
+ require 'spec_helper'
141
+ require 'bbq/rspec'
142
+ require 'bbq/test_user'
143
+ require 'driver_factory'
144
+
145
+ Factory = DriverFactory.new('#{@log_path}')
146
+ Capybara.register_driver :bbq do |app|
147
+ Factory.get_driver(app)
148
+ end
149
+ Capybara.default_driver = :bbq
150
+
151
+ feature 'session pool' do
152
+ scenario 'alice visits page' do
153
+ Factory.drivers_clean?.should be_true
154
+ alice = Bbq::TestUser.new
155
+ alice.visit "/miracle"
156
+ Factory.drivers_clean?.should be_false
157
+ end
158
+
159
+ scenario 'both alice and bob visit page' do
160
+ Factory.drivers_clean?.should be_true
161
+ alice = Bbq::TestUser.new
162
+ bob = Bbq::TestUser.new
163
+ alice.visit "/miracle"
164
+ bob.visit "/miracle"
165
+ Factory.drivers_clean?.should be_false
166
+ end
167
+
168
+ scenario 'bob visits page' do
169
+ Factory.drivers_clean?.should be_true
170
+ bob = Bbq::TestUser.new
171
+ bob.visit "/miracle"
172
+ Factory.drivers_clean?.should be_false
173
+ end
174
+ end
175
+ RSPEC
176
+
177
+ run_cmd 'bundle exec rspec -Itest/dummy/spec -Itest/support test/dummy/spec/acceptance/session_pool_spec.rb'
178
+ assert_match /3 examples, 0 failures/, output
179
+ drivers_created = File.readlines(@log_path).size
180
+ assert_equal 2, drivers_created
181
+ end
182
+
183
+ def test_without_session_pool
184
+ create_file 'test/dummy/spec/acceptance/without_session_pool_spec.rb', <<-RSPEC
185
+ require 'spec_helper'
186
+ require 'bbq/test'
187
+ require 'bbq/test_user'
188
+ require 'driver_factory'
189
+
190
+ Factory = DriverFactory.new('#{@log_path}')
191
+ Capybara.register_driver :bbq do |app|
192
+ Factory.get_driver(app)
193
+ end
194
+ Capybara.default_driver = :bbq
195
+
196
+ feature 'without session pool' do
197
+ scenario 'alice visits page' do
198
+ alice = Bbq::TestUser.new(:pool => false)
199
+ alice.visit "/miracle"
200
+ end
201
+
202
+ scenario 'both alice and bob visit page' do
203
+ alice = Bbq::TestUser.new(:pool => false)
204
+ bob = Bbq::TestUser.new(:pool => false)
205
+
206
+ alice.visit "/miracle"
207
+ bob.visit "/miracle"
208
+ end
209
+
210
+ scenario 'bob visits page' do
211
+ bob = Bbq::TestUser.new(:pool => false)
212
+ bob.visit "/miracle"
213
+ end
214
+
215
+ end
216
+ RSPEC
217
+
218
+ run_cmd 'bundle exec rspec -Itest/dummy/spec -Itest/support test/dummy/spec/acceptance/without_session_pool_spec.rb'
219
+ assert_match /3 examples, 0 failures/, output
220
+ drivers_created = File.readlines(@log_path).size
221
+ assert_equal 4, drivers_created
222
+ end
85
223
  end