java - How can a test access the PSI tree structure of the code it is testing, In IntelliJ IDEA? -


running tests intellij idea ide, want them able custom static analysis on code being tested. end, want tests able access psi tree structure of tested code. possible? how?

here example of usefulness of this: in hibernate, final methods of entity classes should not directly access fields. so, write test checks classes annotated @entity have no final methods reference fields. can't reflection. need access psi tree. example, , there many more use cases such capabilities.

i know couples tests intellij idea. , know write intellij idea plugin implement "inspections" kind of analysis. want write tests. tests can red or green. tests emit meaningful error messages. tests run code, access code through reflection, , also access psi tree.

what trying work on ast outputted parser, 1 way write unit tests extend parsingtestcase. initial behavior of kind of tests print readable version of ast , compare expected file, can override method different tests, while keeping easy way retrieve psi tree:

import com.intellij.testframework.parsingtestcase;  public class myawesometest extends parsingtestcase {      public myawesometest() {         super("relative/path/to/files", "java", new javaparserdefinition());     }      @override     protected boolean skipspaces() {         return true;     }      public void testempty() {         dotest(true); // test relative/path/to/files/empty.java     }      @override     protected void checkresult(@nonnls @testdatafile string targetdataname, final psifile file) throws ioexception {         // stuff here     }  } 

note file names extracted test method names (here, empty), can annoying if want test lots of files, when name known @ runtime. overcome this, can replace dotest() custom method similar things additional name argument.

then run tests, you'll have download intellij (either ultimate or community) , add bunch of jar files located in intellij/lib/ classpath. otherwise, might possible run unit tests in intellij's headless mode, don't know how.

additional info here: https://confluence.jetbrains.com/display/ideadev/testing+intellij+idea+plugins


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -