# # Code Coverage # # require 'simplecov' # SimpleCov.start do # add_filter "/spec/" # end # SimpleCov.at_exit do # SimpleCov.result.format! # Kernel.exec 'open ./coverage/index.html' # end # # CLASS_LOADER_GENERATE_TMP_FILES = true # # RSpec # require 'rspec' require 'fileutils' RSpec.configure do |c| c.filter_run focus: true c.run_all_when_everything_filtered = true end def rspec &block RSpec::Core::ExampleGroup.class_eval &block if block return RSpec::Core::ExampleGroup end rspec do def self.define_matcher *args, &block RSpec::Matchers.define *args do |expected| match do |actual| block.call actual, expected if block end end end end rspec do def self.with_load_path *paths before(:all){paths.each{|path| $LOAD_PATH << path}} after(:all){paths.each{|path| $LOAD_PATH.delete path}} end def with_load_path *paths, &b begin paths.each{|path| $LOAD_PATH << path} b.call ensure paths.each{|path| $LOAD_PATH.delete path} end end def self.with_tmp_spec_dir *args options = args.last.is_a?(Hash) ? args.pop : {} dir = args.first || self.spec_dir options[:before] ||= :all tmp_dir = "/tmp/#{dir.split('/').last}" before options do FileUtils.rm_r tmp_dir if File.exist? tmp_dir FileUtils.cp_r dir, tmp_dir @spec_dir = tmp_dir end after options do FileUtils.rm_r tmp_dir if File.exist? tmp_dir @spec_dir = nil end tmp_dir end def self.with_spec_dir dir before(:all){@spec_dir = dir} after(:all){@spec_dir = nil} end # def self.with_spec_dir # def self.spec_dir dir = nil # @spec_dir ||= calculate_spec_dir_for_current_caller # end def self.spec_dir self.calculate_default_spec_dir || raise(":spec_dir not defined!") end def spec_dir @spec_dir || self.class.spec_dir end def self.calculate_default_spec_dir spec_file_name = caller.find{|line| line =~ /_spec\.rb\:/} return nil unless spec_file_name spec_dir = spec_file_name.sub(/\.rb\:.*/, '') raise "spec dir not exist (#{spec_dir})!" unless File.exist? spec_dir spec_dir end def remove_constants *args args = args.first if args.size == 1 and args.first.is_a?(Array) args.each{|c| Object.send :remove_const, c if Object.const_defined? c} end # def spec_tmp_dir # $spec_tmp_dir || raise("you should call :with_tmp_spec_dir to be able to use :spec_tmp_dir!") # end end # # dirname, parent_dirname # class String unless method_defined? :dirname def dirname File.expand_path(File.dirname(self)) end end end # # instance_stub # class Class def instance_stub! &block new_method = method(:new) stub!(:new).and_return do |*args| instance = new_method.call(*args) block.call instance instance end end end