From dd74c3a23114ec21932fbbfeeec07bda75e46a9e Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 29 Feb 2016 00:24:06 +0100 Subject: [PATCH] Tests --- .../calculations/BaseCalculationEvent.java | 9 ++++++ .../CalculationFinishedEvent.java | 10 ++++++ .../calculator/BaseCalculatorTest.java | 11 +------ .../android/calculator/EditorTest.java | 14 +++++++-- .../FromJsclSimplifyTextProcessorTest.java | 3 +- .../solovyev/android/calculator/Tests.java | 31 +++++++++---------- .../calculator/TextHighlighterTest.java | 9 ++++-- .../calculator/ToJsclTextProcessorTest.java | 2 +- .../FromJsclNumericTextProcessorTest.java | 1 + .../android/calculator/math/MathTypeTest.java | 4 +-- 10 files changed, 58 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/calculations/BaseCalculationEvent.java b/app/src/main/java/org/solovyev/android/calculator/calculations/BaseCalculationEvent.java index 0ed38c53..273cfd7f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/calculations/BaseCalculationEvent.java +++ b/app/src/main/java/org/solovyev/android/calculator/calculations/BaseCalculationEvent.java @@ -16,4 +16,13 @@ public abstract class BaseCalculationEvent { this.expression = expression; this.sequence = sequence; } + + @Override + public String toString() { + return "BaseCalculationEvent{" + + "operation=" + operation + + ", expression='" + expression + '\'' + + ", sequence=" + sequence + + '}'; + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/calculations/CalculationFinishedEvent.java b/app/src/main/java/org/solovyev/android/calculator/calculations/CalculationFinishedEvent.java index bb1aa3e9..8a073f44 100644 --- a/app/src/main/java/org/solovyev/android/calculator/calculations/CalculationFinishedEvent.java +++ b/app/src/main/java/org/solovyev/android/calculator/calculations/CalculationFinishedEvent.java @@ -30,4 +30,14 @@ public final class CalculationFinishedEvent extends BaseCalculationEvent { this.stringResult = stringResult; this.messages = messages; } + + @Override + public String toString() { + return "CalculationFinishedEvent{" + + "super=" + super.toString() + + ", result=" + result + + ", stringResult='" + stringResult + '\'' + + ", messages=" + messages + + "}"; + } } diff --git a/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java b/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java index c67920e9..519235df 100644 --- a/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java @@ -3,16 +3,12 @@ package org.solovyev.android.calculator; import android.content.SharedPreferences; import android.support.annotation.NonNull; import com.squareup.otto.Bus; -import jscl.JsclMathEngine; import org.hamcrest.Description; import org.junit.Before; import org.mockito.ArgumentMatcher; import org.solovyev.android.calculator.calculations.CalculationFailedEvent; import org.solovyev.android.calculator.calculations.CalculationFinishedEvent; -import org.solovyev.android.calculator.functions.FunctionsRegistry; import org.solovyev.android.calculator.jscl.JsclOperation; -import org.solovyev.android.calculator.operators.OperatorsRegistry; -import org.solovyev.android.calculator.operators.PostfixFunctionsRegistry; import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.*; @@ -27,13 +23,8 @@ public abstract class BaseCalculatorTest { public void setUp() throws Exception { bus = mock(Bus.class); calculator = new Calculator(mock(SharedPreferences.class), bus, Tests.sameThreadExecutor()); - final JsclMathEngine mathEngine = JsclMathEngine.getInstance(); - engine = new Engine(mathEngine); - engine.postfixFunctionsRegistry = new PostfixFunctionsRegistry(mathEngine); - engine.functionsRegistry = new FunctionsRegistry(mathEngine); - engine.variablesRegistry = new VariablesRegistry(mathEngine); + engine = Tests.makeEngine(); engine.variablesRegistry.bus = bus; - engine.operatorsRegistry = new OperatorsRegistry(mathEngine); calculator.engine = engine; calculator.preferredPreferences = mock(PreferredPreferences.class); final ToJsclTextProcessor processor = new ToJsclTextProcessor(); diff --git a/app/src/test/java/org/solovyev/android/calculator/EditorTest.java b/app/src/test/java/org/solovyev/android/calculator/EditorTest.java index 60436b25..9a16d43f 100644 --- a/app/src/test/java/org/solovyev/android/calculator/EditorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/EditorTest.java @@ -23,19 +23,27 @@ package org.solovyev.android.calculator; import android.content.SharedPreferences; -import jscl.JsclMathEngine; +import android.os.Build; +import com.squareup.otto.Bus; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.annotation.Config; +import static org.mockito.Mockito.mock; + +@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) +@RunWith(value = RobolectricGradleTestRunner.class) public class EditorTest { private Editor editor; @Before public void setUp() throws Exception { - this.editor = new Editor(Mockito.mock(SharedPreferences.class), new Engine(new JsclMathEngine())); + editor = new Editor(mock(SharedPreferences.class), Tests.makeEngine()); + editor.bus = mock(Bus.class); } @Test diff --git a/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java b/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java index 85ab64f5..073e6dd3 100644 --- a/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java @@ -22,7 +22,6 @@ package org.solovyev.android.calculator; -import jscl.JsclMathEngine; import org.junit.Test; import org.solovyev.android.calculator.text.FromJsclSimplifyTextProcessor; import org.solovyev.android.calculator.variables.CppVariable; @@ -35,7 +34,7 @@ public class FromJsclSimplifyTextProcessorTest { @Test public void testProcess() throws Exception { - final Engine engine = new Engine(new JsclMathEngine()); + final Engine engine = Tests.makeEngine(); FromJsclSimplifyTextProcessor tp = new FromJsclSimplifyTextProcessor(engine); //Assert.assertEquals("(e)", tp.process("(2.718281828459045)")); //Assert.assertEquals("ee", tp.process("2.718281828459045*2.718281828459045")); diff --git a/app/src/test/java/org/solovyev/android/calculator/Tests.java b/app/src/test/java/org/solovyev/android/calculator/Tests.java index e6eadc8b..fb8f5108 100644 --- a/app/src/test/java/org/solovyev/android/calculator/Tests.java +++ b/app/src/test/java/org/solovyev/android/calculator/Tests.java @@ -1,17 +1,13 @@ package org.solovyev.android.calculator; import android.support.annotation.NonNull; -import org.solovyev.android.calculator.calculations.CalculationFailedEvent; -import org.solovyev.android.calculator.calculations.CalculationFinishedEvent; -import org.solovyev.common.msg.Message; +import jscl.JsclMathEngine; +import org.solovyev.android.calculator.functions.FunctionsRegistry; +import org.solovyev.android.calculator.operators.OperatorsRegistry; +import org.solovyev.android.calculator.operators.PostfixFunctionsRegistry; -import java.util.ArrayList; import java.util.concurrent.Executor; -import static org.mockito.Matchers.refEq; -import static org.mockito.Mockito.verify; -import static org.solovyev.android.calculator.jscl.JsclOperation.numeric; - public class Tests { @NonNull @@ -24,13 +20,16 @@ public class Tests { }; } - static void assertError(@NonNull Calculator calculator, @NonNull String expression) { - calculator.evaluate(numeric, expression); - verify(calculator.bus).post(refEq(new CalculationFailedEvent(numeric, expression, 0, new Exception()), "exception", "sequence")); - } - - static void assertEval(@NonNull Calculator calculator, @NonNull String expression, @NonNull String expected) { - calculator.evaluate(numeric, expression); - verify(calculator.bus).post(refEq(new CalculationFinishedEvent(numeric, expression, 0, null, expected, new ArrayList()), "result", "sequence")); + @NonNull + public static Engine makeEngine() { + final JsclMathEngine mathEngine = JsclMathEngine.getInstance(); + mathEngine.setUseGroupingSeparator(true); + mathEngine.setGroupingSeparator(' '); + final Engine engine = new Engine(mathEngine); + engine.postfixFunctionsRegistry = new PostfixFunctionsRegistry(mathEngine); + engine.functionsRegistry = new FunctionsRegistry(mathEngine); + engine.variablesRegistry = new VariablesRegistry(mathEngine); + engine.operatorsRegistry = new OperatorsRegistry(mathEngine); + return engine; } } diff --git a/app/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java b/app/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java index b186312b..35681947 100644 --- a/app/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java @@ -23,11 +23,14 @@ package org.solovyev.android.calculator; import android.graphics.Color; -import jscl.JsclMathEngine; +import android.os.Build; import jscl.MathEngine; import jscl.NumeralBase; 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.text.TextProcessor; import org.solovyev.android.calculator.view.TextHighlighter; @@ -36,13 +39,15 @@ import java.util.Random; import static org.junit.Assert.*; +@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) +@RunWith(value = RobolectricGradleTestRunner.class) public class TextHighlighterTest { private Engine engine; @Before public void setUp() throws Exception { - engine = new Engine(new JsclMathEngine()); + engine = Tests.makeEngine(); } @Test diff --git a/app/src/test/java/org/solovyev/android/calculator/ToJsclTextProcessorTest.java b/app/src/test/java/org/solovyev/android/calculator/ToJsclTextProcessorTest.java index 301b64f9..b261c215 100644 --- a/app/src/test/java/org/solovyev/android/calculator/ToJsclTextProcessorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/ToJsclTextProcessorTest.java @@ -35,7 +35,7 @@ public class ToJsclTextProcessorTest { @Before public void setUp() throws Exception { preprocessor = new ToJsclTextProcessor(); - preprocessor.engine = new Engine(new JsclMathEngine()); + preprocessor.engine = Tests.makeEngine(); } @Test diff --git a/app/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java b/app/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java index 459d754a..4484edf5 100644 --- a/app/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java @@ -37,6 +37,7 @@ public class FromJsclNumericTextProcessorTest { final FromJsclNumericTextProcessor cm = new FromJsclNumericTextProcessor(); final JsclMathEngine me = JsclMathEngine.getInstance(); + me.setGroupingSeparator(' '); final AngleUnit defaultAngleUnits = me.getAngleUnits(); assertEquals("1.22133+23 123i", cm.process(Expression.valueOf("1.22133232+23123*i").numeric())); diff --git a/app/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java b/app/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java index 5d55af0a..b2868fe1 100644 --- a/app/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java @@ -22,11 +22,11 @@ package org.solovyev.android.calculator.math; -import jscl.JsclMathEngine; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.solovyev.android.calculator.Engine; +import org.solovyev.android.calculator.Tests; import static org.junit.Assert.assertEquals; import static org.solovyev.android.calculator.math.MathType.postfix_function; @@ -37,7 +37,7 @@ public class MathTypeTest { @Before public void setUp() throws Exception { - engine = new Engine(new JsclMathEngine()); + engine = Tests.makeEngine(); } @Test