Exponent can't be first in number

+ tests for NumberInputFilter
This commit is contained in:
serso 2016-04-15 22:53:44 +02:00
parent 76b8901a4e
commit 28a573c777
2 changed files with 116 additions and 1 deletions

View File

@ -61,7 +61,7 @@ public class NumberInputFilter implements InputFilter {
findChars(dest, dend, dest.length(), end - start, CHARS);
SpannableStringBuilder filtered = null;
for (int i = end - 1; i >= start; i--) {
for (int i = start; i < end; i++) {
final char c = source.charAt(i);
boolean filter = false;
@ -93,6 +93,9 @@ public class NumberInputFilter implements InputFilter {
} else if (CHARS[CHAR_POINT] >= 0 && CHARS[CHAR_POINT] > i + dstart) {
// no exponent before decimal point
filter = true;
} else if (i + dstart == 0) {
// exponent can't be first
filter = true;
} else {
CHARS[CHAR_EXP] = i + dstart;
}

View File

@ -0,0 +1,112 @@
package org.solovyev.android.text.method;
import android.os.Build;
import android.text.Editable;
import android.text.InputFilter;
import android.text.SpannableStringBuilder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import org.solovyev.android.calculator.BuildConfig;
import static org.junit.Assert.assertEquals;
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(RobolectricGradleTestRunner.class)
public class NumberInputFilterTest {
private Editable editable;
@Before
public void setUp() throws Exception {
editable = new SpannableStringBuilder();
editable.setFilters(new InputFilter[]{new NumberInputFilter()});
}
@Test
public void testShouldNotInsertExponentInTheBeginning() throws Exception {
editable.insert(0, "E");
assertEquals("", editable.toString());
}
@Test
public void testShouldInsertExponentAtTheEnd() throws Exception {
editable.insert(0, "1");
editable.insert(1, "E");
assertEquals("1E", editable.toString());
}
@Test
public void testShouldNotInsertSecondMinusSign() throws Exception {
editable.insert(0, "-");
editable.insert(1, "-");
assertEquals("-", editable.toString());
}
@Test
public void testShouldNotInsertTwoMinusSigns() throws Exception {
editable.insert(0, "--");
assertEquals("-", editable.toString());
}
@Test
public void testShouldInsertSecondMinusSignAfterExponent() throws Exception {
editable.insert(0, "-");
editable.insert(1, "E");
editable.insert(2, "-");
assertEquals("-E-", editable.toString());
}
@Test
public void testShouldInsertSecondMinusSignAlongWithExponent() throws Exception {
editable.insert(0, "-");
editable.insert(1, "E-");
assertEquals("-E-", editable.toString());
}
@Test
public void testShouldNotInsertMinusSignBeforeExistingMinusSIgn() throws Exception {
editable.insert(0, "-");
editable.insert(0, "-");
assertEquals("-", editable.toString());
}
@Test
public void testShouldNotInsertSecondDecimalPoint() throws Exception {
editable.insert(0, "0.2");
editable.insert(3, ".");
assertEquals("0.2", editable.toString());
}
@Test
public void testShouldNotInsertTwoDecimalPoints() throws Exception {
editable.insert(0, "..");
assertEquals(".", editable.toString());
}
@Test
public void testShouldNotInsertDecimalPointAfterExponent() throws Exception {
editable.insert(0, "2E");
editable.insert(2, ".");
assertEquals("2E", editable.toString());
editable.clear();
editable.insert(0, "2E.");
assertEquals("2E", editable.toString());
}
@Test
public void testShouldNotInsertTwoExcponents() throws Exception {
editable.insert(0, "2EE");
assertEquals("2E", editable.toString());
}
@Test
public void testShouldNotInsertExponentBeforeDecimalPoint() throws Exception {
editable.insert(0, "0.2");
editable.insert(0, "E");
assertEquals("0.2", editable.toString());
}
}