Skip to content
Browse files

more rspec adapter features

  • Loading branch information...
1 parent dc7c51e commit 52b7fea6a0cb21a0038bd5320942ed36d5d78f3f @szarski committed Jun 24, 2011
Showing with 168 additions and 16 deletions.
  1. +1 −1 VERSION
  2. +6 −2 lib/file_handler.rb
  3. +84 −13 lib/rspec_adapter.rb
  4. +77 −0 spec/fixturized_spec.rb
View
2 VERSION
@@ -1 +1 @@
-0.4.0
+0.4.1
View
8 lib/file_handler.rb
@@ -1,6 +1,10 @@
module Fixturized::FileHandler
# All filesystem operations are handled here
+ def dump_module
+ Marshal
+ end
+
def fixture_dir
"#{RAILS_ROOT}/fixturized"
end
@@ -13,11 +17,11 @@ def create_fixture_dir
def write_fixture(filename, objects)
Fixturized.create_fixture_dir
- return File.open(self.fixture_path(filename), 'w') {|f| YAML.dump objects, f}
+ return File.open(self.fixture_path(filename), 'w') {|f| self.dump_module.dump objects, f}
end
def load_fixture(filename)
- return YAML.load_file(self.fixture_path(filename))
+ return self.dump_module.load(File.read((self.fixture_path(filename))))
end
def fixture_exists?(filename)
View
97 lib/rspec_adapter.rb
@@ -1,5 +1,37 @@
+class Time
+ class <<self
+ attr_reader :offset
+ alias_method :real_now, :now
+ def now
+ @offset = 0 if @offset.nil?
+ real_now - @offset
+ end
+ alias_method :new, :now
+
+ # Warp to an absolute time in the past or future, making sure it takes
+ # the present as the reference starting point when making the jump.
+ def set(time)
+ reset
+ @offset = now - time
+ end
+
+ # Jump back to present.
+ def reset
+ @offset = 0
+ end
+ end
+end
+
module Fixturized::RspecAdapter
+ FORBIDDEN_METHODS = %w{stubs expects any_instance}
+ def self.cache
+ @cache
+ end
+ def self.cache=val
+ @cache=val
+ end
module ExampleGroupHierarchyExtension
+ SPECIAL_PLACES = %w{ActionMailer::Base.deliveries}
def eval_each_fail_fast(blocks) # :nodoc:
# IF you operate on an array here it will behave weirdly:
# it won't be overwritten the next time an example is run
@@ -10,35 +42,74 @@ def eval_each_fail_fast(blocks) # :nodoc:
if Fixturized::FileHandler.fixture_exists?(filename)
Fixturized::DatabaseHandler.clear_db
data = Fixturized::FileHandler.load_fixture filename
+#puts "read: "+data[:constants].inspect
+ data[:constants].each {|const_name, const_value| #puts(const_name.inspect, const_value.inspect);
+ Object.const_set(const_name, const_value)}
vars = data[:variables]
- #vars.delete "@_proxy"
- #vars.delete "@_result"
- #vars.delete "@method_name"
- #vars.delete "@_backtrace"
- #vars.delete "@_implementation"
set_instance_variables_from_hash(vars)
Fixturized::DatabaseHandler.write_db_data(data[:database])
+ #Time.set(data[:time])
+ data[:special_places].each do |p,val|
+ val = "Marshal.load(#{Marshal.dump(val).inspect})"
+ eval "#{p} = #{val}"
+ end
else
+ Fixturized::RspecAdapter.cache={}
+ consts = Object.constants
blocks.each {|block| instance_eval(&block)}
+ consts = Object.constants - Fixturized::RspecAdapter.startup_consts
+ consts = consts.inject({}) {|r,const_name| r.merge({const_name => Object.const_get(const_name)})}
+ #puts "writing: "+consts.inspect
+ begin
vars = instance_variable_hash.clone
vars.to_a.each do |k,v|
if v.is_a?(Proc)
vars.delete k
end
end
- Fixturized::FileHandler.write_fixture filename, {:database => Fixturized::DatabaseHandler.collect_db_data, :variables => vars}
+ vars.delete "@_proxy"
+ vars.delete "@_result"
+ vars.delete "@method_name"
+ vars.delete "@_backtrace"
+ vars.delete "@_implementation"
+ special_places = SPECIAL_PLACES.inject({}) {|r,p| r.merge(p => eval(p))}
+ stubs = Fixturized::RspecAdapter.cache[:stubs]
+ puts stubs.inspect
+ Fixturized::FileHandler.write_fixture filename, {:database => Fixturized::DatabaseHandler.collect_db_data, :variables => vars, :constants => consts, :time => Time.now, :special_places => special_places}#, :stubs => stubs}
+ rescue Exception => e
+ puts e.message, e.backtrace
+ end
end
end
end
def plug_in
puts "PLUGGING IN!!!!!"
- ::Spec::Example::ExampleMethods.extend Fixturized::RspecAdapter::ExampleGroupHierarchyExtension
- ::Spec::Example::ExampleGroup.include Fixturized::RspecAdapter::ExampleGroupHierarchyExtension
- ::Test::Unit::TestCase.include Fixturized::RspecAdapter::ExampleGroupHierarchyExtension
- end
- self.extend self
-end
+# ::Spec::Example::ExampleMethods.extend Fixturized::RspecAdapter::ExampleGroupHierarchyExtension
+# ::Spec::Example::ExampleMethods.send :include, Fixturized::RspecAdapter::ExampleGroupHierarchyExtension
+# ::Spec::Example::ExampleGroup.send :include, Fixturized::RspecAdapter::ExampleGroupHierarchyExtension
+# ::Spec::Example.extend Fixturized::RspecAdapter::ExampleGroupHierarchyExtension
+# ::Spec::Example::ModelExampleGroup.send :include, Fixturized::RspecAdapter::ExampleGroupHierarchyExtension
+ @startup_consts=Object.constants
+ ::Spec::Rails::Example::ModelExampleGroup.send :include, Fixturized::RspecAdapter::ExampleGroupHierarchyExtension
-Fixturized::RspecAdapter.plug_in
+ Class.class_eval do
+ FORBIDDEN_METHODS.each do |method_name|
+ define_method method_name do |*args|
+ #raise SkipFixturizedError.new "Fixturized does not support methods like #{method_name}, please add skip_fixturized inside this block."
+ Fixturized::RspecAdapter.cache[:stubs] ||= []
+ Fixturized::RspecAdapter.cache[:stubs] << [self.name, method_name, args]
+ end
+ end
+ end
+
+ end
+
+ def startup_consts
+ @startup_consts
+ end
+ self.extend self
+ class SkipFixturizedError < Exception
+ end
+end
View
77 spec/fixturized_spec.rb
@@ -240,11 +240,88 @@ def id; 5; end
describe "engines" do
describe Fixturized::DatabaseHandler::ActiveRecordAndMysqlEngine do
before(:all) do
+ pending
#TODO: hook db here
end
after(:all) do
#TODO: close db connection here
end
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+# def is_model?(value)
+# defined?(ActiveRecord::Base) and value.class.superclass == ActiveRecord::Base
+# def substitute_model(value)
+# return [value.class.to_s, value.id]
+# def load_model(value)
+# klass = eval value.first
+# id = value.last.to_i
+# return klass.find(id)
+# def clear_all_tables
+# interesting_tables.each do |tbl|
+# ActiveRecord::Base.connection.execute "DELETE FROM #{tbl}"
+# end
+# def interesting_tables
+# ActiveRecord::Base.connection.tables.sort.reject do |tbl|
+# ['schema_info', 'sessions', 'public_exceptions'].include?(tbl)
+# end
+# def object_to_data(objects)
+# objects.each do |tbl, fixtures|
+# unless fixtures.to_a.empty?
+# klass = tbl.classify.constantize
+# ActiveRecord::Base.transaction do
+# statement = "INSERT INTO #{tbl} (#{fixtures.first.keys.collect{|k| "`#{k}`"}.join(",")}) " + fixtures.collect do |fixture|
+# "(SELECT #{fixture.values.collect { |value| ActiveRecord::Base.connection.quote(value) }.join(', ')})"
+# end.join(" UNION ")
+# ActiveRecord::Base.connection.execute statement, 'Fixture Insert'
+# end
+# end
+# end
+# def data_to_object
+# objects = {}
+# interesting_tables.each do |tbl|
+# begin
+# klass = tbl.classify.constantize
+# objects[tbl] = klass.find(:all).collect(&:attributes)
+# rescue Exception
+# end
+# end
+# return objects
+# end
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ it "#clear_all_tables should remove all tables contents" do
+ end
end
end
end

0 comments on commit 52b7fea

Please sign in to comment.
Something went wrong with that request. Please try again.