Created
December 23, 2022 19:50
-
-
Save thiagoa/17aa82368e3f06f9f7e32b077aecc6d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.shared_examples_for "interface checker" do | |
def diff_for(array_1, array_2) | |
(array_1 - array_2) + (array_2 - array_1) | |
end | |
def arities_for(object) | |
object.public_methods.map do |m| | |
arity = object.method(m).arity | |
[m, arity == -1 ? anything : arity] | |
end | |
end | |
it "all objects abide by the same interface" do | |
objects.each_cons(2) do |left, right| | |
left_methods = left.public_methods | |
right_methods = right.public_methods | |
diff = diff_for(left_methods, right_methods) | |
expect(left_methods).to( | |
match_array(right_methods), (<<~MESSAGE).split("\n").join(" ") | |
Expected #{left.class} to have the same | |
interface as #{right.class}, however the | |
following extra methods were implemented: #{diff} | |
MESSAGE | |
) | |
end | |
end | |
it "all methods have the same arity" do | |
objects.each_cons(2) do |left, right| | |
left_arities = arities_for(left) | |
right_arities = arities_for(right) | |
diff = diff_for(left_arities, right_arities) | |
expect(left_arities).to( | |
match_array(right_arities), (<<~MESSAGE).split("\n").join(" ") | |
Expected #{left.class} to have the methods | |
with the same arity as #{right.class}, however the | |
following arities do not match: #{diff} | |
MESSAGE | |
) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment