|
33 | 33 |
|
34 | 34 | import java.lang.reflect.Constructor; |
35 | 35 | import java.math.BigInteger; |
| 36 | +import java.nio.ByteBuffer; |
| 37 | +import java.time.Duration; |
36 | 38 | import java.time.LocalDateTime; |
37 | 39 | import java.time.OffsetDateTime; |
38 | 40 | import java.time.ZoneOffset; |
|
51 | 53 | import static org.hamcrest.number.OrderingComparison.lessThan; |
52 | 54 | import static org.junit.Assert.assertEquals; |
53 | 55 | import static org.junit.Assert.assertTrue; |
| 56 | +import static org.junit.Assert.fail; |
54 | 57 |
|
55 | 58 | /** |
56 | 59 | * Generic Literal visitor test |
@@ -238,6 +241,11 @@ public static Iterable<Object[]> generateTestParameters() { |
238 | 241 | {"'abc\\u2300def'", "abc\u2300def"}, |
239 | 242 | {"'\u2300'", "\u2300"}, |
240 | 243 | {"'abc\u2300def'", "abc\u2300def"}, |
| 244 | + // explicit 's' suffix for string literals |
| 245 | + {"\"hello\"s", "hello"}, |
| 246 | + {"'hello's", "hello"}, |
| 247 | + {"\"\"s", "Empty"}, |
| 248 | + {"\"a\"s", "a"}, |
241 | 249 | }); |
242 | 250 | } |
243 | 251 |
|
@@ -880,4 +888,123 @@ public void shouldParse() { |
880 | 888 | assertEquals(expected, new GenericLiteralVisitor(new GremlinAntlrToJava()).visitTraversalCardinality(ctx)); |
881 | 889 | } |
882 | 890 | } |
| 891 | + |
| 892 | + @RunWith(Parameterized.class) |
| 893 | + public static class ValidCharacterLiteralTest { |
| 894 | + @Parameterized.Parameter(value = 0) |
| 895 | + public String script; |
| 896 | + |
| 897 | + @Parameterized.Parameter(value = 1) |
| 898 | + public Character expected; |
| 899 | + |
| 900 | + @Parameterized.Parameters(name = "{0}") |
| 901 | + public static Iterable<Object[]> generateTestParameters() { |
| 902 | + return Arrays.asList(new Object[][]{ |
| 903 | + {"\"a\"c", 'a'}, |
| 904 | + {"'a'c", 'a'}, |
| 905 | + {"\"\\\"\"c", '"'}, |
| 906 | + {"'\\''c", '\''}, |
| 907 | + {"\"\\\\\"c", '\\'}, |
| 908 | + }); |
| 909 | + } |
| 910 | + |
| 911 | + @Test |
| 912 | + public void shouldParse() { |
| 913 | + final GremlinLexer lexer = new GremlinLexer(CharStreams.fromString(script)); |
| 914 | + final GremlinParser parser = new GremlinParser(new CommonTokenStream(lexer)); |
| 915 | + final GremlinParser.CharacterLiteralContext ctx = parser.characterLiteral(); |
| 916 | + assertEquals(expected, new GenericLiteralVisitor(new GremlinAntlrToJava()).visitCharacterLiteral(ctx)); |
| 917 | + } |
| 918 | + } |
| 919 | + |
| 920 | + @RunWith(Parameterized.class) |
| 921 | + public static class ValidDurationLiteralTest { |
| 922 | + @Parameterized.Parameter(value = 0) |
| 923 | + public String script; |
| 924 | + |
| 925 | + @Parameterized.Parameter(value = 1) |
| 926 | + public Duration expected; |
| 927 | + |
| 928 | + @Parameterized.Parameters(name = "{0}") |
| 929 | + public static Iterable<Object[]> generateTestParameters() { |
| 930 | + return Arrays.asList(new Object[][]{ |
| 931 | + {"Duration(\"PT2H30M\")", Duration.ofHours(2).plusMinutes(30)}, |
| 932 | + {"Duration(\"PT0S\")", Duration.ZERO}, |
| 933 | + {"Duration(\"P1DT12H\")", Duration.ofDays(1).plusHours(12)}, |
| 934 | + {"Duration(\"PT-30S\")", Duration.ofSeconds(-30)}, |
| 935 | + }); |
| 936 | + } |
| 937 | + |
| 938 | + @Test |
| 939 | + public void shouldParse() { |
| 940 | + final GremlinLexer lexer = new GremlinLexer(CharStreams.fromString(script)); |
| 941 | + final GremlinParser parser = new GremlinParser(new CommonTokenStream(lexer)); |
| 942 | + final GremlinParser.DurationLiteralContext ctx = parser.durationLiteral(); |
| 943 | + assertEquals(expected, new GenericLiteralVisitor(new GremlinAntlrToJava()).visitDurationLiteral(ctx)); |
| 944 | + } |
| 945 | + } |
| 946 | + |
| 947 | + public static class InvalidDurationLiteralTest { |
| 948 | + @Test |
| 949 | + public void shouldFailOnInvalidDuration() { |
| 950 | + final GremlinLexer lexer = new GremlinLexer(CharStreams.fromString("Duration(\"not-a-duration\")")); |
| 951 | + final GremlinParser parser = new GremlinParser(new CommonTokenStream(lexer)); |
| 952 | + final GremlinParser.DurationLiteralContext ctx = parser.durationLiteral(); |
| 953 | + try { |
| 954 | + new GenericLiteralVisitor(new GremlinAntlrToJava()).visitDurationLiteral(ctx); |
| 955 | + fail("Invalid Duration value should have thrown exception"); |
| 956 | + } catch (GremlinParserException gpe) { |
| 957 | + assertThat(gpe.getMessage().contains("Invalid Duration literal:"), Matchers.is(true)); |
| 958 | + } |
| 959 | + } |
| 960 | + |
| 961 | + @Test(expected = GremlinParserException.class) |
| 962 | + public void shouldFailOnEmptyDuration() { |
| 963 | + final GremlinLexer lexer = new GremlinLexer(CharStreams.fromString("Duration(\"\")")); |
| 964 | + final GremlinParser parser = new GremlinParser(new CommonTokenStream(lexer)); |
| 965 | + final GremlinParser.DurationLiteralContext ctx = parser.durationLiteral(); |
| 966 | + new GenericLiteralVisitor(new GremlinAntlrToJava()).visitDurationLiteral(ctx); |
| 967 | + } |
| 968 | + } |
| 969 | + |
| 970 | + @RunWith(Parameterized.class) |
| 971 | + public static class ValidBinaryLiteralTest { |
| 972 | + @Parameterized.Parameter(value = 0) |
| 973 | + public String script; |
| 974 | + |
| 975 | + @Parameterized.Parameter(value = 1) |
| 976 | + public ByteBuffer expected; |
| 977 | + |
| 978 | + @Parameterized.Parameters(name = "{0}") |
| 979 | + public static Iterable<Object[]> generateTestParameters() { |
| 980 | + return Arrays.asList(new Object[][]{ |
| 981 | + {"Binary(\"AQID\")", ByteBuffer.wrap(new byte[]{1, 2, 3})}, |
| 982 | + {"Binary(\"\")", ByteBuffer.wrap(new byte[]{})}, |
| 983 | + {"Binary(\"AA==\")", ByteBuffer.wrap(new byte[]{0})}, |
| 984 | + }); |
| 985 | + } |
| 986 | + |
| 987 | + @Test |
| 988 | + public void shouldParse() { |
| 989 | + final GremlinLexer lexer = new GremlinLexer(CharStreams.fromString(script)); |
| 990 | + final GremlinParser parser = new GremlinParser(new CommonTokenStream(lexer)); |
| 991 | + final GremlinParser.BinaryLiteralContext ctx = parser.binaryLiteral(); |
| 992 | + assertEquals(expected, new GenericLiteralVisitor(new GremlinAntlrToJava()).visitBinaryLiteral(ctx)); |
| 993 | + } |
| 994 | + } |
| 995 | + |
| 996 | + public static class InvalidBinaryLiteralTest { |
| 997 | + @Test |
| 998 | + public void shouldFailOnInvalidBase64() { |
| 999 | + final GremlinLexer lexer = new GremlinLexer(CharStreams.fromString("Binary(\"!!!not-base64\")")); |
| 1000 | + final GremlinParser parser = new GremlinParser(new CommonTokenStream(lexer)); |
| 1001 | + final GremlinParser.BinaryLiteralContext ctx = parser.binaryLiteral(); |
| 1002 | + try { |
| 1003 | + new GenericLiteralVisitor(new GremlinAntlrToJava()).visitBinaryLiteral(ctx); |
| 1004 | + fail("Invalid Binary/base64 value should have thrown exception"); |
| 1005 | + } catch (GremlinParserException gpe) { |
| 1006 | + assertThat(gpe.getMessage().contains("Invalid Binary literal:"), Matchers.is(true)); |
| 1007 | + } |
| 1008 | + } |
| 1009 | + } |
883 | 1010 | } |
0 commit comments