Permalink
Browse files

add command-line 'j' or 'json' formatter option

  • Loading branch information...
1 parent 972591b commit dcfaa67b6886869e6fcb2878ca5e5a1c6e825806 @alexch alexch committed May 23, 2012
@@ -0,0 +1,22 @@
+Feature: JSON formatter
+
+ Scenario: Formatting example names for retry
+ Given a file named "various_spec.rb" with:
+ """
+ describe "Various" do
+ it "fails" do
+ "fail".should eq("succeed")
+ end
+
+ it "succeeds" do
+ "succeed".should eq("succeed")
+ end
+
+ it "pends"
+ end
+ """
+ When I run `rspec various_spec.rb --format j`
+ Then the output should contain all of these:
+ |"summary_line":"3 examples, 1 failure, 1 pending"}|
+ |{"examples":[{"description":"fails","full_description":"Various fails","status":"failed","file_path":"./various_spec.rb","line_number":2,"exception":|
+ And the exit status should be 1
@@ -843,6 +843,9 @@ def built_in_formatter(key)
when 'p', 'progress'
require 'rspec/core/formatters/progress_formatter'
RSpec::Core::Formatters::ProgressFormatter
+ when 'j', 'json'
+ require 'rspec/core/formatters/json_formatter'
+ RSpec::Core::Formatters::JsonFormatter
end
end
@@ -87,7 +87,7 @@ def stop
# This method is invoked after all of the examples have executed. The next method
# to be invoked after this one is #dump_failures
- # (BaseTextFormtter then calls #dump_failure once for each failed example.)
+ # (BaseTextFormatter then calls #dump_failure once for each failed example.)
def start_dump
end
@@ -18,31 +18,6 @@ def message(message)
(@output_hash[:messages] ||= []) << message
end
- # def example_started(example)
- # super
- # end
- #
- # def example_passed(example)
- # end
- #
- # def example_pending(example)
- # end
- #
- # def example_failed(example)
- # end
-
-
- # def dump_failures
- # return if failed_examples.empty?
- # output.puts
- # output.puts "Failures:"
- # failed_examples.each_with_index do |example, index|
- # output.puts
- # pending_fixed?(example) ? dump_pending_fixed(example, index) : dump_failure(example, index)
- # dump_backtrace(example)
- # end
- # end
-
def dump_summary(duration, example_count, failure_count, pending_count)
super(duration, example_count, failure_count, pending_count)
@output_hash[:summary] = {
@@ -57,46 +32,13 @@ def dump_summary(duration, example_count, failure_count, pending_count)
dump_profile if profile_examples? && failure_count == 0
end
- # stolen from BaseTextFormatter
def summary_line(example_count, failure_count, pending_count)
summary = pluralize(example_count, "example")
summary << ", " << pluralize(failure_count, "failure")
summary << ", #{pending_count} pending" if pending_count > 0
summary
end
- # def dump_profile
- # sorted_examples = examples.sort_by { |example| example.execution_result[:run_time] }.reverse.first(10)
- # output.puts "\nTop #{sorted_examples.size} slowest examples:\n"
- # sorted_examples.each do |example|
- # output.puts " #{example.full_description}"
- # output.puts cyan(" #{red(format_seconds(example.execution_result[:run_time]))} #{red("seconds")} #{format_caller(example.location)}")
- # end
- # end
-
- # def dump_pending
- # unless pending_examples.empty?
- # output.puts
- # output.puts "Pending:"
- # pending_examples.each do |pending_example|
- # output.puts yellow(" #{pending_example.full_description}")
- # output.puts cyan(" # #{pending_example.execution_result[:pending_message]}")
- # output.puts cyan(" # #{format_caller(pending_example.location)}")
- # if pending_example.execution_result[:exception] \
- # && RSpec.configuration.show_failures_in_pending_blocks?
- # dump_failure_info(pending_example)
- # dump_backtrace(pending_example)
- # end
- # end
- # end
- # end
-
- # def seed(number)
- # output.puts
- # output.puts "Randomized with seed #{number}"
- # output.puts
- # end
-
def stop
super
@output_hash[:examples] = examples.map do |example|
@@ -125,57 +67,6 @@ def close
output.close if IO === output && output != $stdout
end
- protected
-
-
- private
-
- # def format_caller(caller_info)
- # backtrace_line(caller_info.to_s.split(':in `block').first)
- # end
- #
- # def dump_backtrace(example)
- # format_backtrace(example.execution_result[:exception].backtrace, example).each do |backtrace_info|
- # output.puts cyan("#{long_padding}# #{backtrace_info}")
- # end
- # end
- #
- # def dump_pending_fixed(example, index)
- # output.puts "#{short_padding}#{index.next}) #{example.full_description} FIXED"
- # output.puts blue("#{long_padding}Expected pending '#{example.metadata[:execution_result][:pending_message]}' to fail. No Error was raised.")
- # end
- #
- # def pending_fixed?(example)
- # example.execution_result[:exception].pending_fixed?
- # end
- #
- # def dump_failure(example, index)
- # output.puts "#{short_padding}#{index.next}) #{example.full_description}"
- # dump_failure_info(example)
- # end
- #
- # def dump_failure_info(example)
- # exception = example.execution_result[:exception]
- # output.puts "#{long_padding}#{red("Failure/Error:")} #{red(read_failed_line(exception, example).strip)}"
- # output.puts "#{long_padding}#{red(exception.class.name << ":")}" unless exception.class.name =~ /RSpec/
- # exception.message.split("\n").each { |line| output.puts "#{long_padding} #{red(line)}" } if exception.message
- # if shared_group = find_shared_group(example)
- # dump_shared_failure_info(shared_group)
- # end
- # end
- #
- # def dump_shared_failure_info(group)
- # output.puts "#{long_padding}Shared Example Group: \"#{group.metadata[:shared_group_name]}\" called from " +
- # "#{backtrace_line(group.metadata[:example_group][:location])}"
- # end
- #
- # def find_shared_group(example)
- # group_and_ancestors(example).find {|group| group.metadata[:shared_group_name]}
- # end
- #
- # def group_and_ancestors(example)
- # example.example_group.ancestors + [example.example_group]
- # end
end
end
end
Oops, something went wrong.

0 comments on commit dcfaa67

Please sign in to comment.