public
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
wycats (author)
Fri Sep 26 23:38:57 -0700 2008
commit  fdc4ec0c04073e4ffca7d484152dbaedf02f1f97
tree    5bb7382bc73d4c13eee8616ebe23541a835f956c
parent  d22b8e6c60b48718e7484a1154a2211c5fa17404
merb-core / spec / spec_helper.rb
100644 133 lines (107 sloc) 3.075 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
$TESTING=true
require "rubygems"
require "spec"
require File.join(File.dirname(__FILE__), "..", "lib", "merb-core")
 
default_options = {:environment => 'test', :adapter => 'runner'}
options = default_options.merge($START_OPTIONS || {})
Merb.start_environment(options)
 
# -- Global custom matchers --
 
# A better +be_kind_of+ with more informative error messages.
#
# The default +be_kind_of+ just says
#
# "expected to return true but got false"
#
# This one says
#
# "expected File but got Tempfile"
 
module Merb
  module Test
    module RspecMatchers
      class IncludeLog
        def initialize(expected)
          @expected = expected
        end
        
        def matches?(target)
          target.rewind
          @text = target.read
          @text =~ (String === @expected ? /#{Regexp.escape @expected}/ : @expected)
        end
        
        def failure_message
          "expected to find `#{@expected}' in the log but got:\n" <<
          @text.map {|s| " #{s}" }.join
        end
        
        def negative_failure_message
          "exected not to find `#{@expected}' in the log but got:\n" <<
          @text.map {|s| " #{s}" }.join
        end
        
        def description
          "include #{@text} in the log"
        end
      end
      
      class BeKindOf
        def initialize(expected) # + args
          @expected = expected
        end
 
        def matches?(target)
          @target = target
          @target.kind_of?(@expected)
        end
 
        def failure_message
          "expected #{@expected} but got #{@target.class}"
        end
 
        def negative_failure_message
          "expected #{@expected} to not be #{@target.class}"
        end
 
        def description
          "be_kind_of #{@target}"
        end
      end
 
      def be_kind_of(expected) # + args
        BeKindOf.new(expected)
      end
      
      def include_log(expected)
        IncludeLog.new(expected)
      end
    end
 
    module Helper
      def running(&blk) blk; end
 
      def executing(&blk) blk; end
 
      def doing(&blk) blk; end
 
      def calling(&blk) blk; end
    end
  end
end
 
Spec::Runner.configure do |config|
  config.include Merb::Test::Helper
  config.include Merb::Test::RspecMatchers
  config.include Merb::Test::Rspec::ViewMatchers
  config.include Merb::Test::RequestHelper
  config.include Merb::Test::RouteHelper
 
  def reset_dependency(name, const = nil)
    Object.send(:remove_const, const) if const && Object.const_defined?(const)
    Merb::BootLoader::Dependencies.dependencies.delete_if do |d|
      d.name == name
    end
    $LOADED_FEATURES.delete("#{name}.rb")
  end
 
  def with_level(level)
    Merb::Config[:log_stream] = StringIO.new
    Merb::Config[:log_level] = level
    Merb.logger = nil
    yield
    Merb::Config[:log_stream]
  end
 
  def capture(stream)
    begin
      stream = stream.to_s
      eval "$#{stream} = StringIO.new"
      yield
      result = eval("$#{stream}").string
    ensure
      eval("$#{stream} = #{stream.upcase}")
    end
 
    result
  end
 
  alias silence capture
end