josephwilk (owner)

Revisions

gist: 75378 Download_button fork
public
Description:
Pdf formatter for Cucumber 0.1.99
Public Clone URL: git://gist.github.com/75378.git
Embed All Files: show embed
pdf_formatter.rb #
1
2
3
4
5
6
7
8
9
10
11
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
51
52
53
54
55
56
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
116
117
118
119
120
121
122
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
148
149
150
151
152
153
154
#cucumber --require pdf_formatter.rb --quiet --dry-run --format Cucumber::Formatter::Pdf features/
 
require 'cucumber/formatter/console'
require 'prawn'
 
class PdfIO
 
  def initialize
    @pdf = Prawn::Document.new :page_size => "A4", :top_margin => 60
  end
 
  def tty?
    false
  end
 
  def method_missing(method, *args)
    @pdf.send(method, *args)
  end
 
end
 
module Cucumber
  module Formatter
    class Pdf < Ast::Visitor
      include Console
      attr_writer :indent
 
      FEATURE_TITLE_STYLE = {:size => 24}
      SCENARIO_TITLE_STYLE = {:size => 16}
      STEP_STYLE = {}
      FOOTER_STYLE = {:size => 12}
      DOC_TITLE_STYLE = {:size => 32, :align => :center}
 
      def initialize(step_mother, io, options, delim='|')
        super(step_mother)
        @io = PdfIO.new
        @options = options
        @delim = delim
        @indent = 0
      end
 
      def visit_features(features)
        super
        @io.render_file("features.pdf")
      end
 
      def visit_feature(feature)
        @indent = 0
        with_color do
          feature.accept(self)
        end
      end
 
      def visit_comment(comment)
        comment.accept(self)
      end
 
      def visit_comment_line(comment_line)
        unless comment_line.blank?
          @io.text(comment_line.indent(@indent))
        end
      end
 
      def visit_tags(tags)
        tags.accept(self)
        if @indent == 1
          @io.text "\n"
        end
      end
 
      def visit_tag_name(tag_name)
        tag = format_string("@#{tag_name}", :tag).indent(@indent)
        @io.text(tag)
        @indent = 1
      end
 
      def visit_feature_name(name)
        name, *narrative = name.split("\n")
        @io.text(name, FEATURE_TITLE_STYLE)
        @io.text(narrative.join("\n")) unless narrative.nil?
        @io.text("\n")
      end
 
      def visit_feature_element(feature_element)
        @indent = 2
        @last_undefined = feature_element.undefined?
        feature_element.accept(self)
        @io.text("\n")
      end
 
      def visit_background(background)
        @indent = 2
        background_already_visible = background.already_visited_steps?
        background.accept(self)
        @io.text("\n") unless background_already_visible
      end
 
      def visit_examples(examples)
        examples.accept(self)
      end
 
      def visit_examples_name(keyword, name)
        @io.text("\n #{keyword} #{name}")
        @indent = 4
      end
 
      def visit_scenario_name(keyword, name, file_line, source_indent)
        line = " #{keyword} #{name}"
        line = format_string(line, :undefined) if @last_undefined
        @io.text(line, SCENARIO_TITLE_STYLE)
        @io.text("\n")
      end
 
      def visit_step(step)
        @indent = 6
        exception = step.accept(self)
        print_exception(exception, @indent) if exception
      end
 
      def visit_step_name(keyword, step_name, status, step_definition, source_indent)
        source_indent = nil unless @options[:source]
        formatted_step_name = format_step(keyword, step_name, status, step_definition, source_indent)
        @io.text(" " + formatted_step_name)
      end
 
      def visit_multiline_arg(multiline_arg, status)
        return if @options[:no_multiline]
        multiline_arg.accept(self, status)
      end
 
      def visit_table_row(table_row, status)
        @io.text(@delim.indent(@indent))
        exception = table_row.accept(self, status)
        @io.text("\n")
        print_exception(exception, 6) if exception
      end
 
      def visit_py_string(string, status)
        s = "\"\"\"\n#{string}\n\"\"\"".indent(@indent)
        s = s.split("\n").map{|l| l =~ /^\s+$/ ? '' : l}.join("\n")
        @io.text(format_string(s, status))
      end
 
      def visit_table_cell(table_cell, status)
        table_cell.accept(self, status)
      end
 
      def visit_table_cell_value(value, width, status)
        @io.text(' ' + format_string((value.to_s || '').ljust(width), status) + " #{@delim}")
      end
 
    end
  end
end