考虑以下示例:
assertEquals( "I am expecting this value on one line.\r\nAnd this value on this line",
"I am expecting this value on one line.\nAnd this value on this line" );
Eclipse 中是否有任何调整或插件可以帮助识别字符串比较中额外的“\r”(或其他不可打印的字符)?
目前的结果比较并不能真正帮助我识别问题:
最佳答案
对于断言必须对“不可打印字符”敏感的情况,您可以使用自定义断言方法,该方法在比较之前将不可打印字符转换为其 unicode 表示形式。这是一些快速编写的代码用于说明(受到 this 和 this 的启发):
package org.gbouallet;
import java.awt.event.KeyEvent;
import org.junit.Assert;
import org.junit.Test;
public class NonPrintableEqualsTest {
@Test
public void test() {
assertNonPrintableEquals(
"I am expecting this value on one line.\r\nAnd this value on this line",
"I am expecting this value on one line.\nAnd this value on this line");
}
private void assertNonPrintableEquals(String string1,
String string2) {
Assert.assertEquals(replaceNonPrintable(string1),
replaceNonPrintable(string2));
}
public String replaceNonPrintable(String text) {
StringBuffer buffer = new StringBuffer(text.length());
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (isPrintableChar(c)) {
buffer.append(c);
} else {
buffer.append(String.format("\\u%04x", (int) c));
}
}
return buffer.toString();
}
public boolean isPrintableChar(char c) {
Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
return (!Character.isISOControl(c)) && c != KeyEvent.CHAR_UNDEFINED
&& block != null && block != Character.UnicodeBlock.SPECIALS;
}
}
关于java - Eclipse junit View 中的不可打印字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21418265/