Skip to content

Commit

Permalink
Changed some minor aspects of the variable name fetching feature
Browse files Browse the repository at this point in the history
  • Loading branch information
fasseg committed Oct 13, 2016
1 parent f5b4f86 commit 115238b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 50 deletions.
6 changes: 3 additions & 3 deletions src/main/java/net/objecthunter/exp4j/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public Expression setVariables(Map<String, Double> variables) {
return this;
}

public Set<String> getVariableTokenNames() {
Set<String> variables = new TreeSet<String>();
for (Token t: tokens) {
public Set<String> getVariableNames() {
final Set<String> variables = new HashSet<String>();
for (final Token t: tokens) {
if (t.getType() == Token.TOKEN_VARIABLE)
variables.add(((VariableToken)t).getName());
}
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/net/objecthunter/exp4j/ExpressionBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import static java.lang.Math.*;
import static org.junit.Assert.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -2652,6 +2652,28 @@ public double apply(double... args) {
assertEquals(expected, result, 0d);
}

@Test
public void testGetVariableNames1() throws Exception{
Expression e = new ExpressionBuilder("b*a-9.24c")
.variables("b", "a", "c")
.build();
Set<String> variableNames = e.getVariableNames();
assertTrue(variableNames.contains("a"));
assertTrue(variableNames.contains("b"));
assertTrue(variableNames.contains("c"));
}

@Test
public void testGetVariableNames2() throws Exception{
Expression e = new ExpressionBuilder("log(bar)-FOO.s/9.24c")
.variables("bar", "FOO.s", "c")
.build();
Set<String> variableNames = e.getVariableNames();
assertTrue(variableNames.contains("bar"));
assertTrue(variableNames.contains("FOO.s"));
assertTrue(variableNames.contains("c"));
}

@Test(expected = IllegalArgumentException.class)
public void testSameVariableAndBuiltinFunctionName() {
Expression e = new ExpressionBuilder("log10(log10)")
Expand Down
17 changes: 13 additions & 4 deletions src/test/java/net/objecthunter/exp4j/ExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

import net.objecthunter.exp4j.function.Functions;
import net.objecthunter.exp4j.operator.Operators;
import net.objecthunter.exp4j.tokenizer.FunctionToken;
import net.objecthunter.exp4j.tokenizer.NumberToken;
import net.objecthunter.exp4j.tokenizer.OperatorToken;
import net.objecthunter.exp4j.tokenizer.Token;
import net.objecthunter.exp4j.tokenizer.*;

import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -53,6 +50,18 @@ public void testExpression2() throws Exception{
assertEquals(0d, exp.evaluate(), 0d);
}

@Test
public void testGetVariableNames1() throws Exception{
Token[] tokens = new Token[] {
new VariableToken("a"),
new VariableToken("b"),
new OperatorToken(Operators.getBuiltinOperator('+', 2))
};
Expression exp = new Expression(tokens);

assertEquals(2, exp.getVariableNames().size());
}

@Test
@Ignore
// If Expression should be threads safe this test must pass
Expand Down
42 changes: 0 additions & 42 deletions src/test/java/net/objecthunter/exp4j/VariablesTest.java

This file was deleted.

0 comments on commit 115238b

Please sign in to comment.