use of com.thoughtworks.gauge.StepValue in project Intellij-Plugin by getgauge.
the class StepCompletionProviderTest method testStepAutoCompletion.
public void testStepAutoCompletion() throws Exception {
myFixture.configureByFiles("SimpleSpec.spec", "SimpleSpec.txt");
Gauge.addModule(myFixture.getModule(), new GaugeService(mockProcess, mockGaugeConnection));
StepValue stepValue1 = new StepValue("This is a step", "This is a step");
StepValue stepValue2 = new StepValue("Hello", "Hello");
StepValue stepValue3 = new StepValue("The step", "The step");
when(mockGaugeConnection.fetchAllSteps()).thenReturn(asList(stepValue1, stepValue2, stepValue3));
myFixture.getEditor().getCaretModel().moveToOffset(49);
myFixture.complete(CompletionType.BASIC, 1);
List<String> strings = myFixture.getLookupElementStrings();
assertEquals(2, strings.size());
assertTrue(strings.containsAll(asList("This is a step", "The step")));
}
use of com.thoughtworks.gauge.StepValue in project Intellij-Plugin by getgauge.
the class StepCompletionProviderTest method testStepAutoCompleteShouldReplaceBracesOfOnlyParams.
public void testStepAutoCompleteShouldReplaceBracesOfOnlyParams() throws Exception {
myFixture.configureByFiles("SimpleSpec.spec", "SimpleSpec.txt");
Gauge.addModule(myFixture.getModule(), new GaugeService(mockProcess, mockGaugeConnection));
StepValue stepValue1 = new StepValue("This is a step with {} and >", "This is a step with <param> and >", asList("param"));
when(mockGaugeConnection.fetchAllSteps()).thenReturn(asList(stepValue1));
myFixture.getEditor().getCaretModel().moveToOffset(49);
myFixture.complete(CompletionType.BASIC, 1);
String expected = "*This is a step with \"param\" and >";
String actual = myFixture.getEditor().getDocument().getText(new TextRange(47, 81));
assertEquals(expected, actual);
}
use of com.thoughtworks.gauge.StepValue in project Intellij-Plugin by getgauge.
the class CreateStepImplFix method addImpl.
private void addImpl(final Project project, final VirtualFile file) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
new WriteCommandAction.Simple(project) {
@Override
protected void run() throws Throwable {
PsiFile psifile = PsiManager.getInstance(project).findFile(file);
if (!FileModificationService.getInstance().prepareFileForWrite(psifile)) {
return;
}
PsiMethod addedStepImpl = addStepImplMethod(psifile);
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(addedStepImpl);
templateMethodName(addedStepImpl, builder);
templateParams(addedStepImpl, builder);
templateBody(addedStepImpl, builder);
userTemplateModify(builder);
}
}.execute();
}
private PsiMethod addStepImplMethod(PsiFile psifile) {
final PsiClass psiClass = PsiTreeUtil.getChildOfType(psifile, PsiClass.class);
PsiDocumentManager.getInstance(project).commitAllDocuments();
StepValue stepValue = step.getStepValue();
final StringBuilder text = new StringBuilder(String.format("@" + Step.class.getName() + "(\"%s\")\n", stepValue.getStepAnnotationText()));
text.append(String.format("public void %s(%s){\n\n", getMethodName(psiClass), getParamList(stepValue.getParameters())));
text.append("}\n");
final PsiMethod stepMethod = JavaPsiFacade.getElementFactory(project).createMethodFromText(text.toString(), psiClass);
PsiMethod addedElement = (PsiMethod) psiClass.add(stepMethod);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(addedElement);
CodeStyleManager.getInstance(project).reformat(psiClass);
addedElement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(addedElement);
return addedElement;
}
private String getParamList(List<String> params) {
StringBuilder paramlistBuilder = new StringBuilder();
for (int i = 0; i < params.size(); i++) {
paramlistBuilder.append("Object arg").append(i);
if (i != params.size() - 1) {
paramlistBuilder.append(", ");
}
}
return paramlistBuilder.toString();
}
private void templateMethodName(PsiMethod addedStepImpl, TemplateBuilder builder) {
PsiIdentifier methodName = addedStepImpl.getNameIdentifier();
builder.replaceElement(methodName, methodName.getText());
}
private void templateParams(PsiMethod addedElement, TemplateBuilder builder) {
PsiParameterList paramsList = addedElement.getParameterList();
PsiParameter[] parameters = paramsList.getParameters();
for (PsiParameter parameter : parameters) {
PsiElement nameIdentifier = parameter.getNameIdentifier();
PsiTypeElement typeElement = parameter.getTypeElement();
if (nameIdentifier != null) {
builder.replaceElement(typeElement, typeElement.getText());
builder.replaceElement(nameIdentifier, nameIdentifier.getText());
}
}
}
private void templateBody(PsiMethod addedElement, TemplateBuilder builder) {
final PsiCodeBlock body = addedElement.getBody();
if (body != null) {
builder.replaceElement(body, new TextRange(2, 2), "");
}
}
private void userTemplateModify(TemplateBuilder builder) {
Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, file, 0), true);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
if (editor != null) {
documentManager.doPostponedOperationsAndUnblockDocument(editor.getDocument());
builder.run(editor, false);
}
}
});
}
Aggregations