diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/AbstractPairMatcherTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/AbstractPairMatcherTest.java index 1d6e394..ab2367b 100644 --- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/AbstractPairMatcherTest.java +++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/AbstractPairMatcherTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2008 IBM Corporation and others. + * Copyright (c) 2006, 2012 IBM Corporation and others. * 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 @@ -26,6 +26,7 @@ import org.eclipse.jface.text.rules.RuleBasedPartitionScanner; import org.eclipse.jface.text.rules.SingleLineRule; import org.eclipse.jface.text.rules.Token; +import org.eclipse.jface.text.source.DefaultCharacterPairMatcher; import org.eclipse.jface.text.source.ICharacterPairMatcher; /** @@ -35,13 +36,27 @@ */ public abstract class AbstractPairMatcherTest extends TestCase { + private boolean fCaretInsideMatchedPair; + + private String fChars; + + public void setCaretInsideMatchedPair(boolean caretInsideMatchedPair) { + fCaretInsideMatchedPair= caretInsideMatchedPair; + } + + public void setChars(String chars) { + fChars = chars; + } + /** * Constructs a new character pair matcher. * * @param chars the characters to match * @return the character pair matcher */ - protected abstract ICharacterPairMatcher createMatcher(final String chars); + protected ICharacterPairMatcher createMatcher(String chars) { + return new DefaultCharacterPairMatcher(chars.toCharArray(), getDocumentPartitioning(), fCaretInsideMatchedPair); + } /** * Returns the partitioning treated by the matcher. @@ -59,14 +74,6 @@ } /* --- T e s t s --- */ - - /** Tests that the test case reader works */ - public void testTestCaseReader() { - performReaderTest("#( )%", 3, 0, "( )"); - performReaderTest("%( )#", 0, 3, "( )"); - performReaderTest("( )%", 3, -1, "( )"); - performReaderTest("#%", 0, 0, ""); - } /** * Very simple checks. @@ -105,12 +112,10 @@ */ public void testCloseMatches() throws BadLocationException { final ICharacterPairMatcher matcher= createMatcher("()[]{}"); - performMatch(matcher, "#()%"); performMatch(matcher, "(%)#"); performMatch(matcher, "#(())%"); performMatch(matcher, "(%())#"); performMatch(matcher, "((%)#)"); - performMatch(matcher, "(#()%)"); matcher.dispose(); } @@ -125,7 +130,6 @@ performMatch(matcher, "(% "); performMatch(matcher, "%( )"); performMatch(matcher, "( % )"); - performMatch(matcher, "( %)"); performMatch(matcher, "%"); matcher.dispose(); } @@ -218,7 +222,7 @@ * @param expectedMatch the expected match * @param expectedString the expected string */ - private void performReaderTest(String testString, int expectedPos, int expectedMatch, String expectedString) { + protected void performReaderTest(String testString, int expectedPos, int expectedMatch, String expectedString) { TestCase t0= createTestCase(testString); assertEquals(expectedPos, t0.fPos); assertEquals(expectedMatch, t0.fMatch); @@ -244,11 +248,11 @@ assertNotNull(region); final boolean isForward= test.fPos > test.fMatch; assertEquals(isForward, matcher.getAnchor() == ICharacterPairMatcher.RIGHT); - // If the match is forward, the curser is one character + // If the match is forward, the cursor is one character // after the start of the match, so we need to count one // step backwards final int offset= isForward ? test.getOffset() : test.getOffset() - 1; - final int length= isForward ? test.getLength() : test.getLength() + 1; + final int length= (isForward && !fCaretInsideMatchedPair) ? test.getLength() : test.getLength() + 1; assertEquals(length, region.getLength()); assertEquals(offset, region.getOffset()); } @@ -271,6 +275,16 @@ int pos= str.indexOf("%"); assertFalse(pos == -1); int match= str.indexOf("#"); + + if (fCaretInsideMatchedPair) { + if (pos - 1 >= 0) { + char ch= str.charAt(pos - 1); + if (fChars.indexOf(ch) % 2 == 1) { + pos-= 1; + } + } + } + // account for the length of the first position marker, // if there is one if (match != -1 && match < pos) pos -= 1; diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/DefaultPairMatcherTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/DefaultPairMatcherTest.java index a59d0ec..930cb12 100644 --- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/DefaultPairMatcherTest.java +++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/DefaultPairMatcherTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2008 IBM Corporation and others. + * Copyright (c) 2006, 2012 IBM Corporation and others. * 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 @@ -13,8 +13,8 @@ import junit.framework.Test; import junit.framework.TestSuite; +import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocumentExtension3; -import org.eclipse.jface.text.source.DefaultCharacterPairMatcher; import org.eclipse.jface.text.source.ICharacterPairMatcher; /** @@ -29,8 +29,9 @@ } protected ICharacterPairMatcher createMatcher(String chars) { - return new DefaultCharacterPairMatcher(chars.toCharArray(), - getDocumentPartitioning()); + setCaretInsideMatchedPair(false); + setChars(chars); + return super.createMatcher(chars); } /* @@ -41,4 +42,38 @@ return IDocumentExtension3.DEFAULT_PARTITIONING; } + /** Tests that the test case reader works */ + public void testTestCaseReader() { + setCaretInsideMatchedPair(false); + setChars("()[]{}"); + performReaderTest("#( )%", 3, 0, "( )"); + performReaderTest("%( )#", 0, 3, "( )"); + performReaderTest("( )%", 3, -1, "( )"); + performReaderTest("#%", 0, 0, ""); + } + + /** + * Close matches. + * + * @throws BadLocationException + */ + public void testCloseMatches1() throws BadLocationException { + final ICharacterPairMatcher matcher= createMatcher("()[]{}"); + performMatch(matcher, "#()%"); + performMatch(matcher, "(#()%)"); + matcher.dispose(); + } + + + /** + * Checks of simple situations where no matches should be found. + * + * @throws BadLocationException + */ + public void testIncompleteMatch1() throws BadLocationException { + final ICharacterPairMatcher matcher= createMatcher("()[]{}"); + performMatch(matcher, "( %)"); + matcher.dispose(); + } + } diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/DefaultPairMatcherTest2.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/DefaultPairMatcherTest2.java new file mode 100644 index 0000000..85e3e30 --- /dev/null +++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/DefaultPairMatcherTest2.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2012 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.jface.text.tests; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.jface.text.IDocumentExtension3; +import org.eclipse.jface.text.source.ICharacterPairMatcher; + +/** + * Tests for the default pair matcher. + * + * @since 3.8 + */ +public class DefaultPairMatcherTest2 extends AbstractPairMatcherTest { + + public static Test suite() { + return new TestSuite(DefaultPairMatcherTest2.class); + } + + protected ICharacterPairMatcher createMatcher(String chars) { + setCaretInsideMatchedPair(true); + setChars(chars); + return super.createMatcher(chars); + } + + /* + * @see org.eclipse.jface.text.tests.AbstractPairMatcherTest#getDocumentPartitioning() + * @since 3.3 + */ + protected String getDocumentPartitioning() { + return IDocumentExtension3.DEFAULT_PARTITIONING; + } + + /** Tests that the test case reader works */ + public void testTestCaseReader() { + setCaretInsideMatchedPair(true); + setChars("()[]{}"); + performReaderTest("#( )%", 2, 0, "( )"); + performReaderTest("%( )#", 0, 3, "( )"); + performReaderTest("( )%", 2, -1, "( )"); + performReaderTest("#%", 0, 0, ""); + } +}