From 28a573c777e22b5e57fbcfeff3bdbdeda364d0b2 Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 15 Apr 2016 22:53:44 +0200 Subject: [PATCH] Exponent can't be first in number + tests for NumberInputFilter --- .../text/method/NumberInputFilter.java | 5 +- .../text/method/NumberInputFilterTest.java | 112 ++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 app/src/test/java/org/solovyev/android/text/method/NumberInputFilterTest.java diff --git a/app/src/main/java/org/solovyev/android/text/method/NumberInputFilter.java b/app/src/main/java/org/solovyev/android/text/method/NumberInputFilter.java index 5600c096..f7114173 100644 --- a/app/src/main/java/org/solovyev/android/text/method/NumberInputFilter.java +++ b/app/src/main/java/org/solovyev/android/text/method/NumberInputFilter.java @@ -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; } diff --git a/app/src/test/java/org/solovyev/android/text/method/NumberInputFilterTest.java b/app/src/test/java/org/solovyev/android/text/method/NumberInputFilterTest.java new file mode 100644 index 00000000..f32c0282 --- /dev/null +++ b/app/src/test/java/org/solovyev/android/text/method/NumberInputFilterTest.java @@ -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()); + } +} \ No newline at end of file