adds a test for the circuit modifier, see #541

This commit is contained in:
hneemann 2020-11-11 18:05:51 +01:00
parent 5e823a4bcf
commit 2099b9c99f
8 changed files with 88 additions and 41 deletions

View File

@ -283,16 +283,16 @@ public class ModelCreator implements Iterable<ModelEntry> {
/**
* 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();

View File

@ -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);

View File

@ -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<Circuit> modification) {
try {
if (modification != null) {

View File

@ -48,7 +48,7 @@ public class CircuitModifierPostClosed implements CircuitModifier, ModelStateObs
if (event.getType().equals(ModelEventType.POSTCLOSED)) {
Modification<Circuit> m = builder.build();
if (m != null)
SwingUtilities.invokeLater(() -> circuitModifier.modify(m));
circuitModifier.modify(m);
}
}
}

View File

@ -39,4 +39,11 @@ public class ModifyAttribute<VALUE> 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;
}
}

View File

@ -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<Circuit> 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<VisualElement> 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<DataField> ma = (ModifyAttribute<DataField>) mod;
DataField data = ma.getValue();
for (int i = 0; i < 30; i++)
assertEquals(i, data.getDataWord(i));
}
}

View File

@ -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);
}
/**

View File

@ -100,10 +100,6 @@
<visualElement>
<elementName>Testcase</elementName>
<elementAttributes>
<entry>
<string>Label</string>
<string>first</string>
</entry>
<entry>
<string>Testdata</string>
<testData>
@ -138,32 +134,6 @@ end loop
</elementAttributes>
<pos x="640" y="260"/>
</visualElement>
<visualElement>
<elementName>Testcase</elementName>
<elementAttributes>
<entry>
<string>Label</string>
<string>second</string>
</entry>
<entry>
<string>Testdata</string>
<testData>
<dataString>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
</dataString>
</testData>
</entry>
</elementAttributes>
<pos x="640" y="340"/>
</visualElement>
</visualElements>
<wires>
<wire>
@ -199,4 +169,5 @@ end loop
<p2 x="600" y="260"/>
</wire>
</wires>
<measurementOrdering/>
</circuit>