use of lombok.ast.printer.StructureFormatter in project android by JetBrains.
the class LombokPsiConverterTest method check.
private static void check(PsiFile psiFile, @Language("JAVA") String source) {
assertTrue(psiFile.getClass().getName(), psiFile instanceof PsiJavaFile);
PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile;
CompilationUnit node = LombokPsiConverter.convert(psiJavaFile);
assertNotNull(node);
String actualStructure;
if (CHECK_POSITIONS) {
StructureFormatter structureFormatter = StructureFormatter.formatterWithPositions();
node.accept(new SourcePrinter(structureFormatter));
actualStructure = structureFormatter.finish();
}
TextFormatter formatter = new TextFormatter();
node.accept(new SourcePrinter(formatter));
String actual = formatter.finish();
Node expectedNode = parse(source);
assertNotNull(expectedNode);
if (CHECK_POSITIONS) {
StructureFormatter structureFormatter = StructureFormatter.formatterWithPositions();
expectedNode.accept(new SourcePrinter(structureFormatter));
String masterStructure = structureFormatter.finish();
assertEquals(masterStructure, actualStructure);
}
formatter = new TextFormatter();
expectedNode.accept(new SourcePrinter(formatter));
String master = formatter.finish();
assertEquals(master, actual);
// Check for resilience to error nodes being present in the AST
Project project = psiFile.getProject();
final PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
final Document document = manager.getDocument(psiFile);
assertNotNull(document);
// fixed seed for test reproducibility
final Random random = new Random(0L);
for (int i = 0; i < 500; i++) {
WriteCommandAction.runWriteCommandAction(project, new Runnable() {
@Override
public void run() {
int pos = random.nextInt(document.getTextLength() - 1);
char ch = (char) (random.nextInt(64) + 32);
double operation = random.nextDouble();
if (operation < 0.33) {
document.insertString(pos, Character.toString(ch));
} else if (operation < 0.67) {
document.replaceString(pos, pos + 1, Character.toString(ch));
} else {
document.deleteString(pos, pos + 1);
}
manager.commitDocument(document);
}
});
node = LombokPsiConverter.convert(psiJavaFile);
assertNotNull(psiJavaFile.getText(), node);
}
}
Aggregations