Class: Diddy::RunResultPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/diddy/run_result_printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(run_result) ⇒ RunResultPrinter

Returns a new instance of RunResultPrinter.



5
6
7
# File 'lib/diddy/run_result_printer.rb', line 5

def initialize(run_result)
  self.run_result = run_result
end

Instance Attribute Details

#run_resultObject

Returns the value of attribute run_result.



3
4
5
# File 'lib/diddy/run_result_printer.rb', line 3

def run_result
  @run_result
end

Instance Method Details

#to_htmlObject

Converts the result to HTML



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
114
115
# File 'lib/diddy/run_result_printer.rb', line 57

def to_html
  html = "<html><head><style type='text/css'>.error { color: red }; .ok { color: green }; pre { display: block; margin: 1em; };</style><title>Run results</title></head><body>"

  # walk over all scripts
  run_result.scripts.each do |script|
    html << "<h2>#{script.description}</h2>"

    # walk of the scenarios
    script.scenarios.each do |scenario|
      # result was ok? than only log the scenario itself
      if scenario.result
        html << "<h3 class='ok' style='color: green;'>#{scenario.description}</h3>"
      else
        # log the error and it's steps
        html << "<h3 class='error' style='color: red;'>#{scenario.description}</h3>"
        html << "<ul>"

        # walk over the steps
        scenario.steps.each do |step|
          # output step
          if step.result
            html << "<li>#{step.description}"
          else
            html << "<li class='error' style='color: red;'>#{step.description}"
          end


          # was there an exception?
          if step.exception
            html << "<pre>#{step.exception.message}\n\n#{step.exception.backtrace.join("\n")}</pre>"
          end

          # are there any sub steps?
          if step.sub_steps.any?
            html << "<ul>"

            # walk over sub steps
            step.sub_steps.each do |sub_step|
              if sub_step.result
                html << "<li>#{sub_step.description}</li>"
              else
                html << "<li class='error' style='color: red;'>#{sub_step.description}</li>"
              end
            end

            html << "</ul></li>"
          else
            html << "</li>"
          end
        end

        html << "</ul>"
      end
    end
  end

  html << "</body>"
  html
end

#to_sObject

Converts the result to a string



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/diddy/run_result_printer.rb', line 12

def to_s
  text = ""

  run_result.scripts.each do |script|
    text << "# #{script.description}\n"

    script.scenarios.each do |scenario|
      if scenario.result
        text << "## #{scenario.description}\n\n"
      else
        text << "## [X] #{scenario.description}\n\n"
      end

      scenario.steps.each do |step|
        if step.result
          text << "  * #{step.description}\n"
        else
          text << "  * [X] #{step.description}\n"
        end

        if step.exception
          text << "#{step.exception.message}: \n\n#{step.exception.backtrace.join("\n")}\n\n"
        end

        if step.sub_steps.any?
          step.sub_steps.each do |sub_step|
            if sub_step.result
              text << "    * #{sub_step.description}\n"
            else
              text << "    * [X] #{sub_step.description}\n"
            end
          end
        end
      end
    end
  end

  text
end