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
fabien (author)
Sun Sep 28 09:40:58 -0700 2008
commit  e3159c418376bab845df40ed733960b62818581a
tree    651355d5c3042bbc210956050d0d58a94bc25b45
parent  f57cc0ce87f02e5a1941fd903f12d55a1d1ad693
merb-core / spec / public / router / spec_helper.rb
100644 145 lines (127 sloc) 3.913 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
134
135
136
137
138
139
140
141
142
143
144
145
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
require 'ostruct'
require 'rubygems'
gem "rspec"
require "spec"
 
module Spec
  module Matchers
    class HaveRoute
      def initialize(expected, exact = false)
        @expected = expected
        @exact = exact
      end
 
      def matches?(target)
        @target = target.last
        @errors = []
        @expected.all? { |param, value| @target[param] == value } && (!@exact || @expected.length == @target.length)
      end
 
      def failure_message
        @target.each do |param, value|
          @errors << "Expected :#{param} to be #{@expected[param].inspect}, but was #{value.inspect}" unless
            @expected[param] == value
        end
        @errors << "Got #{@target.inspect}"
        @errors.join("\n")
      end
 
      def negative_failure_message
        "Expected #{@expected.inspect} not to be #{@target.inspect}, but it was."
      end
 
      def description() "have_route #{@target.inspect}" end
    end
 
    def have_route(expected)
      HaveRoute.new(expected)
    end
    
    def have_exact_route(expected)
      HaveRoute.new(expected, true)
    end
    
    # class HaveNilRoute
    #
    # def matches?(target)
    # @target = target
    # target.last.empty?
    # end
    #
    # def failure_message
    # "Expected a nil route. Got #{@target.inspect}."
    # end
    #
    # def negative_failure_message
    # "Expected not to get a nil route."
    # end
    # end
    #
    def raise_not_found
      raise_error(Merb::ControllerExceptions::NotFound)
    end
  end
  
  module Helpers
    #
    # Creates a single route with the passed conditions and parameters without
    # registering it with Router
    # def route(conditions, params = {})
    # conditions = {:path => conditions} unless Hash === conditions
    # Merb::Router::Route.new(conditions, params)
    # end
    #
    # #
    # # A shortcut for creating a single route and registering it with Router
    # def prepare_named_route(name, from, conditions = {}, to = nil)
    # to, conditions = conditions, {} unless to
    # Merb::Router.prepare {|r| r.match(from, conditions).to(to).name(name) }
    # end
    #
    # def prepare_conditional_route(name, from, conditions, to = {})
    # Merb::Router.prepare {|r| r.match(from, conditions).to(to).name(name) }
    # end
    #
    def prepare_route(from = {}, to = {})
      name = :default
      Merb::Router.prepare {|r| r.match(from).to(to).name(name) }
    end
    
    def simple_request(options = {})
      Request.new({:protocol => "http://", :path => '/'}.merge(options))
    end
 
    #
    # Returns the dispatch parameters for a request by passing the request
    # through Router#match.
    def route_to(path, args = {}, protocol = "http://")
      request = Request.new({:protocol => protocol, :path => path}.merge(args))
      # Merb::Router.match(request)
      Merb::Router.route_for(request)
    end
    
    def match_for(path, args = {}, protocol = "http://")
      Merb::Router.match(Request.new({:protocol => protocol, :path => path}.merge(args)))
    end
    
    def matched_route_for(*args)
      # get route index
      idx = match_for(*args)[0]
    
      Merb::Router.routes[idx]
    end
 
    # Fake request object
    class Request < OpenStruct
      def initialize(hash)
        @table = {}
        hash.each_pair do |key, value|
          @table[key] = value.to_s
        end
      end
      
      def method
        @table[:method] || "get"
      end
 
      def params
        @table
      end
    end
  end
end
 
Spec::Runner.configure do |config|
  config.include(Spec::Helpers)
  config.include(Spec::Matchers)
  config.before(:each) do
    @_root_behavior = Merb::Router.root_behavior
  end
  config.after(:each) do
    Merb::Router.root_behavior = @_root_behavior
    Merb::Router.reset!
  end
end