Skip to content

Commit d1e45c3

Browse files
committed
[tests] Refactorings->Rename doesn't work for TypeScript files #1209
A JUnit test case is added for the issue Issue: #1209
1 parent b88febb commit d1e45c3

File tree

1 file changed

+66
-29
lines changed
  • org.eclipse.wildwebdeveloper.tests/src/org/eclipse/wildwebdeveloper/tests

1 file changed

+66
-29
lines changed

org.eclipse.wildwebdeveloper.tests/src/org/eclipse/wildwebdeveloper/tests/TestJsTs.java

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018, 2022 Red Hat Inc. and others.
2+
* Copyright (c) 2018, 2023 Red Hat Inc. and others.
33
*
44
* This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0
@@ -73,14 +73,31 @@ public void setUpProject() throws CoreException {
7373

7474
@Test
7575
@Timeout(value = 60, unit = TimeUnit.SECONDS)
76-
public void testRefactoringRename() throws Exception {
76+
public void testRefactoringRenameInTypeaScript() throws Exception {
77+
final IFile file = project.getFile("TestJsTs.ts");
78+
String content = "function testVar(test) {\n if (\"truetrue\" == \"true\" + test ) {\n"
79+
+ " return true;\n }\n return false;\n}\n"
80+
+ "print(\"Testing var with true argument == \" + testVar(true));\n"
81+
+ "print(\"Testing var with false argument == \" + testVar(false));\n";
82+
final String oldName = "testVar";
83+
final String newName = "newName";
84+
internalTestRename(file, content, oldName, newName);
85+
}
86+
87+
@Test
88+
@Timeout(value = 60, unit = TimeUnit.SECONDS)
89+
public void testRefactoringRenameInJavaScript() throws Exception {
7790
final IFile file = project.getFile("TestJsTs.js");
7891
String content = "function testVar(test) {\n if (\"truetrue\" == \"true\" + test ) {\n"
7992
+ " return true;\n }\n return false;\n}\n"
8093
+ "print(\"Testing var with true argument == \" + testVar(true));\n"
8194
+ "print(\"Testing var with false argument == \" + testVar(false));\n";
8295
final String oldName = "testVar";
8396
final String newName = "newName";
97+
internalTestRename(file, content, oldName, newName);
98+
}
99+
100+
private void internalTestRename(IFile file, String content, String oldName, String newName) throws Exception {
84101
String newContent = content.replaceAll(oldName, newName);
85102

86103
int offset = content.indexOf(oldName);
@@ -113,41 +130,61 @@ public void testRefactoringRename() throws Exception {
113130
Listener pressOKonRenameDialogPaint = event -> {
114131
if (event.widget instanceof Composite c) {
115132
Shell shell = c.getShell();
116-
if (shell != ideShell && shell.getData().getClass().getName().startsWith(WIZARD_CLASSNAME_TEMPLATE)) {
117-
if (!newTextIsSet.get()) {
118-
newTextIsSet.set(setNewText(c, newName));
119-
System.out.println("testRefactoringRename(): New name is set: " + newName);
120-
}
121-
Set<String> buttons = getButtons(c);
122-
if (WIZARD_RENAME.equals(shell.getText())) {
123-
if (!renameDialogOkPressed.get()) {
124-
if (buttons.contains(BUTTON_OK)) {
125-
System.out.println(
126-
"testRefactoringRename(): WIZARD_RENAME Emulating pressOK when BUTTON_OK");
127-
event.widget.getDisplay().asyncExec(() -> pressOk(shell));
128-
renameDialogOkPressed.set(true);
133+
if (shell != ideShell) {
134+
if (shell.getData().getClass().getName().startsWith(WIZARD_CLASSNAME_TEMPLATE)) {
135+
if (!newTextIsSet.get()) {
136+
newTextIsSet.set(setNewText(c, newName));
137+
System.out.println("testRefactoringRename(): New name is set: " + newName);
138+
}
139+
Set<String> buttons = getButtons(c);
140+
if (WIZARD_RENAME.equals(shell.getText())) {
141+
if (!renameDialogOkPressed.get()) {
142+
if (buttons.contains(BUTTON_OK)) {
143+
System.out.println(
144+
"testRefactoringRename(): WIZARD_RENAME Emulating pressOK when BUTTON_OK");
145+
event.widget.getDisplay().asyncExec(() -> pressOk(shell));
146+
renameDialogOkPressed.set(true);
147+
}
148+
} else if (!renameDialogContinuePressed.get()) {
149+
if (buttons.contains(BUTTON_CONTINUE)) {
150+
System.out.println(
151+
"testRefactoringRename(): WIZARD_RENAME Emulating pressOK when BUTTON_CONTINUE");
152+
event.widget.getDisplay().asyncExec(() -> pressOk(shell));
153+
renameDialogContinuePressed.set(true);
154+
} else if (!renameDialogCancelPressed.get() && buttons.contains(BUTTON_CANCEL)
155+
&& buttons.contains(BUTTON_BACK)) {
156+
System.out.println(
157+
"testRefactoringRename(): WIZARD_RENAME Emulating pressCancel when BUTTON_CANCEL & BUTTON_BACK");
158+
event.widget.getDisplay().asyncExec(() -> pressCancel(shell));
159+
renameDialogCancelPressed.set(true);
160+
}
129161
}
130-
} else if (!renameDialogContinuePressed.get()) {
131-
if (buttons.contains(BUTTON_CONTINUE)) {
132-
System.out.println(
133-
"testRefactoringRename(): WIZARD_RENAME Emulating pressOK when BUTTON_CONTINUE");
134-
event.widget.getDisplay().asyncExec(() -> pressOk(shell));
135-
renameDialogContinuePressed.set(true);
136-
} else if (!renameDialogCancelPressed.get() && buttons.contains(BUTTON_CANCEL)
137-
&& buttons.contains(BUTTON_BACK)) {
138-
System.out.println(
139-
"testRefactoringRename(): WIZARD_RENAME Emulating pressCancel when BUTTON_CANCEL & BUTTON_BACK");
140-
event.widget.getDisplay().asyncExec(() -> pressCancel(shell));
141-
renameDialogCancelPressed.set(true);
162+
} else if (WIZARD_REFACTORING.equals(shell.getText())) {
163+
if (!errorDialogOkPressed.get()) {
164+
if (buttons.contains(BUTTON_OK)) {
165+
System.out.println(
166+
"testRefactoringRename(): WIZARD_REFACTORING Emulating pressOK when BUTTON_OK");
167+
event.widget.getDisplay().asyncExec(() -> pressOk(shell));
168+
errorDialogOkPressed.set(true);
169+
}
142170
}
143171
}
144-
} else if (WIZARD_REFACTORING.equals(shell.getText())) {
172+
} else if (shell.getData().getClass().getName()
173+
.startsWith("org.eclipse.jface.dialogs.MessageDialog")) {
174+
// Most probably it's "The Rename request is not valid at the given position"
175+
// -like error
176+
Set<String> buttons = getButtons(c);
145177
if (!errorDialogOkPressed.get()) {
146178
if (buttons.contains(BUTTON_OK)) {
147179
System.out.println(
148-
"testRefactoringRename(): WIZARD_REFACTORING Emulating pressOK when BUTTON_OK");
180+
"testRefactoringRename(): MESSAGE_DIALOG Emulating pressOK when BUTTON_OK");
149181
event.widget.getDisplay().asyncExec(() -> pressOk(shell));
150182
errorDialogOkPressed.set(true);
183+
} else if (buttons.contains(BUTTON_CANCEL)) {
184+
System.out.println(
185+
"testRefactoringRename(): MESSAGE_DIALOG Emulating pressCancel when BUTTON_CANCEL");
186+
event.widget.getDisplay().asyncExec(() -> pressCancel(shell));
187+
errorDialogOkPressed.set(true); // Report as OK pressed just to say the dialog is closed
151188
}
152189
}
153190
}

0 commit comments

Comments
 (0)