samtools / htsjdk

A Java API for high-throughput sequencing data (HTS) formats.
http://samtools.github.io/htsjdk/
278 stars 244 forks source link

Test case helper and fix EOF detection #1641

Open lbergelson opened 1 year ago

lbergelson commented 1 year ago

This started as me fixing the minor bug in https://github.com/samtools/htsjdk/issues/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:

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.