Ad
  • Default User Avatar

    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. Then decompose(y) == x will return true or any values x and y. Another even more generic solution included the use of method_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:

    if actual == expected
      test_is_passing
    else
      test_is_failing
    end
    

    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).

  • Default User Avatar

    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