Skip to content

Commit

Permalink
Refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Zarnekow authored and Sebastian Zarnekow committed Jul 12, 2017
1 parent afc166e commit 1e5d59a
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.Iterator;
Expand Down Expand Up @@ -336,10 +335,10 @@ public void initializeTokenTypes(ContentAssistTokenTypeMapper mapper, N4JSGramma
}

@Inject
ReflectExtensions reflector;
private final ReflectExtensions reflector = new ReflectExtensions();

@SuppressWarnings("unchecked")
private <T> T reflective(String methodName, Object... args) {
private <T> T reflect(String methodName, Object... args) {
try {
return (T) reflector.invoke(this, methodName, args);
} catch (SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException
Expand All @@ -354,16 +353,14 @@ public Collection<FollowElement> getFollowElements(FollowElement element) {
if (element.getLookAhead() <= 1)
throw new IllegalArgumentException("lookahead may not be less than or equal to 1");
Collection<FollowElement> result = new ArrayList<>();
for (AbstractElement elementToParse : this.<Collection<AbstractElement>> reflective("getElementsToParse",
for (AbstractElement elementToParse : this.<Collection<AbstractElement>> reflect("getElementsToParse",
element)) {
// fix is here
elementToParse = unwrapSingleElementGroups(elementToParse);
// done
String ruleName = getRuleName(elementToParse);
String[][] allRuleNames = reflective("getRequiredRuleNames", ruleName, element.getParamStack(),
elementToParse);
String[][] allRuleNames = reflect("getRequiredRuleNames", ruleName, element.getParamStack(), elementToParse);
for (String[] ruleNames : allRuleNames) {
System.out.println(Arrays.deepToString(ruleNames));
for (int i = 0; i < ruleNames.length; i++) {
AbstractInternalContentAssistParser parser = createParser();
parser.setUnorderedGroupHelper(getUnorderedGroupHelper().get());
Expand Down Expand Up @@ -449,7 +446,7 @@ public UnorderedGroupState snapShot(UnorderedGroup... groups) {

});
}
Collection<FollowElement> elements = reflective("getFollowElements", parser, elementToParse,
Collection<FollowElement> elements = reflect("getFollowElements", parser, elementToParse,
ruleNames, i);
result.addAll(elements);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
package org.eclipse.n4js.tests.contentassist

import com.google.inject.Inject
import java.util.Collection
import java.util.Set
import org.eclipse.n4js.N4JSInjectorProvider
import org.eclipse.n4js.n4JS.Script
import org.eclipse.n4js.services.N4JSGrammarAccess
import java.util.Collection
import java.util.Set
import org.eclipse.xtext.AbstractElement
import org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement
import org.eclipse.xtext.junit4.InjectWith
Expand All @@ -37,16 +37,20 @@ abstract class AbstractContentAssistParserTest extends Assert {
@Inject protected N4JSGrammarAccess grammarAccess
@Inject GrammarPrettyPrinter prettyPrinter

def Collection<FollowElement> getFollowElements(INode node, int start, int end)
def Collection<FollowElement> getFollowElements(INode node, int start, int end, boolean strict)

def Collection<FollowElement> getFollowElements(FollowElement prev)

def Collection<FollowElement> getFollowElements(INode node, String after) {
return node.getFollowElements(0, after.length)
return node.getFollowElements(0, after.length, true)
}

def Collection<FollowElement> getFollowElements(INode node) {
return node.getFollowElements(0, node.totalLength)
return node.getFollowElements(0, node.totalLength, true)
}

def Collection<FollowElement> getFollowElements(INode node, boolean strict) {
return node.getFollowElements(0, node.totalLength, strict)
}

def String prettyPrint(Set<AbstractElement> elements) {
Expand All @@ -56,7 +60,7 @@ abstract class AbstractContentAssistParserTest extends Assert {
@Test
def void testEmptyInput() {
val node = ''.toNode
val followElements = getFollowElements(node, 0, 0)
val followElements = getFollowElements(node, 0, 0, true)
val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(2, grammarElements.size)
assertTrue(grammarElements.contains(grammarAccess.scriptAccess.annotationsAssignment_1))
Expand Down Expand Up @@ -195,7 +199,7 @@ abstract class AbstractContentAssistParserTest extends Assert {
val followElement = getFollowElements(node).head
followElement.assertSubsequentFollowElementsInMemberExpression
}

@Test
def void testAfterDot_IDEBUG481_02() {
val node = '''
Expand Down Expand Up @@ -257,6 +261,183 @@ abstract class AbstractContentAssistParserTest extends Assert {
val followElement = getFollowElements(node).head
followElement.assertSubsequentFollowElementsInMemberExpression
}

@Test
def void testNoModifiers_GH39_nonStrict() {
val node = '''
class A {}
function* foo(req: A) {
yield 5; // removing 5 fixes the problem
req.'''.toNode
val followElements = getFollowElements(node, false)
val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 3, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getFunctionBodyAccess().getBodyAssignment_1_0))
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getRootStatementAccess().getAlternatives))
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getScriptAccess().getScriptElementsAssignment_2))
}

@Test
def void testNoModifiers_GH39_strict() {
val node = '''
class A {}
function* foo(req: A) {
yield 5; // removing 5 fixes the problem
req.'''.toNode
val followElements = getFollowElements(node, true)
val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 2, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getFunctionBodyAccess().getBodyAssignment_1_0))
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getRootStatementAccess().getAlternatives))
}

@Test
def void testPublicModifier_GH39() {
val node = '''
class A {}
public function* foo(req: A) {
yield 5; // removing 5 fixes the problem
req.'''.toNode
val followElements = getFollowElements(node, false)
val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 3, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getFunctionBodyAccess().getBodyAssignment_1_0))
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getRootStatementAccess().getAlternatives))
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getScriptAccess().getScriptElementsAssignment_2))
}

@Test
def void testExportModifier_GH39() {
val node = '''
class A {}
public function* foo(req: A) {
yield 5; // removing 5 fixes the problem
req.'''.toNode
val followElements = getFollowElements(node, false)
val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 3, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getFunctionBodyAccess().getBodyAssignment_1_0))
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getScriptAccess().getScriptElementsAssignment_2))
}

@Test
def void testExportPublicModifier_GH39_nonStrict() {
val node = '''
class A {}
export public function* foo(req: A) {
yield 5; // removing 5 fixes the problem
req.'''.toNode
val followElements = getFollowElements(node, false)
val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 2, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getFunctionBodyAccess().getBodyAssignment_1_0))
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getScriptAccess().getScriptElementsAssignment_2))
}

@Test
def void testExportPublicModifier_GH39_strict() {
val node = '''
class A {}
export public function* foo(req: A) {
yield 5; // removing 5 fixes the problem
req.'''.toNode
val followElements = getFollowElements(node, true)
val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 1, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getFunctionBodyAccess().getBodyAssignment_1_0))
}

@Test
def void testExportPublicModifier_GH39_asi() {
val node = '''
class A {}
export public function* foo(req: A) {
yield 5
req.'''.toNode
val followElements = getFollowElements(node, true)
val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 1, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getFunctionBodyAccess().getBodyAssignment_1_0))
}

@Test
def void testNoModifiersFollowUp_01() {
val node = '''
class A {}
function* foo(req: A) {
yield 5; // removing 5 fixes the problem
req.'''.toNode
val followElement = getFollowElements(node, true).filter[grammarElement == grammarAccess.getFunctionBodyAccess().getBodyAssignment_1_0].head
assertNotNull(followElement)
val followElements = getFollowElements(followElement);

val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 1, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.rootStatementAccess.alternatives))
}

@Test
def void testNoModifiersFollowUp_02() {
val node = '''
class A {}
function* foo(req: A) {
yield 5; // removing 5 fixes the problem
req.'''.toNode
val followElement = getFollowElements(node, true).filter[grammarElement == grammarAccess.getRootStatementAccess().getAlternatives()].head
assertNotNull(followElement)
val followElements = getFollowElements(followElement);

val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 2, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getFunctionBodyAccess().getBodyAssignment_1_0))
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.rootStatementAccess.alternatives))
}

@Test
def void testNoModifiersFollowUp_03() {
val node = '''
class A {}
function* foo(req: A) {
yield 5; // removing 5 fixes the problem
req.'''.toNode
val followElement = getFollowElements(node, false).filter[grammarElement == grammarAccess.getScriptAccess().getScriptElementsAssignment_2()].head
assertNotNull(followElement)
val followElements = getFollowElements(followElement);

val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 0, grammarElements.size)
}

@Test
def void testExportPublicFollowUp_01() {
val node = '''
class A {}
export public function* foo(req: A) {
yield 5; // removing 5 fixes the problem
req.'''.toNode
val followElement = getFollowElements(node, false).filter[grammarElement == grammarAccess.getFunctionBodyAccess().getBodyAssignment_1_0].head
assertNotNull(followElement)
val followElements = getFollowElements(followElement);

val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 1, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.getRootStatementAccess().getAlternatives()))
}

@Test
def void testExportPublicFollowUp_02() {
val node = '''
class A {}
export public function* foo(req: A) {
yield 5; // removing 5 fixes the problem
req.'''.toNode
val followElement = getFollowElements(node, false).filter[grammarElement == grammarAccess.getScriptAccess().getScriptElementsAssignment_2()].head
assertNotNull(followElement)
val followElements = getFollowElements(followElement);

val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 0, grammarElements.size)
}

def assertSubsequentFollowElementsInMemberExpression(FollowElement followElement) {
val followElements = followElement.followElements
Expand All @@ -273,7 +454,7 @@ abstract class AbstractContentAssistParserTest extends Assert {
@Test
def void testAfterDot_nl() {
val node = 'a.\n\n'.toNode
val followElements = getFollowElements(node, 0, 3)
val followElements = getFollowElements(node, 0, 3, true)
val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 1, grammarElements.size)
assertEquals(4, followElements.head.lookAhead)
Expand All @@ -283,7 +464,7 @@ abstract class AbstractContentAssistParserTest extends Assert {
@Test
def void testAfterDot_nl_NextBatchOfFollowElements() {
val node = 'a.\n\n'.toNode
val followElement = getFollowElements(node, 0, 3).head
val followElement = getFollowElements(node, 0, 3, true).head
followElement.assertSubsequentFollowElementsInMemberExpression
}

Expand Down Expand Up @@ -327,7 +508,7 @@ abstract class AbstractContentAssistParserTest extends Assert {
@Test
def void testBinaryOp_03() {
val node = '1\n+2'.toNode
val followElements = getFollowElements(node, 0, 2)
val followElements = getFollowElements(node, 0, 2, true)
val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 18, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.scriptAccess.scriptElementsAssignment_2))
Expand All @@ -337,7 +518,7 @@ abstract class AbstractContentAssistParserTest extends Assert {
@Test
def void testBinaryOp_04() {
val node = '1/*\n*/+2'.toNode
val followElements = getFollowElements(node, 0, 6)
val followElements = getFollowElements(node, 0, 6, true)
val grammarElements = followElements.map [ grammarElement ].toSet
assertEquals(grammarElements.prettyPrint, 18, grammarElements.size)
assertTrue(grammarElements.prettyPrint, grammarElements.contains(grammarAccess.scriptAccess.scriptElementsAssignment_2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import com.google.inject.Provider
import org.eclipse.n4js.N4JSInjectorProvider
import org.eclipse.n4js.ui.contentassist.ContentAssistTokenTypeMapper
import org.eclipse.n4js.ui.contentassist.CustomN4JSParser
import org.eclipse.n4js.ui.contentassist.PatchedRequiredRuleNameComputer
import org.eclipse.n4js.ui.contentassist.TokenSourceFactory
import org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement
import org.eclipse.xtext.ide.editor.contentassist.antlr.RequiredRuleNameComputer
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.nodemodel.INode
Expand All @@ -34,7 +34,7 @@ class CustomParserTest extends AbstractContentAssistParserTest implements Provid
@Inject IUnorderedGroupHelper unorderedGroupHelper
@Inject TokenSourceFactory tokenSourceFactory
@Inject ContentAssistTokenTypeMapper tokenMapper
@Inject RequiredRuleNameComputer ruleNameComputer
@Inject PatchedRequiredRuleNameComputer ruleNameComputer

@Inject extension ReflectExtensions

Expand All @@ -48,12 +48,17 @@ class CustomParserTest extends AbstractContentAssistParserTest implements Provid
]
}

override getFollowElements(INode node, int start, int end) {
parser.getFollowElements(node, start, end, true)
override getFollowElements(INode node, int start, int end, boolean strict) {
parser.getFollowElements(node, start, end, strict)
}

override getFollowElements(FollowElement prev) {
parser.getFollowElements(prev)
if (prev.lookAhead == 1) {
return #{};
} else {
return parser.getFollowElements(prev)
}

}

override get() {
Expand Down

0 comments on commit 1e5d59a

Please sign in to comment.