fix Robolectric tests
This commit is contained in:
parent
de3e1312ca
commit
920b8f87a6
|
@ -31,6 +31,10 @@ public class CalculatorImpl {
|
|||
callback.setValue("0");
|
||||
}
|
||||
|
||||
public void setLastKey(int lastKey) {
|
||||
this.lastKey = lastKey;
|
||||
}
|
||||
|
||||
public void addDigit(int number) {
|
||||
final String currentValue = callback.getDisplayedNumber();
|
||||
final String newValue = removeLeadingZero(currentValue + number);
|
||||
|
|
|
@ -81,6 +81,10 @@ public class MainActivity extends AppCompatActivity implements Calculator {
|
|||
|
||||
@OnClick({R.id.btn_decimal, R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8,
|
||||
R.id.btn_9})
|
||||
public void numpadClick(View view) {
|
||||
numpadClicked(view);
|
||||
}
|
||||
|
||||
public void numpadClicked(View view) {
|
||||
calc.numpadClicked(view);
|
||||
}
|
||||
|
@ -90,9 +94,14 @@ public class MainActivity extends AppCompatActivity implements Calculator {
|
|||
result.setText(value);
|
||||
}
|
||||
|
||||
// used only by Robolectric
|
||||
@Override
|
||||
public void setValueDouble(double d) {
|
||||
setValue(Formatter.doubleToString(d));
|
||||
//lastKey = Constants.DIGIT;
|
||||
calc.setLastKey(Constants.DIGIT);
|
||||
}
|
||||
|
||||
public CalculatorImpl getCalc() {
|
||||
return calc;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,124 +23,124 @@ public class MainActivityTest {
|
|||
|
||||
@Test
|
||||
public void addSimpleDigit() {
|
||||
activity.addDigit(2);
|
||||
activity.getCalc().addDigit(2);
|
||||
assertEquals("2", getDisplayedNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeLeadingZero() {
|
||||
activity.addDigit(0);
|
||||
activity.addDigit(5);
|
||||
activity.getCalc().addDigit(0);
|
||||
activity.getCalc().addDigit(5);
|
||||
assertEquals("5", getDisplayedNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionTest() {
|
||||
String res = calcResult(-1.2, MainActivity.PLUS, 3.4);
|
||||
String res = calcResult(-1.2, Constants.PLUS, 3.4);
|
||||
assertEquals("2.2", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subtractionTest() {
|
||||
String res = calcResult(7.8, MainActivity.MINUS, 2.5);
|
||||
String res = calcResult(7.8, Constants.MINUS, 2.5);
|
||||
assertEquals("5.3", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiplyTest() {
|
||||
String res = calcResult(-3.2, MainActivity.MULTIPLY, 6.6);
|
||||
String res = calcResult(-3.2, Constants.MULTIPLY, 6.6);
|
||||
assertEquals("-21.12", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void divisionTest() {
|
||||
String res = calcResult(18.25, MainActivity.DIVIDE, 5);
|
||||
String res = calcResult(18.25, Constants.DIVIDE, 5);
|
||||
assertEquals("3.65", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void divisionByZero_returnsZero() {
|
||||
String res = calcResult(6, MainActivity.DIVIDE, 0);
|
||||
String res = calcResult(6, Constants.DIVIDE, 0);
|
||||
assertEquals("0", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void moduloTest() {
|
||||
String res = calcResult(6.5, MainActivity.MODULO, 3);
|
||||
String res = calcResult(6.5, Constants.MODULO, 3);
|
||||
assertEquals("0.5", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void powerTest() {
|
||||
String res = calcResult(3, MainActivity.POWER, 6);
|
||||
String res = calcResult(3, Constants.POWER, 6);
|
||||
assertEquals("729", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rootTest() {
|
||||
setDouble(16);
|
||||
handleOperation(MainActivity.ROOT);
|
||||
handleOperation(Constants.ROOT);
|
||||
assertEquals("4", getDisplayedNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearBtnSimpleTest() {
|
||||
setDouble(156);
|
||||
activity.handleClear();
|
||||
activity.getCalc().handleClear();
|
||||
assertEquals("15", getDisplayedNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearBtnComplexTest() {
|
||||
setDouble(-26);
|
||||
activity.handleClear();
|
||||
activity.getCalc().handleClear();
|
||||
assertEquals("-2", getDisplayedNumber());
|
||||
activity.handleClear();
|
||||
activity.getCalc().handleClear();
|
||||
assertEquals("0", getDisplayedNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearBtnLongClick_resetsEverything() {
|
||||
calcResult(-1.2, MainActivity.PLUS, 3.4);
|
||||
activity.handleLongClear();
|
||||
handleOperation(MainActivity.PLUS);
|
||||
calcResult(-1.2, Constants.PLUS, 3.4);
|
||||
activity.getCalc().handleLongClear();
|
||||
handleOperation(Constants.PLUS);
|
||||
setDouble(3);
|
||||
activity.handleResult();
|
||||
activity.getCalc().handleResult();
|
||||
assertEquals("3", getDisplayedNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void complexTest() {
|
||||
setDouble(-12.2);
|
||||
handleOperation(MainActivity.PLUS);
|
||||
handleOperation(Constants.PLUS);
|
||||
setDouble(21);
|
||||
handleOperation(MainActivity.MINUS);
|
||||
handleOperation(Constants.MINUS);
|
||||
assertEquals("8.8", getDisplayedNumber());
|
||||
|
||||
setDouble(1.6);
|
||||
activity.handleEquals();
|
||||
activity.getCalc().handleEquals();
|
||||
assertEquals("7.2", getDisplayedNumber());
|
||||
activity.handleEquals();
|
||||
activity.getCalc().handleEquals();
|
||||
assertEquals("5.6", getDisplayedNumber());
|
||||
|
||||
handleOperation(MainActivity.MULTIPLY);
|
||||
handleOperation(Constants.MULTIPLY);
|
||||
setDouble(5);
|
||||
handleOperation(MainActivity.DIVIDE);
|
||||
handleOperation(Constants.DIVIDE);
|
||||
assertEquals("28", getDisplayedNumber());
|
||||
|
||||
setDouble(4);
|
||||
handleOperation(MainActivity.MODULO);
|
||||
handleOperation(Constants.MODULO);
|
||||
assertEquals("7", getDisplayedNumber());
|
||||
|
||||
setDouble(5);
|
||||
handleOperation(MainActivity.POWER);
|
||||
handleOperation(Constants.POWER);
|
||||
assertEquals("2", getDisplayedNumber());
|
||||
|
||||
setDouble(8);
|
||||
handleOperation(MainActivity.ROOT);
|
||||
handleOperation(Constants.ROOT);
|
||||
assertEquals("16", getDisplayedNumber());
|
||||
|
||||
activity.handleClear();
|
||||
activity.getCalc().handleClear();
|
||||
assertEquals("1", getDisplayedNumber());
|
||||
}
|
||||
|
||||
|
@ -149,14 +149,14 @@ public class MainActivityTest {
|
|||
}
|
||||
|
||||
private void handleOperation(int operation) {
|
||||
activity.handleOperation(operation);
|
||||
activity.getCalc().handleOperation(operation);
|
||||
}
|
||||
|
||||
private String calcResult(double baseValue, int operation, double secondValue) {
|
||||
setDouble(baseValue);
|
||||
handleOperation(operation);
|
||||
setDouble(secondValue);
|
||||
activity.handleResult();
|
||||
activity.getCalc().handleResult();
|
||||
return getDisplayedNumber();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue