Sha256: 61f530bd8ec4b8831505f8c8a2d702fe0bd6e7144e07f8080b2aad5346945413

Contents?: true

Size: 1.66 KB

Versions: 52

Compression:

Stored size: 1.66 KB

Contents

o = Object.new
ms = o.methods
require "minitest"

block = proc do |payload|
  assert_kind_of(Array, payload, 'Payload should be a collection')
  payload.each do |element|
    assert_kind_of(Hash, element, 'Payload collection elements should be hashtables')

    assert_includes(element.keys, 'hello', 'payload elements should has hello key')
  end
end

good_payload = [
  {"hello" => "world1"},
  {"hello" => "world2"}
]

bad1_payload = [
  {"nope" => "world1"},
  {"hello" => "world2"}
]

bad2_payload = {"hello" => "world2"}

class Validator

  include Minitest::Assertions

  attr_accessor :assertions
  def initialize(&block)
    @assertions = 0
    singleton = class << self
      self
    end
    singleton.__send__(:define_method, :__tester__, &block)
    singleton.__send__(:protected, :__tester__)
  end

  def test(payload)
    __tester__(payload)
  rescue Minitest::Assertion => ex
    puts ex.message
  end

end

class Documentation

  def initialize(&block)
    @docs = []
    singleton = class << self
      self
    end
    singleton.__send__(:define_method, :__tester__, &block)
    singleton.__send__(:protected, :__tester__)
  end

  def to_doc
    o = Object.new
    def o.method_missing(*_)
      self
    end

    def o.each(&block)
      block.call(self)
    end

    def o.inspect
      'element'
    end
    __tester__(o)

    @docs.join("\n")
  end

  protected

  def method_missing(method_name,*args,&block)
    @docs << args.select{|arg| arg.is_a?(String) }.last
  end

end


puts Object.new.methods - ms # no output , no monkey patch than

v = Validator.new(&block)
v.test(good_payload)
v.test(bad1_payload)
v.test(bad2_payload)

d = Documentation.new(&block)
puts d.to_doc

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
rack-app-11.0.2 spike/validator_with_minitest.rb
rack-app-11.0.1 spike/validator_with_minitest.rb
rack-app-11.0.0 spike/validator_with_minitest.rb
rack-app-10.0.0 spike/validator_with_minitest.rb
rack-app-9.0.0 spike/validator_with_minitest.rb
rack-app-8.1.0 spike/validator_with_minitest.rb
rack-app-8.0.1 spike/validator_with_minitest.rb
rack-app-8.0.0 spike/validator_with_minitest.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-app-7.7.0/spike/validator_with_minitest.rb
rack-app-7.7.0 spike/validator_with_minitest.rb
rack-app-7.6.5 spike/validator_with_minitest.rb
rack-app-7.6.4 spike/validator_with_minitest.rb
rack-app-7.6.3 spike/validator_with_minitest.rb
rack-app-7.6.2 spike/validator_with_minitest.rb
rack-app-7.6.1 spike/validator_with_minitest.rb
rack-app-7.6.0 spike/validator_with_minitest.rb
rack-app-7.5.2 spike/validator_with_minitest.rb
rack-app-7.5.1 spike/validator_with_minitest.rb
rack-app-7.5.0 spike/validator_with_minitest.rb
rack-app-7.4.0 spike/validator_with_minitest.rb