gpt4 book ai didi

java - Eclipse junit View 中的不可打印字符

转载 作者:行者123 更新时间:2023-11-30 08:26:30 24 4
gpt4 key购买 nike

考虑以下示例:

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”(或其他不可打印的字符)?

目前的结果对比并不能真正帮助我找出问题所在: extra carriage return result comparison

最佳答案

对于断言必须对“不可打印字符”敏感的情况,您可以使用自定义断言方法,在比较之前将不可打印字符转换为其 unicode 表示形式。这是一些快速编写的代码(受 thisthis 启发):

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com