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