Class: CukeSniffer::Formatter
- Inherits:
-
Object
- Object
- CukeSniffer::Formatter
- Includes:
- Constants
- Defined in:
- lib/cuke_sniffer/formatter.rb
Overview
- Author
-
Robert Cochran ([email protected])
- Copyright
-
Copyright © 2014 Robert Cochran
- License
-
Distributes under the MIT License
Mixins: CukeSniffer::Constants Static class used to generate output for the CukeSniffer::CLI object.
Constant Summary
Constants included from Constants
Constants::COMMENT_REGEX, Constants::DATE_REGEX, Constants::DEFAULT_OUTPUT_FILE_NAME, Constants::FILE_IGNORE_LIST, Constants::HOOK_REGEX, Constants::HOOK_STYLES, Constants::MARKUP_SOURCE, Constants::SCENARIO_TITLE_STYLES, Constants::STEP_DEFINITION_REGEX, Constants::STEP_REGEX, Constants::STEP_STYLES, Constants::TAG_REGEX, Constants::THRESHOLDS
Class Method Summary collapse
-
.build_page(cuke_sniffer, erb_file) ⇒ Object
Returns an ERB page built up for the passed file name.
-
.console_improvement_list(improvement_list) ⇒ Object
Formats the improvement list data for summary.
-
.console_summary(name, summary) ⇒ Object
Formats the section data for a summary object.
-
.extract_markup(template_name = "markup.html.erb", markup_source = MARKUP_SOURCE) ⇒ Object
Returns the markup for a desired erb file.
-
.format_html_file_name(file_name) ⇒ Object
Assigns an html extension if one is not provided for the passed file name.
-
.get_output_summary_nodes(cuke_sniffer) ⇒ Object
Returns a string of formatted output for all the object sections of summary.
-
.output_console(cuke_sniffer) ⇒ Object
Prints out a summary of the results and the list of improvements to be made.
-
.output_html(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME, template_name = "standard_template") ⇒ Object
Creates a html file with the collected project details file_name defaults to “cuke_sniffer_results.html” unless specified Second parameter used for passing into the markup.
-
.output_min_html(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME) ⇒ Object
Creates a html file with minimum information: Summary, Rules, Improvement List.
-
.output_xml(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME) ⇒ Object
Creates a xml file with the collected project details file_name defaults to “cuke_sniffer.xml” unless specified cuke_sniffer.output_xml Or cuke_sniffer.output_xml(“cuke_sniffer01-01-0001.xml”).
-
.rules_template(cuke_sniffer) ⇒ Object
Returns the Rules erb page that utilizes sub page sections of enabled and disabled rules.
-
.sort_cuke_sniffer_lists(cuke_sniffer) ⇒ Object
Sorts all of the lists on a cuke_sniffer object to be in descending order for each objects score.
Class Method Details
.build_page(cuke_sniffer, erb_file) ⇒ Object
Returns an ERB page built up for the passed file name
62 63 64 |
# File 'lib/cuke_sniffer/formatter.rb', line 62 def self.build_page(cuke_sniffer, erb_file) ERB.new(extract_markup(erb_file)).result(binding) end |
.console_improvement_list(improvement_list) ⇒ Object
Formats the improvement list data for summary
41 42 43 44 45 46 47 |
# File 'lib/cuke_sniffer/formatter.rb', line 41 def self.console_improvement_list(improvement_list) output = " Improvements to make:\n" improvement_list.each do |improvement, count| output << " (#{count}) #{improvement}\n" end output end |
.console_summary(name, summary) ⇒ Object
Formats the section data for a summary object
33 34 35 36 37 38 |
# File 'lib/cuke_sniffer/formatter.rb', line 33 def self.console_summary(name, summary) " #{name}\n" + " Min: #{summary[:min]}\n" + " Max: #{summary[:max]}\n" + " Average: #{summary[:average]}\n" end |
.extract_markup(template_name = "markup.html.erb", markup_source = MARKUP_SOURCE) ⇒ Object
Returns the markup for a desired erb file.
115 116 117 118 119 120 121 122 |
# File 'lib/cuke_sniffer/formatter.rb', line 115 def self.extract_markup(template_name = "markup.html.erb", markup_source = MARKUP_SOURCE) markup_location = "#{markup_source}/#{template_name}" markup = "" File.open(markup_location).lines.each do |line| markup << line end markup end |
.format_html_file_name(file_name) ⇒ Object
Assigns an html extension if one is not provided for the passed file name
67 68 69 70 71 72 73 |
# File 'lib/cuke_sniffer/formatter.rb', line 67 def self.format_html_file_name(file_name) if file_name =~ /\.html$/ file_name else file_name + ".html" end end |
.get_output_summary_nodes(cuke_sniffer) ⇒ Object
Returns a string of formatted output for all the object sections of summary
24 25 26 27 28 29 30 |
# File 'lib/cuke_sniffer/formatter.rb', line 24 def self.get_output_summary_nodes(cuke_sniffer) output = "" [:features, :scenarios, :step_definitions, :hooks].each do |summary_section| output += console_summary(summary_section.to_s.gsub("_", " ").capitalize, cuke_sniffer.summary[summary_section]) end output end |
.output_console(cuke_sniffer) ⇒ Object
Prints out a summary of the results and the list of improvements to be made
13 14 15 16 17 18 19 20 21 |
# File 'lib/cuke_sniffer/formatter.rb', line 13 def self.output_console(cuke_sniffer) summary = cuke_sniffer.summary output = "Suite Summary" + " Total Score: #{summary[:total_score]}\n" + get_output_summary_nodes(cuke_sniffer) + console_improvement_list(summary[:improvement_list]) puts output end |
.output_html(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME, template_name = "standard_template") ⇒ Object
Creates a html file with the collected project details file_name defaults to “cuke_sniffer_results.html” unless specified Second parameter used for passing into the markup.
cuke_sniffer.output_html
Or
cuke_sniffer.output_html("results01-01-0001.html")
55 56 57 58 59 |
# File 'lib/cuke_sniffer/formatter.rb', line 55 def self.output_html(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME, template_name = "standard_template") cuke_sniffer = sort_cuke_sniffer_lists(cuke_sniffer) output = ERB.new(extract_markup("#{template_name}.html.erb")).result(binding) File.open(format_html_file_name(file_name), 'w') do |f| f.write(output) end end |
.output_min_html(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME) ⇒ Object
Creates a html file with minimum information: Summary, Rules, Improvement List. file_name defaults to “cuke_sniffer_results.html” unless specified Second parameter used for passing into the markup.
cuke_sniffer.output_min_html
Or
cuke_sniffer.output_min_html("results01-01-0001.html")
81 82 83 |
# File 'lib/cuke_sniffer/formatter.rb', line 81 def self.output_min_html(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME) output_html(cuke_sniffer, file_name, "min_template") end |
.output_xml(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME) ⇒ Object
Creates a xml file with the collected project details file_name defaults to “cuke_sniffer.xml” unless specified
cuke_sniffer.output_xml
Or
cuke_sniffer.output_xml("cuke_sniffer01-01-0001.xml")
95 96 97 98 99 100 101 102 103 |
# File 'lib/cuke_sniffer/formatter.rb', line 95 def self.output_xml(cuke_sniffer, file_name = DEFAULT_OUTPUT_FILE_NAME) file_name = file_name + ".xml" unless file_name =~ /\.xml$/ doc = Nokogiri::XML::Document.new doc.root = cuke_sniffer.to_xml open(file_name, "w") do |file| file << doc.serialize end end |
.rules_template(cuke_sniffer) ⇒ Object
Returns the Rules erb page that utilizes sub page sections of enabled and disabled rules
86 87 88 |
# File 'lib/cuke_sniffer/formatter.rb', line 86 def self.rules_template(cuke_sniffer) ERB.new(extract_markup("rules.html.erb")).result(binding) end |
.sort_cuke_sniffer_lists(cuke_sniffer) ⇒ Object
Sorts all of the lists on a cuke_sniffer object to be in descending order for each objects score.
106 107 108 109 110 111 112 |
# File 'lib/cuke_sniffer/formatter.rb', line 106 def self.sort_cuke_sniffer_lists(cuke_sniffer) cuke_sniffer.features = cuke_sniffer.features.sort_by { |feature| feature.total_score }.reverse cuke_sniffer.step_definitions = cuke_sniffer.step_definitions.sort_by { |step_definition| step_definition.score }.reverse cuke_sniffer.hooks = cuke_sniffer.hooks.sort_by { |hook| hook.score }.reverse cuke_sniffer.rules = cuke_sniffer.rules.sort_by { |rule| rule.score }.reverse cuke_sniffer end |