Skip to content
Snippets Groups Projects
Commit d90a241d authored by Almasty007's avatar Almasty007
Browse files

ajout tests

parent 5207dd04
No related branches found
No related tags found
No related merge requests found
Pipeline #10827 failed
...@@ -147,4 +147,12 @@ public class CalcEngine ...@@ -147,4 +147,12 @@ public class CalcEngine
public int getLeftOperand() { public int getLeftOperand() {
return leftOperand; return leftOperand;
} }
public void setPreviousOperator(char previousOperator) {
this.previousOperator = previousOperator;
}
public void setDisplayValue(int displayValue) {
this.displayValue = displayValue;
}
} }
...@@ -37,4 +37,12 @@ public class Calculator ...@@ -37,4 +37,12 @@ public class Calculator
public static void main(String[] args) { public static void main(String[] args) {
(new Calculator()).show(); (new Calculator()).show();
} }
public CalcEngine getEngine() {
return engine;
}
public UserInterface getGui() {
return gui;
}
} }
...@@ -2,6 +2,7 @@ package fr.nancy.iut; ...@@ -2,6 +2,7 @@ package fr.nancy.iut;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.*; import javax.swing.border.*;
...@@ -17,6 +18,7 @@ public class UserInterface ...@@ -17,6 +18,7 @@ public class UserInterface
implements ActionListener implements ActionListener
{ {
private CalcEngine calc; private CalcEngine calc;
private boolean showingAuthor; private boolean showingAuthor;
private JFrame frame; private JFrame frame;
...@@ -163,4 +165,18 @@ public class UserInterface ...@@ -163,4 +165,18 @@ public class UserInterface
showingAuthor = !showingAuthor; showingAuthor = !showingAuthor;
} }
public boolean isVisible() {
return frame.isVisible();
}
public JTextField getDisplay() {
return display;
}
public boolean isShowingAuthor() {
return showingAuthor;
}
} }
...@@ -3,6 +3,9 @@ package fr.nancy.iut; ...@@ -3,6 +3,9 @@ package fr.nancy.iut;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/** /**
* Test the CalcEngine class. * Test the CalcEngine class.
...@@ -181,6 +184,40 @@ public class CalcEngineTest { ...@@ -181,6 +184,40 @@ public class CalcEngineTest {
assertEquals(0, calcEngine.getLeftOperand()); assertEquals(0, calcEngine.getLeftOperand());
} }
@Test
public void testMinusOperator() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
CalcEngine calcEngine = new CalcEngine();
Method methodeRenduePublique = CalcEngine.class.getDeclaredMethod("applyPreviousOperator", null);
methodeRenduePublique.setAccessible(true);
calcEngine.numberPressed(7);
calcEngine.setPreviousOperator('-');
methodeRenduePublique.invoke(calcEngine, null);
calcEngine.setPreviousOperator('-');
calcEngine.setDisplayValue(0);
calcEngine.numberPressed(3);
assertEquals(-7, calcEngine.getLeftOperand());
assertEquals(3, calcEngine.getDisplayValue());
calcEngine.equals();
assertEquals(-10, calcEngine.getDisplayValue());
}
@Test
public void testPlusOperator() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
CalcEngine calcEngine = new CalcEngine();
Method methodeRenduePublique = CalcEngine.class.getDeclaredMethod("applyPreviousOperator", null);
methodeRenduePublique.setAccessible(true);
calcEngine.numberPressed(7);
calcEngine.setPreviousOperator('+');
methodeRenduePublique.invoke(calcEngine, null);
calcEngine.setPreviousOperator('+');
calcEngine.setDisplayValue(0);
calcEngine.numberPressed(3);
assertEquals(7, calcEngine.getLeftOperand());
assertEquals(3, calcEngine.getDisplayValue());
calcEngine.equals();
assertEquals(10, calcEngine.getDisplayValue());
}
......
package fr.nancy.iut;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class CalculatorTest {
@Test
public void testConstructor() {
Calculator calculator = new Calculator();
assertTrue(calculator != null);
assertTrue(calculator.getGui() != null);
assertTrue(calculator.getEngine() != null);
}
@Test
public void testAfficher() {
Calculator calculator = new Calculator();
UserInterface gui = calculator.getGui();
gui.setVisible(false);
assertFalse(gui.isVisible());
calculator.show();
assertTrue(gui.isVisible());
}
@Test
public void testMainCalculator() {
Calculator.main(new String[0]);
}
}
package fr.nancy.iut;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class UserInterfaceTest {
// @Test
// public void testActionPerformed() {
// UserInterface userInterface = new UserInterface(new CalcEngine());
// userInterface.actionPerformed(null);
// assertTrue(true);
// }
@Test
public void testRedisplay() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
CalcEngine calcEngine = new CalcEngine();
UserInterface userInterface = new UserInterface(calcEngine);
calcEngine.numberPressed(2);
Method methodeRenduePublique = UserInterface.class.getDeclaredMethod("redisplay", null);
methodeRenduePublique.setAccessible(true);
methodeRenduePublique.invoke(userInterface, null);
JTextField jTextField = userInterface.getDisplay();
assertEquals(0, jTextField.getText().compareTo("2"));
assertEquals(2, calcEngine.getDisplayValue());
}
@Test
public void testShowInfo() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
CalcEngine calcEngine = new CalcEngine();
UserInterface userInterface = new UserInterface(calcEngine);
assertTrue(userInterface.isShowingAuthor());
Method methodeRenduePublique = UserInterface.class.getDeclaredMethod("showInfo", null);
methodeRenduePublique.setAccessible(true);
methodeRenduePublique.invoke(userInterface, null);
assertFalse(userInterface.isShowingAuthor());
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment