格式化输出,以便Intellij Idea显示两个文本的差异 [英] Formatting output so that Intellij Idea shows diffs for two texts

查看:144
本文介绍了格式化输出,以便Intellij Idea显示两个文本的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在日志中打印一条消息,其中intellij想法将提供一种比较两个对象(字符串)的好方法。对于失败的junit断言记录的错误消息,会自动发生这种情况:

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)

<点击查看差异>条目实际上显示为Intellij Idea输出窗口中的链接。当你点击链接时,会打开一个比较窗口,显示两个值(就像比较两个文件一样)。

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.

推荐答案

IntelliJ IDEA是使用硬编码的正则表达式。如果文本与模式匹配,则建议单击以查看差异。

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

模式为:

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

输出应符合 assertEquals 的格式或 assertThat

确切的模式在IDEA的代码中有些分散,但有些是例如此处

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

这篇关于格式化输出,以便Intellij Idea显示两个文本的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆