Class: Cucumber::Formatter::Pdf

Inherits:
Object
  • Object
show all
Includes:
Console, Io, FileUtils
Defined in:
lib/cucumber/formatter/pdf.rb

Constant Summary

Constants included from Console

Console::FORMATS

Constants included from ANSIColor

ANSIColor::ALIASES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Io

#ensure_dir, #ensure_file, #ensure_io

Methods included from Console

#announce, #embed, #empty_announcements, #format_step, #format_string, #print_announcement, #print_announcements, #print_counts, #print_elements, #print_exception, #print_passing_wip, #print_snippets, #print_stats, #print_steps, #print_table_row_announcements, #print_tag_limit_warnings

Methods included from ANSIColor

#cukes, define_grey, define_real_grey, #green_cukes, #grey, #red_cukes, #yellow_cukes

Methods included from Summary

#scenario_summary, #step_summary

Methods included from Duration

#format_duration

Constructor Details

#initialize(step_mother, path_or_io, options) ⇒ Pdf

Returns a new instance of Pdf.



28
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
59
# File 'lib/cucumber/formatter/pdf.rb', line 28

def initialize(step_mother, path_or_io, options)
  @step_mother = step_mother
  @file = ensure_file(path_or_io, "pdf")

  if(options[:dry_run])
    @status_colors = { :passed => BLACK, :skipped => BLACK, :undefined => BLACK, :failed => BLACK}
  else
    @status_colors = { :passed => '055902', :skipped => GREY, :undefined => 'F27405', :failed => '730202'}
  end

  @pdf = Prawn::Document.new
  @scrap = Prawn::Document.new
  @doc = @scrap
  @options = options
  @exceptions = []
  @indent = 0
  @buffer = []
  load_cover_page_image
  @pdf.text "\n\n\nCucumber features", :align => :center, :size => 32
  @pdf.text "Generated: #{Time.now.strftime("%Y-%m-%d %H:%M")}", :size => 10, :at => [0, 24]
  @pdf.text "Command: <code>cucumber #{ARGV.join(" ")}</code>", :size => 10, :at => [0,10]
  unless options[:dry_run]
    @pdf.bounding_box [450,100] , :width => 100 do  
      @pdf.text 'Legend', :size => 10
      @status_colors.each do |k,v|
        @pdf.fill_color v
        @pdf.text k.to_s, :size => 10
        @pdf.fill_color BLACK
      end
    end
  end
end

Instance Attribute Details

#indent=(value) ⇒ Object (writeonly)

Sets the attribute indent

Parameters:

  • value

    the value to set the attribute indent to.



26
27
28
# File 'lib/cucumber/formatter/pdf.rb', line 26

def indent=(value)
  @indent = value
end

Instance Method Details

#after_background(background) ⇒ Object



149
150
151
# File 'lib/cucumber/formatter/pdf.rb', line 149

def after_background(background)
  @in_background = nil
end

#after_feature(feature) ⇒ Object



104
105
106
# File 'lib/cucumber/formatter/pdf.rb', line 104

def after_feature(feature)
  flush
end

#after_feature_element(feature_element) ⇒ Object



100
101
102
# File 'lib/cucumber/formatter/pdf.rb', line 100

def after_feature_element(feature_element)
  flush
end

#after_features(features) ⇒ Object



76
77
78
79
# File 'lib/cucumber/formatter/pdf.rb', line 76

def after_features(features)
  @pdf.render_file(@file.path)
  puts "\ndone"
end

#background_name(keyword, name, file_colon_line, source_indent) ⇒ Object



188
189
190
# File 'lib/cucumber/formatter/pdf.rb', line 188

def background_name(keyword, name, file_colon_line, source_indent)
  feature_element_name(keyword, name)
end

#before_background(background) ⇒ Object



145
146
147
# File 'lib/cucumber/formatter/pdf.rb', line 145

def before_background(background)
  @in_background = true
end

#before_multiline_arg(table) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/cucumber/formatter/pdf.rb', line 153

def before_multiline_arg(table)
  return if @hide_this_step
  if(table.kind_of? Cucumber::Ast::Table)
    keep_with do
      @doc.table(table.rows, :headers => table.headers, :position => :center, :row_colors => ['ffffff', 'f0f0f0'])
    end
  end
end

#before_outline_table(table) ⇒ Object

using row_color hack to highlight each row correctly



163
164
165
166
167
168
169
# File 'lib/cucumber/formatter/pdf.rb', line 163

def before_outline_table(table)
  return if @hide_this_step
  row_colors = table.example_rows.map { |r| @status_colors[r.status] unless r.status == :skipped}
  keep_with do
    @doc.table(table.rows, :headers => table.headers, :position => :center, :row_colors => row_colors)
  end
end

#before_py_string(string) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/cucumber/formatter/pdf.rb', line 171

def before_py_string(string)
  return if @hide_this_step
  s = %{"""\n#{string}\n"""}.indent(10)
  s = s.split("\n").map{|l| l =~ /^\s+$/ ? '' : l}
  s.each do |line|
    line.gsub!('<', '&lt;')
    line.gsub!('>', '&gt;')
    keep_with { @doc.text line, :size => 8 }
  end
end

#examples_name(keyword, name) ⇒ Object



192
193
194
# File 'lib/cucumber/formatter/pdf.rb', line 192

def examples_name(keyword, name)
  feature_element_name(keyword, name)
end

#feature_element_name(keyword, name) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cucumber/formatter/pdf.rb', line 108

def feature_element_name(keyword, name)
  names = name.empty? ? [name] : name.split("\n")
  print "."
  STDOUT.flush

  keep_with do
    @doc.move_down(20)
    @doc.fill_color GREY
    @doc.text("#{keyword}", :size => 8)
    @doc.fill_color BLACK
    @doc.text("#{names[0]}", :size => 16)
    names[1..-1].each { |s| @doc.text(s, :size => 12) }
    @doc.text("\n")
  end
end

#feature_name(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cucumber/formatter/pdf.rb', line 81

def feature_name(name)
  @pdf.start_new_page
  name["Feature:"] = "" if name["Feature:"]
  names = name.split("\n")
  @pdf.fill_color GREY
  @pdf.text('Feature', :align => :center)
  @pdf.fill_color BLACK
  names.each_with_index do |nameline, i|
    case i
    when 0
      @pdf.text(nameline.strip, :size => 30, :align => :center )
      @pdf.text("\n")
    else
      @pdf.text(nameline.strip, :size => 12)
    end
  end
  @pdf.move_down(30)
end

#load_cover_page_imageObject



61
62
63
64
65
# File 'lib/cucumber/formatter/pdf.rb', line 61

def load_cover_page_image()
  if (!load_image("features/support/logo.png"))
    load_image("features/support/logo.jpg")
  end
end

#load_image(image_path) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/cucumber/formatter/pdf.rb', line 67

def load_image(image_path)
  begin
    @pdf.image open(image_path, "rb"), :position => :center, :width => 500
    true
  rescue Errno::ENOENT
    false
  end
end

#scenario_name(keyword, name, file_colon_line, source_indent) ⇒ Object



196
197
198
# File 'lib/cucumber/formatter/pdf.rb', line 196

def scenario_name(keyword, name, file_colon_line, source_indent)
  feature_element_name(keyword, name)
end

#step_name(keyword, step_match, status, source_indent, background) ⇒ Object



139
140
141
142
143
# File 'lib/cucumber/formatter/pdf.rb', line 139

def step_name(keyword, step_match, status, source_indent, background)
  return if @hide_this_step
  line = "<b>#{keyword}</b> #{step_match.format_args("%s").gsub('<', '&lt;').gsub('>', '&gt;')}"
  colorize(line, status)
end

#step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cucumber/formatter/pdf.rb', line 124

def step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
  @hide_this_step = false
  if exception
    if @exceptions.include?(exception)
      @hide_this_step = true
      return
    end
    @exceptions << exception
  end
  if status != :failed && @in_background ^ background
    @hide_this_step = true
    return
  end
end

#tag_name(tag_name) ⇒ Object



182
183
184
185
186
# File 'lib/cucumber/formatter/pdf.rb', line 182

def tag_name(tag_name)
  return if @hide_this_step
  tag = format_string(tag_name, :tag).indent(@indent)
  # TODO should we render tags at all? skipped for now. difficult to place due to page breaks
end