~matteo-collina/+junk/gipieffe

« back to all changes in this revision

Viewing changes to stories/rest_auth_stories_helper.rb

  • Committer: Matteo Collina
  • Date: 2008-11-02 09:46:31 UTC
  • Revision ID: matteo.collina@gmail.com-20081102094631-h9w18le32hew28xp
 * Generated login system by the restful-authentication-i18n plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# If you have a global stories helper, move this line there:
 
2
include AuthenticatedTestHelper
 
3
 
 
4
# Most of the below came out of code from Ben Mabey
 
5
# http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/
 
6
 
 
7
# These allow exceptions to come through as opposed to being caught and having non-helpful responses returned.
 
8
ActionController::Base.class_eval do
 
9
  def perform_action
 
10
    perform_action_without_rescue
 
11
  end
 
12
end
 
13
Dispatcher.class_eval do
 
14
  def self.failsafe_response(output, status, exception = nil)
 
15
    raise exception
 
16
  end
 
17
end
 
18
 
 
19
#
 
20
# Sugar for turning a story's attribute list into list, array, etc.
 
21
#
 
22
module ToFooFromStory
 
23
  def ToFooFromStory.fix_key key
 
24
    key.downcase.gsub(/\s+/, '_')
 
25
  end
 
26
  def ToFooFromStory.fix_value value
 
27
    return '' if !value
 
28
    value.strip!
 
29
    case 
 
30
    when value =~ /^'(.*)'$/    then value = $1
 
31
    when value =~ /^"(.*)"$/    then value = $1
 
32
    when value == 'nil!'        then value = nil
 
33
    when value == 'non-nil!'    then value = be_nil
 
34
    when value =~ /^#\{(.*)\}$/ then value = eval($1)
 
35
    end
 
36
    value
 
37
  end
 
38
  # Converts a key: value list found in the steps into a hash.  
 
39
  # Example:
 
40
  #   ISBN: '0967539854' and comment: 'I love this book' and Quality rating: '4'
 
41
  #   # => {"quality_rating"=>"4", "isbn"=>"0967539854", "comment"=>"I love this book"}
 
42
  def to_hash_from_story
 
43
    hsh = self.split(/,? and |, /).inject({}) do |hash_so_far, key_value|
 
44
      key, value = key_value.split(":")
 
45
      if !value then warn "Couldn't understand story '#{self}': only understood up to the part '#{hash_so_far.to_yaml}'" end
 
46
      hash_so_far.merge(ToFooFromStory::fix_key(key) => ToFooFromStory::fix_value(value))
 
47
    end
 
48
  end
 
49
  # Coverts an attribute list found in the steps into an array
 
50
  # Example:
 
51
  #   login, email, updated_at, and gravatar
 
52
  #   # => ['login', 'email', 'updated_at', 'gravatar']
 
53
  def to_array_from_story
 
54
    self.split(/,? and |, /).map do |value|
 
55
      ToFooFromStory::fix_value(value)
 
56
    end
 
57
  end
 
58
end
 
59
class String
 
60
  include ToFooFromStory        
 
61
end
 
62
 
 
63
def instantize(string)
 
64
  instance_variable_get("@#{string}")
 
65
end
 
66
 
 
67
#
 
68
# Spew response onto screen -- painful but scrolling >> debugger
 
69
#
 
70
def dump_response
 
71
  # note that @request and @template won't to_yaml and that @session includes @cgi
 
72
  response_methods = response.instance_variables         - ['@request', '@template', '@cgi']
 
73
  request_methods  = response.request.instance_variables - ['@session_options_with_string_keys', '@cgi', '@session']
 
74
  response_methods.map!{|attr| attr.gsub(/^@/,'')}.sort!
 
75
  request_methods.map!{ |attr| attr.gsub(/^@/,'')}.sort!
 
76
  puts '', '*' * 75, 
 
77
    response.instance_values.slice(*response_methods).to_yaml,
 
78
    "*" * 75, '',
 
79
    response.request.instance_values.slice(*request_methods).to_yaml,
 
80
    "*" * 75, ''  
 
81
end