From fcda10fb9320e1a44bde1ef2ef0aee064e81cad5 Mon Sep 17 00:00:00 2001 From: Bruno Medeiros Date: Fri, 16 Oct 2015 19:50:48 +0100 Subject: [PATCH] LANG: ICharSource changes. +LangPartitionScannerTest. --- .../CharacterScanner_ReaderHelper_Test.java | 11 +-- .../core/text/LangPartitionScannerTest.java | 76 +++++++++++++++++++ .../text/format/LangAutoEditStrategyTest.java | 2 +- .../lang/tests/CommonLexerRuleTest.java | 55 ++++++++++++++ .../tooling/ops/util/SourceLinesInfo.java | 2 +- .../tooling/parser/lexer/BasicCharSource.java | 5 +- .../parser/lexer/CharacterLexingRule.java | 13 +++- .../tooling/parser/lexer/LexingUtils.java | 55 +++++++++----- .../lang/utils/parse/IBasicCharSource.java | 26 +++++-- .../lang/utils/parse/ICharSource.java | 6 +- .../lang/utils/parse/ParseSource_Test.java | 49 +++++++----- .../lang/utils/parse/StringParseSource.java | 3 + .../melnorme/utilbox/core/Assert.java | 12 ++- .../melnorme/utilbox/misc/MiscUtil.java | 8 +- .../melnorme/utilbox/tests/CommonTest.java | 7 ++ .../melnorme/utilbox/tests/CommonTestExt.java | 6 -- .../utilbox/tests/CommonTestUtils.java | 9 ++- 17 files changed, 270 insertions(+), 75 deletions(-) create mode 100644 plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/LangPartitionScannerTest.java create mode 100644 plugin_tooling/src-lang/melnorme/lang/tests/CommonLexerRuleTest.java diff --git a/plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/CharacterScanner_ReaderHelper_Test.java b/plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/CharacterScanner_ReaderHelper_Test.java index c079db7d4..f17b4ee1e 100644 --- a/plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/CharacterScanner_ReaderHelper_Test.java +++ b/plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/CharacterScanner_ReaderHelper_Test.java @@ -16,13 +16,14 @@ import org.junit.Test; import melnorme.lang.utils.parse.ICharSource; +import melnorme.lang.utils.parse.ICharacterReader; import melnorme.lang.utils.parse.ParseSource_Test; public class CharacterScanner_ReaderHelper_Test { public static class ReaderHelper_ParseSource_Test extends ParseSource_Test { @Override - protected ICharSource createParseSource(String source) { + protected ICharacterReader createParseSource(String source) { BufferedRuleBasedScannerExt charScanner = new BufferedRuleBasedScannerExt(); charScanner.setRange(new Document(source), 0, source.length()); return new CharacterScanner_ReaderHelper(charScanner); @@ -51,7 +52,7 @@ protected void checkBufferedCount(ICharSource parseSource, int expected) { assertTrue(charScanner.lookaheadString(4, 0).equals("")); assertTrue(reader.lookahead() == 'a'); - reader.consume(); + reader.consume2(); assertTrue(charScanner.getOffset() == 1); assertTrue(charScanner.lookahead(0) == 'b'); assertTrue(charScanner.lookaheadString(0, 0).equals("")); @@ -59,8 +60,8 @@ protected void checkBufferedCount(ICharSource parseSource, int expected) { assertTrue(charScanner.lookaheadString(1, 2).equals("cd")); assertTrue(charScanner.lookaheadString(3, 0).equals("")); - reader.consume(); - reader.consume(); + reader.consume2(); + reader.consume2(); assertTrue(charScanner.getOffset() == 3); assertTrue(charScanner.lookahead(0) == 'd'); assertTrue(reader.lookahead() == 'd'); @@ -71,7 +72,7 @@ protected void checkBufferedCount(ICharSource parseSource, int expected) { reader = new CharacterScanner_ReaderHelper(charScanner); assertTrue(charScanner.lookahead(0) == 'a'); - assertTrue(reader.consume() == 'a'); + assertTrue(reader.consume2() == 'a'); assertTrue(charScanner.lookahead(0) == 'b'); assertTrue(charScanner.getOffset() == 1); assertTrue(reader.lookahead(1) == 'c'); diff --git a/plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/LangPartitionScannerTest.java b/plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/LangPartitionScannerTest.java new file mode 100644 index 000000000..c13302ec9 --- /dev/null +++ b/plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/LangPartitionScannerTest.java @@ -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]); + } + } + +} \ No newline at end of file diff --git a/plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/format/LangAutoEditStrategyTest.java b/plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/format/LangAutoEditStrategyTest.java index ef41e0384..cd71fba68 100644 --- a/plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/format/LangAutoEditStrategyTest.java +++ b/plugin_ide.core.tests/src-lang/melnorme/lang/ide/core/text/format/LangAutoEditStrategyTest.java @@ -77,7 +77,7 @@ public boolean parenthesesAsBlocks() { public static final String NEUTRAL_SRCX; static { - NEUTRAL_SRCX = MiscUtil.getClassResourceAsString(LangAutoEditStrategyTest.class, "sample_block_code"); + NEUTRAL_SRCX = MiscUtil.getClassResource(LangAutoEditStrategyTest.class, "sample_block_code"); } public static final String PENDING_WS1 = " "; diff --git a/plugin_tooling/src-lang/melnorme/lang/tests/CommonLexerRuleTest.java b/plugin_tooling/src-lang/melnorme/lang/tests/CommonLexerRuleTest.java new file mode 100644 index 000000000..b6293a728 --- /dev/null +++ b/plugin_tooling/src-lang/melnorme/lang/tests/CommonLexerRuleTest.java @@ -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); + } + } + +} \ No newline at end of file diff --git a/plugin_tooling/src-lang/melnorme/lang/tooling/ops/util/SourceLinesInfo.java b/plugin_tooling/src-lang/melnorme/lang/tooling/ops/util/SourceLinesInfo.java index e9778fbe0..5fdecd3d5 100644 --- a/plugin_tooling/src-lang/melnorme/lang/tooling/ops/util/SourceLinesInfo.java +++ b/plugin_tooling/src-lang/melnorme/lang/tooling/ops/util/SourceLinesInfo.java @@ -51,7 +51,7 @@ protected void consumeNewLine(StringParseSource parser) { return; } - parser.consume(); + parser.consume2(); } } diff --git a/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/BasicCharSource.java b/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/BasicCharSource.java index eaf252adb..b9a059463 100644 --- a/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/BasicCharSource.java +++ b/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/BasicCharSource.java @@ -23,11 +23,12 @@ public BasicCharSource() { } @Override - public final int consume() throws EXC { + public final char consume2() throws EXC { checkedHasNext = false; int next = lookahead(); + assertTrue(next >= 0); doConsume(); - return next; + return (char) next; } protected abstract void doConsume() throws EXC; diff --git a/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/CharacterLexingRule.java b/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/CharacterLexingRule.java index 7b136e2b5..5d674f22b 100644 --- a/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/CharacterLexingRule.java +++ b/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/CharacterLexingRule.java @@ -37,7 +37,13 @@ protected boolean consumeBody(ICharacterReader reader) { return reader.tryConsume('\'') || consumeCommonEscape(reader) || consumeUnicodeEscapeSequence(reader); }; - return consumeAnyExceptNullOr(reader, '\''); + if(reader.lookaheadIsEOF() || reader.lookahead() == '\'') { + return false; + } + if(reader.lookahead() == 0) { + return false; // Should the null character really not be allowed? + } + return reader.consumeAny(); } protected boolean consumeEnd(ICharacterReader reader) { @@ -75,10 +81,11 @@ protected boolean consumeUnicodeEscapeSequence(ICharacterReader reader) { int la = reader.lookahead(); // This is not accurate to any spec, but is good enough. - if(!(la == '{' || la == '}' || isHexDigit(la))) { + if(la == '{' || la == '}' || isHexDigit(la)) { + reader.consume2(); + } else { break; } - reader.consume(); } return true; diff --git a/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/LexingUtils.java b/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/LexingUtils.java index f6bbc2cb2..32e7c560d 100644 --- a/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/LexingUtils.java +++ b/plugin_tooling/src-lang/melnorme/lang/tooling/parser/lexer/LexingUtils.java @@ -15,23 +15,20 @@ public class LexingUtils { - public static boolean consumeAnyExceptNullOr(IBasicCharSource reader, int character) - throws E { - int ch = reader.lookahead(); - if(ch != character && ch != 0) { - reader.consume(); - return true; - } - return false; + /** + * Consume until given delimiter char is found (also included). + */ + public static String consumeUntilDelimiter(ICharSource reader, + char delimiter) throws E { + return consumeUntilDelimiter(reader, delimiter, delimiter); } - - /** - * Consume a string delimited by give delimiter char, - * with given escapeChar acting a possible escape (use -1 for no escapeChar) + /** + * Consume until given delimiter char is found (also included). + * Given escapeChar can escape itself, and delimiter */ - public static String consumeDelimitedString(IBasicCharSource reader, - int delimiter, int escapeChar) throws E { + public static String consumeUntilDelimiter(ICharSource reader, + char delimiter, char escapeChar) throws E { StringBuilder sb = new StringBuilder(); while(reader.hasCharAhead()) { @@ -42,11 +39,11 @@ public static String consumeDelimitedString(IBasicCharSour break; } else if(consumedChar == escapeChar && reader.hasCharAhead()) { - int lookahead = reader.lookahead(); - if(lookahead == delimiter || lookahead == escapeChar) { - - char escapedChar = reader.nextChar(); - sb.append(escapedChar); + + char secondChar = reader.lookaheadChar(); + if(secondChar == delimiter || secondChar == escapeChar) { + reader.nextChar(); + sb.append(secondChar); continue; } } @@ -57,12 +54,30 @@ else if(consumedChar == escapeChar && reader.hasCharAhead()) { return sb.toString(); } + + public static void advanceDelimitedString(ICharSource reader, + char delimiter, char escapeChar) throws E { + + while(reader.hasCharAhead()) { + + char consumedChar = reader.nextChar(); + + if(consumedChar == delimiter) { + break; + } + else if(consumedChar == escapeChar) { + reader.consumeAny(); // consumed escaped char + } + + } + } + /* ----------------- whitespace ----------------- */ public static int skipWhitespace(IBasicCharSource reader) throws E { int count = 0; while(Character.isWhitespace(reader.lookahead())) { - reader.consume(); + reader.consume2(); count++; } return count; diff --git a/plugin_tooling/src-lang/melnorme/lang/utils/parse/IBasicCharSource.java b/plugin_tooling/src-lang/melnorme/lang/utils/parse/IBasicCharSource.java index 9314c737c..4536d23f4 100644 --- a/plugin_tooling/src-lang/melnorme/lang/utils/parse/IBasicCharSource.java +++ b/plugin_tooling/src-lang/melnorme/lang/utils/parse/IBasicCharSource.java @@ -18,7 +18,15 @@ public interface IBasicCharSource { int lookahead() throws EXC; /** @return the next character in the stream, advancing the stream. */ - int consume() throws EXC; + char consume2() throws EXC; + + default char nextChar() throws EXC { + return consume2(); + } + + default boolean lookaheadIsEOF() throws EXC { + return !hasCharAhead(); + } default boolean hasCharAhead() throws EXC { return lookahead() != -1; @@ -30,18 +38,20 @@ default char lookaheadChar() throws EXC { return (char) la; } - default char nextChar() throws EXC { - int ch = consume(); - assertTrue(ch != -1); - return (char) ch; - } - /* ----------------- ----------------- */ /** Read next character if it is equal to given character. @return true if a character was read. */ default boolean tryConsume(char character) throws EXC { if(lookahead() == character) { - consume(); + consume2(); + return true; + } + return false; + } + + default boolean consumeAny() throws EXC { + if(hasCharAhead()) { + consume2(); return true; } return false; diff --git a/plugin_tooling/src-lang/melnorme/lang/utils/parse/ICharSource.java b/plugin_tooling/src-lang/melnorme/lang/utils/parse/ICharSource.java index c531e94c6..9b93c0f15 100644 --- a/plugin_tooling/src-lang/melnorme/lang/utils/parse/ICharSource.java +++ b/plugin_tooling/src-lang/melnorme/lang/utils/parse/ICharSource.java @@ -36,13 +36,9 @@ default int lookahead() throws EXC { /* ----------------- Some common helpers ----------------- */ - default boolean lookaheadIsEOF() throws EXC { - return !hasCharAhead(); - } - default void consume(int amount) throws EXC { while(amount-- > 0) { - consume(); + consume2(); } } diff --git a/plugin_tooling/src-lang/melnorme/lang/utils/parse/ParseSource_Test.java b/plugin_tooling/src-lang/melnorme/lang/utils/parse/ParseSource_Test.java index 85cd7df0a..a92c981ec 100644 --- a/plugin_tooling/src-lang/melnorme/lang/utils/parse/ParseSource_Test.java +++ b/plugin_tooling/src-lang/melnorme/lang/utils/parse/ParseSource_Test.java @@ -12,16 +12,18 @@ import static melnorme.utilbox.core.Assert.AssertNamespace.assertTrue; +import org.junit.Test; + import melnorme.lang.tests.CommonToolingTest; +import melnorme.lang.tooling.parser.lexer.CharacterReader_SubReader; import melnorme.lang.tooling.parser.lexer.LexingUtils; - -import org.junit.Test; +import melnorme.utilbox.core.Assert.AssertFailedException; public abstract class ParseSource_Test extends CommonToolingTest { protected final String TEST_SOURCE = "abcdef"; - protected ICharSource parseSource; + protected ICharacterReader parseSource; protected String source; protected int sourceIx; protected int lookahead; @@ -32,7 +34,7 @@ protected void init(String source) { this.sourceIx = 0; } - protected abstract ICharSource createParseSource(String source); + protected abstract ICharacterReader createParseSource(String source); @Test public void test() throws Exception { test$(); } @@ -90,9 +92,9 @@ protected void testCharSource() throws Exception { // Test consume with buffered checkBufferedCount(parseSource, 2); - assertTrue(parseSource.consume() == lookahead); sourceIx++; + assertTrue(parseSource.consume2() == lookahead); sourceIx++; checkBufferedCount(parseSource, 1); - assertTrue(parseSource.consume() == source.charAt(sourceIx)); sourceIx++; + assertTrue(parseSource.consume2() == source.charAt(sourceIx)); sourceIx++; checkBufferedCount(parseSource, 0); @@ -100,24 +102,24 @@ protected void testCharSource() throws Exception { lookahead = testLookahead(source.charAt(sourceIx)); // Test consume with buffered checkBufferedCount(parseSource, 1); - assertTrue(lookahead == parseSource.consume()); sourceIx++; + assertTrue(lookahead == parseSource.consume2()); sourceIx++; assertTrue(lookahead == 'c'); checkBufferedCount(parseSource, 0); - assertTrue(parseSource.consume() == source.charAt(sourceIx)); + assertTrue(parseSource.consume2() == source.charAt(sourceIx)); sourceIx++; while(sourceIx < source.length()) { int ch = testLookahead(source.charAt(sourceIx)); - assertTrue(parseSource.consume() == ch); + assertTrue(parseSource.consume2() == ch); sourceIx++; } // EOF testLookahead(-1); - assertTrue(parseSource.consume() == -1); + verifyThrows(() -> parseSource.consume2(), AssertFailedException.class); } protected void checkBufferedCount(ICharSource parseSource, int expected) { @@ -140,14 +142,27 @@ public static int testLookAhead(ICharSource parseSource, int expected) throws public void test_consumeDelimited$() throws Exception { init("blah"); - assertEquals(LexingUtils.consumeDelimitedString(parseSource, '|', '#'), "blah"); + testConsumeDelimitedString(parseSource, '|', '#', "blah"); init("one|two|three##|four#|xxx|###|five"); - assertEquals(LexingUtils.consumeDelimitedString(parseSource, '|', '#'), "one"); - assertEquals(LexingUtils.consumeDelimitedString(parseSource, '|', '#'), "two"); - assertEquals(LexingUtils.consumeDelimitedString(parseSource, '|', '#'), "three#"); - assertEquals(LexingUtils.consumeDelimitedString(parseSource, '|', '#'), "four|xxx"); - assertEquals(LexingUtils.consumeDelimitedString(parseSource, '|', '#'), "#|five"); + testConsumeDelimitedString(parseSource, '|', '#', "one"); + testConsumeDelimitedString(parseSource, '|', '#', "two"); + testConsumeDelimitedString(parseSource, '|', '#', "three#"); + testConsumeDelimitedString(parseSource, '|', '#', "four|xxx"); + testConsumeDelimitedString(parseSource, '|', '#', "#|five"); + } + + protected void testConsumeDelimitedString(ICharacterReader parseSource, char delimiter, char escapeChar, + String expected) throws Exception { + CharacterReader_SubReader subReader = new CharacterReader_SubReader(parseSource); + LexingUtils.consumeUntilDelimiter(subReader, delimiter, escapeChar); + + OffsetBasedCharacterReader parseSource_ = (OffsetBasedCharacterReader) parseSource; + int originalReadOffset = parseSource_.readOffset; + + assertEquals(LexingUtils.consumeUntilDelimiter(parseSource, delimiter, escapeChar), expected); + + assertTrue(parseSource_.readOffset - originalReadOffset == subReader.readOffset); } @Test @@ -168,7 +183,7 @@ public static int testLookAhead(ICharSource parseSource, int expected) throws public static class StringParseSource_Test extends ParseSource_Test { @Override - protected ICharSource createParseSource(String source) { + protected ICharacterReader createParseSource(String source) { return new StringParseSource(source); } diff --git a/plugin_tooling/src-lang/melnorme/lang/utils/parse/StringParseSource.java b/plugin_tooling/src-lang/melnorme/lang/utils/parse/StringParseSource.java index 64fbf0c6d..5f1a72005 100644 --- a/plugin_tooling/src-lang/melnorme/lang/utils/parse/StringParseSource.java +++ b/plugin_tooling/src-lang/melnorme/lang/utils/parse/StringParseSource.java @@ -10,6 +10,8 @@ *******************************************************************************/ package melnorme.lang.utils.parse; +import static melnorme.utilbox.core.Assert.AssertNamespace.assertTrue; + public class StringParseSource extends OffsetBasedCharacterReader implements ICharacterReader { protected final String source; @@ -49,6 +51,7 @@ public int bufferedCharCount() { @Override public void consume(int amount) throws RuntimeException { readOffset += amount; + assertTrue(readOffset <= source.length()); } /* ----------------- ----------------- */ diff --git a/plugin_tooling/src-util/melnorme/utilbox/core/Assert.java b/plugin_tooling/src-util/melnorme/utilbox/core/Assert.java index 330d21b98..889879c98 100644 --- a/plugin_tooling/src-util/melnorme/utilbox/core/Assert.java +++ b/plugin_tooling/src-util/melnorme/utilbox/core/Assert.java @@ -27,6 +27,8 @@ public class Assert { protected static final AssertHandler assertHandler; // Poor mans dependency injection + public static boolean assertionFailureExpected = false; // Only tests should modify this field + static { String assertHandlerStr = System.getProperty(Assert.class.getName() + ".handler"); if(assertHandlerStr == null) { @@ -66,7 +68,7 @@ protected void handleAssert(String message) { * Clients should not expect this to be thrown. */ @SuppressWarnings("serial") - protected static class AssertFailedException extends RuntimeException { + public static class AssertFailedException extends RuntimeException { public AssertFailedException(String message) { super(message); @@ -84,7 +86,13 @@ public String toString() { protected static void checkAssertion(boolean condition, String message) { if(assertHandler != null && condition == false) { - assertHandler.handleAssert(message); // USEFUL TIP: place Breakpoint here + if(assertionFailureExpected) { + // This does same thing as else branch, but code is separated + // so that this case won't trigger the IDE breakpoint + assertHandler.handleAssert(message); + } else { + assertHandler.handleAssert(message); // USEFUL TIP: place IDE breakpoint here + } } } diff --git a/plugin_tooling/src-util/melnorme/utilbox/misc/MiscUtil.java b/plugin_tooling/src-util/melnorme/utilbox/misc/MiscUtil.java index 55ca7d4f8..7783eb265 100644 --- a/plugin_tooling/src-util/melnorme/utilbox/misc/MiscUtil.java +++ b/plugin_tooling/src-util/melnorme/utilbox/misc/MiscUtil.java @@ -141,17 +141,17 @@ public static boolean isUncheckedException(Throwable throwable) { return throwable instanceof RuntimeException || throwable instanceof Error; } - public static String getClassResourceAsString(Class klass, String resourceName) { - return getClassResourceAsString(klass, resourceName, StringUtil.UTF8); + public static String getClassResource(Class klass, String resourceName) { + return getClassResource(klass, resourceName, StringUtil.UTF8); } - public static String getClassResourceAsString(Class klass, String resourceName, Charset charset) { + public static String getClassResource(Class klass, String resourceName, Charset charset) { try { InputStream resourceStream = klass.getResourceAsStream(resourceName); assertNotNull(resourceStream); return readAllBytesFromStream(resourceStream).toString(charset); } catch (IOException e) { - throw melnorme.utilbox.core.ExceptionAdapter.unchecked(e); + throw assertFail(); } } diff --git a/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTest.java b/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTest.java index 00ce226e4..1e973950a 100644 --- a/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTest.java +++ b/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTest.java @@ -18,6 +18,7 @@ import org.junit.Before; +import melnorme.utilbox.misc.MiscUtil; import melnorme.utilbox.misc.SimpleLogger; /** @@ -54,4 +55,10 @@ public void printSeparator() throws Exception { } } + /* ----------------- ----------------- */ + + public String getClassResource(String resourceName) { + return MiscUtil.getClassResource(getClass(), resourceName); + } + } \ No newline at end of file diff --git a/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTestExt.java b/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTestExt.java index 9ac96212c..b07bfaf8b 100644 --- a/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTestExt.java +++ b/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTestExt.java @@ -112,10 +112,4 @@ public static String findMatch(String input, String regex, int resultGroup) { return matcher.group(resultGroup); } - /* ----------------- ----------------- */ - - protected String getClassResourceAsString(String resourceName) { - return getClassResourceAsString(getClass(), resourceName); - } - } \ No newline at end of file diff --git a/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTestUtils.java b/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTestUtils.java index 9fcbf5793..71d21e8bd 100644 --- a/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTestUtils.java +++ b/plugin_tooling/src-util/melnorme/utilbox/tests/CommonTestUtils.java @@ -29,6 +29,7 @@ import melnorme.utilbox.collections.HashSet2; import melnorme.utilbox.collections.Indexable; import melnorme.utilbox.core.Assert; +import melnorme.utilbox.core.Assert.AssertFailedException; import melnorme.utilbox.core.CoreUtil; import melnorme.utilbox.core.fntypes.ThrowingRunnable; import melnorme.utilbox.misc.ArrayUtil; @@ -141,6 +142,10 @@ public static void verifyThrows(ThrowingRunnable runn public static void verifyThrows(ThrowingRunnable runnable, Class klass, String exceptionString) { try { + if(klass != null && AssertFailedException.class.isAssignableFrom(klass)) { + Assert.assertionFailureExpected = true; + } + runnable.run(); assertFail(); } catch(Throwable e) { @@ -150,6 +155,8 @@ public static void verifyThrows(ThrowingRunnable runn if(exceptionString != null) { assertTrue(e.toString().contains(exceptionString)); } + } finally { + Assert.assertionFailureExpected = false; } } @@ -285,7 +292,7 @@ public static void appendStringToFile(File file, String string) { } public static String getClassResourceAsString(Class klass, String resourceName) { - return MiscUtil.getClassResourceAsString(klass, resourceName); + return MiscUtil.getClassResource(klass, resourceName); } } \ No newline at end of file