Skip to content

Instantly share code, notes, and snippets.

@pepsin
Last active June 25, 2020 07:44
Show Gist options
  • Save pepsin/5105228 to your computer and use it in GitHub Desktop.
Save pepsin/5105228 to your computer and use it in GitHub Desktop.
module Annotations
def annotations(meth=nil)
return @__annotations__[meth] if meth
@__annotations__
end
private
def method_added(m)
(@__annotations__ ||= {})[m] = @__last_annotation__ if @__last_annotation__
@__last_annotation__ = nil
super
end
def method_missing(meth, *args)
return super unless /\A_/ =~ meth
@__last_annotation__ ||= {}
@__last_annotation__[meth[1..-1].to_sym] = args.size == 1 ? args.first : args
end
end
class Module
def test!(meth=nil)
if self.annotations(meth)[:v]
v_test(meth)
else
if self.annotations(meth)[:correct].class == Hash
mock_test(meth)
elsif self.annotations(meth)[:correct]
result_test(meth)
end
end
end
private
def v_test(meth)
method_str = "self.new.send :#{meth }," + self.annotations(meth)[:v].join(',')
p "#{meth}:Success" if eval(method_str) == self.annotations(meth)[:correct]
end
def result_test(meth)
p "#{meth}:Success" if self.new.send(meth) == self.annotations(meth)[:correct]
end
def mock_test(meth)
self.class_eval do
undef_method self.annotations(meth)[:correct][:mock]
end
begin
self.new.send(meth)
rescue NameError
p "#{meth}:Success"
end
end
def annotate!
extend Annotations
end
end
class A
annotate!
_correct "Foo"
def m1
"Foo"
end
_correct mock: :m1
def m2
m1
end
_v 1,2
_correct 3
def m3(a, b)
a + b
end
end
A.test!(:m1)
A.test!(:m2)
A.test!(:m3)
#p A.annotations(:m3)[:correct]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment