提交 464b5882 编写于 作者: H haocao

Add scan nchars test cases for TokenizerTest.

上级 f65638bc
......@@ -17,9 +17,7 @@
package com.dangdang.ddframe.rdb.sharding.parsing.lexer.analyzer;
import com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.Literals;
import com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.Token;
import com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.TokenType;
import com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.*;
import org.junit.Test;
import org.mockito.internal.matchers.apachecommons.ReflectionEquals;
......@@ -28,9 +26,9 @@ import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertTrue;
public final class TokenizerTest {
private final Dictionary dictionary = new Dictionary();
@Test
public void assertSkipWhitespace() {
String sql = "SELECT *\tFROM\rTABLE_XXX\n";
......@@ -43,14 +41,14 @@ public final class TokenizerTest {
assertThat(tokenizer.skipWhitespace(), is(expected));
}
}
@Test
public void assertSkipCommentWithoutComment() {
String sql = "SELECT * FROM XXX_TABLE";
Tokenizer tokenizer = new Tokenizer(sql, dictionary, sql.indexOf("_"));
assertThat(tokenizer.skipComment(), is(sql.indexOf("_")));
}
@Test
public void assertSkipSingleLineComment() {
String singleLineCommentWithHyphen = "--x\"y`z\n";
......@@ -63,7 +61,7 @@ public final class TokenizerTest {
expected = sql.indexOf("/") + singleLineCommentWithSlash.length();
assertThat(slashTokenizer.skipComment(), is(expected));
}
@Test
public void assertSkipSingleLineMySQLComment() {
String comment = "#x\"y`z\n";
......@@ -72,7 +70,7 @@ public final class TokenizerTest {
int expected = sql.indexOf("#") + comment.length();
assertThat(tokenizer.skipComment(), is(expected));
}
@Test
public void assertSkipMultipleLineComment() {
String comment = "/*--xyz \n WHERE XX=1 //xyz*/";
......@@ -81,7 +79,7 @@ public final class TokenizerTest {
int expected = sql.indexOf("/") + comment.length();
assertThat(tokenizer.skipComment(), is(expected));
}
@Test(expected = UnterminatedCharException.class)
public void assertSkipMultipleLineCommentUnterminatedCharException() {
String comment = "/*--xyz \n WHERE XX=1 //xyz";
......@@ -89,7 +87,7 @@ public final class TokenizerTest {
Tokenizer tokenizer = new Tokenizer(sql, dictionary, sql.indexOf("/"));
tokenizer.skipComment();
}
@Test
public void assertSkipHint() {
String comment = "/*--xyz \n WHERE XX=1 //xyz*/";
......@@ -98,7 +96,7 @@ public final class TokenizerTest {
int expected = sql.indexOf("/") + comment.length();
assertThat(tokenizer.skipHint(), is(expected));
}
@Test(expected = UnterminatedCharException.class)
public void assertSkipHintUnterminatedCharException() {
String comment = "/*--xyz \n WHERE XX=1 //xyz";
......@@ -106,7 +104,7 @@ public final class TokenizerTest {
Tokenizer tokenizer = new Tokenizer(sql, dictionary, sql.indexOf("/"));
tokenizer.skipHint();
}
@Test
public void assertScanVariable() {
String sql = "SELECT * FROM XXX_TABLE %s WHERE YY>2";
......@@ -119,7 +117,7 @@ public final class TokenizerTest {
Tokenizer tokenizer = new Tokenizer(formatSql, dictionary, formatSql.indexOf("@"));
assertTrue(new ReflectionEquals(tokenizer.scanVariable()).matches(new Token(Literals.VARIABLE, literals, formatSql.indexOf("WHERE") - 1)));
}
@Test
public void assertScanNumber() {
String sql = "SELECT * FROM XXX_TABLE WHERE XX=%s";
......@@ -142,16 +140,40 @@ public final class TokenizerTest {
assertScanHexDecimal(sql, "0x1e", Literals.HEX);
assertScanHexDecimal(sql, "0x-1e", Literals.HEX);
}
private void assertScanNumber(final String sql, final String literals, final TokenType type) {
String formatSql = String.format(sql, literals);
Tokenizer tokenizer = new Tokenizer(formatSql, dictionary, sql.indexOf("=") + 1);
assertTrue(new ReflectionEquals(tokenizer.scanNumber()).matches(new Token(type, literals, formatSql.length())));
}
private void assertScanHexDecimal(final String sql, final String literals, final TokenType type) {
String formatSql = String.format(sql, literals);
Tokenizer tokenizer = new Tokenizer(formatSql, dictionary, sql.indexOf("=") + 1);
assertTrue(new ReflectionEquals(tokenizer.scanHexDecimal()).matches(new Token(type, literals, formatSql.length())));
}
@Test
public void assertScanNChars() {
String sql = "SELECT * FROM ORDER, XX_TABLE AS `table` WHERE YY=N'xx' And group =-1 GROUP BY YY";
Tokenizer tokenizer = new Tokenizer(sql, dictionary, sql.indexOf("ORDER"));
assertTrue(new ReflectionEquals(tokenizer.scanIdentifier()).matches(new Token(Literals.IDENTIFIER, "ORDER", sql.indexOf(","))));
tokenizer = new Tokenizer(sql, dictionary, sql.indexOf("GROUP"));
assertTrue(new ReflectionEquals(tokenizer.scanIdentifier()).matches(new Token(DefaultKeyword.GROUP, "GROUP", sql.indexOf("BY") - 1)));
tokenizer = new Tokenizer(sql, dictionary, sql.indexOf("`"));
assertTrue(new ReflectionEquals(tokenizer.scanIdentifier()).matches(new Token(Literals.IDENTIFIER, "`table`", sql.indexOf("WHERE") - 1)));
tokenizer = new Tokenizer(sql, dictionary, sql.indexOf("YY"));
assertTrue(new ReflectionEquals(tokenizer.scanIdentifier()).matches(new Token(Literals.IDENTIFIER, "YY", sql.indexOf("="))));
tokenizer = new Tokenizer(sql, dictionary, sql.indexOf("=-"));
assertTrue(new ReflectionEquals(tokenizer.scanSymbol()).matches(new Token(Symbol.EQ, "=", sql.indexOf("=-") + 1)));
tokenizer = new Tokenizer(sql, dictionary, sql.indexOf("'"));
assertTrue(new ReflectionEquals(tokenizer.scanChars()).matches(new Token(Literals.CHARS, "xx", sql.indexOf("And") - 1)));
}
@Test(expected = UnterminatedCharException.class)
public void assertScanChars() {
String sql = "SELECT * FROM XXX_TABLE AS `TEST";
Tokenizer tokenizer = new Tokenizer(sql, dictionary, sql.indexOf("`"));
tokenizer.scanChars();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册