Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test case helper and fix EOF detection #1641

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Conversation

lbergelson
Copy link
Member

This started as me fixing the minor bug in #1614. I got annoyed with something that I am regularly annoyed with, needing different data providers for the severals sets of test cases I want to run even though they run the same code.

I often want to right the following test cases for the same function:

  • The function matches a given value
  • The function mismatches the value
  • The function throws a specific exception.

I added a helper that lets you capture those cases in one data provider.

Expected.match(42)
Expected.mismatch("bleh")
Expected.exception(UserException)

It's used as an element in the data provider and then tested by calling it's test function:

  @DataProvider
    public Object[][] testCases() {
       ...                         
     {utf8File, StandardCharsets.UTF_8, Expected.match("🐋 UTF8 is Great 🐋")},
     {utf8File, StandardCharsets.ISO_8859_1, Expected.mismatch("🐋 UTF8 is Great 🐋")}
     {missingTerminator, StandardCharsets.UTF_8, Expected.exception(EOFException.class)},
     ...
   }

@Test(dataProvider = "testCases")
public void testAllCases(String filename, Charset charset, Expected<String> expected) {
     expected.test(() -> {
         try(final LittleEndianInputStream in = new LittleEndianInputStream(new BufferedInputStream(Files.newInputStream(Paths.get(filename))))){
             return in.readString(charset);
         }
     });            
} 

It seems like a useful thing to me, but I'm wanted to get feedback on the idea.

This is more complicated than originally described because the file encoding isn't really described and java bytes include negative values.
EOF is triggered correctly, but it's also triggered on various potential characters which become negative values when they are truncated to byte.
It's unclear if this is meant to only read ASCII, ISO 8859-1, or UTF-8 but nothing outside of the ascii space works correctly.
}
}

final class ComparisonExpected<T> implements Expected<T> {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is weirdly scoped because you're not yet allowed to put private classes in an interface.

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.

1 participant