lib/hornsby.rb in sinsiliux-hornsby-0.2.5 vs lib/hornsby.rb in sinsiliux-hornsby-0.3.0

- old
+ new

@@ -6,65 +6,85 @@ def self.framework_root RAILS_ROOT rescue Rails.root rescue Merb.root rescue '' end cattr_reader :scenarios + cattr_accessor :executed_scenarios # @@namespaces = {} @@scenarios = {} @@executed_scenarios = Set.new @@global_executed_scenarios = [] @@global_context = HornsbyContext @@context = nil def self.configure_rspec(config, options = {}) - load(options[:filename]) + load(options) - @@context = @@global_context - @@global_scenarios = Hornsby.build(options[:scenarios]) if options[:scenarios] - @@global_executed_scenarios = @@executed_scenarios.to_a - - config.include(HornsbySpecHelper) - + config.include(HornsbyHelper) config.before do - Hornsby.send(:class_variable_set, '@@context', Hornsby.send(:class_variable_get, '@@global_context').clone) - Hornsby.send(:class_variable_set, '@@executed_scenarios', Set.new(Hornsby.send(:class_variable_get, '@@global_executed_scenarios'))) - Hornsby.copy_ivars(self, true) - ActiveRecord::Base.connection.increment_open_transactions - ActiveRecord::Base.connection.transaction_joinable = false - ActiveRecord::Base.connection.begin_db_transaction + Hornsby.setup(self) end - config.after do - ActiveRecord::Base.connection.rollback_db_transaction - ActiveRecord::Base.connection.decrement_open_transactions + Hornsby.teardown end end + def self.configure_test(config, options) + load(options) + + config.send(:include, HornsbyHelper) + config.setup do + Hornsby.setup(self) + end + config.teardown do + Hornsby.teardown + end + end + + def self.setup(current_context) + @@context = @@global_context.clone + @@executed_scenarios = Set.new(@@global_executed_scenarios) + copy_ivars(current_context, true) + ActiveRecord::Base.connection.increment_open_transactions + ActiveRecord::Base.connection.transaction_joinable = false + ActiveRecord::Base.connection.begin_db_transaction + end + + def self.teardown + ActiveRecord::Base.connection.rollback_db_transaction + ActiveRecord::Base.connection.decrement_open_transactions + end + def self.build(*names) scenarios = names.map {|name| @@scenarios[name.to_sym] or raise "scenario #{name} not found"} scenarios.each {|s| s.build} end def self.[](name) end - def self.load(scenarios_file=nil) + def self.load(options = {}) return unless @@scenarios.empty? delete_tables - scenarios_file ||= File.join(framework_root, 'spec', 'hornsby_scenarios.rb') + scenarios_file = options[:filename] || File.join(framework_root, 'spec', 'hornsby_scenarios.rb') self.module_eval File.read(scenarios_file) + + @@context = @@global_context + @@global_scenarios = Hornsby.build(options[:scenarios]) if options[:scenarios] + @@global_executed_scenarios = @@executed_scenarios.to_a end def self.scenario(scenario, &block) self.new(scenario, &block) end - def self.delete_tables - tables.each { |t| ActiveRecord::Base.connection.delete(@@delete_sql % t) } + def self.delete_tables(*args) + args = tables if args.blank? + args.each { |t| ActiveRecord::Base.connection.delete(@@delete_sql % t) } end def self.tables ActiveRecord::Base.connection.tables - skip_tables end @@ -132,15 +152,26 @@ raise error end end -module HornsbySpecHelper +module HornsbyHelper def hornsby_scenario(*names) Hornsby.build(*names) Hornsby.copy_ivars(self) end - def hornsby_clear - Hornsby.delete_tables + def hornsby_clear(*args) + options = args.extract_options! + Hornsby.delete_tables(*args) + + if options[:undo] == :all + Hornsby.executed_scenarios.clear + else + undo = [options[:undo]].flatten.compact + unless (not_found = undo - Hornsby.executed_scenarios.to_a).blank? + raise(ArgumentError, "Scenario(s) #{not_found} not found") + end + Hornsby.executed_scenarios -= undo + end end end