Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / radiant_specs / spec / matchers / .svn / text-base / render_matcher.rb.svn-base
bloba18a6607bffde94b662f1814b724b6979ac15822
1 module Spec
2   module Rails
3     module Matchers
4       
5       class RenderTags
6         def initialize(content = nil)
7           @content = content
8         end
9         
10         def matches?(page)
11           @actual = render_content_with_page(@content, page)
12           if @expected.kind_of?(Regexp)
13             @expected = nil
14             @matching = @expected
15           end
16           case
17             when @error_message: false
18             when @expected: @actual == @expected
19             when @matching: @actual =~ @matching
20             else true
21           end
22         rescue => @actual_error
23           if @expected_error_message
24             @actual_error.message === @expected_error_message
25           else
26             @error_thrown = true
27             false
28           end
29         end
30         
31         def failure_message
32           action = @expected.nil? ? "render and match #{@matching.inspect}" : "render as #{@expected.inspect}"
33           unless @error_thrown
34             unless @expected_error_message
35               if @content
36                 "expected #{@content.inspect} to #{action}, but got #{@actual.inspect}"
37               else
38                 "expected page to #{action}, but got #{@actual.inspect}"
39               end
40             else
41               "expected rendering #{@content.inspect} to throw exception with message #{@expected_error_message.inspect}, but was #{@actual_error.message.inspect}"
42             end
43           else
44             "expected #{@content.inspect} to render, but an exception was thrown #{@actual_error.message}"
45           end
46         end
47         
48         def description
49           "render tags #{@expected.inspect}"
50         end
51         
52         def as(output)
53           @expected = output
54           self
55         end
56         
57         def matching(regexp)
58           @matching = regexp
59           self
60         end
61         
62         def with_error(message)
63           @expected_error_message = message
64           self
65         end
66         
67         def on(url)
68           url = test_host + "/" + url unless url =~ %r{^[^/]+\.[^/]+}
69           url = 'http://' + url unless url =~ %r{^http://}
70           uri = URI.parse(url)
71           @request_uri = uri.request_uri unless uri.request_uri == '/'
72           @host = uri.host
73           self
74         end
75         
76         private
77           def render_content_with_page(tag_content, page)
78             page.request = ActionController::TestRequest.new
79             page.request.request_uri = @request_uri || page.url
80             page.request.host = @host || test_host
81             page.response = ActionController::TestResponse.new
82             if tag_content.nil?
83               page.render
84             else
85               page.send(:parse, tag_content)
86             end
87           end
88           
89           def test_host
90             "testhost.tld"
91           end
92       end
93       
94       # page.should render(input).as(output)
95       # page.should render(input).as(output).on(url)
96       # page.should render(input).matching(/hello world/)
97       # page.should render(input).with_error(message)
98       def render(input)
99         RenderTags.new(input)
100       end
101       
102       # page.should render_as(output)
103       def render_as(output)
104         RenderTags.new.as(output)
105       end
106       
107     end
108   end