mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-06-05 21:49:13 +02:00
add some Formula tests
This commit is contained in:
@ -50,8 +50,10 @@ public class MainActivityTest {
|
||||
press(R.id.btn_6);
|
||||
press(R.id.btn_equals);
|
||||
checkResult("3.5");
|
||||
checkFormula("-2.5+6");
|
||||
press(R.id.btn_equals);
|
||||
checkResult("9.5");
|
||||
checkFormula("3.5+6");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -63,6 +65,7 @@ public class MainActivityTest {
|
||||
press(R.id.btn_3);
|
||||
press(R.id.btn_equals);
|
||||
checkResult("4.8");
|
||||
checkFormula("7.8-3");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -72,6 +75,7 @@ public class MainActivityTest {
|
||||
press(R.id.btn_4);
|
||||
press(R.id.btn_equals);
|
||||
checkResult("8");
|
||||
checkFormula("2*4");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -82,6 +86,7 @@ public class MainActivityTest {
|
||||
press(R.id.btn_4);
|
||||
press(R.id.btn_equals);
|
||||
checkResult("2.5");
|
||||
checkFormula("10/4");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -91,6 +96,7 @@ public class MainActivityTest {
|
||||
press(R.id.btn_0);
|
||||
press(R.id.btn_equals);
|
||||
checkResult("0");
|
||||
checkFormula("8/0");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -100,6 +106,7 @@ public class MainActivityTest {
|
||||
press(R.id.btn_2);
|
||||
press(R.id.btn_equals);
|
||||
checkResult("1");
|
||||
checkFormula("7%2");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -109,6 +116,7 @@ public class MainActivityTest {
|
||||
press(R.id.btn_3);
|
||||
press(R.id.btn_equals);
|
||||
checkResult("8");
|
||||
checkFormula("2^3");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -116,6 +124,7 @@ public class MainActivityTest {
|
||||
press(R.id.btn_9);
|
||||
press(R.id.btn_root);
|
||||
checkResult("3");
|
||||
checkFormula("√9");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -145,6 +154,7 @@ public class MainActivityTest {
|
||||
press(R.id.btn_2);
|
||||
press(R.id.btn_equals);
|
||||
checkResult("2");
|
||||
checkFormula("");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -154,29 +164,36 @@ public class MainActivityTest {
|
||||
press(R.id.btn_5);
|
||||
press(R.id.btn_minus);
|
||||
checkResult("7");
|
||||
checkFormula("2+5");
|
||||
|
||||
press(R.id.btn_3);
|
||||
press(R.id.btn_multiply);
|
||||
checkResult("4");
|
||||
checkFormula("7-3");
|
||||
|
||||
press(R.id.btn_5);
|
||||
press(R.id.btn_divide);
|
||||
checkResult("20");
|
||||
checkFormula("4*5");
|
||||
|
||||
press(R.id.btn_2);
|
||||
press(R.id.btn_modulo);
|
||||
checkResult("10");
|
||||
checkFormula("20/2");
|
||||
|
||||
press(R.id.btn_4);
|
||||
press(R.id.btn_power);
|
||||
checkResult("2");
|
||||
checkFormula("10%4");
|
||||
|
||||
press(R.id.btn_8);
|
||||
press(R.id.btn_modulo);
|
||||
checkResult("256");
|
||||
checkFormula("2^8");
|
||||
|
||||
press(R.id.btn_root);
|
||||
checkResult("16");
|
||||
checkFormula("√256");
|
||||
|
||||
press(R.id.btn_clear);
|
||||
checkResult("1");
|
||||
@ -196,4 +213,8 @@ public class MainActivityTest {
|
||||
private void checkResult(String desired) {
|
||||
onView(withId(R.id.result)).check(matches(withText(desired)));
|
||||
}
|
||||
|
||||
private void checkFormula(String desired) {
|
||||
onView(withId(R.id.formula)).check(matches(withText(desired)));
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package calculator.simplemobiletools.com.simple_calculator;
|
||||
|
||||
public class CalculatorImpl {
|
||||
private String displayedValue;
|
||||
private String displayedFormula;
|
||||
private double baseValue;
|
||||
private double secondValue;
|
||||
private boolean resetValue;
|
||||
@ -38,6 +39,7 @@ public class CalculatorImpl {
|
||||
lastKey = "";
|
||||
lastOperation = "";
|
||||
displayedValue = "";
|
||||
displayedFormula = "";
|
||||
isFirstOperation = true;
|
||||
}
|
||||
|
||||
@ -48,6 +50,7 @@ public class CalculatorImpl {
|
||||
|
||||
private void setFormula(String value) {
|
||||
callback.setFormula(value);
|
||||
displayedFormula = value;
|
||||
}
|
||||
|
||||
private void updateFormula() {
|
||||
@ -90,6 +93,10 @@ public class CalculatorImpl {
|
||||
return Double.parseDouble(getDisplayedNumber());
|
||||
}
|
||||
|
||||
public String getDisplayedFormula() {
|
||||
return displayedFormula;
|
||||
}
|
||||
|
||||
public void handleResult() {
|
||||
secondValue = getDisplayedNumberAsDouble();
|
||||
calculateResult();
|
||||
@ -97,9 +104,8 @@ public class CalculatorImpl {
|
||||
}
|
||||
|
||||
public void calculateResult() {
|
||||
if (!isFirstOperation) {
|
||||
if (!isFirstOperation)
|
||||
updateFormula();
|
||||
}
|
||||
|
||||
switch (lastOperation) {
|
||||
case Constants.PLUS:
|
||||
|
@ -38,42 +38,49 @@ public class MainActivityTest {
|
||||
public void additionTest() {
|
||||
String res = calcResult(-1.2, Constants.PLUS, 3.4);
|
||||
assertEquals("2.2", res);
|
||||
checkFormula("-1.2+3.4");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subtractionTest() {
|
||||
String res = calcResult(7.8, Constants.MINUS, 2.5);
|
||||
assertEquals("5.3", res);
|
||||
checkFormula("7.8-2.5");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiplyTest() {
|
||||
String res = calcResult(-3.2, Constants.MULTIPLY, 6.6);
|
||||
assertEquals("-21.12", res);
|
||||
checkFormula("-3.2*6.6");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void divisionTest() {
|
||||
String res = calcResult(18.25, Constants.DIVIDE, 5);
|
||||
assertEquals("3.65", res);
|
||||
checkFormula("18.25/5");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void divisionByZero_returnsZero() {
|
||||
String res = calcResult(6, Constants.DIVIDE, 0);
|
||||
assertEquals("0", res);
|
||||
checkFormula("6/0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void moduloTest() {
|
||||
String res = calcResult(6.5, Constants.MODULO, 3);
|
||||
assertEquals("0.5", res);
|
||||
checkFormula("6.5%3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void powerTest() {
|
||||
String res = calcResult(3, Constants.POWER, 6);
|
||||
assertEquals("729", res);
|
||||
checkFormula("3^6");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -81,6 +88,7 @@ public class MainActivityTest {
|
||||
setDouble(16);
|
||||
handleOperation(Constants.ROOT);
|
||||
assertEquals("4", getDisplayedNumber());
|
||||
checkFormula("√16");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -107,6 +115,7 @@ public class MainActivityTest {
|
||||
setDouble(3);
|
||||
activity.getCalc().handleResult();
|
||||
assertEquals("3", getDisplayedNumber());
|
||||
checkFormula("");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -116,29 +125,36 @@ public class MainActivityTest {
|
||||
setDouble(21);
|
||||
handleOperation(Constants.MINUS);
|
||||
assertEquals("8.8", getDisplayedNumber());
|
||||
checkFormula("-12.2+21");
|
||||
|
||||
setDouble(1.6);
|
||||
activity.getCalc().handleEquals();
|
||||
assertEquals("7.2", getDisplayedNumber());
|
||||
checkFormula("8.8-1.6");
|
||||
activity.getCalc().handleEquals();
|
||||
assertEquals("5.6", getDisplayedNumber());
|
||||
checkFormula("7.2-1.6");
|
||||
|
||||
handleOperation(Constants.MULTIPLY);
|
||||
setDouble(5);
|
||||
handleOperation(Constants.DIVIDE);
|
||||
assertEquals("28", getDisplayedNumber());
|
||||
checkFormula("5.6*5");
|
||||
|
||||
setDouble(4);
|
||||
handleOperation(Constants.MODULO);
|
||||
assertEquals("7", getDisplayedNumber());
|
||||
checkFormula("28/4");
|
||||
|
||||
setDouble(5);
|
||||
handleOperation(Constants.POWER);
|
||||
assertEquals("2", getDisplayedNumber());
|
||||
checkFormula("7%5");
|
||||
|
||||
setDouble(8);
|
||||
handleOperation(Constants.ROOT);
|
||||
assertEquals("16", getDisplayedNumber());
|
||||
checkFormula("√256");
|
||||
|
||||
activity.getCalc().handleClear();
|
||||
assertEquals("1", getDisplayedNumber());
|
||||
@ -152,6 +168,10 @@ public class MainActivityTest {
|
||||
activity.getCalc().handleOperation(operation);
|
||||
}
|
||||
|
||||
private void checkFormula(String desired) {
|
||||
assertEquals(desired, getDisplayedFormula());
|
||||
}
|
||||
|
||||
private String calcResult(double baseValue, String operation, double secondValue) {
|
||||
setDouble(baseValue);
|
||||
handleOperation(operation);
|
||||
@ -163,4 +183,8 @@ public class MainActivityTest {
|
||||
private String getDisplayedNumber() {
|
||||
return activity.getCalc().getDisplayedNumber();
|
||||
}
|
||||
|
||||
private String getDisplayedFormula() {
|
||||
return activity.getCalc().getDisplayedFormula();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user