Sha256: 81165bc73b9a9169c8f307e4cb3cb738747036418f7512d2da02ac9cf6a7113f

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Yardcheck
  class Observation
    include Concord.new(:documentation, :event)

    def violations
      param_violations + return_violations
    end

    def source_code
      documentation.source
    end

    def source_location
      documentation.location_pointer
    end

    def test_location
      event.example_location
    end

    def method_shorthand
      documentation.shorthand
    end

    def documented_param(name)
      documentation.params.fetch(name)
    end

    def observed_param(name)
      event.params.fetch(name)
    end

    def documented_return_type
      documentation.return_type
    end

    def actual_return_type
      event.return_value.type
    end

    def documentation_warnings
      documentation.warnings
    end

    private

    def param_violations
      overlapping_keys = documentation.params.keys & event.params.keys

      overlapping_keys.map do |key|
        type_definition = documentation.params.fetch(key)
        test_value      = event.params.fetch(key)

        next if type_definition.match?(test_value)
        Violation::Param.new(key, self)
      end
    end

    def return_violations
      invalid_return_type? ? [Violation::Return.new(self)] : []
    end

    def invalid_return_type?
      documentation.return_type &&
        !documentation.return_type.match?(event.return_value) &&
        !event.raised? &&
        !event.initialize?
    end
  end # Observation
end # Yardcheck

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yardcheck-0.0.1 lib/yardcheck/observation.rb