Sha256: 42370371718c39844272824083b3b4e7fcd0852bef34d7cf8ad28b7d3dcffc80

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

require 'spec_helper'
require 'rspec_api_documentation/dsl'

def has_attribute(name, type, options = {})
  (metadata[:attributes] ||= {})[name] = options.merge(type: type)
end

def assert_attributes(json)
  expect(json).to be_a (example.metadata[:array] ? Array : Hash)
  example.metadata[:attributes].each do |name, options|
    values = Array.wrap(json).map{|item| item[name.to_s]}
    assert_attribute_types(values, options[:type], options[:can_be_nil])
  end
end

def random_values_for_attributes
  {}.tap do |values|
    example.metadata[:attributes].each do |name, options|
      can_be_nil = options[:can_be_nil] && (name != example.metadata[:on])
      values[name] = random_attribute_value options.merge(can_be_nil: can_be_nil)
    end
  end
end

def random_attribute_value(options)
  if options[:can_be_nil] && [true, false].sample
    nil
  else
    case options[:type]
      when :string then [*('a'..'z'), *('A'..'Z')].sample(Random.rand 32).join
      when :integer then Random.rand(2**16)
      when :url then "http://example.com/#{SecureRandom.urlsafe_base64}"
    end
  end
end

def assert_attribute_types(values, expected_type, can_be_nil)
  values.compact! if can_be_nil
  expect(values).to all_match_type expected_type
end

def matches_type?(value, type)
  case type
    when :url then value =~ URI::regexp
    else value.is_a? type.to_s.classify.constantize
  end
end

RSpec::Matchers.define :all_match_type do |type|
  match do |values|
    values.all? {|value| matches_type? value, type}
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rspec-api-0.0.3 lib/rspec-api/attributes_helper.rb
rspec-api-0.0.2 lib/rspec-api/attributes_helper.rb