Module: Cuca::Test::Helpers

Defined in:
lib/cuca/test/helpers.rb

Overview

Some function that should help you testing your widgets and controllers

Instance Method Summary collapse

Instance Method Details

#cgi_status_to_text(status) ⇒ Object

Functional Tests



47
48
49
50
# File 'lib/cuca/test/helpers.rb', line 47

def cgi_status_to_text(status)
  require 'cgi'
  CGI::HTTP_STATUS[status] || status
end

#cgi_to_result(cgi, other = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cuca/test/helpers.rb', line 52

def cgi_to_result(cgi, other = {})
  op = cgi.out_params

  result = OpenStruct.new  
  result.status     = cgi_status_to_text(op['status'])
  result.status_cgi = op['status']
  result.type       = op['type']
  result.content    = cgi.out_content
  result.cookies    = cgi.output_cookies
  other.each { |k,v| result.send("#{k}=".intern, v) }
  result 
end

#get(target, query_parameters = {}) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/cuca/test/helpers.rb', line 72

def get(target, query_parameters= {})
  @cgi = CGIEmu.new({'PATH_INFO' => target, 
                     'QUERY_PARAMS' => query_parameters,
                     'HTTP_COOKIE' => @test_http_cookie})
  @app = Cuca::App.new
  t = measure_time { @app.cgicall(@cgi) }
  cgi_to_result(@app.cgi, :time => t)
end

#init(app_path = '/', params = {}) ⇒ Object

init the application. call this from the setup method.



16
17
18
19
20
# File 'lib/cuca/test/helpers.rb', line 16

def init(app_path = '/', params = {})
 @cgi = CGIEmu.new({'PATH_INFO' => app_path, 'QUERY_PARAMS' => params})
 @app = Cuca::App.new
 @app.load_support_files(Cuca::URLMap.new($cuca_path+'/app', app_path))
end

#measure_timeObject



66
67
68
69
70
# File 'lib/cuca/test/helpers.rb', line 66

def measure_time
  t = Time.now.to_f
  yield
  Time.now.to_f - t
end

#post(target, request_parameters = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cuca/test/helpers.rb', line 81

def post(target, request_parameters = {})
  require 'uri'
  body = URI.encode_www_form(request_parameters)
  @cgi = CGIEmu.new({'PATH_INFO'      => target, 
                     'REQUEST_METHOD' => 'POST', 
                     'CONTENT_LENGTH' => body.size, 
                     'CONTENT' =>  body,
                     'HTTP_COOKIE' => @test_http_cookie})
  @app = Cuca::App.new
  t = measure_time { @app.cgicall(@cgi) }
  cgi_to_result(@app.cgi, :time => t)
end

#widget(widget_class, *args, &block) ⇒ Object

this will create a widget instance with params and block as passed to this function. Also it will pass current instance variables to the assigns.



26
27
28
29
30
31
32
# File 'lib/cuca/test/helpers.rb', line 26

def widget(widget_class, *args, &block)
 a = {}
 instance_variables.each do |v|
       a[v.gsub(/\@/,'')] = self.instance_variable_get(v)
 end
 return widget_class.new({:assigns => a, :args => args}, &block)
end

#widget_p(widget_class, *args, &block) ⇒ Object

same as above but enable the profiler to stdout



35
36
37
38
39
40
41
# File 'lib/cuca/test/helpers.rb', line 35

def widget_p(widget_class, *args, &block)
 a = {}
 instance_variables.each do |v|
       a[v.gsub(/\@/,'')] = self.instance_variable_get(v)
 end
 return widget_class.new({:assigns => a, :args => args, :profiler => $stdout}, &block)
end