Skip to content

Commit

Permalink
Some invalid smarts
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Jun 24, 2019
1 parent 24c3b57 commit f760265
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,8 @@ else if (sub2 != null)
// final check
boolean finish() {
// check for unclosed rings, components, and branches
if (numRingOpens != 0 || curComponentId != 0 || !stack.isEmpty()) {
if (numRingOpens != 0 || curComponentId != 0 ||
!stack.isEmpty() || bond != null) {
error = "Unclosed ring, component group, or branch";
return false;
}
Expand Down Expand Up @@ -1805,6 +1806,8 @@ public boolean parse() {
case '!':
case '/':
case '\\':
if (prev == null)
return false;
unget();
if (!parseBondExpr())
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.Test;
import org.openscience.cdk.CDKTestCase;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.isomorphism.matchers.Expr;
import org.openscience.cdk.silent.SilentChemObjectBuilder;
import org.openscience.cdk.smarts.Smarts;

Expand All @@ -36,10 +37,16 @@
*/
public class ParserTest extends CDKTestCase {

private static final class InvalidSmarts extends Exception {
public InvalidSmarts(String message) {
super(message);
}
}

private void parse(String smarts, int flav) throws Exception {
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
if (!Smarts.parse(builder.newAtomContainer(), smarts, flav))
throw new Exception(Smarts.getLastErrorMesg());
throw new InvalidSmarts(Smarts.getLastErrorMesg());
}

private void parse(String smarts) throws Exception {
Expand Down Expand Up @@ -1505,4 +1512,14 @@ public void atomMaps2() throws Exception {
parse("O=C1NCCSc2ccccc12");
}

@Test(expected = InvalidSmarts.class)
public void testBondPrefix() throws Exception {
parse("-CCO");
}

@Test(expected = InvalidSmarts.class)
public void trailingBond() throws Exception {
parse("CCO-");
}

}

0 comments on commit f760265

Please sign in to comment.