简体   繁体   中英

how to use hamcrest to compare text ignoring tabs?

I have a test, I want to assert its result:

    assertThat(cofmanString, new IsEqualIgnoringCase(FileUtils.readFileToString(new File("/Users/myFile.txt"))));

in Intellij I see the strings are identical including tabs and newlines

actual:

在此处输入图片说明

expected: 在此处输入图片说明

but the test fails like this:

在此处输入图片说明

which hamcrest matcher can i use to compare the strings and succeed?

You can use this:

assertThat(cofmanString, equalToIgnoringWhiteSpace(FileUtils.readFileToString(
new File("/Users/myFile.txt")).toLowerCase()));

You can see more for IsEqualIgnoringWhiteSpace here

There is no "ignore tabs" option, but you can just remove all tabs before comparing by applying .replace("\\t", "") to each term:

assertThat(cofmanString.replace("\t", ""), new IsEqualIgnoringCase(
  FileUtils.readFileToString(new File("/Users/myFile.txt"))).replace("\t", ""));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM