2017-07-30 14:52:10 -07:00
|
|
|
package org.nwapw.abacus.tests;
|
|
|
|
|
|
|
|
import org.junit.Assert;
|
2017-07-30 19:21:26 -07:00
|
|
|
import org.junit.BeforeClass;
|
2017-07-30 14:52:10 -07:00
|
|
|
import org.junit.Test;
|
2017-07-30 19:21:26 -07:00
|
|
|
import org.nwapw.abacus.Abacus;
|
|
|
|
import org.nwapw.abacus.function.Function;
|
|
|
|
import org.nwapw.abacus.function.Operator;
|
|
|
|
import org.nwapw.abacus.function.OperatorAssociativity;
|
|
|
|
import org.nwapw.abacus.function.OperatorType;
|
2017-07-30 14:52:10 -07:00
|
|
|
import org.nwapw.abacus.lexing.pattern.Match;
|
2017-07-30 19:21:26 -07:00
|
|
|
import org.nwapw.abacus.number.NumberInterface;
|
2017-07-30 14:52:10 -07:00
|
|
|
import org.nwapw.abacus.parsing.LexerTokenizer;
|
2017-07-30 19:21:26 -07:00
|
|
|
import org.nwapw.abacus.plugin.Plugin;
|
2017-07-30 14:52:10 -07:00
|
|
|
import org.nwapw.abacus.tree.TokenType;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class TokenizerTests {
|
|
|
|
|
2017-07-30 19:21:26 -07:00
|
|
|
private static Abacus abacus = new Abacus();
|
|
|
|
private static LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
|
|
|
private static Function subtractFunction = new Function() {
|
|
|
|
@Override
|
|
|
|
protected boolean matchesParams(NumberInterface[] params) {
|
|
|
|
return params.length == 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
|
|
return params[0].subtract(params[1]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private static Plugin testPlugin = new Plugin(abacus.getPluginManager()) {
|
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
|
|
|
registerOperator("+", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,
|
|
|
|
0, subtractFunction));
|
|
|
|
registerOperator("-", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,
|
|
|
|
0, subtractFunction));
|
|
|
|
registerFunction("subtract", subtractFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private static void assertTokensMatch(List<Match<TokenType>> tokenList, TokenType[] expectedTypes) {
|
|
|
|
Assert.assertNotNull(tokenList);
|
|
|
|
Assert.assertEquals(tokenList.size(), expectedTypes.length);
|
|
|
|
for (int i = 0; i < expectedTypes.length; i++) {
|
|
|
|
Assert.assertEquals(expectedTypes[i], tokenList.get(i).getType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
public static void prepareTests() {
|
|
|
|
abacus.getPluginManager().addListener(lexerTokenizer);
|
|
|
|
abacus.getPluginManager().addInstantiated(testPlugin);
|
|
|
|
abacus.getPluginManager().load();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testInteger() {
|
2017-07-30 21:11:32 -07:00
|
|
|
assertTokensMatch(lexerTokenizer.tokenizeString("11"), new TokenType[]{TokenType.NUM});
|
2017-07-30 19:21:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testLeadingZeroDecimal() {
|
2017-07-30 21:11:32 -07:00
|
|
|
assertTokensMatch(lexerTokenizer.tokenizeString("0.1"), new TokenType[]{TokenType.NUM});
|
2017-07-30 19:21:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testNonLeadingDecimal() {
|
2017-07-30 21:11:32 -07:00
|
|
|
assertTokensMatch(lexerTokenizer.tokenizeString(".1"), new TokenType[]{TokenType.NUM});
|
2017-07-30 19:21:26 -07:00
|
|
|
}
|
2017-07-30 14:52:10 -07:00
|
|
|
|
|
|
|
@Test
|
2017-07-30 19:21:26 -07:00
|
|
|
public void testSimpleChars() {
|
|
|
|
TokenType[] types = {
|
|
|
|
TokenType.OPEN_PARENTH,
|
|
|
|
TokenType.WHITESPACE,
|
|
|
|
TokenType.COMMA,
|
|
|
|
TokenType.CLOSE_PARENTH
|
|
|
|
};
|
|
|
|
assertTokensMatch(lexerTokenizer.tokenizeString("( ,)"), types);
|
2017-07-30 14:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-07-30 19:21:26 -07:00
|
|
|
public void testFunctionParsing() {
|
|
|
|
TokenType[] types = {
|
|
|
|
TokenType.FUNCTION,
|
|
|
|
TokenType.OPEN_PARENTH,
|
|
|
|
TokenType.NUM,
|
|
|
|
TokenType.COMMA,
|
|
|
|
TokenType.NUM,
|
|
|
|
TokenType.CLOSE_PARENTH
|
|
|
|
};
|
|
|
|
assertTokensMatch(lexerTokenizer.tokenizeString("subtract(1,2)"), types);
|
2017-07-30 14:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-07-30 21:11:32 -07:00
|
|
|
public void testOperatorParsing() {
|
2017-07-30 19:21:26 -07:00
|
|
|
TokenType[] types = {
|
|
|
|
TokenType.NUM,
|
|
|
|
TokenType.OP,
|
|
|
|
TokenType.NUM
|
|
|
|
};
|
|
|
|
assertTokensMatch(lexerTokenizer.tokenizeString("1-1"), types);
|
2017-07-30 14:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-07-30 21:11:32 -07:00
|
|
|
public void testSanitizedOperators() {
|
2017-07-30 19:21:26 -07:00
|
|
|
TokenType[] types = {
|
|
|
|
TokenType.NUM,
|
|
|
|
TokenType.OP,
|
|
|
|
TokenType.NUM
|
|
|
|
};
|
|
|
|
assertTokensMatch(lexerTokenizer.tokenizeString("1+1"), types);
|
2017-07-30 14:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|