sinsiliux-hornsby 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -57,6 +57,21 @@ can pass :undo option with list of scenarios to mark as not executed or :all if
57
57
  hornsby_clear :fruits, :undo => :apples # Deletes fruits table and marks :apples scenario as not executed
58
58
  hornsby_clear :undo => :all # Deletes all tables and marks all scenario as not executed (fresh start)
59
59
 
60
+ Hornsby searches for scenario files in this particular order in Rails (Merb) root:
61
+ * hornsby_scenarios.rb
62
+ * hornsby_scenarios/*.rb
63
+ * hornsby_scenario.rb
64
+ * hornsby_scenario/*.rb
65
+ * spec/hornsby_scenarios.rb
66
+ * spec/hornsby_scenarios/*.rb
67
+ * spec/hornsby_scenario.rb
68
+ * spec/hornsby_scenario/*.rb
69
+ * test/hornsby_scenarios.rb
70
+ * test/hornsby_scenarios/*.rb
71
+ * test/hornsby_scenario.rb
72
+ * test/hornsby_scenario/*.rb
73
+ You can pass :root option to override framework root and :filename option to pass custom filename pattern
74
+
60
75
  == Setup
61
76
 
62
77
  The easiest way to install this gem for Ruby on Rails is just add this line to config/environment.rb (or config/environments/test.rb):
@@ -72,6 +87,12 @@ Lastly you could use it as plugin:
72
87
 
73
88
  ruby script/plugin install git://github.com/sinsiliux/hornsby.git
74
89
 
90
+ Hornsby scenarios is activated by calling enable_hornsby. For specifics on how to call that in your testing framework see a little lower.
91
+ enable_hornsby supports these parameters:
92
+ * root - custom framework root if automatic detection fails for some reason (eg. not rails/merb project)
93
+ * filename - custom files pattern with hornsby scenarios
94
+ * scenarios - list of hornsby scenarios that should be preloaded (available in all tests, never reloaded so they're much faster)
95
+
75
96
  === RSpec
76
97
 
77
98
  Add the following to spec_helper.rb
@@ -79,32 +100,19 @@ Add the following to spec_helper.rb
79
100
  Spec::Runner.configure do |config|
80
101
  ...
81
102
 
82
- Hornsby.configure_rspec(config, :filename => 'scenarios.rb', :scenarios => :preloaded_scenario)
103
+ config.enable_hornsby :filename => 'scenarios.rb', :scenarios => :preloaded_scenario
83
104
  end
84
105
 
85
- configure_rspec supports two parameters:
86
- * filename - file with hornsby scenarios (by default Rails.root/spec/hornsby_scenarios.rb)
87
- * scenarios - list of hornsby scenarios that should be preloaded (available in all tests, never reloaded so they're much faster)
88
-
89
106
  === Test::Unit
90
107
 
91
108
  Add the following lines to test_helper.rb
92
109
 
93
- Hornsby.load(:filename => File.join('..', 'spec', 'hornsby_scenario.rb'), :scenarios => :preloaded_scenario)
94
110
  class ActiveSupport::TestCase
95
- include HornsbyHelper
96
-
97
- def setup
98
- Hornsby.setup(self)
99
- end
111
+ ...
100
112
 
101
- def teardown
102
- Hornsby.teardown
103
- end
113
+ enable_hornsby
104
114
  end
105
115
 
106
- Hornsby.load supports same options as configure_rspec.
107
-
108
116
  == Advanced Usage
109
117
 
110
118
  Its just ruby, right? So go nuts:
@@ -130,6 +138,8 @@ If you'd like simply to load your scenarios into a database, use the rake task l
130
138
  == TODO
131
139
 
132
140
  * Add scenario namespaces for better organisation.
141
+ * Add ability to revert one scenario.
142
+ * Add preloading scenarios for whole block of tests.
133
143
 
134
144
  == Credits
135
145
 
@@ -141,4 +151,4 @@ The code is based on Err's code found in this post: http://errtheblog.com/post/7
141
151
 
142
152
  == License
143
153
 
144
- MIT, see LICENCE
154
+ MIT, see LICENCE
@@ -0,0 +1,15 @@
1
+ class Hornsby
2
+ module Context
3
+ def self.execute(&block)
4
+ module_eval(&block) if block
5
+ end
6
+
7
+ def self.copy_ivars(to, reload = false)
8
+ instance_variables.each do |iv|
9
+ v = instance_variable_get(iv)
10
+ v.reload if reload and v.respond_to?(:reload)
11
+ to.instance_variable_set(iv, v)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ class Hornsby
2
+ class ScenarioNotFoundError < NameError
3
+ def initialize(*args)
4
+ @scenarios = args
5
+ end
6
+
7
+ def to_s
8
+ "Scenario(s) not found '#{@scenarios.join(',')}'"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ class Hornsby
2
+ module Helper
3
+ def hornsby_scenario(*names)
4
+ Hornsby.build(*names)
5
+ Hornsby.copy_ivars(self)
6
+ end
7
+
8
+ alias :hornsby_scenarios :hornsby_scenario
9
+
10
+ def hornsby_clear(*args)
11
+ options = args.extract_options!
12
+ Hornsby.delete_tables(*args)
13
+
14
+ if options[:undo] == :all
15
+ Hornsby.executed_scenarios.clear
16
+ else
17
+ undo = [options[:undo]].flatten.compact
18
+ unless (not_found = undo - Hornsby.executed_scenarios.to_a).blank?
19
+ raise(ArgumentError, "Scenario(s) #{not_found} not found")
20
+ end
21
+ Hornsby.executed_scenarios -= undo
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ module Spec
2
+ module Runner
3
+ class Configuration
4
+ def enable_hornsby(options = {})
5
+ Hornsby.load(options)
6
+
7
+ include(Hornsby::Helper)
8
+ before do
9
+ Hornsby.setup(self)
10
+ end
11
+ after do
12
+ Hornsby.teardown
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Test
2
+ module Unit
3
+ class TestCase
4
+ def run_with_hornsby(result, &progress_block)
5
+ Hornsby.setup(self)
6
+ run_without_hornsby(result, &progress_block)
7
+ Hornsby.teardown
8
+ end
9
+
10
+ def self.enable_hornsby(options = {})
11
+ include Hornsby::Helper
12
+ Hornsby.load(options)
13
+ alias_method_chain :run, :hornsby
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/hornsby.rb CHANGED
@@ -1,11 +1,19 @@
1
- require File.join(File.dirname(__FILE__), 'hornsby_context')
1
+ require File.join(File.dirname(__FILE__), 'hornsby/context')
2
+ require File.join(File.dirname(__FILE__), 'hornsby/helper')
3
+ require File.join(File.dirname(__FILE__), 'hornsby/errors')
4
+ if defined? Spec
5
+ require File.join(File.dirname(__FILE__), 'hornsby/rspec_extensions')
6
+ else
7
+ require File.join(File.dirname(__FILE__), 'hornsby/test_unit_extensions')
8
+ end
2
9
 
3
10
  class Hornsby
4
- @@delete_sql = "DELETE FROM %s"
11
+ SCENARIO_FILES = [nil, 'spec', 'test'].product(['hornsby_scenarios', 'hornsby_scenario']).map do |path|
12
+ path = File.join(*path.compact)
13
+ ["#{path}.rb", File.join(path, "*.rb")]
14
+ end.flatten
5
15
 
6
- def self.framework_root
7
- RAILS_ROOT rescue Rails.root rescue Merb.root rescue ''
8
- end
16
+ @@delete_sql = "DELETE FROM %s"
9
17
 
10
18
  cattr_reader :scenarios
11
19
  cattr_accessor :executed_scenarios
@@ -14,31 +22,15 @@ class Hornsby
14
22
  @@executed_scenarios = Set.new
15
23
  @@global_executed_scenarios = []
16
24
 
17
- @@global_context = HornsbyContext
25
+ @@global_context = Hornsby::Context
18
26
  @@context = nil
19
27
 
20
- def self.configure_rspec(config, options = {})
21
- load(options)
22
-
23
- config.include(HornsbyHelper)
24
- config.before do
25
- Hornsby.setup(self)
26
- end
27
- config.after do
28
- Hornsby.teardown
29
- end
28
+ def self.framework_root
29
+ @@framework_root ||= RAILS_ROOT rescue Rails.root rescue Merb.root rescue nil
30
30
  end
31
31
 
32
- def self.configure_test(config, options)
33
- load(options)
34
-
35
- config.send(:include, HornsbyHelper)
36
- config.setup do
37
- Hornsby.setup(self)
38
- end
39
- config.teardown do
40
- Hornsby.teardown
41
- end
32
+ def self.configure_rspec(config, options = {})
33
+ raise '### This is deprecated! Please use config.enable_hornsby instead of Hornsby.configure_rspec(config)'
42
34
  end
43
35
 
44
36
  def self.setup(current_context)
@@ -56,26 +48,37 @@ class Hornsby
56
48
  end
57
49
 
58
50
  def self.build(*names)
59
- scenarios = names.map {|name| @@scenarios[name.to_sym] or raise "scenario #{name} not found"}
51
+ scenarios = names.map {|name| @@scenarios[name.to_sym] or raise ScenarioNotFoundError, name}
60
52
 
61
53
  scenarios.each {|s| s.build}
62
54
  end
63
55
 
64
- def self.[](name)
65
- end
66
-
67
56
  def self.load(options = {})
68
57
  return unless @@scenarios.empty?
69
58
 
70
59
  delete_tables
71
- scenarios_file = options[:filename] || File.join(framework_root, 'spec', 'hornsby_scenarios.rb')
72
- self.module_eval File.read(scenarios_file)
60
+ @@framework_root = options[:root] if options[:root]
61
+ load_scenarios_files(options[:filename] || SCENARIO_FILES)
73
62
 
74
63
  @@context = @@global_context
75
64
  @@global_scenarios = Hornsby.build(options[:scenarios]) if options[:scenarios]
76
65
  @@global_executed_scenarios = @@executed_scenarios.to_a
77
66
  end
78
67
 
68
+ def self.load_scenarios_files(*patterns)
69
+ patterns.flatten!
70
+ patterns.collect! {|pattern| File.join(framework_root, pattern)} if framework_root
71
+
72
+ patterns.each do |pattern|
73
+ unless (files = Dir.glob(pattern)).empty?
74
+ files.each{|f| self.module_eval File.read(f)}
75
+ return
76
+ end
77
+ end
78
+
79
+ raise "Scenarios file not found! Put scenarios in #{patterns.join(' or ')} or pass custom filename with :filename option"
80
+ end
81
+
79
82
  def self.scenario(scenario, &block)
80
83
  self.new(scenario, &block)
81
84
  end
@@ -100,21 +103,21 @@ class Hornsby
100
103
  attr_reader :scenario
101
104
 
102
105
  def initialize(scenario, &block)
103
- case scenario
106
+ @scenario, @parents = parse_name(scenario)
107
+ @block = block
108
+
109
+ @@scenarios[@scenario] = self
110
+ end
111
+
112
+ def parse_name(name)
113
+ case name
104
114
  when Hash
105
- parents = scenario.values.first
106
- @parents = Array === parents ? parents : [parents]
107
- scenario = scenario.keys.first
115
+ return name.keys.first.to_sym, [name.values.first].flatten.map{|sc| parse_name(sc).first}
108
116
  when Symbol, String
109
- @parents = []
117
+ return name.to_sym, []
110
118
  else
111
- raise "I don't know how to build `#{scenario.inspect}'"
112
- end
113
-
114
- @scenario = scenario.to_sym
115
- @block = block
116
-
117
- @@scenarios[@scenario] = self
119
+ raise TypeError, "Pass scenarios names as strings or symbols only, cannot build scenario '#{name.inspect}'"
120
+ end
118
121
  end
119
122
 
120
123
  def say(*messages)
@@ -134,7 +137,7 @@ class Hornsby
134
137
 
135
138
  def build_parent_scenarios(context)
136
139
  @parents.each do |p|
137
- parent = self.class.scenarios[p] or raise "parent scenario [#{p}] not found!"
140
+ parent = @@scenarios[p] or raise ScenarioNotFoundError, p
138
141
 
139
142
  parent.build_parent_scenarios(context)
140
143
  parent.build_scenario(context)
@@ -145,33 +148,10 @@ class Hornsby
145
148
  yield
146
149
  rescue StandardError => error
147
150
  puts
148
- say "There was an error building scenario `#{@scenario}'", error.inspect
151
+ say "There was an error building scenario '#{@scenario}'", error.inspect
149
152
  puts
150
153
  puts error.backtrace
151
154
  puts
152
155
  raise error
153
156
  end
154
- end
155
-
156
-
157
- module HornsbyHelper
158
- def hornsby_scenario(*names)
159
- Hornsby.build(*names)
160
- Hornsby.copy_ivars(self)
161
- end
162
-
163
- def hornsby_clear(*args)
164
- options = args.extract_options!
165
- Hornsby.delete_tables(*args)
166
-
167
- if options[:undo] == :all
168
- Hornsby.executed_scenarios.clear
169
- else
170
- undo = [options[:undo]].flatten.compact
171
- unless (not_found = undo - Hornsby.executed_scenarios.to_a).blank?
172
- raise(ArgumentError, "Scenario(s) #{not_found} not found")
173
- end
174
- Hornsby.executed_scenarios -= undo
175
- end
176
- end
177
- end
157
+ end
@@ -32,6 +32,9 @@ scenario(:cherry_basket => [:big_cherry, :cherry]) do
32
32
  @basket = [@cherry, @big_cherry]
33
33
  end
34
34
 
35
+
36
+ scenario :parent_not_existing => :not_existing
37
+
35
38
  # Hornsby.namespace(:pitted_fruit) do
36
39
  # scenario(:peach) do
37
40
  # @peach = Fruit.create! :species => 'peach'
data/spec/hornsby_spec.rb CHANGED
@@ -155,6 +155,26 @@ describe Hornsby do
155
155
  end
156
156
  end
157
157
 
158
+ describe 'errors' do
159
+ it 'should raise ScenarioNotFoundError when scenario could not be found' do
160
+ lambda {
161
+ hornsby_scenario :not_existing
162
+ }.should raise_error(Hornsby::ScenarioNotFoundError, "Scenario(s) not found 'not_existing'")
163
+ end
164
+
165
+ it 'should raise ScenarioNotFoundError when scenario parent could not be found' do
166
+ lambda {
167
+ hornsby_scenario :parent_not_existing
168
+ }.should raise_error(Hornsby::ScenarioNotFoundError, "Scenario(s) not found 'not_existing'")
169
+ end
170
+
171
+ it 'should raise TypeError when scenario name is not symbol or string' do
172
+ lambda {
173
+ Hornsby.new(1)
174
+ }.should raise_error(TypeError, "Pass scenarios names as strings or symbols only, cannot build scenario '1'")
175
+ end
176
+ end
177
+
158
178
  #describe "with pitted namespace" do
159
179
  # before do
160
180
  # Hornsby.build('pitted:peach').copy_ivars(self)
data/spec/spec_helper.rb CHANGED
@@ -21,5 +21,5 @@ require 'db/fruit'
21
21
  require 'db/tree'
22
22
 
23
23
  Spec::Runner.configure do |config|
24
- Hornsby.configure_rspec(config, :filename => File.join('hornsby_scenario.rb'), :scenarios => :big_cherry)
24
+ Hornsby.configure_rspec(config, :root => File.join(spec_dir, '..'), :scenarios => :big_cherry)
25
25
  end
@@ -0,0 +1,188 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'shoulda'
3
+
4
+ class HornsbyTest < ActiveSupport::TestCase
5
+ context "with just_apple scenario" do
6
+ setup do
7
+ hornsby_scenario :just_apple
8
+ end
9
+
10
+ should "create @apple" do
11
+ assert(!(@apple.nil?))
12
+ end
13
+
14
+ should "create Fruit @apple" do
15
+ assert(@apple.instance_of?(Fruit))
16
+ end
17
+
18
+ should "not create @banana" do
19
+ assert(@banana.nil?)
20
+ end
21
+
22
+ should "have correct species" do
23
+ assert(@apple.species == 'apple')
24
+ end
25
+ end
26
+
27
+ context "with bananas_and_apples scenario" do
28
+ setup do
29
+ hornsby_scenario :bananas_and_apples
30
+ end
31
+
32
+ should "have correct @apple species" do
33
+ assert(@apple.species == 'apple')
34
+ end
35
+
36
+ should "have correct @banana species" do
37
+ assert(@banana.species == 'banana')
38
+ end
39
+ end
40
+
41
+ context "with fruit scenario" do
42
+ setup do
43
+ hornsby_scenario :fruit
44
+ end
45
+
46
+ should "have 2 fruits" do
47
+ assert(@fruit.size == 2)
48
+ end
49
+
50
+ should "have an @apple" do
51
+ assert(@apple.species == 'apple')
52
+ end
53
+
54
+ should "have an @orange" do
55
+ assert(@orange.species == 'orange')
56
+ end
57
+
58
+ should "have no @banana" do
59
+ assert(@banana.nil?)
60
+ end
61
+ end
62
+
63
+ context 'with preloaded cherry scenario' do
64
+ should "have correct size after changed by second test" do
65
+ assert(@cherry.average_diameter == 3)
66
+ @cherry.update_attribute(:average_diameter, 1)
67
+ assert(@cherry.average_diameter == 1)
68
+ end
69
+
70
+ should "have correct size" do
71
+ assert(@cherry.average_diameter == 3)
72
+ @cherry.update_attribute(:average_diameter, 5)
73
+ assert(@cherry.average_diameter == 5)
74
+ end
75
+
76
+ should "create big cherry" do
77
+ assert(@big_cherry.species == 'cherry')
78
+ end
79
+ end
80
+
81
+ context 'hornsby_clear' do
82
+ setup do
83
+ hornsby_scenario :just_apple
84
+ end
85
+
86
+ should "clear scenarios when calling hornsby_clear" do
87
+ hornsby_clear
88
+ assert(Fruit.count == 0)
89
+ end
90
+
91
+ should "clear only tables passed" do
92
+ Tree.create!(:name => 'oak')
93
+ hornsby_clear :fruits
94
+ assert(Tree.count == 1)
95
+ assert(Fruit.count == 0)
96
+ end
97
+
98
+ should "mark scenarios as undone when passed :undone option" do
99
+ hornsby_scenario :fruit
100
+ hornsby_clear :undo => [:just_apple]
101
+ assert(Fruit.count == 0)
102
+ hornsby_scenario :fruit
103
+ assert(Fruit.count == 1)
104
+ end
105
+
106
+ should "mark all scenarios as undone when passed :undone option as :all" do
107
+ hornsby_scenario :fruit
108
+ hornsby_clear :undo => :all
109
+ assert(Fruit.count == 0)
110
+ hornsby_scenario :fruit
111
+ assert(Fruit.count == 2)
112
+ end
113
+
114
+ should "raise error when not executed scenarios passed to :undo option" do
115
+ assert_raise(ArgumentError) do
116
+ hornsby_clear :undo => :just_orange
117
+ end
118
+ end
119
+ end
120
+
121
+ context 'with many apples scenario' do
122
+ setup do
123
+ hornsby_scenario :many_apples, :cherry, :cherry_basket
124
+ end
125
+
126
+ should "create only one apple" do
127
+ assert(Fruit.all(:conditions => 'species = "apple"').size == 1)
128
+ end
129
+
130
+ should "create only two cherries even if they were preloaded" do
131
+ assert(Fruit.all(:conditions => 'species = "cherry"').size == 2)
132
+ end
133
+
134
+ should "contain cherries in basket if basket is loaded in test and cherries preloaded" do
135
+ assert(@basket == [@cherry, @big_cherry])
136
+ end
137
+ end
138
+
139
+ context 'transactions' do
140
+ setup do
141
+ hornsby_scenario :just_apple
142
+ end
143
+
144
+ should "drop only inner transaction" do
145
+ assert(!(@apple.reload.nil?))
146
+ begin
147
+ ActiveRecord::Base.transaction do
148
+ f = Fruit.create(:species => 'orange')
149
+ assert(!(f.reload.nil?))
150
+ raise 'some error'
151
+ end
152
+ rescue
153
+ end
154
+ assert(!(@apple.reload.nil?))
155
+ assert(Fruit.find_by_species('orange').nil?)
156
+ end
157
+ end
158
+
159
+ context 'errors' do
160
+ should 'raise ScenarioNotFoundError when scenario could not be found' do
161
+ assert_raise(Hornsby::ScenarioNotFoundError, "Scenario(s) not found 'not_existing'") do
162
+ hornsby_scenario :not_existing
163
+ end
164
+ end
165
+
166
+ should 'raise ScenarioNotFoundError when scenario parent could not be found' do
167
+ assert_raise(Hornsby::ScenarioNotFoundError, "Scenario(s) not found 'not_existing'") do
168
+ hornsby_scenario :parent_not_existing
169
+ end
170
+ end
171
+
172
+ should 'raise TypeError when scenario name is not symbol or string' do
173
+ assert_raise(TypeError, "Pass scenarios names as strings or symbols only, cannot build scenario '1'") do
174
+ Hornsby.new(1)
175
+ end
176
+ end
177
+ end
178
+
179
+ #describe "with pitted namespace" do
180
+ # before do
181
+ # Hornsby.build('pitted:peach').copy_ivars(self)
182
+ # end
183
+
184
+ # it "should have @peach" do
185
+ # @peach.species.should == 'peach'
186
+ # end
187
+ #end
188
+ end
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'fileutils'
3
+ require 'activerecord'
4
+ require 'test/unit'
5
+ require 'active_record/test_case'
6
+ begin
7
+ require 'mysqlplus'
8
+ rescue LoadError
9
+ end
10
+
11
+ spec_dir = File.join(File.dirname(__FILE__), '..', 'spec')
12
+
13
+ ActiveRecord::Base.logger = Logger.new("debug.log")
14
+
15
+ databases = YAML::load(IO.read(spec_dir + "/db/database.yml"))
16
+ db_info = databases[ENV["DB"] || "test"]
17
+ ActiveRecord::Base.establish_connection(db_info)
18
+ load(File.join(spec_dir, "db", "schema.rb"))
19
+
20
+ require spec_dir + '/../lib/hornsby'
21
+ require spec_dir + '/db/fruit'
22
+ require spec_dir + '/db/tree'
23
+
24
+ class ActiveSupport::TestCase
25
+ enable_hornsby :root => File.join(File.dirname(__FILE__), '..'), :scenarios => :big_cherry
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinsiliux-hornsby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrius Chamentauskas
@@ -33,7 +33,11 @@ extra_rdoc_files: []
33
33
 
34
34
  files:
35
35
  - lib/hornsby.rb
36
- - lib/hornsby_context.rb
36
+ - lib/hornsby/context.rb
37
+ - lib/hornsby/helper.rb
38
+ - lib/hornsby/errors.rb
39
+ - lib/hornsby/rspec_extensions.rb
40
+ - lib/hornsby/test_unit_extensions.rb
37
41
  - lib/tasks/hornsby_tasks.rake
38
42
  - README.rdoc
39
43
  - LICENSE
@@ -71,3 +75,5 @@ test_files:
71
75
  - spec/db/fruit.rb
72
76
  - spec/db/database.yml.example
73
77
  - spec/db/schema.rb
78
+ - test/test_helper.rb
79
+ - test/hornsby_test.rb
@@ -1,13 +0,0 @@
1
- module HornsbyContext
2
- def self.execute(&block)
3
- module_eval(&block) if block
4
- end
5
-
6
- def self.copy_ivars(to, reload = false)
7
- instance_variables.each do |iv|
8
- v = instance_variable_get(iv)
9
- v.reload if reload and v.respond_to?(:reload)
10
- to.instance_variable_set(iv, v)
11
- end
12
- end
13
- end