Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
You're quite right. This is some code I tried on many katas on codewars to see how many of them I can solve with a generic solution like this. I create an object and I'm changing the eigenclass of that object by overwriting the
==
method. Thendecompose(y) == x
will return true or any valuesx
andy
. Another even more generic solution included the use ofmethod_missing
to ensure that every method called on the object also returned an equality object. This ensured that even testing for derived values like the length of an array would not fail this solution.The point why this actually works isn't so much seen here though. Any test framework has to check for equality at some point like this:
So this solution just fakes an always passing test (see https://github.com/Codewars/kata-test-framework-ruby/blob/master/framework.rb at line 147). There is no way to prevent that kind of solution, but some way to make it harder. I think there should be some tests using
Test.expect(expected == actual)
then you can't simply modify the comparison of the object you return, but you'd have to monkey patch the class of what is expected. When the kata would test for several values like length of the array, specific elements, iteration over it, and such it would be a major hazzle figuring out everything.Just a little reminder that this solution is of course a fake. It doesn't solve the actual problem, it just demonstrates that green tests aren't a good metric, because no actual behavior might be implemented (doesn't have to be obvious like here though).
What in the world?? If you could message me what is going on here I'd really appreciate it.
My guess:
The test class used in Codewars at some point uses the return value from decompose() to compare with the parameritized expected value... and for some reason this intercepts the inhertiance chain of whatever ruby method is being used to compare identity.
Eg.. decompose(11) == [10,4,2,1] ----> EQUALITY == ([10,4,2,1])
=> true
Am I right? :O