Sha256: 10e362e5d298838a5b49ec1d3cd90a5165e5428de0dfc2210f6ef02637844b0a

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

module QUnited

  # Contains results data from a QUnit JavaScript test. Useful for passing data
  # to formatters.
  class QUnitTestResult
    class AssertionResult
      attr_accessor :data

      def initialize(assertion_data)
        @data = assertion_data
      end

      def message
        data[:message] || 'Failed assertion, no message given.'
      end

      def result
        if data[:result]
          :passed
        else
          data[:message] =~ /^Died on test/ ? :error : :failed
        end
      end

      def passed?; result == :passed end
      def failed?; result == :failed end
      def error?;  result == :error end

      [:expected, :actual].each do |prop|
        define_method(prop) do
          data[prop]
        end
      end
    end

    def self.from_json(json)
      self.new clean_up_result(YAML.load(json))
    end

    attr_accessor :data

    def initialize(test_data)
      @data = test_data
    end

    def passed?; result == :passed end
    def failed?; result == :failed end
    def error?;  result == :error end

    def result
      @result ||= if assertions.find { |a| a.error? }
        :error
      else
        assertions.find { |a| a.failed? } ? :failed : :passed
      end
    end

    def assertions
      @assertions ||= data[:assertion_data].map { |assertion_data| AssertionResult.new assertion_data }
    end

    [:name, :module_name, :duration, :file].each do |prop|
      define_method(prop) do
        data[prop]
      end
    end

    private

    # Turn String keys into Symbols and convert Strings representing dates
    # and numbers into their appropriate objects.
    def self.clean_up_result(test_result)
      test_result = symbolize_keys(test_result)
      test_result[:start] = DateTime.parse(test_result[:start])
      test_result[:assertion_data].map! { |data| symbolize_keys data }
      test_result
    end

    def self.symbolize_keys(hash)
      new_hash = {}
      hash.keys.each { |key| new_hash[key.to_sym] = hash[key] }
      new_hash
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
qunited-0.4.2 lib/qunited/qunit_test_result.rb
qunited-0.4.1 lib/qunited/qunit_test_result.rb
qunited-0.4.0 lib/qunited/qunit_test_result.rb