Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting output so that Intellij Idea shows diffs for two texts

Tags:

I would like to be able to print in the logs a message for which intellij idea would present a nice way of comparing two objects (strings). This happens automatically for the error message logged by a failed junit assert:

 assertEquals("some\nString", "another\nString");  => org.junit.ComparisonFailure:  <Click to see difference>     at org.junit.Assert.assertEquals(Assert.java:123)     at org.junit.Assert.assertEquals(Assert.java:145)     at com.something.DummyTest.testDummy(DummyTest.java:89) 

The <Click to see difference> entry is actually displayed as a link in the output window of the Intellij Idea. When you click on the link, a compare window opens which shows the two values (just like you would compare two files).

Simply throwing an exception is not acceptable because I would like to log multiple objects to compare. I already tried logging a text, but I wasn't able to convince idea to compare the two texts.

like image 602
botismarius Avatar asked Jun 07 '12 15:06

botismarius


People also ask

How to compare two text files in IntelliJ?

In the Project tool window, select the files you want to compare and choose Compare Files, or press Ctrl+D . Alternatively, select one file, choose Compare With from its context menu, and select a file that is outside your project.

How to see Diff in IntelliJ?

To open the Diff & Merge page, open settings by pressing Ctrl+Alt+S and navigate to Tools | Diff & Merge. Click this button to scroll both differences panes simultaneously. If this button is released, each of the panes can be scrolled independently.

How do I compare changes in Intellij?

Compare the current revision of a file or folder with a revision in the same branch Select a file or a folder in the Project tool window, and choose <your_VCS> | Compare With from the context menu. Choose a revision you want to compare the current file or folder version with from the dialog that opens.

Where is output in Intellij?

Run tool window The Run tool window displays output generated by your application. If you are running multiple applications, each one is displayed in a tab named after the run/debug configuration applied. If you re-run an application, the new output overwrites the contents of the tab.


1 Answers

IntelliJ IDEA is using the hardcoded regular expression. If the text matches the pattern, it will suggest to click to view the difference.

The pattern is:

expected:<bla-blah> but was:<blah-blah-blah> 

Output should match the format of assertEquals or assertThat.

The exact patterns are somewhat scattered around the code in IDEA, but some are e.g. here.

like image 162
CrazyCoder Avatar answered Sep 21 '22 13:09

CrazyCoder