Skip to content

Instantly share code, notes, and snippets.

@gilleain
Created July 21, 2011 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilleain/1097259 to your computer and use it in GitHub Desktop.
Save gilleain/1097259 to your computer and use it in GitHub Desktop.
bsh script
import org.openscience.cdk.Molecule;
import org.openscience.cdk.io.CDKSourceCodeWriter;
import org.openscience.cdk.io.MDLReader;
import org.eclipse.jgit.api.*;
import org.eclipse.jgit.lib.*;
import org.eclipse.jgit.storage.file.*;
element = bsh.args[0];
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.readEnvironment().findGitDir().build();
Git git = new Git(repository);
status = git.status().call();
molFilePaths = status.getAdded();
// convert the molfiles to cdk calls
codeFrags = new HashMap();
for (molFilePath : molFilePaths) {
molFile = new File(molFilePath);
mdlReader = new MDLReader(new FileReader(molFile));
mol = mdlReader.read(new Molecule());
mdlReader.close();
name = new File(molFilePath).getName();
name = name.substring(0, name.indexOf("."));
print(molFilePath);
sw = new StringWriter();
sourceWriter = new CDKSourceCodeWriter(sw);
sourceWriter.getIOSettings()[0].setSetting("false");
sourceWriter.write(mol);
sourceWriter.close();
//print(sw.toString());
String code = sw.toString();
code = code.replace("}", "");
code = code.replace("\n", "\n ");
code = "\n @Test\n public void test_" + name + "() throws Exception " + code;
codeFrags.put(name, code);
molFile.delete();
}
// read in the test class
testClass = new File("src/test/org/openscience/cdk/atomtype/CDKAtomTypeMatcherTest.java");
javaFileReader = new BufferedReader(new FileReader(testClass));
String line;
buffer = new ArrayList();
inFix = false;
seenFix = false;
molName = "";
while ((line = javaFileReader.readLine()) != null) {
if (line.contains("fix_" + element) && !inFix) {
buffer.remove(buffer.size() - 1); // remove the @Test line
inFix = true;
} else if (inFix && line.contains("String[] expectedTypes")) {
buffer.add(codeFrags.get(molName));
buffer.add(line.replaceFirst("\\s(expectedTypes.*)=", " expectedTypes ="));
} else if (inFix && line.contains("assertAtomTypes")) {
buffer.add(line.replaceFirst("\\s(expectedTypes.*),", " expectedTypes,")
.replaceFirst("\\s(mol.*)\\)", " mol\\)"));
buffer.add(" }\n");
} else if (inFix && line.contains("}")) {
inFix = false;
seenFix = true;
} else if (inFix && line.contains("String molName")) {
molName = line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\""));
} else if (seenFix) {
buffer.add(line);
} else if (!inFix) {
buffer.add(line);
}
}
javaFileReader.close();
testClass.delete();
// print out the modified test file
fileWriter = new BufferedWriter(new FileWriter(testClass));
for (String l : buffer) {
fileWriter.write(l);
if (!l.endsWith("\n")) {
fileWriter.newLine();
}
}
fileWriter.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment