module GoldenRose class TestableSummary def initialize(testable_summary) @testable_summary = testable_summary end def items @items ||= compact_results(iterate_subtests(tests)) end def tests raise GeneratingError, 'Tests not present.' unless @testable_summary['Tests'] @testable_summary['Tests'] end def name @testable_summary['TestName'] end def formatted_time hours = total_time / (60 * 60) minutes = (total_time / 60) % 60 seconds = total_time % 60 "".tap do |time| time << "#{pluralized(hours, 'hour')}, " if hours > 0 time << "#{pluralized(minutes, 'minute')} and " if minutes > 0 time << "#{pluralized(seconds, 'second')}" end end def failures_count @failures_count ||= items.reduce(0) { |count, item| count + item[:failures_count].to_i } end def total_tests_count @total_tests_count ||= items.reduce(0) do |count, item| count + (item[:subtests] ? item[:subtests].count : 0) end end def passing_count total_tests_count - failures_count end def total_time @total_time ||= items.reduce(0) do |time, item| subtests_time = if item[:subtests] item[:subtests].reduce(0) { |time, item| time + item[:time].to_f } else 0 end time + subtests_time end.round end def pluralized(number, noun) "#{number} #{noun}#{'s' if number != 1}" end private def iterate_subtests(items) items.map do |subtest| child_subtests = subtest["Subtests"] next if child_subtests && child_subtests.empty? if child_subtests ParentItem.new(subtest).tap do |item| item.subtests = iterate_subtests(child_subtests) end.to_h else ChildItem.new(subtest).to_h end end.compact end def compact_results(items) items.map do |subtest| subtests = subtest[:subtests] if subtests.any?{|sbt| sbt[:subtests]} compact_results(subtests) else subtest end end.flatten.compact end end end