Skip to content

Commit

Permalink
Fixed main window blocking tests execution
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzodbr committed Jun 6, 2024
1 parent 2823ce5 commit 9e6f975
Showing 1 changed file with 68 additions and 9 deletions.
77 changes: 68 additions & 9 deletions src/test/java/it/univr/wordautomata/WordAutomataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,66 @@
import it.univr.wordautomata.model.State;
import it.univr.wordautomata.model.Transition;
import it.univr.wordautomata.utils.Methods;
import javafx.application.Platform;

/**
* Test class for {@link WordAutomata}.
*/
public class WordAutomataTest {

/**
* An instance of the WordAutomata class.
*/
private static final WordAutomata instance = new WordAutomata();

/**
* A list of test files.
*/
private static final List<File> testFiles = List.of(
Methods.getResource(WordAutomataTest.class, "tests", "deterministic.automata"),
Methods.getResource(WordAutomataTest.class, "tests", "rejected.automata"),
Methods.getResource(WordAutomataTest.class, "tests", "stats.automata"));

/**
* A list of test words.
*/
private static final List<String> testWords = List.of(
"xxxy",
"xy",
"yxy");

/**
* A list of test results.
*/
private static final List<Boolean> testResults = List.of(
true,
true,
true);
false);

/**
* A list of paths to test.
*/
private static final List<List<String>> testPaths = List.of(
List.of("xxx", "y"),
List.of("x", "y"),
List.of("yxy"));

/**
* Test method for performing path tests.
* Iterates through the test files and performs path tests for each file.
*/
@Test
public void testPath() {
for (int i = 0; i < testFiles.size(); i++)
performPathTest(testFiles.get(i), testWords.get(i), testResults.get(i));
}

/*
* Check if the path is (not) found correctly
/**
* Test method for performing path tests.
*
* @param file the file to test
* @param word the word to test
* @param found the expected result
*/
private void performPathTest(File file, String word, boolean found) {
DigraphEdgeList<State, Transition> graph = AutomataSaver.read(file, true);
Expand All @@ -62,7 +93,6 @@ private void performPathTest(File file, String word, boolean found) {
} else {
assertNull(path);
}

}

/*
Expand All @@ -75,6 +105,16 @@ public void testSaveAndRead() {
}
}

/**
* Performs the save and read test for a given file.
* Reads a DigraphEdgeList from the file, updates the graph in the Model,
* modifies the final state of each vertex, saves the modified graph to the
* file,
* reads the modified graph from the file, and asserts that the original and
* modified graphs are equal.
*
* @param file the file to perform the save and read test on
*/
private void performSaveAndReadTest(File file) {
DigraphEdgeList<State, Transition> graph = AutomataSaver.read(file, true);
assertNotNull(graph);
Expand All @@ -89,27 +129,46 @@ private void performSaveAndReadTest(File file) {
DigraphEdgeList<State, Transition> newGraph = AutomataSaver.read(file, true);
assertNotNull(newGraph);
assertEquals(graph, newGraph);

Model.getInstance().updateGraph(graph);

graph.vertices().forEach(v -> {
v.element().setFinal(!v.element().isFinal());
});

AutomataSaver.save(file, true);
}

/**
* Test case for the path GUI.
* This method starts the platform, runs the instance, and performs path GUI
* tests
* for each test file and test path.
*/
@Test
public void testPathGUI() {
instance.run();
Platform.startup(() -> {
instance.run();

for (int i = 0; i < testFiles.size(); i++)
performPathGUITest(testFiles.get(i), testPaths.get(i));
for (int i = 0; i < testFiles.size(); i++)
performPathGUITest(testFiles.get(i), testPaths.get(i));
});
}

/*
/**
* Check, when the path is found, if the GUI is updated correctly;
* otherwise, check if the GUI is updated correctly when the path is not found
*
* @param file the file to test
* @param expected the expected result
*/
private void performPathGUITest(File file, List<String> expected) {
DigraphEdgeList<State, Transition> graph = AutomataSaver.read(file, true);
assertNotNull(graph);
Model.getInstance().updateGraph(graph);

var children = instance.getComponents().getBottomBar().getTransitionsPanelHBox().getChildren();

System.err.println(children);
assertNotNull(children);
assertEquals(children.size(), expected.size());
Expand Down

0 comments on commit 9e6f975

Please sign in to comment.