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 *args list = if args.first.is_a? Array args.first else args end before(:all){list.each{|path| $LOAD_PATH << path}} after(:all){list.each{|path| $LOAD_PATH.delete path}} 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 class String unless "".respond_to? :dirname def dirname File.expand_path(File.dirname(self)) end end unless "".respond_to? :parent_dirname def parent_dirname File.expand_path(self + "/..") end end end