<commit_before>package com.jetbrains.python.codeInsight.testIntegration;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PythonFileType;
import com.jetbrains.python.codeInsight.imports.AddImportHelper;
import com.jetbrains.python.psi.*;

import java.util.List;

/**
 * User: catherine
 */
public class PyTestGenerator {
  public PyTestGenerator() {
  }
  private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.codeInsight.testIntegration.PyTestGenerator");
  public PsiElement generateTest(final Project project, final CreateTestDialog dialog) {
    return PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(new Computable<PsiElement>() {
      public PsiElement compute() {
        return ApplicationManager.getApplication().runWriteAction(new Computable<PsiElement>() {
          public PsiElement compute() {
            try {
              IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();

              String fileName = dialog.getFileName();
              if (!fileName.endsWith(".py"))
                fileName = fileName + "." + PythonFileType.INSTANCE.getDefaultExtension();

              StringBuilder fileText = new StringBuilder();
              fileText.append("class ").append(dialog.getClassName()).append("(TestCase):\n  ");
              List<String> methods = dialog.getMethods();
              if (methods.size() == 0)
                fileText.append("pass\n");

              for (String method : methods)
                fileText.append("def ").append(method).append("(self):\n    self.fail()\n\n");

              PsiFile psiFile = PyUtil.getOrCreateFile(
                dialog.getTargetDir() + "/" + fileName, project);
              AddImportHelper.addImportFrom(psiFile, "unittest", "TestCase", null, AddImportHelper.ImportPriority.BUILTIN);

              PyElement createdClass = PyElementGenerator.getInstance(project).createFromText(
                LanguageLevel.forElement(psiFile), PyClass.class,
                                                           fileText.toString());
              createdClass = (PyElement)psiFile.addAfter(createdClass, psiFile.getLastChild());

              createdClass.navigate(false);
              return psiFile;
            }
            catch (IncorrectOperationException e) {
              LOG.warn(e);
              return null;
            }
          }
        });
      }
    });
  }
}<commit_msg>fixed PY-4731 Create new test for class: respect user indentation settings on creation<commit_after>package com.jetbrains.python.codeInsight.testIntegration;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PythonFileType;
import com.jetbrains.python.codeInsight.imports.AddImportHelper;
import com.jetbrains.python.psi.*;

import java.util.List;

/**
 * User: catherine
 */
public class PyTestGenerator {
  public PyTestGenerator() {
  }
  private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.codeInsight.testIntegration.PyTestGenerator");
  public PsiElement generateTest(final Project project, final CreateTestDialog dialog) {
    return PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(new Computable<PsiElement>() {
      public PsiElement compute() {
        return ApplicationManager.getApplication().runWriteAction(new Computable<PsiElement>() {
          public PsiElement compute() {
            try {
              IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();

              String fileName = dialog.getFileName();
              if (!fileName.endsWith(".py"))
                fileName = fileName + "." + PythonFileType.INSTANCE.getDefaultExtension();

              StringBuilder fileText = new StringBuilder();
              fileText.append("class ").append(dialog.getClassName()).append("(TestCase):\n  ");
              List<String> methods = dialog.getMethods();
              if (methods.size() == 0)
                fileText.append("pass\n");

              for (String method : methods)
                fileText.append("def ").append(method).append("(self):\n    self.fail()\n\n");

              PsiFile psiFile = PyUtil.getOrCreateFile(
                dialog.getTargetDir() + "/" + fileName, project);
              AddImportHelper.addImportFrom(psiFile, "unittest", "TestCase", null, AddImportHelper.ImportPriority.BUILTIN);

              PyElement createdClass = PyElementGenerator.getInstance(project).createFromText(
                LanguageLevel.forElement(psiFile), PyClass.class,
                                                           fileText.toString());
              createdClass = (PyElement)psiFile.addAfter(createdClass, psiFile.getLastChild());

              CodeStyleManager.getInstance(project).reformat(psiFile);
              createdClass.navigate(false);
              return psiFile;
            }
            catch (IncorrectOperationException e) {
              LOG.warn(e);
              return null;
            }
          }
        });
      }
    });
  }
}
{'fraction_non_alphanumeric': 0.10722689075630253, 'fraction_numerical': 0.0010084033613445378, 'mean_word_length': 2.8567725210628647, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5631920', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1538, 'n_words': 255}