Skip to content

Commit

Permalink
add more tests and setCommandPath in PushToTeXworks (JabRef#3197)
Browse files Browse the repository at this point in the history
Co-Authored-By: JohannBiorck <85625348+johannbiorck@users.noreply.github.com>
Co-Authored-By: LACHIRI ILIAS <67273129+lachiri-ilias@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 29, 2024
1 parent 5b69389 commit bd834b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
28 changes: 17 additions & 11 deletions src/main/java/org/jabref/gui/push/PushToTeXworks.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public PushToTeXworks(DialogService dialogService, PreferencesService preference
*
* @return The display name for the push operation.
*/

@Override
public String getDisplayName() {
return NAME;
Expand All @@ -37,23 +36,30 @@ public String getDisplayName() {
*
* @return The icon for the TeXworks application.
*/

@Override
public JabRefIcon getApplicationIcon() {
return IconTheme.JabRefIcons.DEFAULT_GROUP_ICON; // Temporary Icon that needs to be changed
}


/**
* Constructs the command line arguments for pushing citations to TeXworks.
* The method formats the citation key and prefixes/suffixes as per user preferences
* before invoking TeXworks with the command to insert text.
*
* @param keyString The citation key to be pushed.
* @return An array of {@code String} containing the command line to execute.
*/
/**
* Constructs the command line arguments for pushing citations to TeXworks.
* The method formats the citation key and prefixes/suffixes as per user preferences
* before invoking TeXworks with the command to insert text.
*
* @param keyString The citation key to be pushed.
* @return An array of {@code String} containing the command line to execute.
*/
@Override
protected String[] getCommandLine(String keyString) {
return new String[] {commandPath, "--insert-text", "%s%s%s".formatted(getCitePrefix(), keyString, getCiteSuffix())};
}

/**
* Gets the tooltip for the TeXworks push operation. The tooltip is used to display a short description of the push operation in the GUI.
*
* @param newCommandPath The new command path to be set.
*/
public void setCommandPath(String newCommandPath) {
commandPath = newCommandPath;
}
}
42 changes: 26 additions & 16 deletions src/test/java/org/jabref/gui/push/PushToTeXworksTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package org.jabref.gui.push;

import java.util.Map;

import javafx.beans.property.SimpleMapProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;

import org.jabref.gui.DialogService;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.icon.JabRefIcon;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.push.CitationCommandString;
import org.jabref.preferences.PreferencesService;
import org.jabref.preferences.PushToApplicationPreferences;
Expand All @@ -23,13 +27,12 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;


import javafx.beans.property.MapProperty;
import java.util.Arrays;


// @Disabled("Needs running TeXworks in the background. Start TeXworks with &")
class PushToTeXworksTest {

private PushToTeXworks pushToTeXworks;

@BeforeEach
Expand All @@ -43,28 +46,25 @@ public void setup() {

// Mock the return value for getCommandPaths()
String teXworksClientPath = "/usr/bin/texworks"; // For linux OS tobe changed in Windows and Mac
String displayName = "TeXworks";
String displayName = "TeXworks";

Map<String, String> commandPaths = Map.of(displayName, teXworksClientPath);
ObservableMap<String, String> observableCommandPaths = FXCollections.observableMap(commandPaths);
when(pushToApplicationPreferences.getCommandPaths()).thenReturn(new SimpleMapProperty<>(observableCommandPaths));
when(preferencesService.getPushToApplicationPreferences()).thenReturn(pushToApplicationPreferences);



//Mock the return value for getCiteCommand()
CitationCommandString mockCiteCommand = mock(CitationCommandString.class);
when(mockCiteCommand.prefix()).thenReturn("");
when(mockCiteCommand.suffix()).thenReturn("");
when(externalApplicationsPreferences.getCiteCommand()).thenReturn(mockCiteCommand);

// Mock the return value for getExternalApplicationsPreferences()
when(preferencesService.getExternalApplicationsPreferences()).thenReturn(externalApplicationsPreferences);
when(preferencesService.getExternalApplicationsPreferences()).thenReturn(externalApplicationsPreferences);

// Create a new instance of PushToTeXworks
pushToTeXworks = new PushToTeXworks(dialogService, preferencesService);


// Verify that the command path is correctly set in the preferences
MapProperty<String, String> actualCommandPaths = pushToApplicationPreferences.getCommandPaths();
Map<String, String> actualMap = actualCommandPaths.get();
Expand All @@ -85,23 +85,23 @@ void testDisplayName() {
assertEquals("TeXworks", pushToTeXworks.getDisplayName());
}


/**
* To verify that the PushToTeXworks class correctly returns the command line for TeXworks.
* The command line is used to execute the application from the command line.
*
* Method: getCommandLine()
*
* [TO FIX]: Find a way set a command in the test
*
*/
@Ignore("Needs to be fixed. Find a way to set a command in the test.")
@Test
void testGetCommandLine() {
String keyString = "TestKey";
String[] expectedCommand = new String[] {"/usr/bin/texworks", "--insert-text", "TestKey"};


pushToTeXworks.setCommandPath("/usr/bin/texworks"); // TO add the function

String[] actualCommand = pushToTeXworks.getCommandLine(keyString);

assertArrayEquals(expectedCommand, actualCommand);
}

/**
Expand All @@ -116,6 +116,16 @@ void testGetCommandLine() {
void pushEntries() {
pushToTeXworks.pushEntries(null, null, "key1,key2");
}
}


/**
* To verify that the PushToTeXworks class correctly returns the tooltip for TeXworks.
* The tooltip is used to display a short description of the application in the GUI.
*
* Method: getTooltip()
*
*/
@Test
void testGetTooltip() {
assertEquals("Push entries to external application (TeXworks)", pushToTeXworks.getTooltip());
}
}

0 comments on commit bd834b9

Please sign in to comment.