From 2099b9c99f9436679c763ccbbb4dfd49949aa989 Mon Sep 17 00:00:00 2001 From: hneemann Date: Wed, 11 Nov 2020 18:05:51 +0100 Subject: [PATCH] adds a test for the circuit modifier, see #541 --- .../digital/draw/model/ModelCreator.java | 8 +-- .../java/de/neemann/digital/gui/Main.java | 5 +- .../gui/components/CircuitComponent.java | 3 +- .../components/CircuitModifierPostClosed.java | 2 +- .../modification/ModifyAttribute.java | 7 ++ .../gui/components/CircuitModifierTest.java | 69 +++++++++++++++++++ .../digital/integration/TestExamples.java | 4 +- .../dig/test/{eeprom.dig_ => eeprom.dig} | 31 +-------- 8 files changed, 88 insertions(+), 41 deletions(-) create mode 100644 src/test/java/de/neemann/digital/gui/components/CircuitModifierTest.java rename src/test/resources/dig/test/{eeprom.dig_ => eeprom.dig} (84%) diff --git a/src/main/java/de/neemann/digital/draw/model/ModelCreator.java b/src/main/java/de/neemann/digital/draw/model/ModelCreator.java index 0d616db37..357142510 100644 --- a/src/main/java/de/neemann/digital/draw/model/ModelCreator.java +++ b/src/main/java/de/neemann/digital/draw/model/ModelCreator.java @@ -283,16 +283,16 @@ public class ModelCreator implements Iterable { /** * Creates the model. * - * @param runsInGui if true the wires are attached to the values + * @param attachWires if true the wires are attached to the values * @return the model * @throws PinException PinException * @throws NodeException NodeException */ - public Model createModel(boolean runsInGui) throws PinException, NodeException { - Model m = new Model().setAllowGlobalValues(runsInGui); + public Model createModel(boolean attachWires) throws PinException, NodeException { + Model m = new Model().setAllowGlobalValues(attachWires); for (Net n : netList) - n.interconnect(m, runsInGui); + n.interconnect(m, attachWires); for (ModelEntry e : entries) e.applyInputs(); diff --git a/src/main/java/de/neemann/digital/gui/Main.java b/src/main/java/de/neemann/digital/gui/Main.java index affe24352..6e9b281ab 100644 --- a/src/main/java/de/neemann/digital/gui/Main.java +++ b/src/main/java/de/neemann/digital/gui/Main.java @@ -43,13 +43,13 @@ import de.neemann.digital.gui.components.testing.TestAllDialog; import de.neemann.digital.gui.components.testing.ValueTableDialog; import de.neemann.digital.gui.components.tree.LibraryTreeModel; import de.neemann.digital.gui.components.tree.SelectTree; -import de.neemann.digital.gui.tutorial.InitialTutorial; import de.neemann.digital.gui.release.CheckForNewRelease; import de.neemann.digital.gui.remote.DigitalHandler; import de.neemann.digital.gui.remote.RemoteException; import de.neemann.digital.gui.remote.RemoteSever; import de.neemann.digital.gui.state.State; import de.neemann.digital.gui.state.StateManager; +import de.neemann.digital.gui.tutorial.InitialTutorial; import de.neemann.digital.hdl.printer.CodePrinter; import de.neemann.digital.hdl.verilog2.VerilogGenerator; import de.neemann.digital.hdl.vhdl2.VHDLGenerator; @@ -1384,7 +1384,8 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS circuitComponent.setCopy(modelCreator.getCircuit()); // Allows the model to modify the circuit - CircuitModifierPostClosed cmpc = new CircuitModifierPostClosed(circuitComponent); + CircuitModifierPostClosed cmpc = new CircuitModifierPostClosed( + modification -> SwingUtilities.invokeLater(() -> circuitComponent.modify(modification))); modelCreator.connectToGui(cmpc); model.addObserver(cmpc); diff --git a/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java b/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java index 23a396b92..61f0d3de3 100644 --- a/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java +++ b/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java @@ -52,7 +52,7 @@ import static de.neemann.digital.draw.shapes.GenericShape.SIZE2; * ToDo: refactoring of repaint logic. Its to complex now. * ToDo: class is to large, move the MouseController classes to their own package */ -public class CircuitComponent extends JComponent implements ChangedListener, LibraryListener, CircuitModifier { +public class CircuitComponent extends JComponent implements ChangedListener, LibraryListener { /** * The delete icon, also used from {@link de.neemann.digital.gui.components.terminal.TerminalDialog} */ @@ -469,7 +469,6 @@ public class CircuitComponent extends JComponent implements ChangedListener, Lib * * @param modification the modification */ - @Override public void modify(Modification modification) { try { if (modification != null) { diff --git a/src/main/java/de/neemann/digital/gui/components/CircuitModifierPostClosed.java b/src/main/java/de/neemann/digital/gui/components/CircuitModifierPostClosed.java index 20a850efa..9fce5e19b 100644 --- a/src/main/java/de/neemann/digital/gui/components/CircuitModifierPostClosed.java +++ b/src/main/java/de/neemann/digital/gui/components/CircuitModifierPostClosed.java @@ -48,7 +48,7 @@ public class CircuitModifierPostClosed implements CircuitModifier, ModelStateObs if (event.getType().equals(ModelEventType.POSTCLOSED)) { Modification m = builder.build(); if (m != null) - SwingUtilities.invokeLater(() -> circuitModifier.modify(m)); + circuitModifier.modify(m); } } } diff --git a/src/main/java/de/neemann/digital/gui/components/modification/ModifyAttribute.java b/src/main/java/de/neemann/digital/gui/components/modification/ModifyAttribute.java index a38362898..8f937eaff 100644 --- a/src/main/java/de/neemann/digital/gui/components/modification/ModifyAttribute.java +++ b/src/main/java/de/neemann/digital/gui/components/modification/ModifyAttribute.java @@ -39,4 +39,11 @@ public class ModifyAttribute extends ModificationOfVisualElement { VisualElement ve = getVisualElement(circuit); ve.getElementAttributes().set(key, value); } + + /** + * @return the modified value, only used in some tests + */ + public VALUE getValue() { + return value; + } } diff --git a/src/test/java/de/neemann/digital/gui/components/CircuitModifierTest.java b/src/test/java/de/neemann/digital/gui/components/CircuitModifierTest.java new file mode 100644 index 000000000..ce2d25c2f --- /dev/null +++ b/src/test/java/de/neemann/digital/gui/components/CircuitModifierTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2020 Helmut Neemann + * Use of this source code is governed by the GPL v3 license + * that can be found in the LICENSE file. + */ +package de.neemann.digital.gui.components; + +import de.neemann.digital.core.Model; +import de.neemann.digital.core.NodeException; +import de.neemann.digital.core.element.Keys; +import de.neemann.digital.core.memory.DataField; +import de.neemann.digital.draw.elements.Circuit; +import de.neemann.digital.draw.elements.PinException; +import de.neemann.digital.draw.elements.VisualElement; +import de.neemann.digital.draw.library.ElementLibrary; +import de.neemann.digital.draw.library.ElementNotFoundException; +import de.neemann.digital.draw.model.ModelCreator; +import de.neemann.digital.draw.shapes.ShapeFactory; +import de.neemann.digital.gui.components.modification.ModifyAttribute; +import de.neemann.digital.integration.Resources; +import de.neemann.digital.testing.TestCaseDescription; +import de.neemann.digital.testing.TestCaseElement; +import de.neemann.digital.testing.TestExecutor; +import de.neemann.digital.testing.TestingDataException; +import de.neemann.digital.testing.parser.ParserException; +import de.neemann.digital.undo.Modification; +import junit.framework.TestCase; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +public class CircuitModifierTest extends TestCase { + + private Modification mod; + + public void testEEPROMModification() throws IOException, ElementNotFoundException, PinException, NodeException, TestingDataException, ParserException { + // load circuit + File file = new File(Resources.getRoot(), "dig/test/eeprom.dig"); + ElementLibrary library = new ElementLibrary(); + ShapeFactory shapeFactory = new ShapeFactory(library); + Circuit c = Circuit.loadCircuit(file, shapeFactory); + + // pic test case + List l = c.getElements(v -> v.equalsDescription(TestCaseElement.DESCRIPTION)); + assertEquals(1, l.size()); + TestCaseDescription testCase = l.get(0).getElementAttributes().get(Keys.TESTDATA); + + // create the model and configure it like it was running in the gui + // fetch a modification in mod + CircuitModifierPostClosed cmpc = new CircuitModifierPostClosed(modification -> mod = modification); + ModelCreator mc = new ModelCreator(c, library); + Model model = mc.createModel(false); + mc.connectToGui(cmpc); + model.addObserver(cmpc); + + // executes a test case which writes to the eeprom + new TestExecutor(testCase, model).execute(); + + // there must by a modification + assertNotNull(mod); + + // check if it is the correct modification + ModifyAttribute ma = (ModifyAttribute) mod; + DataField data = ma.getValue(); + for (int i = 0; i < 30; i++) + assertEquals(i, data.getDataWord(i)); + } +} \ No newline at end of file diff --git a/src/test/java/de/neemann/digital/integration/TestExamples.java b/src/test/java/de/neemann/digital/integration/TestExamples.java index f0d075749..4798a1d7d 100644 --- a/src/test/java/de/neemann/digital/integration/TestExamples.java +++ b/src/test/java/de/neemann/digital/integration/TestExamples.java @@ -49,8 +49,8 @@ public class TestExamples extends TestCase { */ public void testTestExamples() throws Exception { File examples = new File(Resources.getRoot(), "/dig/test"); - assertEquals(192, new FileScanner(this::check).scan(examples)); - assertEquals(180, testCasesInFiles); + assertEquals(193, new FileScanner(this::check).scan(examples)); + assertEquals(181, testCasesInFiles); } /** diff --git a/src/test/resources/dig/test/eeprom.dig_ b/src/test/resources/dig/test/eeprom.dig similarity index 84% rename from src/test/resources/dig/test/eeprom.dig_ rename to src/test/resources/dig/test/eeprom.dig index 43dd98725..e4b83e286 100644 --- a/src/test/resources/dig/test/eeprom.dig_ +++ b/src/test/resources/dig/test/eeprom.dig @@ -100,10 +100,6 @@ Testcase - - Label - first - Testdata @@ -138,32 +134,6 @@ end loop - - Testcase - - - Label - second - - - Testdata - - A CS OE WE D_in D_out -0 0 0 0 Z Z - -# Data written by the first test case -# is still present in the EEPROM. - -# read from EEPROM -loop(n,30) -(n) 1 1 0 Z (n) -end loop - - - - - - @@ -199,4 +169,5 @@ end loop + \ No newline at end of file