Timeline for Is it bad practice to compare string representation on an object instead of its properties during unit tests?
Current License: CC BY-SA 4.0
8 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 22, 2022 at 22:16 | comment | added | supercat | @Bergi: I think the question was about whether comparing the string representations of objects in a manner agnostic as to their meaning would be an acceptable alternative to doing more detailed analysis of the objects in question. If an object's state can be fully encapsulated in a string, that may work, but in the scenario you describe the meaning of a pointer that equals 0x08675309 would depend upon the values of other pointers that code within the class definition might not know about. | |
| Jun 22, 2022 at 22:05 | comment | added | Bergi | We don't expect it to be the same in all possible program executions, we only expect it to have a particular value for the execution of the unit test. Of course you wouldn't hardcode 0x08675309 in the test, rather you'd get the address of the object that you care about. | |
| Jun 22, 2022 at 21:45 | comment | added | supercat | @Bergi: The fact that a particular object happens to be at address 0x08675309 on one particular program execution says nothing meaningful about whether address 0x08675309 will identify that same object on any future program execution. | |
| Jun 22, 2022 at 21:26 | comment | added | Bergi | Sorry for mentioning Java, you're right there… But I don't get your other point. In unit testing, we do not care about the entire universe of objects. If we care about an object's identity, we can stringify its memory address, if we only are about structural equality, we can stringify its content values. (If we care about the object graph, its a bit more complicated, but even that can be serialised into a string). Either way, the string captures all the information that we care about, and can be used for comparisons in testing. | |
| Jun 22, 2022 at 21:16 | comment | added | supercat | @Bergi: An object reference contains, as part of its implied encapsulated state, the whereabouts of every single other reference to that object that exists anywhere in the universe. If one were to simultaneously change every reference to an object so that they all identified a different object whose fields contained the same contents, that would not affect the state encapsulated by the object, but it's impossible to tell if running two different programs to the same point would leave an object in the same state without knowing the whereabouts of every reference to it. | |
| Jun 22, 2022 at 21:13 | comment | added | supercat |
@Bergi: Java's default hashCode() method, used by toString(), is designed so that references to the same object will always yield the same value, and references to different objects will usually yield different value, but there is no guarantee that to completely unrelated objects won't have the same hash code, and the odds of that happening are not even terribly remote if many objects' hash codes are observed shortly after the objects are created. Further, there is no way code can know what the "correct" hash code (or default toString() representation) of an object should be.
|
|
| Jun 22, 2022 at 20:59 | comment | added | Bergi |
Why couldn't pointers be expressed as strings? Java's default toString method does basically that.
|
|
| Jun 22, 2022 at 17:36 | history | answered | supercat | CC BY-SA 4.0 |