Pharo: How to compare objects in different inspectors?

I am using Pharo 4. Suppose my objects have already implemented good equality and hash methods.

How to visually compare two or N objects in different inspectors? Visually, I mean side-by-side comparisons, where I can easily distinguish differences.

I tried allInstances, but after a while it gets tedious.

+4
source share
2 answers

I would suggest creating a class DifferenceFinderspecifically designed for the class whose instances you are trying to compare. For example, suppose you want to compare points. Then you will have a class PointDifferenceFinderwith three instance variables p, q, differenceand a protocol in lines

compare: aPoint with: anotherPoint
  p := aPoint.
  q := anotherPoint.
  self compareClass ifFalse: [^self].
  self compareX ifFalse: [^self].
  self compareY ifFalse: [^self].

Where

compareClass
  ^p class == q class
    ifFalse: [difference := 'not of the same class'];
    yourself

compareX
  ^p x = q x
    ifFalse: [difference := 'not with the same x'];
    yourself

compareY
  ^p y = q y
    ifFalse: [difference := 'not with the same y'];
    yourself

Of course, the case Pointis simple, but this should give you an idea. Depending on your needs, you may just have a simple finder or one more complex one.

0
source

You can be inspired by viewing the diff browser, which you can select at runtime GLMBasicExamples open. With Glamor, you can easily create your own browser to help you with this. The diff example itself is 15 lines of code:

| browser | 
browser := GLMTabulator new.
browser 
    row: [:r | r column: #one; column: #two];
    row: #diff.
browser transmit to: #one; andShow: [ :a |
    a list 
        display: #first ].
browser transmit to: #two; andShow: [ :a | 
    a list
        display: #second ].
browser transmit to: #diff; from: #one; from: #two; andShow: [ :a | 
    a diff
        display: [ :one :two | {one asString . two asString}] ].
browser openOn: #(#(abc def ghi) #(abc xyz))

diff DiffMorph .

.
0

Source: https://habr.com/ru/post/1629724/


All Articles