Skip to content

Commit

Permalink
Update Regex definition parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingsilverfin committed Aug 3, 2022
1 parent 26650e1 commit 076f2c2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion java/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ public ThingConstraint.Value<?> visitPredicate(TypeQLParser.PredicateContext ctx
// LITERAL INPUT VALUES ====================================================

public String getRegex(TerminalNode string) {
return unescapeRegex(unquoteString(string.getText()));
return unescapeRegex(unescapeString(unquoteString(string.getText())));
}

@Override
Expand Down
15 changes: 7 additions & 8 deletions java/parser/test/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,6 @@ public void testEscapeString() {
final String attributeValue = "This has \"double quotes\" and a single-quoted backslash: '\\'";
final String escapedValue = attributeValue.replace("\\", "\\\\").replace("\"", "\\\"");


final String query = "insert\n" +
"$_ isa movie,\n" +
" has title \"" + escapedValue + "\";";
Expand Down Expand Up @@ -1273,23 +1272,23 @@ public void whenParsingAggregateWithWrongName_Throw() {
public void defineAttributeTypeRegex() {
final String query = "define\n" +
"digit sub attribute,\n" +
" regex '\\d';";
" regex '\\\\d';";
TypeQLDefine parsed = parseQuery(query);
TypeQLDefine expected = define(type("digit").sub("attribute").regex("\\d"));
assertQueryEquals(expected, parsed, query.replace("'", "\""));
}

@Test
public void undefineAttributeTypeRegex() {
final String query = "undefine\ndigit regex '\\d';";
final String query = "undefine\ndigit regex '\\\\d';";
TypeQLUndefine parsed = parseQuery(query);
TypeQLUndefine expected = undefine(type("digit").regex("\\d"));
assertQueryEquals(expected, parsed, query.replace("'", "\""));
}

@Test
public void regexPredicateParsesCharacterClassesCorrectly() {
final String query = "match\n$x like '\\d';";
final String query = "match\n$x like '\\\\d';";
TypeQLMatch parsed = parseQuery(query);
TypeQLMatch expected = match(var("x").like("\\d"));
assertQueryEquals(expected, parsed, query.replace("'", "\""));
Expand All @@ -1299,29 +1298,29 @@ public void regexPredicateParsesCharacterClassesCorrectly() {
public void regexPredicateParsesQuotesCorrectly() {
final String query = "match\n$x like '\\\"';";
TypeQLMatch parsed = parseQuery(query);
TypeQLMatch expected = match(var("x").like("\\\""));
TypeQLMatch expected = match(var("x").like("\""));
assertQueryEquals(expected, parsed, query.replace("'", "\""));
}

@Test
public void regexPredicateParsesBackslashesCorrectly() {
final String query = "match\n$x like '\\\\';";
TypeQLMatch parsed = parseQuery(query);
TypeQLMatch expected = match(var("x").like("\\\\"));
TypeQLMatch expected = match(var("x").like("\\"));
assertQueryEquals(expected, parsed, query.replace("'", "\""));
}

@Test
public void regexPredicateParsesNewlineCorrectly() {
final String query = "match\n$x like '\\n';";
final String query = "match\n$x like '\\\\n';";
TypeQLMatch parsed = parseQuery(query);
TypeQLMatch expected = match(var("x").like("\\n"));
assertQueryEquals(expected, parsed, query.replace("'", "\""));
}

@Test
public void regexPredicateParsesForwardSlashesCorrectly() {
final String query = "match\n$x like '\\/';";
final String query = "match\n$x like '\\\\/';";
TypeQLMatch parsed = parseQuery(query);
TypeQLMatch expected = match(var("x").like("/"));
assertQueryEquals(expected, parsed, query.replace("'", "\""));
Expand Down
2 changes: 1 addition & 1 deletion java/pattern/constraint/ThingConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ public java.lang.String toString() {
StringBuilder operation = new StringBuilder();

if (predicate().equals(LIKE)) {
operation.append(LIKE).append(SPACE).append(quoteString(escapeRegex(value())));
operation.append(LIKE).append(SPACE).append(quoteString(escapeString(escapeRegex(value()))));
} else if (predicate().equals(EQ)) {
operation.append(quoteString(escapeString(value())));
} else {
Expand Down
3 changes: 2 additions & 1 deletion java/pattern/constraint/TypeConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import static com.vaticle.typeql.lang.common.exception.ErrorMessage.INVALID_CASTING;
import static com.vaticle.typeql.lang.common.exception.ErrorMessage.MISSING_PATTERNS;
import static com.vaticle.typeql.lang.common.util.Strings.escapeRegex;
import static com.vaticle.typeql.lang.common.util.Strings.escapeString;
import static com.vaticle.typeql.lang.common.util.Strings.quoteString;
import static com.vaticle.typeql.lang.pattern.variable.UnboundVariable.hidden;

Expand Down Expand Up @@ -380,7 +381,7 @@ public TypeConstraint.Regex asRegex() {

@Override
public String toString() {
return REGEX.toString() + SPACE + quoteString(escapeRegex(regex().pattern()));
return REGEX.toString() + SPACE + quoteString(escapeString(escapeRegex(regex().pattern())));
}

@Override
Expand Down

0 comments on commit 076f2c2

Please sign in to comment.