Class: Mutx::View::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/mutx/view/parser.rb

Class Method Summary collapse

Class Method Details

.adapt_to_mutx(source, result) ⇒ Object

Parses cucumber report html doc Divides in each parts of document

Parameters:

  • source (String)

    the html code

  • the (Mutx::Result)

    result object



10
11
12
13
14
15
16
# File 'lib/mutx/view/parser.rb', line 10

def self.adapt_to_mutx(source, result)
    source.gsub! 'Collapse All</p></div></div></div>',"
      Collapse All</p></div></div></div>
        <div>#{mutx_info(result)}</div>"
  # end
  source.inspect
end

.extract_summary(source) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/mutx/view/parser.rb', line 31

def self.extract_summary source
  if no_scenario_but_green?(source) # 0 scenarios executed
    get_elapsed_time_for_zero(source)
  elsif has_scenarios_executed?(source) # scenarios executed > 0
    get_scenarios_summary(source)
  else
    "running"
  end
end

.feature_html(content = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/mutx/view/parser.rb', line 78

def self.feature_html content=nil
  html = "<table>
  "
  content.each_line do |line|
    html += "#{self.resolve_style(line)}
    "
  end
  html += "</table>"
end

.finished_statement?(source) ⇒ Boolean

Checks if report says that execution is finised

Parameters:

  • source (String)

    the entire report html code

Returns:

  • (Boolean)

    true if report says finished



51
52
53
# File 'lib/mutx/view/parser.rb', line 51

def self.finished_statement? source
  !(source =~ /Finished in .*s seconds/).nil?
end

.get_elapsed_time_for_zero(source) ⇒ Object



70
71
72
# File 'lib/mutx/view/parser.rb', line 70

def self.get_elapsed_time_for_zero(source)
  "0 Scenarios " + source.scan(/Finished in <strong>0m0.000s seconds/).first.gsub("<strong>","")
end

.get_scenarios_summary(source) ⇒ Object



74
75
76
# File 'lib/mutx/view/parser.rb', line 74

def self.get_scenarios_summary(source)
  source.scan(/\d+\sscenario.*\)/).first.gsub(/\<.+\>/,' - ')
end

.get_status(source) ⇒ Object

Returns the status present on html cucumber report

Parameters:

  • the (String)

    entire report html code



44
45
46
# File 'lib/mutx/view/parser.rb', line 44

def self.get_status(source)
   source.scan(/\d\sscenarios?\s\(\d+\s(\w+)/i).flatten.first if finished_statement?(source)
end

.has_scenarios_executed?(source) ⇒ Boolean

Checks if has scenario results summary

Parameters:

  • the (String)

    entire report html source code

Returns:

  • (Boolean)

    true if there is scenarios executed



65
66
67
# File 'lib/mutx/view/parser.rb', line 65

def self.has_scenarios_executed? source
  (source.include? "scenario") and (source.scan(/\d+\sscenario.*\)/).size > 0)
end

.mutx_info(result) ⇒ String

This is the information added to html report

Parameters:

Returns:

  • (String)

    the html to be added



21
22
23
24
25
26
27
28
29
# File 'lib/mutx/view/parser.rb', line 21

def self.mutx_info(result)
  info  = "<h2><strong>Task:</strong> #{result.task_name}</h2>"
  info += "<h4>Execution name: #{result.execution_name.gsub('-_-',' ')}</h4>" unless result.execution_name.empty?
  info += "<h4>Command: #{result.command}</h4>"
  #info += "<h4>Custom Params: #{result.custom_params_values.split('=').last}</h4>"
  info += "<h4>Started: #{result.started_at_formatted}</h4>"
  info += "<h4>Commit ID: #{result.git_log.split('\n').first}</h4>"
  info += "<h4><input type='button' onclick='window.close();' value='Close this window' />"
end

.no_scenario_but_green?(source) ⇒ Boolean

Checks if result report is about no scenarios executed (scenarios not found)

Parameters:

  • the (String)

    entire report html source code

Returns:

  • (Boolean)

    true if is finished (green) and no scenarios executed (It means that is empty. No scenarios executed)



58
59
60
# File 'lib/mutx/view/parser.rb', line 58

def self.no_scenario_but_green?(source)
  source.include? '0m0.000s seconds'
end

.resolve_style(line) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mutx/view/parser.rb', line 88

def self.resolve_style line
  line = line.gsub("<","&lt;").gsub(">","&gt;").gsub("'","").gsub('"',"").gsub("\n","<br>").gsub("&lt;","<b>&lt;").gsub("&gt;","&gt;</b>")
  styled = case line
  when /#.*/
    "<tr style='color:grey;'><td>#{line}</td></tr>"
  when /^Característica|Feature.*/
    "<tr style='color:black;'><td><b>#{line}</b></td></tr>"
  when /^Given|When|Then|And|But|\*|Dado|Cuando|Entonces|Y|Pero.*/
    "<tr style='color:blue;'><td>&nbsp;&nbsp;&nbsp;&nbsp;#{line}</td></tr>"
  when /^Scenario|Escenario|Esquema|Antecedentes|Background.*/
    "<tr style='color:red;'><td><b>#{line}</b></td></tr>"
  when /^Examples|Ejemplo.*/
    "<tr style='color:red;'><td><b>&nbsp;&nbsp;&nbsp;#{line}</b></td></tr>"
  when /\|.*/
    "<tr style='color:red;'><td>&nbsp;&nbsp;&nbsp;#{line}</td></tr>"
  when /^@.*/
    "<tr><td><br></td></tr><tr style='color:green;'><td>#{line}</td></tr>"
  else
    "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;#{line}</td></tr>"
  end
end