Skip to content

Commit

Permalink
More verbose unit testing with hamcrest matchers.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Oct 6, 2016
1 parent efef10c commit 93922bf
Showing 1 changed file with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openscience.cdk.fingerprint;

import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.CDKTestCase;
import org.openscience.cdk.fingerprint.CircularFingerprinter.FP;
Expand All @@ -10,6 +9,14 @@
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIn.isIn;

/**
* @cdk.module test-standard
*/
Expand Down Expand Up @@ -91,6 +98,11 @@ public void testMol7() throws Exception {

private void checkFPSmartsForMolecule(String moleculeSmiles,
String expectedFPSmarts[][]) throws Exception {

Set<String> expected = new HashSet<>();
for (String[] strs : expectedFPSmarts)
Collections.addAll(expected, strs);

// expectedFPSmarts[][] is a double array because for each smarts
// several equivalent variants
// of the smarts are given e.g. CCC C(C)C
Expand All @@ -99,22 +111,13 @@ private void checkFPSmartsForMolecule(String moleculeSmiles,
CircularFingerprinter circ = new CircularFingerprinter();
circ.calculate(mol);
int numFP = circ.getFPCount();

Set<String> actual = new HashSet<>();
for (int i = 0; i < numFP; i++) {
FP fp = circ.getFP(i);
String smarts = circ.getFPSmarts(fp, mol);
int res = findSmarts(smarts, expectedFPSmarts);
Assert.assertEquals("serching fp smarts: " + smarts, true, res >= 0);
actual.add(circ.getFPSmarts(fp, mol));
}
}

private int findSmarts(String smarts, String smartsSet[][]) {
for (int i = 0; i < smartsSet.length; i++) {
String s[] = smartsSet[i];
for (int k = 0; k < s.length; k++)
if (s[k].equals(smarts))
return i;
}
return -1;
assertThat(actual, everyItem(isIn(expected)));
}

}

0 comments on commit 93922bf

Please sign in to comment.