This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LANG: ICharSource changes. +LangPartitionScannerTest.
- Loading branch information
1 parent
5212036
commit fcda10f
Showing
17 changed files
with
270 additions
and
75 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
76 changes: 76 additions & 0 deletions
76
plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/LangPartitionScannerTest.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,76 @@ | ||
package melnorme.lang.ide.core.text; | ||
/******************************************************************************* | ||
* Copyright (c) 2015 Bruno Medeiros and other Contributors. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Bruno Medeiros - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
|
||
|
||
import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull; | ||
import static melnorme.utilbox.core.Assert.AssertNamespace.assertTrue; | ||
import static melnorme.utilbox.core.CoreUtil.downCast; | ||
|
||
import org.eclipse.jface.text.BadPositionCategoryException; | ||
import org.eclipse.jface.text.Document; | ||
import org.eclipse.jface.text.Position; | ||
import org.eclipse.jface.text.TypedPosition; | ||
import org.eclipse.jface.text.rules.FastPartitioner; | ||
|
||
import melnorme.utilbox.misc.ArrayUtil; | ||
import melnorme.utilbox.tests.CommonTest; | ||
|
||
public class LangPartitionScannerTest extends CommonTest { | ||
|
||
protected static final String NL = "\n"; | ||
protected Document document; | ||
protected FastPartitioner fp; | ||
protected boolean recreateDocSetup = true; | ||
|
||
public LangPartitionScannerTest() { | ||
super(); | ||
} | ||
|
||
|
||
protected void testPartitions(String source, Enum<?>... expectedPositions) throws BadPositionCategoryException { | ||
testPartitions(source, ArrayUtil.map(expectedPositions, | ||
(enm) -> enm.toString(), String.class)); | ||
} | ||
|
||
protected void testPartitions(String source, String... expectedPositions) throws BadPositionCategoryException { | ||
Position[] positions = calculatePartitions(source); | ||
checkPositions(positions, expectedPositions); | ||
} | ||
|
||
protected Position[] calculatePartitions(String docContents) throws BadPositionCategoryException { | ||
setupDocument(docContents); | ||
Position[] positions = getPartitionPositions(); | ||
return positions; | ||
} | ||
protected void setupDocument(String docContents) { | ||
if(recreateDocSetup){ | ||
document = new Document(docContents); | ||
fp = LangDocumentPartitionerSetup.getInstance().setupDocument(document); | ||
} else { | ||
document.set(docContents); | ||
assertNotNull(fp); | ||
} | ||
} | ||
protected Position[] getPartitionPositions() throws BadPositionCategoryException { | ||
return document.getPositions(fp.getManagingPositionCategories()[0]); | ||
} | ||
|
||
protected void checkPositions(Position[] positions, String[] expectedPositions) { | ||
assertTrue(positions.length == expectedPositions.length); | ||
for (int i = 0; i < positions.length; i++) { | ||
TypedPosition position = downCast(positions[i], TypedPosition.class); | ||
assertTrue(position.getType() == expectedPositions[i]); | ||
} | ||
} | ||
|
||
} |
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
55 changes: 55 additions & 0 deletions
55
plugin_tooling/src-lang/melnorme/lang/tests/CommonLexerRuleTest.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,55 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2015 Bruno Medeiros and other Contributors. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Bruno Medeiros - initial API and implementation | ||
*******************************************************************************/ | ||
package melnorme.lang.tests; | ||
|
||
|
||
import melnorme.lang.tooling.parser.lexer.ILexingRule; | ||
import melnorme.lang.utils.parse.StringParseSource; | ||
|
||
public abstract class CommonLexerRuleTest extends CommonToolingTest { | ||
|
||
public CommonLexerRuleTest() { | ||
super(); | ||
} | ||
|
||
public void testRule(String source) { | ||
testRule(source, true); | ||
} | ||
|
||
public void testRule(String source, boolean terminatedSuccessfuly) { | ||
testRule(source, source.length()); | ||
if(terminatedSuccessfuly) { | ||
// if rule terminated successfully, adding a suffix will make no diference to token size | ||
testRule(source + getRuleNeutralSuffix(), source.length()); | ||
} | ||
} | ||
|
||
protected String getRuleNeutralSuffix() { | ||
return "xxxx"; | ||
} | ||
|
||
public void testRule(String source, int expectedTokenLength) { | ||
testRule(createLexingRule(), source, expectedTokenLength); | ||
} | ||
|
||
protected abstract ILexingRule createLexingRule(); | ||
|
||
public static void testRule(ILexingRule lexRule, String source, int expectedTokenLength) { | ||
StringParseSource reader = new StringParseSource(source); | ||
boolean isMatch = lexRule.tryMatch(reader); | ||
|
||
assertEquals(isMatch, expectedTokenLength > 0); | ||
if(isMatch) { | ||
assertEquals(reader.getReadOffset(), expectedTokenLength); | ||
} | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.