registries
This commit is contained in:
@@ -1,46 +1,58 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.solovyev.android</groupId>
|
||||
<artifactId>calculatorpp-parent</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.solovyev.android</groupId>
|
||||
<artifactId>calculatorpp-core</artifactId>
|
||||
<version>1.3.2</version>
|
||||
<name>Calculator++ Application Core</name>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.intellij</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.solovyev</groupId>
|
||||
<artifactId>jscl</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.simpleframework</groupId>
|
||||
<artifactId>simple-xml</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.solovyev.android</groupId>
|
||||
<artifactId>calculatorpp-parent</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.solovyev.android</groupId>
|
||||
<artifactId>calculatorpp-core</artifactId>
|
||||
<version>1.3.2</version>
|
||||
<name>Calculator++ Application Core</name>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.solovyev</groupId>
|
||||
<artifactId>common-text</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>1.9.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.intellij</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.solovyev</groupId>
|
||||
<artifactId>jscl</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.simpleframework</groupId>
|
||||
<artifactId>simple-xml</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/30/11
|
||||
* Time: 1:03 AM
|
||||
*/
|
||||
public abstract class AbstractCalculatorMathRegistry<T extends MathEntity, P extends MathPersistenceEntity> implements CalculatorMathRegistry<T> {
|
||||
|
||||
@NotNull
|
||||
private final MathRegistry<T> mathRegistry;
|
||||
|
||||
@NotNull
|
||||
private final String prefix;
|
||||
|
||||
@NotNull
|
||||
private final MathEntityDao<P> mathEntityDao;
|
||||
|
||||
protected AbstractCalculatorMathRegistry(@NotNull MathRegistry<T> mathRegistry,
|
||||
@NotNull String prefix,
|
||||
@NotNull MathEntityDao<P> mathEntityDao) {
|
||||
this.mathRegistry = mathRegistry;
|
||||
this.prefix = prefix;
|
||||
this.mathEntityDao = mathEntityDao;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@NotNull
|
||||
protected abstract Map<String, String> getSubstitutes();
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDescription(@NotNull String mathEntityName) {
|
||||
final String stringName;
|
||||
|
||||
final Map<String, String> substitutes = getSubstitutes();
|
||||
final String substitute = substitutes.get(mathEntityName);
|
||||
if (substitute == null) {
|
||||
stringName = prefix + mathEntityName;
|
||||
} else {
|
||||
stringName = prefix + substitute;
|
||||
}
|
||||
|
||||
return mathEntityDao.getDescription(stringName);
|
||||
}
|
||||
|
||||
public synchronized void load() {
|
||||
final MathEntityPersistenceContainer<P> persistenceContainer = mathEntityDao.load();
|
||||
|
||||
if (persistenceContainer != null) {
|
||||
for (P entity : persistenceContainer.getEntities()) {
|
||||
if (!contains(entity.getName())) {
|
||||
add(createBuilder(entity));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!");
|
||||
for (Var var : vars) {
|
||||
Log.d(AndroidVarsRegistry.class.getName(), var.toString());
|
||||
}*/
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected abstract JBuilder<? extends T> createBuilder(@NotNull P entity);
|
||||
|
||||
|
||||
@Override
|
||||
public synchronized void save() {
|
||||
final MathEntityPersistenceContainer<P> container = createPersistenceContainer();
|
||||
|
||||
for (T entity : this.getEntities()) {
|
||||
if (!entity.isSystem()) {
|
||||
final P persistenceEntity = transform(entity);
|
||||
if (persistenceEntity != null) {
|
||||
container.getEntities().add(persistenceEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.mathEntityDao.save(container);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected abstract P transform(@NotNull T entity);
|
||||
|
||||
@NotNull
|
||||
protected abstract MathEntityPersistenceContainer<P> createPersistenceContainer();
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<T> getEntities() {
|
||||
return mathRegistry.getEntities();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<T> getSystemEntities() {
|
||||
return mathRegistry.getSystemEntities();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T add(@NotNull JBuilder<? extends T> JBuilder) {
|
||||
return mathRegistry.add(JBuilder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(@NotNull T var) {
|
||||
mathRegistry.remove(var);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> getNames() {
|
||||
return mathRegistry.getNames();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull String name) {
|
||||
return mathRegistry.contains(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(@NotNull String name) {
|
||||
return mathRegistry.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getById(@NotNull Integer id) {
|
||||
return mathRegistry.getById(id);
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/7/12
|
||||
* Time: 6:43 PM
|
||||
*/
|
||||
public interface MathEntityDao<T extends MathPersistenceEntity> {
|
||||
|
||||
void save(@NotNull MathEntityPersistenceContainer<T> container);
|
||||
|
||||
@Nullable
|
||||
MathEntityPersistenceContainer<T> load();
|
||||
|
||||
@Nullable
|
||||
String getDescription(@NotNull String descriptionId);
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 5:03 PM
|
||||
*/
|
||||
public interface MathEntityPersistenceContainer<T extends MathPersistenceEntity> {
|
||||
|
||||
public List<T> getEntities();
|
||||
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 5:27 PM
|
||||
*/
|
||||
public interface MathPersistenceEntity {
|
||||
|
||||
@NotNull
|
||||
String getName();
|
||||
}
|
@@ -354,7 +354,7 @@ public enum MathType {
|
||||
final String s = CollectionsUtils.find(mathType.getTokens(), startsWithFinder);
|
||||
if (s != null) {
|
||||
if ( s.length() == 1 ) {
|
||||
if (hexMode || JsclMathEngine.instance.getNumeralBase() == NumeralBase.hex) {
|
||||
if (hexMode || JsclMathEngine.getInstance().getNumeralBase() == NumeralBase.hex) {
|
||||
final Character ch = s.charAt(0);
|
||||
if ( NumeralBase.hex.getAcceptableCharacters().contains(ch) ) {
|
||||
return new Result(MathType.digit, s);
|
||||
|
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.solovyev.android.calculator.MathPersistenceEntity;
|
||||
import org.solovyev.common.collections.CollectionsUtils;
|
||||
import org.solovyev.common.text.CollectionTransformations;
|
||||
import org.solovyev.common.text.StringMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 5:25 PM
|
||||
*/
|
||||
|
||||
@Root
|
||||
public class AFunction implements MathPersistenceEntity {
|
||||
|
||||
@Element
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Element
|
||||
@NotNull
|
||||
private String content;
|
||||
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String parameterNames;
|
||||
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(@NotNull String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(@NotNull String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getParameterNames() {
|
||||
return parameterNames;
|
||||
}
|
||||
|
||||
public void setParameterNames(@Nullable String[] parameterNames) {
|
||||
this.parameterNames = CollectionTransformations.formatValue(CollectionsUtils.asList(parameterNames), ";", new StringMapper());
|
||||
}
|
||||
|
||||
public void setParameterNames(@Nullable String parameterNames) {
|
||||
this.parameterNames = parameterNames;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String[] getParameterNamesAsArray() {
|
||||
final List<String> parameterNamesAsList = CollectionTransformations.split(parameterNames, ";");
|
||||
return parameterNamesAsList.toArray(new String[parameterNamesAsList.size()]);
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import jscl.math.function.CustomFunction;
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 5:15 PM
|
||||
*/
|
||||
@Root
|
||||
public class Functions implements MathEntityPersistenceContainer<AFunction> {
|
||||
|
||||
@ElementList(type = CustomFunction.class)
|
||||
private List<AFunction> functions = new ArrayList<AFunction>();
|
||||
|
||||
public Functions() {
|
||||
}
|
||||
|
||||
public List<AFunction> getEntities() {
|
||||
return functions;
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.JsclMathEngine;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.mockito.Mockito;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistory;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/7/12
|
||||
* Time: 6:30 PM
|
||||
*/
|
||||
public class AbstractCalculatorTest {
|
||||
|
||||
protected static void staticSetUp() throws Exception {
|
||||
CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class));
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static CalculatorEngineImpl newCalculatorEngine() {
|
||||
final CalculatorMathRegistry varsRegistry = Mockito.mock(CalculatorMathRegistry.class);
|
||||
//Mockito.when(varsRegistry.get())
|
||||
|
||||
final CalculatorEngineImpl result = new CalculatorEngineImpl(JsclMathEngine.getInstance(), varsRegistry, Mockito.mock(CalculatorMathRegistry.class), Mockito.mock(CalculatorMathRegistry.class), Mockito.mock(CalculatorMathRegistry.class), null);
|
||||
result.init();
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -1,200 +1,201 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
* Time: 12:44
|
||||
*/
|
||||
public class CalculatorEditorImplTest {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditor calculatorEditor;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.calculatorEditor = new CalculatorEditorImpl(new CalculatorImpl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsert() throws Exception {
|
||||
CalculatorEditorViewState viewState = this.calculatorEditor.getViewState();
|
||||
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("");
|
||||
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("test");
|
||||
|
||||
Assert.assertEquals("test", viewState.getText());
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("test");
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("");
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("1234567890");
|
||||
Assert.assertEquals("testtest1234567890", viewState.getText());
|
||||
Assert.assertEquals(18, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveCursorLeft();
|
||||
viewState = this.calculatorEditor.insert("9");
|
||||
Assert.assertEquals("testtest12345678990", viewState.getText());
|
||||
Assert.assertEquals(18, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setCursorOnStart();
|
||||
viewState = this.calculatorEditor.insert("9");
|
||||
Assert.assertEquals("9testtest12345678990", viewState.getText());
|
||||
Assert.assertEquals(1, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.erase();
|
||||
viewState = this.calculatorEditor.insert("9");
|
||||
Assert.assertEquals("9testtest12345678990", viewState.getText());
|
||||
Assert.assertEquals(1, viewState.getSelection());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErase() throws Exception {
|
||||
this.calculatorEditor.setText("");
|
||||
this.calculatorEditor.erase();
|
||||
|
||||
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.setText("test");
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("tes", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("te", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("t", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.setText("1234");
|
||||
this.calculatorEditor.moveCursorLeft();
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("124", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("14", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("4", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.setText("1");
|
||||
this.calculatorEditor.moveCursorLeft();
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("1", this.calculatorEditor.getViewState().getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveSelection() throws Exception {
|
||||
this.calculatorEditor.setText("");
|
||||
|
||||
CalculatorEditorViewState viewState = this.calculatorEditor.moveSelection(0);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(2);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(100);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-3);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-100);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("0123456789");
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(0);
|
||||
Assert.assertEquals(10, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(1);
|
||||
Assert.assertEquals(10, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-2);
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(1);
|
||||
Assert.assertEquals(9, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-9);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-10);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(2);
|
||||
Assert.assertEquals(2, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(2);
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-6);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetText() throws Exception {
|
||||
CalculatorEditorViewState viewState = this.calculatorEditor.setText("test");
|
||||
|
||||
Assert.assertEquals("test", viewState.getText());
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("testtest");
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("");
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("testtest", 0);
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("testtest", 2);
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(2, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", 0);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", 3);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", -3);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("test");
|
||||
Assert.assertEquals("test", viewState.getText());
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", 2);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
}
|
||||
}
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
* Time: 12:44
|
||||
*/
|
||||
public class CalculatorEditorImplTest extends AbstractCalculatorTest {
|
||||
|
||||
@NotNull
|
||||
private CalculatorEditor calculatorEditor;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
this.calculatorEditor = new CalculatorEditorImpl(CalculatorLocatorImpl.getInstance().getCalculator());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsert() throws Exception {
|
||||
CalculatorEditorViewState viewState = this.calculatorEditor.getViewState();
|
||||
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("");
|
||||
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("test");
|
||||
|
||||
Assert.assertEquals("test", viewState.getText());
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("test");
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("");
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("1234567890");
|
||||
Assert.assertEquals("testtest1234567890", viewState.getText());
|
||||
Assert.assertEquals(18, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveCursorLeft();
|
||||
viewState = this.calculatorEditor.insert("9");
|
||||
Assert.assertEquals("testtest12345678990", viewState.getText());
|
||||
Assert.assertEquals(18, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setCursorOnStart();
|
||||
viewState = this.calculatorEditor.insert("9");
|
||||
Assert.assertEquals("9testtest12345678990", viewState.getText());
|
||||
Assert.assertEquals(1, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.erase();
|
||||
viewState = this.calculatorEditor.insert("9");
|
||||
Assert.assertEquals("9testtest12345678990", viewState.getText());
|
||||
Assert.assertEquals(1, viewState.getSelection());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErase() throws Exception {
|
||||
this.calculatorEditor.setText("");
|
||||
this.calculatorEditor.erase();
|
||||
|
||||
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.setText("test");
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("tes", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("te", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("t", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.setText("1234");
|
||||
this.calculatorEditor.moveCursorLeft();
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("124", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("14", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("4", this.calculatorEditor.getViewState().getText());
|
||||
|
||||
this.calculatorEditor.setText("1");
|
||||
this.calculatorEditor.moveCursorLeft();
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("1", this.calculatorEditor.getViewState().getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveSelection() throws Exception {
|
||||
this.calculatorEditor.setText("");
|
||||
|
||||
CalculatorEditorViewState viewState = this.calculatorEditor.moveSelection(0);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(2);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(100);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-3);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-100);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("0123456789");
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(0);
|
||||
Assert.assertEquals(10, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(1);
|
||||
Assert.assertEquals(10, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-2);
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(1);
|
||||
Assert.assertEquals(9, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-9);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-10);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(2);
|
||||
Assert.assertEquals(2, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(2);
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-6);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetText() throws Exception {
|
||||
CalculatorEditorViewState viewState = this.calculatorEditor.setText("test");
|
||||
|
||||
Assert.assertEquals("test", viewState.getText());
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("testtest");
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("");
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("testtest", 0);
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("testtest", 2);
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(2, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", 0);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", 3);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", -3);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("test");
|
||||
Assert.assertEquals("test", viewState.getText());
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", 2);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.math;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.AbstractCalculatorTest;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/5/11
|
||||
* Time: 1:25 AM
|
||||
*/
|
||||
public class MathTypeTest extends AbstractCalculatorTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void staticSetUp() throws Exception {
|
||||
AbstractCalculatorTest.staticSetUp();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetType() throws Exception {
|
||||
Assert.assertEquals(MathType.function, MathType.getType("sin", 0, false).getMathType());
|
||||
Assert.assertEquals(MathType.text, MathType.getType("sn", 0, false).getMathType());
|
||||
Assert.assertEquals(MathType.text, MathType.getType("s", 0, false).getMathType());
|
||||
Assert.assertEquals(MathType.text, MathType.getType("", 0, false).getMathType());
|
||||
|
||||
try {
|
||||
Assert.assertEquals(MathType.text, MathType.getType("22", -1, false).getMathType());
|
||||
Assert.fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
Assert.assertEquals(MathType.text, MathType.getType("22", 2, false).getMathType());
|
||||
Assert.fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
|
||||
Assert.assertEquals("atanh", MathType.getType("atanh", 0, false).getMatch());
|
||||
}
|
||||
|
||||
/* @Test
|
||||
public void testPostfixFunctionsProcessing() throws Exception {
|
||||
|
||||
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart("5!", 1));
|
||||
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("!", 1));
|
||||
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart("5.4434234!", 9));
|
||||
org.junit.Assert.assertEquals(1, MathType.getPostfixFunctionStart("2+5!", 3));
|
||||
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+5.4434234!", 14));
|
||||
org.junit.Assert.assertEquals(14, MathType.getPostfixFunctionStart("2.23+5.4434234*5!", 16));
|
||||
org.junit.Assert.assertEquals(14, MathType.getPostfixFunctionStart("2.23+5.4434234*5.1!", 18));
|
||||
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*5.1)!", 20));
|
||||
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*(5.1+1))!", 24));
|
||||
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*sin(5.1+1))!", 27));
|
||||
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("sin(5)!", 6));
|
||||
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart(")!", ")!".indexOf("!")));
|
||||
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("sin(5sin(5sin(5)))!", 18));
|
||||
org.junit.Assert.assertEquals(2, MathType.getPostfixFunctionStart("2+sin(5sin(5sin(5)))!", 20));
|
||||
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1+1))!", 30));
|
||||
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1E2+e))!", "2.23+sin(5.4434234*sin(5.1E2+e))!".indexOf("!")));
|
||||
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1E2+5 555 555))!", "2.23+sin(5.4434234*sin(5.1E2+5 555 555))!".indexOf("!")));
|
||||
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234^sin(5.1E2!+5'555'555))!", "2.23+sin(5.4434234^sin(5.1E2!+5'555'555))!".lastIndexOf("!")));
|
||||
}*/
|
||||
}
|
Reference in New Issue
Block a user