require_relative 'scenario' module Sweet module Format module Cucumber class Feature class << self def from_output(data) data = JSON.parse(data, symbolize_names: true) if data.is_a? String backgrounds = [] scenarios = [] data[:elements].each do |element| scenario = Scenario.from_output element scenario.file_path = data[:uri] if scenario.is_background? unless backgrounds.any? do |bg| bg.line_number == scenario.line_number end backgrounds << scenario end else scenarios << scenario end end tags = (data[:tags].map { |tag| tag[:name] } if data[:tags]) new file_path: data[:uri], keyword: data[:keyword], name: data[:name], description: data[:description], tags: tags, backgrounds: backgrounds, scenarios: scenarios end end attr_accessor :file_path, :keyword, :name, :description, :tags, :scenarios, :backgrounds attr_accessor :passed, :failed, :pending, :skipped def initialize(opts = {}) @file_path = opts[:file_path] @keyword = opts[:keyword] @name = opts[:name] @description = opts[:description] @tags = opts[:tags] || [] @passed = [] @failed = [] @pending = [] @skipped = [] @backgrounds = opts[:backgrounds] || [] @scenarios = opts[:scenarios] || [] [@backgrounds, @scenarios].each do |scenarios| scenarios.map! do |scenario| if scenario.is_a? Hash Scenario.new scenario else scenario end scenario.file_path = @file_path scenario end end organize_scenarios end def duration [@scenarios, @backgrounds].flatten.inject(0.0) do |sum, scenario| sum += scenario.duration end end def total_tests @scenarios.count end def total_passed @passed.count end def total_failed @failed.count end def total_pending @pending.count end def total_skipped @skipped.count end private def organize_scenarios background_failed = false @backgrounds.each do |background| background_failed = true if background.failed? end @scenarios.each do |scenario| if background_failed @failed << scenario if scenario.status == :passed scenario.status = :failed scenario.duration = 0.0 end next end case scenario.status when :passed @passed << scenario when :failed @failed << scenario when :pending @pending << scenario when :skipped @skipped << scenario end end end end end end end