void (@NotNull String fromImport, @NotNull String newName, @Nullable String anchorName, boolean before, @NotNull String result) { final PyElementGenerator generator = PyElementGenerator.getInstance(myFixture.getProject()); final LanguageLevel languageLevel = LanguageLevel.PYTHON27; final PyFromImportStatement fromImportElem = generator.createFromText(languageLevel, PyFromImportStatement.class, fromImport, new int[]{0}); final PyImportElement anchor; if (anchorName != null) { anchor = ContainerUtil.find(fromImportElem.getImportElements(), importElem -> importElem.getImportedQName().toString().equals(anchorName)); assertNotNull(anchor); } else { anchor = null; } final PyImportElement newNameElem = generator.createImportElement(languageLevel, newName, null); if (before) { fromImportElem.addBefore(newNameElem, anchor); } else { fromImportElem.addAfter(newNameElem, anchor); } assertEquals(result, fromImportElem.getText()); }