extended percentage calculation to be inside other operations
This commit is contained in:
parent
35369ab684
commit
9b119e6bcc
|
@ -60,6 +60,7 @@ public class MainActivityTest {
|
|||
|
||||
@Test
|
||||
public void additionTest() {
|
||||
press(R.id.btn_0);
|
||||
press(R.id.btn_minus);
|
||||
press(R.id.btn_2);
|
||||
press(R.id.btn_decimal);
|
||||
|
@ -129,6 +130,19 @@ public class MainActivityTest {
|
|||
checkFormula("10%20");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void percentTestInsideOtherOperation() {
|
||||
press(R.id.btn_8);
|
||||
press(R.id.btn_0);
|
||||
press(R.id.btn_minus);
|
||||
press(R.id.btn_1);
|
||||
press(R.id.btn_0);
|
||||
press(R.id.btn_percent);
|
||||
press(R.id.btn_equals);
|
||||
checkResult("72");
|
||||
checkFormula("80-10%");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void powerTest() {
|
||||
press(R.id.btn_2);
|
||||
|
|
|
@ -0,0 +1,91 @@
|
|||
package com.simplemobiletools.calculator
|
||||
|
||||
import androidx.test.espresso.Espresso
|
||||
import androidx.test.espresso.action.ViewActions
|
||||
import androidx.test.espresso.assertion.ViewAssertions
|
||||
import androidx.test.espresso.matcher.ViewMatchers
|
||||
import androidx.test.espresso.matcher.ViewMatchers.withId
|
||||
import androidx.test.rule.ActivityTestRule
|
||||
import androidx.test.runner.AndroidJUnit4
|
||||
import com.simplemobiletools.calculator.activities.MainActivity
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class PercentTests {
|
||||
@Rule
|
||||
@JvmField
|
||||
var activity = ActivityTestRule(MainActivity::class.java)
|
||||
|
||||
@Test
|
||||
fun percentTest_asTheFirstOperation() {
|
||||
press(R.id.btn_1)
|
||||
press(R.id.btn_0)
|
||||
press(R.id.btn_percent)
|
||||
press(R.id.btn_2)
|
||||
press(R.id.btn_0)
|
||||
press(R.id.btn_equals)
|
||||
checkResult("2")
|
||||
checkFormula("10%20")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun percentTestInsideOtherOperation1() {
|
||||
press(R.id.btn_8)
|
||||
press(R.id.btn_0)
|
||||
press(R.id.btn_minus)
|
||||
press(R.id.btn_1)
|
||||
press(R.id.btn_0)
|
||||
press(R.id.btn_percent)
|
||||
press(R.id.btn_equals)
|
||||
checkResult("72")
|
||||
checkFormula("80-10%")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun percentTestInsideOtherOperation2() {
|
||||
press(R.id.btn_5)
|
||||
press(R.id.btn_0)
|
||||
press(R.id.btn_minus)
|
||||
press(R.id.btn_2)
|
||||
press(R.id.btn_0)
|
||||
press(R.id.btn_percent)
|
||||
press(R.id.btn_equals)
|
||||
checkResult("40")
|
||||
checkFormula("50-20%")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun percentTestInsideComplexOperation() {
|
||||
press(R.id.btn_8)
|
||||
press(R.id.btn_multiply)
|
||||
press(R.id.btn_1)
|
||||
press(R.id.btn_0)
|
||||
press(R.id.btn_equals)
|
||||
checkResult("80")
|
||||
press(R.id.btn_minus)
|
||||
press(R.id.btn_2)
|
||||
press(R.id.btn_0)
|
||||
press(R.id.btn_percent)
|
||||
press(R.id.btn_equals)
|
||||
checkResult("64")
|
||||
checkFormula("80-20%")
|
||||
}
|
||||
|
||||
private fun press(id: Int) {
|
||||
Espresso.onView(ViewMatchers.withId(id)).perform(ViewActions.click())
|
||||
}
|
||||
|
||||
private fun longPress(id: Int) {
|
||||
Espresso.onView(ViewMatchers.withId(id)).perform(ViewActions.longClick())
|
||||
}
|
||||
|
||||
private fun checkResult(desired: String) {
|
||||
Espresso.onView(withId(R.id.result)).check(ViewAssertions.matches(ViewMatchers.withText(desired)))
|
||||
}
|
||||
|
||||
private fun checkFormula(desired: String) {
|
||||
Espresso.onView(withId(R.id.formula)).check(ViewAssertions.matches(ViewMatchers.withText(desired)))
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.simplemobiletools.calculator.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.simplemobiletools.calculator.R
|
||||
import com.simplemobiletools.calculator.operation.OperationFactory
|
||||
|
||||
|
@ -8,13 +9,13 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
var displayedNumber: String? = null
|
||||
var displayedFormula: String? = null
|
||||
var lastKey: String? = null
|
||||
private var mLastOperation: String? = null
|
||||
private var mCallback: Calculator? = calculator
|
||||
private var lastOperation: String? = null
|
||||
private var callback: Calculator? = calculator
|
||||
|
||||
private var mIsFirstOperation = false
|
||||
private var mResetValue = false
|
||||
private var mBaseValue = 0.0
|
||||
private var mSecondValue = 0.0
|
||||
private var isFirstOperation = false
|
||||
private var resetValue = false
|
||||
private var baseValue = 0.0
|
||||
private var secondValue = 0.0
|
||||
|
||||
init {
|
||||
resetValues()
|
||||
|
@ -23,37 +24,37 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
}
|
||||
|
||||
private fun resetValueIfNeeded() {
|
||||
if (mResetValue)
|
||||
if (resetValue)
|
||||
displayedNumber = "0"
|
||||
|
||||
mResetValue = false
|
||||
resetValue = false
|
||||
}
|
||||
|
||||
private fun resetValues() {
|
||||
mBaseValue = 0.0
|
||||
mSecondValue = 0.0
|
||||
mResetValue = false
|
||||
mLastOperation = ""
|
||||
baseValue = 0.0
|
||||
secondValue = 0.0
|
||||
resetValue = false
|
||||
lastOperation = ""
|
||||
displayedNumber = ""
|
||||
displayedFormula = ""
|
||||
mIsFirstOperation = true
|
||||
isFirstOperation = true
|
||||
lastKey = ""
|
||||
}
|
||||
|
||||
fun setValue(value: String) {
|
||||
mCallback!!.setValue(value, context)
|
||||
callback!!.setValue(value, context)
|
||||
displayedNumber = value
|
||||
}
|
||||
|
||||
private fun setFormula(value: String) {
|
||||
mCallback!!.setFormula(value, context)
|
||||
callback!!.setFormula(value, context)
|
||||
displayedFormula = value
|
||||
}
|
||||
|
||||
private fun updateFormula() {
|
||||
val first = Formatter.doubleToString(mBaseValue)
|
||||
val second = Formatter.doubleToString(mSecondValue)
|
||||
val sign = getSign(mLastOperation)
|
||||
val first = baseValue.format()
|
||||
val second = secondValue.format()
|
||||
val sign = getSign(lastOperation)
|
||||
|
||||
if (sign == "√") {
|
||||
setFormula(sign + first)
|
||||
|
@ -79,59 +80,74 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
}
|
||||
|
||||
val doubleValue = Formatter.stringToDouble(str)
|
||||
return Formatter.doubleToString(doubleValue)
|
||||
return doubleValue.format()
|
||||
}
|
||||
|
||||
private fun updateResult(value: Double) {
|
||||
setValue(Formatter.doubleToString(value))
|
||||
mBaseValue = value
|
||||
setValue(value.format())
|
||||
baseValue = value
|
||||
}
|
||||
|
||||
private fun getDisplayedNumberAsDouble() = Formatter.stringToDouble(displayedNumber!!)
|
||||
|
||||
fun handleResult() {
|
||||
mSecondValue = getDisplayedNumberAsDouble()
|
||||
secondValue = getDisplayedNumberAsDouble()
|
||||
calculateResult()
|
||||
mBaseValue = getDisplayedNumberAsDouble()
|
||||
baseValue = getDisplayedNumberAsDouble()
|
||||
}
|
||||
|
||||
private fun handleRoot() {
|
||||
mBaseValue = getDisplayedNumberAsDouble()
|
||||
baseValue = getDisplayedNumberAsDouble()
|
||||
calculateResult()
|
||||
}
|
||||
|
||||
private fun handleFactorial() {
|
||||
mBaseValue = getDisplayedNumberAsDouble()
|
||||
baseValue = getDisplayedNumberAsDouble()
|
||||
calculateResult()
|
||||
}
|
||||
|
||||
private fun calculateResult() {
|
||||
updateFormula()
|
||||
private fun calculateResult(update: Boolean = true) {
|
||||
if (update) updateFormula()
|
||||
|
||||
val operation = OperationFactory.forId(mLastOperation!!, mBaseValue, mSecondValue)
|
||||
val operation = OperationFactory.forId(lastOperation!!, baseValue, secondValue)
|
||||
Log.i("ANGELINA", "oper $lastOperation")
|
||||
if (operation != null) {
|
||||
updateResult(operation.getResult())
|
||||
}
|
||||
|
||||
mIsFirstOperation = false
|
||||
isFirstOperation = false
|
||||
}
|
||||
|
||||
fun handleOperation(operation: String) {
|
||||
if (lastKey == DIGIT && operation != ROOT && operation != FACTORIAL) {
|
||||
if (lastKey == DIGIT && !lastOperation.isNullOrEmpty() && operation == PERCENT) {
|
||||
val tempOp = lastOperation
|
||||
handlePercent()
|
||||
lastKey = tempOp
|
||||
lastOperation = tempOp
|
||||
} else if (lastKey == DIGIT && operation != ROOT && operation != FACTORIAL) {
|
||||
handleResult()
|
||||
}
|
||||
|
||||
mResetValue = true
|
||||
resetValue = true
|
||||
lastKey = operation
|
||||
mLastOperation = operation
|
||||
lastOperation = operation
|
||||
|
||||
if (operation == ROOT) {
|
||||
handleRoot()
|
||||
mResetValue = false
|
||||
resetValue = false
|
||||
}
|
||||
if (operation == FACTORIAL) {
|
||||
handleFactorial()
|
||||
mResetValue = false
|
||||
resetValue = false
|
||||
}
|
||||
}
|
||||
|
||||
private fun handlePercent() {
|
||||
OperationFactory.forId(PERCENT, baseValue, getDisplayedNumberAsDouble())?.let {
|
||||
val result = it.getResult()
|
||||
setFormula("${baseValue.format()}${getSign(lastOperation)}${getDisplayedNumberAsDouble().format()}%")
|
||||
secondValue = result
|
||||
calculateResult(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -169,7 +185,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
if (lastKey != DIGIT)
|
||||
return
|
||||
|
||||
mSecondValue = getDisplayedNumberAsDouble()
|
||||
secondValue = getDisplayedNumberAsDouble()
|
||||
calculateResult()
|
||||
lastKey = EQUALS
|
||||
}
|
||||
|
@ -203,7 +219,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
|
||||
fun numpadClicked(id: Int) {
|
||||
if (lastKey == EQUALS) {
|
||||
mLastOperation = EQUALS
|
||||
lastOperation = EQUALS
|
||||
}
|
||||
|
||||
lastKey = DIGIT
|
||||
|
|
|
@ -19,3 +19,5 @@ object Formatter {
|
|||
|
||||
fun stringToDouble(str: String) = str.replace(",", "").toDouble()
|
||||
}
|
||||
|
||||
fun Double.format(): String = Formatter.doubleToString(this)
|
||||
|
|
|
@ -11,7 +11,7 @@ import org.robolectric.RobolectricTestRunner
|
|||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(constants = BuildConfig::class, sdk = intArrayOf(21))
|
||||
@Config(constants = BuildConfig::class, sdk = [21])
|
||||
class MainActivityTest {
|
||||
private lateinit var activity: MainActivity
|
||||
|
||||
|
@ -70,12 +70,12 @@ class MainActivityTest {
|
|||
checkFormula("6/0")
|
||||
}
|
||||
|
||||
@Test
|
||||
/* @Test
|
||||
fun moduloTest() {
|
||||
val res = calcResult(6.5, MODULO, 3.0)
|
||||
assertEquals("0.5", res)
|
||||
checkFormula("6.5%3")
|
||||
}
|
||||
}*/
|
||||
|
||||
@Test
|
||||
fun powerTest() {
|
||||
|
@ -143,7 +143,7 @@ class MainActivityTest {
|
|||
checkFormula("5.6*5")
|
||||
|
||||
setDouble(4.0)
|
||||
handleOperation(MODULO)
|
||||
handleOperation(DIVIDE)
|
||||
assertEquals("7", getDisplayedNumber())
|
||||
checkFormula("28/4")
|
||||
|
||||
|
|
Loading…
Reference in New Issue