Class: ApiMiniTester::TestFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/api_mini_tester/test_formatter.rb

Constant Summary collapse

RESULT_SECTIONS =
i[status headers body timing].freeze
SECTION_TRANSLATE =
{
  status: "Status",
  headers: "Headers",
  body: "Body",
  timing: "Timing"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results) ⇒ TestFormatter

Returns a new instance of TestFormatter.



17
18
19
# File 'lib/api_mini_tester/test_formatter.rb', line 17

def initialize(results)
  @results = results
end

Instance Attribute Details

#resultsObject

Returns the value of attribute results.



15
16
17
# File 'lib/api_mini_tester/test_formatter.rb', line 15

def results
  @results
end

Instance Method Details

#array_in(array) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/api_mini_tester/test_formatter.rb', line 130

def array_in(array)
  list = ''
  array.each do |item|
    if item.instance_of?(Array)
      list << array_in(item)
    elsif item.instance_of?(Hash)
      list << hash_in(item)
    end
  end
  list
end

#clean_up_infinityObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/api_mini_tester/test_formatter.rb', line 118

def clean_up_infinity
  res = results.dup
  res[:scenarios].each do |scenario|
    scenario[:steps].each do |step|
      step[:timing].each do |timing|
        timing[:exp] = "Not Specified" if timing.is_a?(Hash) && timing[:exp] && timing[:exp] == Float::INFINITY
      end
    end
  end
  res
end

#failed_in_scenario(scenario) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/api_mini_tester/test_formatter.rb', line 96

def failed_in_scenario(scenario)
  count = 0
  scenario[:steps].each do |step|
    [:status, :headers, :body, :timing].each do |section|
      count += step[section].map { |res| res[:result] }.select(&:!).size
    end
  end
  count
end

#hash_in(hash) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/api_mini_tester/test_formatter.rb', line 142

def hash_in(hash)
  list = ''
  hash.each do |k, v|
    if v.instance_of?(Hash)
      list << hash_in(v)
    elsif v.instance_of?(Array)
      list << array_in(v)
    elsif k == :result
      list << (v ? '.' : 'E')
    end
  end
  list
end

#hostnameObject



106
107
108
# File 'lib/api_mini_tester/test_formatter.rb', line 106

def hostname
  Socket.gethostname
end

#md_section_content(step, section) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'lib/api_mini_tester/test_formatter.rb', line 185

def md_section_content(step, section)
  output = []
  output << md_section_header(SECTION_TRANSLATE[section], nil, 4)
  step[section].each do |status|
    output << "* `#{status[:result]}`: #{status[:desc]}"
  end
  output << ""
  output
end

#md_section_header(header, desc, level) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/api_mini_tester/test_formatter.rb', line 160

def md_section_header(header, desc, level)
  output = []
  output << "#{'#' * level} #{header}"
  output << ""
  if desc
    output << "Desc: #{desc}"
    output << ""
  end
  output
end

#md_step_header(step) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/api_mini_tester/test_formatter.rb', line 171

def md_step_header(step)
  output = []
  output.push(*md_section_header(step[:name], step[:desc], 3))
  output.push(*md_section_header("Call", nil, 4))
  step[:url].each do |url|
    output << "* #{url[:desc]}"
  end
  step[:method].each do |method|
    output << "* #{method[:desc]}"
  end
  output << ""
  output
end

#name_to_camelcase(name) ⇒ Object



110
111
112
# File 'lib/api_mini_tester/test_formatter.rb', line 110

def name_to_camelcase(name)
  name.gsub(/[[:space:]]+/, '_').downcase.gsub(/(?:^|_)([a-z])/) { $1.upcase }
end

#scenarios_countObject



64
65
66
# File 'lib/api_mini_tester/test_formatter.rb', line 64

def scenarios_count
  results[:scenarios].size
end

#step_timing(step) ⇒ Object



114
115
116
# File 'lib/api_mini_tester/test_formatter.rb', line 114

def step_timing(step)
  step[:timing].first ? step[:timing].first[:real] : 0
end

#steps_count(scenario) ⇒ Object



68
69
70
# File 'lib/api_mini_tester/test_formatter.rb', line 68

def steps_count(scenario)
  scenario[:steps].size
end

#test_count(step) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/api_mini_tester/test_formatter.rb', line 72

def test_count(step)
  count = 0
  [:status, :headers, :body, :timing].each do |section|
    count += step[section].size
  end
  count
end

#tests_in_scenario(scenario) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/api_mini_tester/test_formatter.rb', line 80

def tests_in_scenario(scenario)
  count = 0
  scenario[:steps].each do |step|
    count += test_count(step)
  end
  count
end

#time_in_scenario(scenario) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/api_mini_tester/test_formatter.rb', line 88

def time_in_scenario(scenario)
  time = 0.0
  scenario[:steps].each do |step|
    time += step[:timing].first[:real]
  end
  time
end

#timestampObject



60
61
62
# File 'lib/api_mini_tester/test_formatter.rb', line 60

def timestamp
  Time.now.strftime("%FT%T%:z")
end

#to_jsonObject



21
22
23
# File 'lib/api_mini_tester/test_formatter.rb', line 21

def to_json
  clean_up_infinity.to_json
end

#to_junit_xmlObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/api_mini_tester/test_formatter.rb', line 29

def to_junit_xml
  xml = Builder::XmlMarkup.new(indent: 2)
  xml.instruct! :xml, encoding: "UTF-8"
  xml.testsuites do
    results[:scenarios].each do |scenario|
      xml.testsuite name: scenario[:name],
                    timestamp: timestamp,
                    hostname: hostname,
                    tests: tests_in_scenario(scenario),
                    skipped: 0,
                    failures: failed_in_scenario(scenario),
                    errors: 0,
                    time: time_in_scenario(scenario) do
        scenario[:steps].each do |step|
          classname = name_to_camelcase(step[:name])
          RESULT_SECTIONS.each do |section|
            step[section].each do |s|
              xml.testcase classname: classname,
                          name: s[:name],
                          time: step_timing(step),
                          file: "./#{classname}.rb"  do
                s[:result] ? nil : xml.failure(message: s[:desc])
              end
            end
          end
        end
      end
    end
  end
end

#to_markdownObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/api_mini_tester/test_formatter.rb', line 195

def to_markdown
  output = []
  output << md_section_header(results[:name], results[:desc], 1)
  results[:scenarios].each do |scenario|
    output << md_section_header(scenario[:name], scenario[:desc], 2)
    scenario[:steps].each do |step|
      output.push(*md_step_header(step))
      RESULT_SECTIONS.each do |section|
        output.push(*md_section_content(step, section))
      end
    end
  end
  output.join("\n")
end

#to_simpleObject



156
157
158
# File 'lib/api_mini_tester/test_formatter.rb', line 156

def to_simple
  hash_in results
end

#to_yamlObject



25
26
27
# File 'lib/api_mini_tester/test_formatter.rb', line 25

def to_yaml
  clean_up_infinity.to_yaml
end