Class: Webpoke::Test
- Inherits:
-
Object
- Object
- Webpoke::Test
- Defined in:
- lib/Webpoke/Test.rb
Overview
A test
Instance Method Summary collapse
- #default_success(code, body) ⇒ Object
- #dependant? ⇒ Boolean
- #depends_on(otherTest) ⇒ Object
-
#describe ⇒ Object
Returns the test description definition (so that we can auto-generate documentation).
- #did_run? ⇒ Boolean
-
#initialize(&block) ⇒ Test
constructor
A new instance of Test.
- #on(response, &block) ⇒ Object
-
#passed?(response, body) ⇒ Boolean
Run the test.
- #pt(v) ⇒ Object
- #should_parse? ⇒ Boolean
- #success(&block) ⇒ Object
-
#to_s ⇒ Object
Returns the test description.
Constructor Details
#initialize(&block) ⇒ Test
Returns a new instance of Test.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/Webpoke/Test.rb', line 8 def initialize(&block) @parse = true @on_success = []; instance_eval(&block); @group = @group || '' @method = @method || 'get' @headers = @headers || {} @on_success = []; @ran = false; end |
Instance Method Details
#default_success(code, body) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/Webpoke/Test.rb', line 42 def default_success (code, body) if @should_fail return code > 399 else return (200..226).include?(code) end end |
#dependant? ⇒ Boolean
115 116 117 |
# File 'lib/Webpoke/Test.rb', line 115 def dependant? @dependant end |
#depends_on(otherTest) ⇒ Object
37 38 39 40 |
# File 'lib/Webpoke/Test.rb', line 37 def depends_on otherTest otherTest.on_success << self self.dependant = true; end |
#describe ⇒ Object
Returns the test description definition (so that we can auto-generate documentation)
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 |
# File 'lib/Webpoke/Test.rb', line 85 def describe query = [] if (@query) @query.each do |k,v| query << {key: k, v:self.pt(v)} end end data = [] if (@data) @data.each do |k,v| data << {key: k, v:self.pt(v)} end end return { description: @description, url: @url, method: @method, query: query, headers: @headers, data: data, sampleOutput: @sampleOutput, metadata: @metadata } end |
#did_run? ⇒ Boolean
23 24 25 |
# File 'lib/Webpoke/Test.rb', line 23 def did_run? @ran end |
#on(response, &block) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/Webpoke/Test.rb', line 27 def on (response, &block) if (response == 'success') @on_success << block else @on_error << block end end |
#passed?(response, body) ⇒ Boolean
Run the test
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/Webpoke/Test.rb', line 123 def passed? (response, body) @ran = true @response = { body: body, code: response } if !self.default_success(response, body) return false end if @success begin result = @success.call(response, body) rescue Exception => e result = false raise Webpoke::TestSuccessError.new(e., e) end else result = self.default_success(response, body) end return result end |
#pt(v) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/Webpoke/Test.rb', line 64 def pt(v) clase = v.class.to_s case clase when 'Fixnum' clase = 'Int' when 'Hash' v.each do |k,nv| v[k] = self.pt(nv) end return v when 'TrueClass', 'FalseClass' clase = 'Boolean' end clase.downcase end |
#should_parse? ⇒ Boolean
19 20 21 |
# File 'lib/Webpoke/Test.rb', line 19 def should_parse? return @parse end |
#success(&block) ⇒ Object
50 51 52 |
# File 'lib/Webpoke/Test.rb', line 50 def success (&block) @success = block end |
#to_s ⇒ Object
Returns the test description
59 60 61 |
# File 'lib/Webpoke/Test.rb', line 59 def to_s @description end |