Sha256: aae6aaf88e7e5a4a4f154a47bcdd49b7fefecca295e5aec5c9b0b0d03385c2b5

Contents?: true

Size: 1.81 KB

Versions: 22

Compression:

Stored size: 1.81 KB

Contents

module JSONHelper
  UNDEFINED = Object.new.freeze
  private_constant :UNDEFINED

  def assert_json(path = nil, expected_value = UNDEFINED, document: response.body)
    value = follow_path(path.to_s.split('.'), document: document)
    if block_given?
      yield value
    elsif expected_value == UNDEFINED
      raise ArgumentError, "Missing either expected_value or a block"
    elsif expected_value.nil?
      assert_nil value
    else
      assert_equal expected_value, value
    end
  end

  def assert_json_keys(path, keys = nil, document: response.body)
    keys, path = path, nil if keys.nil?

    value = follow_path(path.to_s.split('.'), document: document)
    case value
    when Hash
      assert_equal keys.sort, value.keys.sort
    else
      assert false, "Expected #{path} to be a Hash, was: #{value.inspect}"
    end
  end

  def assert_no_json(path, document: response.body)
    segments = path.to_s.split('.')
    last_segment = segments.pop
    leaf = follow_path(segments, document: document)
    case leaf
    when Hash
      refute leaf.key?(last_segment), "Expected #{leaf.inspect} not to include #{last_segment.inspect}"
    when Array
      refute leaf.size > last_segment.to_i, "Expected #{leaf.inspect} to not have element at index #{last_segment.to_i}"
    else
      assert false, "Expected #{leaf.inspect} to be a Hash or Array"
    end
  end

  private

  def follow_path(segments, document:)
    parsed_json = ActiveSupport::JSON.decode(document)
    segments.inject(parsed_json) do |object, key|
      case object
      when Hash
        assert_includes object, key
        object[key]
      when Array
        assert object.size > key.to_i, "#{object.inspect} have no property #{key}"
        object[key.to_i]
      else
        assert false, "Expected #{object.inspect} to be a Hash or Array"
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
shipit-engine-0.31.0 test/helpers/json_helper.rb
shipit-engine-0.30.0 test/helpers/json_helper.rb
shipit-engine-0.29.0 test/helpers/json_helper.rb
shipit-engine-0.28.1 test/helpers/json_helper.rb
shipit-engine-0.28.0 test/helpers/json_helper.rb
shipit-engine-0.27.1 test/helpers/json_helper.rb
shipit-engine-0.27.0 test/helpers/json_helper.rb
shipit-engine-0.26.0 test/helpers/json_helper.rb
shipit-engine-0.25.1 test/helpers/json_helper.rb
shipit-engine-0.25.0 test/helpers/json_helper.rb
shipit-engine-0.24.0 test/helpers/json_helper.rb
shipit-engine-0.23.1 test/helpers/json_helper.rb
shipit-engine-0.23.0 test/helpers/json_helper.rb
shipit-engine-0.22.0 test/helpers/json_helper.rb
shipit-engine-0.21.0 test/helpers/json_helper.rb
shipit-engine-0.20.1 test/helpers/json_helper.rb
shipit-engine-0.20.0 test/helpers/json_helper.rb
shipit-engine-0.19.0 test/helpers/json_helper.rb
shipit-engine-0.18.1 test/helpers/json_helper.rb
shipit-engine-0.18.0 test/helpers/json_helper.rb