FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add assertions for Paths/Files/InputStreams to validate the encoding by pigelvy · Pull Request #3649 · assertj/assertj · GitHub

Add assertions for Paths/Files/InputStreams to validate the encoding - #3649

Open
pigelvy wants to merge 1 commit into
assertj:3.xfrom
pigelvy:feature/add_charset_encoding_assertions
Open

Add assertions for Paths/Files/InputStreams to validate the encoding#3649
pigelvy wants to merge 1 commit into
assertj:3.xfrom
pigelvy:feature/add_charset_encoding_assertions

Conversation

pigelvy commented Nov 5, 2024

Copy link
Copy Markdown

This PR adds isEncodedIn(Charset), isEncodedIn(Charset, boolean), isNotEncodedIn(Charset), isNotEncodedIn(Charset, boolean).

Even though detecting encoding issues might not be 100% possible because some charsets can simply map some inputs to something else valide to them, there are some cases where having assertions can be handly such as:

  • Explicit invalid or unmappable bytes.
  • The simple presence of the replacement char.

The assertions I added, are based on internal testing functions that I use in various projects. I though it would be a good addition to AssertJ.

Check List:

joel-costigliola left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@pigelvy thanks for the PR, here's a first round of comments (nothing major).

}

/**
* Synonyme of {@link #isNotEncodedIn(Charset, boolean)} with {@code lenient = false}.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

typo: Synonym (no e)

*
* @param charset the expected encoding of the actual file.
* @return {@code this} assertion object.
* @since 3.26.4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

will be 3.27.0 thanks !

.hasMessageContaining("line4 is nok");
}

private File writeToTempFile(String str, Charset charset) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think this can be replaced by TempFileUtil createTempFileWithContent(String content, Charset charset) method

assertThat(file).isEncodedIn(UTF_8, true);

// Test strict validation does not accept the replacement char
assertThatCode(() -> assertThat(file).isEncodedIn(UTF_8, false))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Use AssertionsUtil.expectAssertionError instead to split the WHEN and THEN part of the test

}

@Test
void should_validate_lenient() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

uaw GIVEN WHEN THEN comment to identify better the test steps

*
* @author Ludovic VIEGAS
*/
public class ShouldBeEncodedIn extends BasicErrorMessageFactory {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Add a unit tests for this class as in ShouldBeCloseTo_create_Test

throw failures.failure(info, shouldHaveNoParent(actual));
}

public void assertIsEncodedIn(WritableAssertionInfo info, File actual, Charset charset, boolean lenient) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

use AssertionInfo instead of WritableAssertionInfo as we are not changing it

* @author Ludovic VIEGAS
*/
public class EncodingIssue {
private final long line;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

rename to lineNumber

*/
public class EncodingIssue {
private final long line;
private final String string;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

rename to line

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL