Module: Futest::Helpers
- Defined in:
- lib/futest/helpers.rb
Constant Summary collapse
- CMD =
{ :a? => 'is', :a => 'is', :eq => '==', :ne => '!=', :gt => '>', :gte => '>=', :lt => '<', :lte => '<=', :in => 'in', :nin => 'nin', :has => 'has' }
Instance Method Summary collapse
-
#err(*args) ⇒ Object
Print error message.
-
#green(s) ⇒ Object
Colorize input green.
-
#is(v1, v2 = :udef, n = line(caller)) ⇒ Object
Equality tester.
-
#line(q) ⇒ Object
Get the line number.
-
#out(s, c = :green) ⇒ Object
Colorize output, 33 is :green (default), 31 is :red.
-
#pull(*args) ⇒ Object
Pulls any data source and returns the response.
-
#red(s) ⇒ Object
Colorize input red.
-
#show ⇒ Object
Show the last @body in the browser.
-
#stop(str, model = nil, n = line(caller)) ⇒ Object
Prints error message and stops execution.
-
#test(*args) ⇒ Object
Prints the test and runs setup methods.
Instance Method Details
#err(*args) ⇒ Object
Print error message
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/futest/helpers.rb', line 149 def err(*args) x = args[0] # Pass :v or :vv to print more information about the error puts x.backtrace.join("\n") if args.include?(:vv) puts x. if args.include?(:v) # Normally just print the first line in the message x.backtrace.first.match(/(\/.+\/.*.rb):(\d{1,9}):/) stop(%{#{x.}\n=> ~/#{$1.split('/')[3..-1].join('/')}}, nil, $2) if $1 and $2 # Print more if the backtrace doesn't match stop(%{#{x.}\n=> #{x.backtrace[0..60].join("\n")}}) end |
#green(s) ⇒ Object
Colorize input green
143 |
# File 'lib/futest/helpers.rb', line 143 def green(s); out(s); end |
#is(v1, v2 = :udef, n = line(caller)) ⇒ Object
Equality tester
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 |
# File 'lib/futest/helpers.rb', line 38 def is(v1, v2 = :udef, n = line(caller)) # Use :a? if v2 is not a string and looks like a class v2 = {:a? => v2} if !v2.is_a?(String) and v2.to_s[0] =~ /[A-Z]/ # Use :eq if v2 is defined and it's not a Hash v2 = {:eq => v2} if v2 != :udef and !v2.is_a?(Hash) # Symbolize keys k, v = (v2.is_a?(Hash) ? v2 : {}).inject({}){|q,(k,v)|q[k.to_sym] = v; q}.to_a.flatten # Extract values s = ["#{v1.class} #{v1} #{CMD[k]} #{v.class} #{v}", nil, n] # Set @debug = true to print info puts s.join('-') if @debug # Stop unless true stop(*s) unless case k when nil then !!v1 when :eq then v1 == v when :ne then v1 != v when :gt then v1 > v when :gte then v1 >= v when :lt then v1 < v when :lte then v1 <= v when :in then v2[:in].include?(v1) when :nin then !v2[:nin].include?(v1) when :has then v1.has_key?(v2[:has]) when :a?, :a then v1.is_a?(v) else false end end |
#line(q) ⇒ Object
Get the line number
165 166 167 |
# File 'lib/futest/helpers.rb', line 165 def line(q) q.first.split(':')[1] end |
#out(s, c = :green) ⇒ Object
Colorize output, 33 is :green (default), 31 is :red
138 139 140 |
# File 'lib/futest/helpers.rb', line 138 def out(s, c = :green) z = {:green => 33, :red => 31}; %{\e[#{z[c] || c}m#{s}\e[0m} end |
#pull(*args) ⇒ Object
Pulls any data source and returns the response
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 |
# File 'lib/futest/helpers.rb', line 78 def pull(*args) # Define $host in your test helper # or @host before you call pull # set @base to add paths to @host @host ||= $host @base ||= ($base || '') stop('@host not defined') unless @host @method = args[0].is_a?(Symbol) ? args.delete_at(0) : :get @path = (args[0] || '/') @params = (args[1] || {}) @headers = (args[2] || {}) @headers.merge!(:cookies => @cookies) if @cookies args.each do |a| @params.merge!(a) if a.is_a?(Hash) @method = a if a.is_a?(Symbol) @path = a if a.is_a?(String) end # Add host and base to url @url = @host + (@base || '') + @path o = { :method => @method, :url => @url, :timeout => 2, :payload => @params, :headers => @headers } RestClient::Request.execute(o){|z| @page = z} # Make result available in instance variables [:code, :cookies, :headers, :history].each{|i| instance_variable_set("@#{i}", @page.send(i))} @raw = @page.raw_headers @body = @page.body end |
#red(s) ⇒ Object
Colorize input red
146 |
# File 'lib/futest/helpers.rb', line 146 def red(s); out(s, :red); end |
#show ⇒ Object
Show the last @body in the browser
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/futest/helpers.rb', line 116 def show stop('@body is not defined') unless @body # Add @host and @base to all links in HTML to fetch CSS and images @body.scan(/(<.*(src|href)=["'](\/?.+)["'].*>)/).each do |m| @body.gsub!(m[0], m[0].gsub(m[2], "#{@host}#{@base}#{m[2][0] == '/' ? m[2] : "/#{m[2]}"}")) unless %w[ht //].include?(m[2][0..1]) end # Write body to tmp file name = "/tmp/#{Time.now.to_i}_fushow.html" File.open(name, 'w'){|f| f.write(@body)} # Open with default browser, set Futest.show to change this `#{Futest.show} #{name}` end |
#stop(str, model = nil, n = line(caller)) ⇒ Object
Prints error message and stops execution
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/futest/helpers.rb', line 18 def stop(str, model = nil, n = line(caller)) m = "#{n}: #{str}" # Support for errors when using Object DB ORM if model and model.errors and model.errors.any? q = model.errors. rescue model.errors m += ":\n=> " + q.each{|k, v| q[k] = v.join(', ')}.to_json[1..-2].gsub('","', '", "') end puts red(%{#{m}\n}); exit(0) end |
#test(*args) ⇒ Object
Prints the test and runs setup methods
31 32 33 34 35 |
# File 'lib/futest/helpers.rb', line 31 def test(*args) n = args[-1].is_a?(Integer) ? args[-1] : line(caller) args.select{|r| r.is_a?(Symbol)}.each{|k| send(k)} puts green("#{n}: #{args[0]}") end |