Skip to content

Commit

Permalink
Merge pull request #39 from bia-technologies/feature/27
Browse files Browse the repository at this point in the history
27 / Генерация моков для метода
  • Loading branch information
alkoleft authored Feb 28, 2024
2 parents d283cd5 + 5103ba8 commit 0e28eb5
Show file tree
Hide file tree
Showing 19 changed files with 437 additions and 77 deletions.
Binary file added viewer/icons/full/actions16/mock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions viewer/icons/full/actions16/mock.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class TemplatesProvider {

public static final String TEST_MODULE = "module"; //$NON-NLS-1$
public static final String TEST_METHOD = "method"; //$NON-NLS-1$
public static final String MOCK_FUNCTION = "mock_function"; //$NON-NLS-1$
public static final String MOCK_PROCEDURE = "mock_procedure"; //$NON-NLS-1$
private static final String BSL_FILE_EXTENSION = "bsl"; //$NON-NLS-1$
private static final String FOLDER_RU = "/templates/ru/"; //$NON-NLS-1$
private static final String FOLDER_EN = "/templates/en/"; //$NON-NLS-1$
Expand All @@ -57,6 +59,14 @@ public Optional<String> getTestSuiteStructureTemplateString(IV8Project project)
return Optional.ofNullable(getStringTemplate(project, TEST_MODULE));
}

public Optional<String> getMockFunctionTemplateString(IV8Project project) {
return Optional.ofNullable(getStringTemplate(project, MOCK_FUNCTION));
}

public Optional<String> getMockProcedureTemplateString(IV8Project project) {
return Optional.ofNullable(getStringTemplate(project, MOCK_PROCEDURE));
}

private Optional<URL> getBundleEntry(String path) {
URL url = TemplatesProvider.class.getResource(path);
return Optional.ofNullable(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package ru.biatech.edt.junit.ui.commands;

import com._1c.g5.v8.dt.bsl.model.Module;
import com._1c.g5.v8.dt.core.platform.IConfigurationProject;
import com._1c.g5.v8.dt.metadata.mdclass.CommonModule;
import lombok.experimental.UtilityClass;
import org.eclipse.core.commands.ExecutionEvent;
Expand All @@ -27,12 +28,14 @@
import org.eclipse.xtext.ui.editor.XtextEditor;
import ru.biatech.edt.junit.ui.dialogs.Dialogs;
import ru.biatech.edt.junit.ui.editor.EditorHelper;
import ru.biatech.edt.junit.ui.testitemaction.GenerateMock;
import ru.biatech.edt.junit.ui.testitemaction.GotoTestCaseAction;
import ru.biatech.edt.junit.ui.testitemaction.GotoTestSuiteAction;
import ru.biatech.edt.junit.ui.testitemaction.ITestItemAction;
import ru.biatech.edt.junit.ui.testitemaction.NewTestCaseAction;
import ru.biatech.edt.junit.ui.testitemaction.NewTestSuiteAction;
import ru.biatech.edt.junit.ui.testitemaction.UpdateTestSuiteAction;
import ru.biatech.edt.junit.v8utils.Projects;
import ru.biatech.edt.junit.yaxunit.Constants;
import ru.biatech.edt.junit.yaxunit.Engine;

Expand Down Expand Up @@ -82,6 +85,7 @@ private void execute(final Module module, final String methodName) {
private List<ITestItemAction> getActions(Module module, String methodName) {
var actions = new ArrayList<ITestItemAction>();
var testSuiteName = Engine.getTestSuiteName(module);
var isConfiguration = Projects.getParentProject(module) instanceof IConfigurationProject;

var existingTestSuites = Engine.getTestSuites(module).collect(Collectors.toList());
var existingTestModules = existingTestSuites.stream()
Expand All @@ -105,6 +109,9 @@ private List<ITestItemAction> getActions(Module module, String methodName) {
.filter(tm -> tm.getName().equals(methodName) || tm.getName().startsWith(methodName + '_'))
.map(m -> new GotoTestCaseAction(testSuite, m.getName())))
.forEach(actions::add);
if (isConfiguration) {
actions.add(new GenerateMock(module, methodName));
}
}
return actions;
}
Expand Down
26 changes: 13 additions & 13 deletions viewer/src/main/java/ru/biatech/edt/junit/ui/dialogs/Dialogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,32 @@
@UtilityClass
public class Dialogs {

public Optional<IExtensionProject> selectTestProject(EObject source, String sourceName) {
var projectsStream = source != null ? Engine.getTestProjects(source) : Engine.getTestProjects();
var projects = projectsStream.collect(Collectors.toMap(IV8Project::getProject, Function.identity()));

public Optional<IExtensionProject> selectProject(List<IExtensionProject> projects, String message) {
if (projects.size() == 1) {
return Optional.of(projects.values().iterator().next());
return Optional.of(projects.get(0));
}

var projectsMap = projects.stream().collect(Collectors.toMap(IV8Project::getProject, Function.identity()));
var shell = TestViewerPlugin.ui().getActiveWorkbenchShell();
if (projects.size() > 1) {
var message = MessageFormat.format(Messages.Dialogs_Select_TestProjects_ForTestSuite, sourceName);
var result = selectItem(projects.keySet(), Messages.Dialogs_Select_Projects_Title, message, null);
var result = selectItem(projectsMap.keySet(), Messages.Dialogs_Select_Project_Title, message, null);
if (result != null) {
return Optional.of(projects.get(result));
return Optional.of(projectsMap.get(result));
}
} else {
var message = Messages.Message_RelatedProjectsNotFound;
if (source != null) {
message += source.toString();
}
MessageDialog.openInformation(shell, Messages.Dialogs_Information_Title, message);
var notFoundMessage = Messages.Message_RelatedProjectsNotFound;
MessageDialog.openInformation(shell, Messages.Dialogs_Information_Title, notFoundMessage);
}

return Optional.empty();
}

public Optional<IExtensionProject> selectTestProject(EObject source) {
var projectsStream = source != null ? Engine.getTestProjects(source) : Engine.getTestProjects();
String message = MessageFormat.format(Messages.Dialogs_Select_TestProject_ForTestSuite, Present.getPresent(source));
return selectProject(projectsStream.collect(Collectors.toList()), message);
}

public MethodReference selectMethodReference(List<MethodReference> items) {
return selectItem(items, Messages.Dialogs_Select_Method_Title, Messages.Dialogs_Select_Method_Message, new MethodReferenceLabelProvider());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ public class Messages extends NLS {
public static String Dialogs_Select_Module_Title;
public static String Dialogs_Select_Module_Message;

public static String Dialogs_Select_TestProjects_ForTestSuite;
public static String Dialogs_Select_TestProject_ForTestSuite;
public static String Message_RelatedProjectsNotFound;
public static String Dialogs_Select_Action_Message;
public static String Dialogs_Select_Action_Title;
public static String Dialogs_Select_Method_Message;
public static String Dialogs_Select_Method_Title;
public static String Dialogs_Select_Projects_Title;
public static String Dialogs_Select_Project_Title;
public static String Dialogs_Select_Project_ForMock;

static {
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Dialogs_Select_Method_Title=\u0412\u044B\u0431\u043E\u0440 \u043C\u0435\u0442\u0
Dialogs_Select_Methods_ForTestSuite=\u041C\u0435\u0442\u043E\u0434\u044B \u0434\u043B\u044F \u0434\u043E\u0431\u0430\u0432\u043B\u0435\u043D\u0438\u044F \u0432 \u0442\u0435\u0441\u0442\u043E\u0432\u044B\u0439 \u043D\u0430\u0431\u043E\u0440 {0}
Dialogs_Select_Module_Message=\u0412\u044B\u0431\u043E\u0440 \u043C\u043E\u0434\u0443\u043B\u044F
Dialogs_Select_Module_Title=\u0412\u044B\u0431\u043E\u0440 \u043C\u043E\u0434\u0443\u043B\u044F
Dialogs_Select_Projects_Title=\u0412\u044B\u0431\u043E\u0440 \u043F\u0440\u043E\u0435\u043A\u0442\u0430
Dialogs_Select_TestProjects_ForTestSuite=\u0412\u044B\u0431\u0435\u0440\u0438\u0442\u0435 \u043F\u0440\u043E\u0435\u043A\u0442, \u0432 \u043A\u043E\u0442\u043E\u0440\u044B\u0439 \u043D\u0435\u043E\u0431\u0445\u043E\u0434\u0438\u043C\u043E \u0434\u043E\u0431\u0430\u0432\u0438\u0442\u044C \u0442\u0435\u0441\u0442\u043E\u0432\u044B\u0439 \u043D\u0430\u0431\u043E\u0440 {0}
Dialogs_Select_Project_ForMock=\u0412\u044B\u0431\u0435\u0440\u0438\u0442\u0435 \u043F\u0440\u043E\u0435\u043A\u0442, \u0432 \u043A\u043E\u0442\u043E\u0440\u044B\u0439 \u043D\u0435\u043E\u0431\u0445\u043E\u0434\u0438\u043C\u043E \u0434\u043E\u0431\u0430\u0432\u0438\u0442\u044C \u043C\u043E\u043A \u0434\u043B\u044F {0}
Dialogs_Select_Project_Title=\u0412\u044B\u0431\u043E\u0440 \u043F\u0440\u043E\u0435\u043A\u0442\u0430
Dialogs_Select_TestProject_ForTestSuite=\u0412\u044B\u0431\u0435\u0440\u0438\u0442\u0435 \u043F\u0440\u043E\u0435\u043A\u0442, \u0432 \u043A\u043E\u0442\u043E\u0440\u044B\u0439 \u043D\u0435\u043E\u0431\u0445\u043E\u0434\u0438\u043C\u043E \u0434\u043E\u0431\u0430\u0432\u0438\u0442\u044C \u0442\u0435\u0441\u0442\u043E\u0432\u044B\u0439 \u043D\u0430\u0431\u043E\u0440 {0}
Message_RelatedProjectsNotFound=\u0421\u043D\u0430\u0447\u0430\u043B\u0430 \u0441\u043E\u0437\u0434\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043D\u0438\u0435 \u0434\u043B\u044F \u0442\u0435\u0441\u0442\u043E\u0432, \u0441\u0432\u044F\u0437\u0430\u043D\u043D\u043E\u0435 \u0441 \u043F\u0440\u043E\u0435\u043A\u0442\u043E\u043C.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*******************************************************************************
* Copyright (c) 2024 BIA-Technologies Limited Liability Company.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/

package ru.biatech.edt.junit.ui.editor;

import com._1c.g5.v8.dt.bsl.model.Module;
import com._1c.g5.v8.dt.bsl.ui.editor.BslXtextDocument;
import com._1c.g5.v8.dt.core.platform.IV8Project;
import lombok.Data;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.ui.editor.XtextEditor;
import ru.biatech.edt.junit.v8utils.Projects;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Data
public class EditionContext {
private final XtextEditor editor;
private final BslXtextDocument document;
private final Module module;
private final IV8Project project;
private final List<TextEdit> changes = new ArrayList<>();
private IParser parser;

public EditionContext(XtextEditor editor) {
this.editor = editor;
document = EditorHelper.getDocument(editor);
module = EditorHelper.getParsedModule(document);
project = Projects.getParentProject(module);
}

public IParser getParser() {
if (parser == null) {
document.readOnly(state -> parser = state.getParser());
}
return parser;
}

public void insert(int offset, String text) {
changes.add(new InsertEdit(offset, text));
}

public void replace(int offset, int length, String content) {
changes.add(new ReplaceEdit(offset, length, content));
}

public void append(String text) {
changes.add(new InsertEdit(document.getLength(), text));
}

public int lastOffset() {
var ordered = changes.stream()
.sorted(Comparator.comparingInt(TextEdit::getOffset))
.toArray(TextEdit[]::new);
return ordered[ordered.length - 1].getOffset() + 2;

}

public boolean apply() {
return EditorHelper.applyChanges(document, changes);
}

public void revealLast() {
editor.selectAndReveal(lastOffset(), 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.eclipse.handly.snapshot.NonExpiringSnapshot;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.ui.IEditorInput;
Expand Down Expand Up @@ -174,7 +173,7 @@ public boolean isCommentNode(ILeafNode leafNode) {
&& "SL_COMMENT".equalsIgnoreCase(((TerminalRule) leafNode.getGrammarElement()).getName()); //$NON-NLS-1$
}

public boolean applyChanges(IDocument document, List<InsertEdit> changes) {
public boolean applyChanges(IDocument document, List<TextEdit> changes) {

var textEdit = new MultiTextEdit();
changes.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*******************************************************************************
* Copyright (c) 2024 BIA-Technologies Limited Liability Company.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/

package ru.biatech.edt.junit.ui.testitemaction;

import com._1c.g5.v8.dt.bsl.model.Module;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.swt.graphics.Image;
import ru.biatech.edt.junit.TestViewerPlugin;
import ru.biatech.edt.junit.ui.viewsupport.ImageProvider;
import ru.biatech.edt.junit.ui.viewsupport.LabelStylerFactory;
import ru.biatech.edt.junit.yaxunit.MockCreator;

import java.text.MessageFormat;

public class GenerateMock implements ITestItemAction {
private final Module module;
private final String methodName;

public GenerateMock(Module module, String methodName) {
this.module = module;
this.methodName = methodName;
}

@Override
public String getPresent() {
return INDENT + MessageFormat.format(Messages.GenerateMock_Present, methodName);
}

@Override
public Image getIcon(ImageProvider provider) {
return provider.getActionNewMock();
}

@Override
public StyledString getStyledString() {
return LabelStylerFactory.format(INDENT + Messages.GenerateMock_Present, StyledString.COUNTER_STYLER, methodName);
}

@Override
public void run() {
var creator = new MockCreator(module, methodName);

try {
creator.createMock();
} catch (CoreException e) {
TestViewerPlugin.log().logError("Не удалось создать мок", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Messages extends NLS {
public static String UpdateTestSuiteAction_Description;
public static String UpdateTestSuiteAction_NoMethodsToAdd;
public static String UpdateTestSuiteAction_Present;
public static String GenerateMock_Present;

static {
// initialize resource bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#################################################################################
GenerateMock_Present=\u0421\u043E\u0437\u0434\u0430\u0442\u044C/\u043E\u0431\u043D\u043E\u0432\u0438\u0442\u044C \u043C\u043E\u043A \u0434\u043B\u044F {0}
GotoTestCaseAction_Present=\u041F\u0435\u0440\u0435\u0439\u0442\u0438 \u043A \u0442\u0435\u0441\u0442\u0443 {0}
GotoTestSuiteAction_Present=\u041F\u0435\u0440\u0435\u0439\u0442\u0438 \u043A \u0442\u0435\u0441\u0442\u043E\u0432\u043E\u043C\u0443 \u043D\u0430\u0431\u043E\u0440\u0443 (\u043C\u043E\u0434\u0443\u043B\u044E) {0}
NewTestCaseAction_Failed=\u0412\u043E \u0432\u0440\u0435\u043C\u044F \u0441\u043E\u0437\u0434\u0430\u043D\u0438\u044F \u0442\u0435\u0441\u0442\u043E\u0432\u043E\u0433\u043E \u043C\u0435\u0442\u043E\u0434\u0430 \u0432\u043E\u0437\u043D\u0438\u043A\u043B\u0430 \u043E\u0448\u0438\u0431\u043A\u0430
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class ImageProvider {
public static final String ACTION_GOTO_METHOD = "actions16/goto-method.png"; //$NON-NLS-1$
public static final String ACTION_COMPARE = "actions16/compare.png"; //$NON-NLS-1$
public static final String ACTION_COMPARE_DISABLED = "actions16/compare-disable.png"; //$NON-NLS-1$
public static final String ACTION_NEW_MOCK = "actions16/mock.png"; //$NON-NLS-1$

@Getter
private final ImageDescriptor suiteIconDescriptor = getImageDescriptor(SUITE_ICON);
Expand Down Expand Up @@ -138,6 +139,8 @@ public class ImageProvider {
private final Image newTestSuite = createManagedImage(ACTION_NEW_TESTSUITE);
@Getter(lazy = true)
private final Image actionNewTestCase = createManagedImage(ACTION_NEW_TESTCASE);
@Getter(lazy = true)
private final Image actionNewMock = createManagedImage(ACTION_NEW_MOCK);

public static ImageDescriptor getImageDescriptor(String relativePath) {
return TestViewerPlugin.ui().getImageDescriptor(relativePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com._1c.g5.v8.dt.bsl.model.Method;
import com._1c.g5.v8.dt.bsl.model.Module;
import com._1c.g5.v8.dt.metadata.mdclass.CommonModule;
import com._1c.g5.v8.dt.metadata.mdclass.MdObject;
import com._1c.g5.v8.dt.ui.util.Labeler;
import lombok.experimental.UtilityClass;
import org.eclipse.core.resources.IProject;
Expand All @@ -33,7 +34,7 @@ public String getPresent(Object item) {

} else if (item instanceof Module) {
return getPresent((Module) item);
} else if (item instanceof CommonModule) {
} else if (item instanceof MdObject) {
return getPresent((CommonModule) item);
}
return item.toString();
Expand All @@ -46,14 +47,14 @@ public String getPresent(Method item) {
public String getPresent(Module item) {
return Labeler.path(item, '.')
.skipCommonNode()
.stopAfter(IProject.class)
.stopBefore(IProject.class)
.label();
}

public String getPresent(CommonModule item) {
public String getPresent(MdObject item) {
return Labeler.path(item, '.')
.skipCommonNode()
.stopAfter(IProject.class)
.stopBefore(IProject.class)
.label();
}
public String getShortPresent(CommonModule item) {
Expand Down
Loading

0 comments on commit 0e28eb5

Please sign in to comment.