This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
forked from nawforce/apex-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix wrong quoting characters for SOSL FIND queries
- Loading branch information
Showing
10 changed files
with
209 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
jvm/src/test/java/com/nawforce/apexparser/SOSLParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.nawforce.apexparser; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import javafx.util.Pair; | ||
|
||
import static com.nawforce.apexparser.SyntaxErrorCounter.createParser; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class SOSLParserTest { | ||
@Test | ||
void testBasicQuery() { | ||
Pair<ApexParser, SyntaxErrorCounter> parserAndCounter = createParser("[Find 'something' RETURNING Account]"); | ||
ApexParser.SoslLiteralContext context = parserAndCounter.getKey().soslLiteral(); | ||
assertNotNull(context); | ||
assertEquals(0, parserAndCounter.getValue().getNumErrors()); | ||
} | ||
|
||
@Test | ||
void testEmbeddedQuote() { | ||
Pair<ApexParser, SyntaxErrorCounter> parserAndCounter = createParser("[Find 'some\\'thing' RETURNING Account]"); | ||
ApexParser.SoslLiteralContext context = parserAndCounter.getKey().soslLiteral(); | ||
assertNotNull(context); | ||
assertEquals(0, parserAndCounter.getValue().getNumErrors()); | ||
} | ||
|
||
@Test | ||
void testBracesFail() { | ||
Pair<ApexParser, SyntaxErrorCounter> parserAndCounter = createParser("[Find {something} RETURNING Account]"); | ||
ApexParser.SoslLiteralContext context = parserAndCounter.getKey().soslLiteral(); | ||
assertNotNull(context); | ||
assertEquals(1, parserAndCounter.getValue().getNumErrors()); | ||
} | ||
|
||
@Test | ||
void testBracesOnAltFormat() { | ||
Pair<ApexParser, SyntaxErrorCounter> parserAndCounter = createParser("[Find {something} RETURNING Account]"); | ||
ApexParser.SoslLiteralAltContext context = parserAndCounter.getKey().soslLiteralAlt(); | ||
assertNotNull(context); | ||
assertEquals(0, parserAndCounter.getValue().getNumErrors()); | ||
} | ||
|
||
@Test | ||
void testQuotesFailOnAltFormat() { | ||
Pair<ApexParser, SyntaxErrorCounter> parserAndCounter = createParser("[Find 'something' RETURNING Account]"); | ||
ApexParser.SoslLiteralAltContext context = parserAndCounter.getKey().soslLiteralAlt(); | ||
assertNotNull(context); | ||
assertEquals(1, parserAndCounter.getValue().getNumErrors()); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
jvm/src/test/java/com/nawforce/apexparser/SyntaxErrorCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.nawforce.apexparser; | ||
|
||
import javafx.util.Pair; | ||
import org.antlr.v4.runtime.*; | ||
|
||
public class SyntaxErrorCounter extends BaseErrorListener { | ||
private int numErrors = 0; | ||
|
||
@Override | ||
public void syntaxError( | ||
Recognizer<?, ?> recognizer, | ||
Object offendingSymbol, | ||
int line, | ||
int charPositionInLine, | ||
String msg, | ||
RecognitionException e) { | ||
this.numErrors += 1; | ||
} | ||
|
||
public int getNumErrors() { | ||
return this.numErrors; | ||
} | ||
|
||
public static Pair<ApexParser, SyntaxErrorCounter> createParser(String input) { | ||
ApexLexer lexer = new ApexLexer(new CaseInsensitiveInputStream(CharStreams.fromString(input))); | ||
CommonTokenStream tokens = new CommonTokenStream(lexer); | ||
ApexParser parser = new ApexParser(tokens); | ||
|
||
parser.removeErrorListeners(); | ||
SyntaxErrorCounter errorCounter = new SyntaxErrorCounter(); | ||
parser.addErrorListener(errorCounter); | ||
|
||
return new Pair<>(parser, errorCounter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
{ | ||
"transform": { | ||
"^.+\\.jsx?$": "babel-jest" | ||
}, | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?)$", | ||
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"] | ||
} | ||
"transform": { | ||
"^.+\\.jsx?$": "babel-jest" | ||
}, | ||
"testRegex": "/__tests__/.*Test.js$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json", | ||
"node" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
SoslLiteralAltContext, | ||
SoslLiteralContext | ||
} from "../ApexParser"; | ||
import { createParser } from "./SyntaxErrorCounter"; | ||
|
||
test('testBasicQuery', () => { | ||
const [parser, errorCounter] = createParser("test.sosl", "[Find 'something' RETURNING Account]") | ||
|
||
const context = parser.soslLiteral() | ||
|
||
expect(context).toBeInstanceOf(SoslLiteralContext) | ||
expect(errorCounter.getNumErrors()).toEqual(0) | ||
}) | ||
|
||
test('testEmbeddedQuote', () => { | ||
const [parser, errorCounter] = createParser("test.sosl", "[Find 'some\\'thing' RETURNING Account]") | ||
|
||
const context = parser.soslLiteral() | ||
|
||
expect(context).toBeInstanceOf(SoslLiteralContext) | ||
expect(errorCounter.getNumErrors()).toEqual(0) | ||
}) | ||
|
||
test('testBracesFail', () => { | ||
const [parser, errorCounter] = createParser("test.sosl", "[Find {something} RETURNING Account]") | ||
|
||
const context = parser.soslLiteral() | ||
|
||
expect(context).toBeInstanceOf(SoslLiteralContext) | ||
expect(errorCounter.getNumErrors()).toEqual(1) | ||
}) | ||
|
||
test('testBracesOnAltFormat', () => { | ||
const [parser, errorCounter] = createParser("test.sosl", "[Find {something} RETURNING Account]") | ||
|
||
const context = parser.soslLiteralAlt() | ||
|
||
expect(context).toBeInstanceOf(SoslLiteralAltContext) | ||
expect(errorCounter.getNumErrors()).toEqual(0) | ||
}) | ||
|
||
test('testQuotesFailOnAltFormat', () => { | ||
const [parser, errorCounter] = createParser("test.sosl", "[Find 'something' RETURNING Account]") | ||
|
||
const context = parser.soslLiteralAlt() | ||
|
||
expect(context).toBeInstanceOf(SoslLiteralAltContext) | ||
expect(errorCounter.getNumErrors()).toEqual(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ANTLRErrorListener, CommonTokenStream, RecognitionException, Recognizer, Token } from "antlr4ts"; | ||
import { ApexLexer } from "../ApexLexer"; | ||
import { ApexParser } from "../ApexParser"; | ||
import { CaseInsensitiveInputStream } from "../CaseInsensitiveInputStream"; | ||
|
||
export class SyntaxErrorCounter implements ANTLRErrorListener<Token> { | ||
numErrors = 0 | ||
|
||
syntaxError(recognizer: Recognizer<Token, any>, | ||
offendingSymbol: Token, line: number, charPositionInLine: number, msg: string, | ||
e: RecognitionException | undefined): any { | ||
this.numErrors += 1; | ||
} | ||
|
||
getNumErrors(): number { | ||
return this.numErrors; | ||
} | ||
} | ||
|
||
export function createParser(userData: string, input: string): [ApexParser, SyntaxErrorCounter] { | ||
const lexer = new ApexLexer(new CaseInsensitiveInputStream(userData, input)) | ||
const tokens = new CommonTokenStream(lexer); | ||
const parser = new ApexParser(tokens) | ||
|
||
parser.removeErrorListeners() | ||
const errorCounter = new SyntaxErrorCounter(); | ||
parser.addErrorListener(errorCounter); | ||
|
||
return [parser, errorCounter]; | ||
} |