require'cucumber/formatter/console'require'fileutils'moduleCucumbermoduleFormatter# This formatter prints features to plain text - exactly how they were parsed,# just prettier. That means with proper indentation and alignment of table columns.## If the output is STDOUT (and not a file), there are bright colours to watch too.#classPretty<Ast::VisitorincludeFileUtilsincludeConsoleattr_writer:indentdefinitialize(step_mother,io,options,delim='|')super(step_mother)@io=io@options=options@delim=delim@indent=0@exceptions=[]enddefvisit_features(features)superprint_summaryunless@options[:autoformat]enddefvisit_feature(feature)@indent=0if@options[:autoformat]file=File.join(@options[:autoformat],feature.file)dir=File.dirname(file)mkdir_p(dir)unlessFile.directory?(dir)File.open(file,Cucumber.file_mode('w'))do|io|@io=iosuperendelsesuperendenddefvisit_comment(comment)comment.accept(self)enddefvisit_comment_line(comment_line)unlesscomment_line.blank?@io.puts(comment_line.indent(@indent))@io.flushendenddefvisit_tags(tags)tags.accept(self)if@indent==1@io.puts@io.flushendenddefvisit_tag_name(tag_name)tag=format_string("@#{tag_name}",:tag).indent(@indent)@io.print(tag)@io.flush@indent=1enddefvisit_feature_name(name)@io.puts(name)@io.puts@io.flushenddefvisit_feature_element(feature_element)@indent=2super@io.puts@io.flushenddefvisit_background(background)@indent=2@in_background=truesuper@in_background=nil@io.puts@io.flushenddefvisit_background_name(keyword,name,file_colon_line,source_indent)visit_feature_element_name(keyword,name,file_colon_line,source_indent)enddefvisit_examples_name(keyword,name)names=name.empty??[name]:name.split("\n")@io.puts("\n#{keyword}#{names[0]}")names[1..-1].each{|s|@io.puts" #{s}"}@io.flush@indent=6enddefvisit_scenario_name(keyword,name,file_colon_line,source_indent)visit_feature_element_name(keyword,name,file_colon_line,source_indent)enddefvisit_feature_element_name(keyword,name,file_colon_line,source_indent)names=name.empty??[name]:name.split("\n")line=" #{keyword}#{names[0]}"@io.print(line)if@options[:source]line_comment=" # #{file_colon_line}".indent(source_indent)@io.print(format_string(line_comment,:comment))end@io.putsnames[1..-1].each{|s|@io.puts" #{s}"}@io.flushenddefvisit_step(step)@indent=6superenddefvisit_step_result(keyword,step_match,multiline_arg,status,exception,source_indent,background)ifexceptionreturnif@exceptions.index(exception)@exceptions<<exceptionendreturnifstatus!=:failed&&@in_background^background@status=statussuperenddefvisit_step_name(keyword,step_match,status,source_indent,background)source_indent=nilunless@options[:source]formatted_step_name=format_step(keyword,step_match,status,source_indent)@io.puts(" "+formatted_step_name)enddefvisit_multiline_arg(multiline_arg)returnif@options[:no_multiline]superenddefvisit_exception(exception,status)print_exception(exception,status,@indent)@io.flushenddefvisit_table_row(table_row)@io.print@delim.indent(@indent)super@io.putsiftable_row.exception&&!@exceptions.index(table_row.exception)print_exception(table_row.exception,:failed,@indent)endenddefvisit_py_string(string)s=%{"""\n#{string}\n"""}.indent(@indent)s=s.split("\n").map{|l|l=~/^\s+$/?'':l}.join("\n")@io.puts(format_string(s,@status))@io.flushenddefvisit_table_cell_value(value,width,status)status||=@status||:passed@io.print(' '+format_string((value.to_s||'').ljust(width),status)+::Term::ANSIColor.reset(" #{@delim}"))@io.flushendprivatedefprint_summaryprint_countsprint_snippets(@options)endendendend