This commit is contained in:
serso 2016-02-29 00:24:06 +01:00
parent abb67a9b0b
commit dd74c3a231
10 changed files with 58 additions and 36 deletions

View File

@ -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 +
'}';
}
}

View File

@ -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 +
"}";
}
}

View File

@ -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();

View File

@ -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

View File

@ -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"));

View File

@ -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<Message>()), "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;
}
}

View File

@ -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

View File

@ -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

View File

@ -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()));

View File

@ -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