-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Contracts fail when a method is redefined (overridden) in a subclass #1
Comments
Thank you for testing I did not built any support for class inheritance because the project was initially a proof of concept. Today, I've just added this new feature with a quick patch. In the future, I'll refactor the existing design in a more maintainable way. If you have other ideas or contributions, do not hesitate to tell me about it. Be aware that adding a new precondition in the |
Nicolas - Thanks much for the rapid response and fix. I've tested the new version and verified that the original problem is fixed - the "undefined method" error no longer occurs and the pre- and post-conditions are checked for a subclass that inherits a method with a contract. However, it appears that there's an error in the logic for inherited preconditions: if an inherited method with a precondition is redefined with an additional precondition, when the subclass precondition is checked it is "and"ed with the inherited precondition, effectively making the resulting entire precondition for the inherited method stronger than in the parent. The precondition should actually be weaker - that is, it should be "or"ed with the parent precondition. (Your statement "An instance of SimpleChild should be able to replace an instance of Simple without breaking any contracts." is correct and compatible with the "or"ing interpretation.) [See, e.g.: I had a look at your code to see if I could implement a fix, but I'm pretty inexperienced with ruby and your code is quite advanced, so I gave up. By the way, postconditions are also "and"ed in your implementation, which is correct. (e.g.: I put together a small test case (a further simplified version of the test code I initially posted) to illustrate the problem. I'll include this code in a followup. Thanks! |
Here is the test code I promised. I simply added the precondition 'true' in the inherited version of 'multiplied_by' in SimpleChild. In the test, e.multiplied_by(6) should pass the precondition, since the complete preconditiion is:
|
I've modified the pre/post-conditions inheritance behavior as you suggested in the version 0.2.3. When there is no preconditions, should the expected behavior be:
|
Hi Nicolas - Again, I'm impressed with your quick response on this. (Are Re. when there is no precondition defined in a subclass method - I plan on testing your latest changes some time today, and will let you Thanks! On Thu, Jun 13, 2013 at 3:25 AM, Nicolas Zermati
|
Hi Nicolas - I just forked/branched some tests that show a couple remaining bugs and Let me know if you have any questions, etc. Thanks! On Thu, Jun 13, 2013 at 3:25 AM, Nicolas Zermati
|
I've fixed the code to pass most of your examples. Could you tell me if you find remaining issues about inheritance in version 0.2.4 ? |
Nicolas - I ran all of the tests I've created for ruby_contracts and the only ones
So of the 3 problems I reported last time ("wrong # of arguments" reported I also discovered a minor bug - if class B, subclass of A, has a redefined On Fri, Jun 14, 2013 at 4:42 AM, Nicolas Zermati
|
Post-conditions blocs take the result of the method as first argument then all the arguments passed to it. For instance: |
Of course if you have a non working example for this, I'll be happy to see it and add it to the test suite. |
It's ok, I found this one. |
I released the version 0.2.5 that will fix this issue. |
I opened an issue #3 to answer the last part of your message. |
You're right - about postconditions/result. So the problem in this case On Sat, Jun 15, 2013 at 2:38 AM, Nicolas Zermati
|
The README could be improved, I give you that :-) I'll make something more appealing in the future. |
I wrote some simple test code to try out ruby_contracts with inheritance and am getting an error in a redefined method (in SimpleChild, subclass of Simple) where the method in the parent has a precondition and postcondition. My code is inserted below. The redefined method is multiplied_by. I get an error both when defining a new precondition for the method in the subclass and when this precondition is commented out. The errors messages for these two cases are different, but very similar - it appears to be the same cause.
The error message with the subclass precondition is:
and without the precondition:
I believe this is a bug. Note: If I remove the multiplied_by method definition in SimpleChild, the test script runs without the error.
By the way, I like this module - it seems well designed and well implemented, and I especially like that assertion checking can be disabled. Hopefully this bug is relatively easy to fix. Thanks!
The text was updated successfully, but these errors were encountered: