diff options
author | Dani Megert | 2004-04-14 15:46:14 +0000 |
---|---|---|
committer | Dani Megert | 2004-04-14 15:46:14 +0000 |
commit | 8048882152e3dd8bb9dd4d25ae2968c9aba05fdb (patch) | |
tree | 11941e4c47f030341fa942a532f882605d089677 /org.eclipse.text.tests/src/org/eclipse/text | |
parent | feec0ceb0b6a70b3211729f5051a965caf07b8c3 (diff) | |
download | eclipse.platform.text-8048882152e3dd8bb9dd4d25ae2968c9aba05fdb.tar.gz eclipse.platform.text-8048882152e3dd8bb9dd4d25ae2968c9aba05fdb.tar.xz eclipse.platform.text-8048882152e3dd8bb9dd4d25ae2968c9aba05fdb.zip |
Initial Release - work in progressInitial_Release
Diffstat (limited to 'org.eclipse.text.tests/src/org/eclipse/text')
13 files changed, 4872 insertions, 0 deletions
diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/ChildDocumentTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/ChildDocumentTest.java new file mode 100644 index 00000000000..49a12b4e7d6 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/ChildDocumentTest.java @@ -0,0 +1,504 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests; + +import junit.awtui.TestRunner; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.BadPositionCategoryException; +import org.eclipse.jface.text.DefaultLineTracker; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.Position; +import org.eclipse.jface.text.projection.ChildDocument; +import org.eclipse.jface.text.projection.ChildDocumentManager; + +public class ChildDocumentTest extends TestCase { + + private IDocument fDocument; + private Document fParent; + private ChildDocumentManager fManager; + + + public ChildDocumentTest(String name) { + super(name); + } + + + public static void main(String args[]) { + String a[] = { "org.eclipse.jface.text.test.ChildDocumentTest"}; + TestRunner.main(a); + } + + protected void checkPositions(Position[] positions) { + + try { + + Position[] v= fDocument.getPositions(IDocument.DEFAULT_CATEGORY); + assertTrue("invalid number of positions", v.length == positions.length); + + for (int i= 0; i < positions.length; i++) { + assertEquals(print(v[i]) + " != " + print(positions[i]), positions[i], v[i]); + } + + } catch (BadPositionCategoryException x) { + assertTrue("BadPositionCategoryException thrown", false); + } + + } + + protected void checkPositions(Position[] expected, Position[] actual) { + + assertTrue("invalid number of positions", expected.length == actual.length); + + for (int i= 0; i < expected.length; i++) { + assertEquals(print(actual[i]) + " != " + print(expected[i]), expected[i], actual[i]); + } + + } + + protected String print(Position p) { + return "[" + p.getOffset() + "," + p.getLength() + "]"; + } + + protected void checkLineInformationConsistency() { + DefaultLineTracker textTracker= new DefaultLineTracker(); + textTracker.set(fDocument.get()); + + int textLines= textTracker.getNumberOfLines(); + int trackerLines= fDocument.getNumberOfLines(); + + assertEquals("Child document store and child line tracker are inconsistent", trackerLines, textLines); + + for (int i= 0; i < trackerLines; i++) { + IRegion trackerLine= null; + IRegion textLine= null; + try { + trackerLine= fDocument.getLineInformation(i); + textLine= textTracker.getLineInformation(i); + } catch (BadLocationException e) { + assertTrue("BadLocationException thrown", false); + } + assertEquals("Child document store and child line tracker are inconsistent", trackerLine.getOffset(), textLine.getOffset()); + assertEquals("Child document store and child line tracker are inconsistent", trackerLine.getLength(), textLine.getLength()); + } + } + + protected void setUp() { + + fParent= new Document(); + + String text= + "package TestPackage;\n" + + "/*\n" + + "* comment\n" + + "*/\n" + + " public class Class {\n" + + " // comment1\n" + + " public void method1() {\n" + + " }\n" + + " // comment2\n" + + " public void method2() {\n" + + " }\n" + + " }\n"; + + fParent.set(text); + fManager= new ChildDocumentManager(); + try { + fDocument= fManager.createSlaveDocument(fParent); + if (fDocument instanceof ChildDocument) { + ChildDocument child= (ChildDocument) fDocument; + child.setParentDocumentRange(0, fParent.getLength()); + } + } catch (BadLocationException x) { + assertTrue(false); + } + + try { + + fDocument.addPosition(new Position( 0, 20)); + fDocument.addPosition(new Position( 21, 15)); + fDocument.addPosition(new Position( 38, 111)); + fDocument.addPosition(new Position( 61, 12)); + fDocument.addPosition(new Position( 75, 27)); + fDocument.addPosition(new Position(105, 12)); + fDocument.addPosition(new Position(119, 27)); + + } catch (BadLocationException x) { + assertTrue("initilization failed", false); + } + } + + public static Test suite() { + return new TestSuite(ChildDocumentTest.class); + } + + protected void tearDown () { + fDocument= null; + } + + public void testDelete1() { + + try { + + fDocument.replace(21, 16, null); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 0), + new Position( 22, 111), + new Position( 45, 12), + new Position( 59, 27), + new Position( 89, 12), + new Position(103, 27) + }; + + checkPositions(positions); + } + + public void testEditScript1() { + + // 1. step + + try { + + fDocument.replace(0, fDocument.getLength(), null); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 0) + }; + + checkPositions(positions); + + + // 2. step + try { + + fDocument.replace(0, 0, "\t"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + positions= new Position[] { + new Position( 1, 0) + }; + + checkPositions(positions); + + } + + public void testFindPositions() { + + try { + + fDocument.addPosition(new Position( 21, 13)); + fDocument.addPosition(new Position( 0, 19)); + fDocument.addPosition(new Position( 21, 14)); + fDocument.addPosition(new Position( 21, 16)); + fDocument.addPosition(new Position( 0, 0)); + fDocument.addPosition(new Position( 104, 1)); + fDocument.addPosition(new Position( 120, 1)); + fDocument.addPosition(new Position( 119, 1)); + + } catch (BadLocationException x) { + assertTrue("initilization failed", false); + } + + + Position[] positions= new Position[] { + new Position( 0, 0), + new Position( 0, 19), + new Position( 0, 20), + new Position( 21, 16), + new Position( 21, 14), + new Position( 21, 13), + new Position( 21, 15), + new Position( 38, 111), + new Position( 61, 12), + new Position( 75, 27), + new Position(104, 1), + new Position(105, 12), + new Position(119, 1), + new Position(119, 27), + new Position(120, 1) + }; + + checkPositions(positions); + + } + + public void testInsert1() { + + try { + + fDocument.replace(0, 0, "//comment\n"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 10, 20), + new Position( 31, 15), + new Position( 48, 111), + new Position( 71, 12), + new Position( 85, 27), + new Position(115, 12), + new Position(129, 27) + }; + + checkPositions(positions); + } + + public void testInsert2() { + + try { + + fDocument.replace(61, 0, "//comment\n"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 15), + new Position( 38, 121), + new Position( 71, 12), + new Position( 85, 27), + new Position(115, 12), + new Position(129, 27) + }; + + checkPositions(positions); + } + + public void testInsert3() { + + try { + + fDocument.replace(101, 0, "//comment\n"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 15), + new Position( 38, 121), + new Position( 61, 12), + new Position( 75, 37), + new Position(115, 12), + new Position(129, 27) + }; + + checkPositions(positions); + } + + public void testInsert4() { + + try { + + fDocument.replace(20, 0, "// comment"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + System.out.print(fDocument.get()); + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 31, 15), + new Position( 48, 111), + new Position( 71, 12), + new Position( 85, 27), + new Position(115, 12), + new Position(129, 27) + }; + + checkPositions(positions); + } + + public void testReplace1() { + + try { + + fDocument.replace(8, 11, "pkg1"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 13), + new Position( 14, 15), + new Position( 31, 111), + new Position( 54, 12), + new Position( 68, 27), + new Position( 98, 12), + new Position(112, 27) + }; + + checkPositions(positions); + } + + public void testReplace2() { + + try { + + fDocument.replace(21, 16, "//comment\n"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 10), + new Position( 32, 111), + new Position( 55, 12), + new Position( 69, 27), + new Position( 99, 12), + new Position(113, 27) + }; + + checkPositions(positions); + } + + public void testReplace3() { + + Position[] actual= new Position[] { + new Position(0, 150), + }; + + try { + + fDocument.addPosition(actual[0]); + fDocument.replace(0, 150, "xxxxxxxxxx"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] expected= new Position[] { + new Position(0, 10) + }; + + checkPositions(expected, actual); + } + + /* + * Replace in the parent document at the end offset of the child document + * + * [formatting] IllegalArgumentException when formatting comment code snippet in segmented mode + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=51594 + */ + public void testReplace4() { + try { + int start= fParent.getLineOffset(5); + int end= fParent.getLineOffset(8); + ChildDocument child= (ChildDocument) fDocument; + child.setParentDocumentRange(start, end - start); + fParent.replace(end, 1, "x"); + checkLineInformationConsistency(); + } catch (BadLocationException e) { + assertTrue("BadLocationException thrown", false); + } + } + + public void testAppend() { + + Position[] actual= new Position[] { + new Position(0, 2), + }; + + try { + + fDocument.replace(0, 150, null); + fDocument.replace(fDocument.getLength(), 0, "xx"); + fDocument.addPosition(actual[0]); + fDocument.replace(fDocument.getLength(), 0, "xxxxxxxx"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] expected= new Position[] { + new Position(0, 2) + }; + + checkPositions(expected, actual); + } + + + public void testShiftLeft() { + + try { + + fDocument.replace(73, 1, null); + fDocument.replace(98, 1, null); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 15), + new Position( 38, 109), + new Position( 61, 12), + new Position( 74, 26), + new Position(103, 12), + new Position(117, 27) + }; + + checkPositions(positions); + } + + public void testShiftRight() { + + try { + + fDocument.replace( 73, 0, "\t"); + fDocument.replace(100, 0, "\t"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 15), + new Position( 38, 113), + new Position( 61, 12), + new Position( 76, 28), + new Position(107, 12), + new Position(121, 27) + }; + + checkPositions(positions); + } +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/DocumentExtensionTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/DocumentExtensionTest.java new file mode 100644 index 00000000000..751773e2287 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/DocumentExtensionTest.java @@ -0,0 +1,346 @@ +/********************************************************************** +Copyright (c) 2000, 2002 IBM Corp. and others. +All rights reserved. This program and the accompanying materials +are made available under the terms of the Common Public License v1.0 +which accompanies this distribution, and is available at +http://www.eclipse.org/legal/cpl-v10.html + +Contributors: + IBM Corporation - Initial implementation +**********************************************************************/ +package org.eclipse.text.tests; + + +import java.util.ArrayList; +import java.util.List; + +import junit.awtui.TestRunner; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.DocumentEvent; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IDocumentExtension; +import org.eclipse.jface.text.IDocumentListener; +import org.eclipse.jface.text.projection.ChildDocument; +import org.eclipse.jface.text.projection.ChildDocumentManager; + + + +public class DocumentExtensionTest extends TestCase { + + + static class Listener implements IDocumentListener { + + int fRepetitions= 1; + private int fInvocations= 0; + + public void documentAboutToBeChanged(DocumentEvent e) { + ++ fInvocations; + } + + public void documentChanged(DocumentEvent e) { + + if (fInvocations > fRepetitions) { + fInvocations= 0; + return; + } + + if (e.getDocument() instanceof IDocumentExtension) { + IDocumentExtension extension= (IDocumentExtension) e.getDocument(); + Replace replace= getReplace(e); + if (replace != null) + extension.registerPostNotificationReplace(this, replace); + } + } + + protected Replace getReplace(DocumentEvent e) { + return null; + } + } + + static class Replace implements IDocumentExtension.IReplace { + + int fOffset; + int fLength; + String fText; + + public Replace() { + } + + /* + * @see IReplace#perform(IDocument, IDocumentListener) + */ + public void perform(IDocument document, IDocumentListener owner) { + try { + document.replace(fOffset, fLength, fText); + } catch (BadLocationException x) { + assertTrue(false); + } + } + } + + static class TestDocumentEvent extends DocumentEvent { + + public TestDocumentEvent(IDocument document, int offset, int length, String text) { + super(document, offset, length, text); + } + + public boolean isSameAs(DocumentEvent e) { + return (e.getDocument() == getDocument() && + e.getOffset() == getOffset() && + e.getLength() == getLength() && + ((e.getText() == null && getText() == null) || e.getText().equals(getText()))); + } + } + + static class TestDocumentListener implements IDocumentListener { + + private IDocument fDocument1; + private List fTrace1; + private TestDocumentEvent fExpected1; + + private List fTrace2; + private TestDocumentEvent fExpected2; + + private boolean fPopped= false; + + public TestDocumentListener(IDocument d1, List t1, IDocument d2, List t2) { + fDocument1= d1; + fTrace1= t1; + fTrace2= t2; + } + + public void documentAboutToBeChanged(DocumentEvent received) { + if (!fPopped) { + fPopped= true; + fExpected1= (TestDocumentEvent) fTrace1.remove(0); + fExpected2= (TestDocumentEvent) fTrace2.remove(0); + } + + TestDocumentEvent e= (received.getDocument() == fDocument1 ? fExpected1 : fExpected2); + assertTrue(e.isSameAs(received)); + } + + public void documentChanged(DocumentEvent received) { + TestDocumentEvent e= (received.getDocument() == fDocument1 ? fExpected1 : fExpected2); + assertTrue(e.isSameAs(received)); + fPopped= false; + } + } + + + public DocumentExtensionTest(String name) { + super(name); + } + + + public static void main(String args[]) { + String a[] = { "org.eclipse.jface.text.test.DocumentExtensionTest"}; + TestRunner.main(a); + } + + public static Test suite() { + return new TestSuite(DocumentExtensionTest.class); + } + + public void testAppend() { + Listener listener= new Listener() { + protected Replace getReplace(DocumentEvent e) { + String t= e.getText(); + if (t != null && t.length() > 0) { + Replace r= new Replace(); + r.fOffset= (e.getOffset() + t.length()); + r.fLength= 0; + r.fText= "x"; + return r; + } + return null; + } + }; + + IDocument document= new Document(); + document.addDocumentListener(listener); + + try { + document.replace(0, 0, "c"); + document.replace(0, 0, "b"); + document.replace(0, 0, "a"); + } catch (BadLocationException x) { + assertTrue(false); + } + + assertTrue("axbxcx".equals(document.get())); + } + + public void testRemove() { + Listener listener= new Listener() { + protected Replace getReplace(DocumentEvent e) { + String t= e.getText(); + if (t == null || t.length() == 0) { + Replace r= new Replace(); + r.fOffset= e.getOffset(); + r.fLength= 0; + r.fText= "y"; + return r; + } + return null; + } + }; + + IDocument document= new Document("abc"); + document.addDocumentListener(listener); + + try { + document.replace(2, 1, null); + document.replace(1, 1, null); + document.replace(0, 1, null); + } catch (BadLocationException x) { + assertTrue(false); + } + + assertTrue("yyy".equals(document.get())); + } + + public void testRepeatedAppend() { + Listener listener= new Listener() { + protected Replace getReplace(DocumentEvent e) { + String t= e.getText(); + if (t != null && t.length() > 0) { + Replace r= new Replace(); + r.fOffset= (e.getOffset() + t.length()); + r.fLength= 0; + r.fText= "x"; + return r; + } + return null; + } + }; + listener.fRepetitions= 5; + + IDocument document= new Document(); + document.addDocumentListener(listener); + + try { + document.replace(0, 0, "c"); + document.replace(0, 0, "b"); + document.replace(0, 0, "a"); + } catch (BadLocationException x) { + assertTrue(false); + } + + assertTrue("axxxxxbxxxxxcxxxxx".equals(document.get())); + } + + private List createTrace(IDocument document, int repetitions) { + int i; + List trace= new ArrayList(); + + trace.add(new TestDocumentEvent(document, 0, 0, "c")); + for (i= 0; i < repetitions; i++) + trace.add(new TestDocumentEvent(document, 1 + i, 0, "x")); + + trace.add(new TestDocumentEvent(document, 0, 0, "b")); + for (i= 0; i < repetitions; i++) + trace.add(new TestDocumentEvent(document, 1 + i, 0, "x")); + + trace.add(new TestDocumentEvent(document, 0, 0, "a")); + for (i= 0; i < repetitions; i++) + trace.add(new TestDocumentEvent(document, 1 + i, 0, "x")); + + return trace; + } + + private void internalTestChildDocument(boolean modifyParent, boolean postModifyParent, int repetitions) { + + IDocument childDocument= null; + IDocument parentDocument= new Document(); + ChildDocumentManager manager= new ChildDocumentManager(); + try { + childDocument= manager.createSlaveDocument(parentDocument); + if (childDocument instanceof ChildDocument) { + ChildDocument child= (ChildDocument) childDocument; + child.setParentDocumentRange(0, parentDocument.getLength()); + } + } catch (BadLocationException x) { + assertTrue(false); + } + + TestDocumentListener l= new TestDocumentListener( + parentDocument, createTrace(parentDocument, repetitions), + childDocument, createTrace(childDocument, repetitions)); + parentDocument.addDocumentListener(l); + childDocument.addDocumentListener(l); + + Listener modifier= new Listener() { + protected Replace getReplace(DocumentEvent e) { + String t= e.getText(); + if (t != null && t.length() > 0) { + Replace r= new Replace(); + r.fOffset= (e.getOffset() + t.length()); + r.fLength= 0; + r.fText= "x"; + return r; + } + return null; + } + }; + modifier.fRepetitions= repetitions; + + IDocument document= postModifyParent ? parentDocument : childDocument; + document.addDocumentListener(modifier); + + document= modifyParent ? parentDocument : childDocument; + + try { + document.replace(0, 0, "c"); + document.replace(0, 0, "b"); + document.replace(0, 0, "a"); + } catch (BadLocationException x) { + assertTrue(false); + } + } + + public void testChildDocumentPP() { + internalTestChildDocument(true, true, 1); + } + + public void testChildDocumentCC() { + internalTestChildDocument(false, false, 1); + } + + public void testChildDocumentRepeatedPP() { + internalTestChildDocument(true, true, 5); + } + + public void testChildDocumentRepeatedCC() { + internalTestChildDocument(false, false, 5); + } + + public void testChildDocumentPC() { + try { + internalTestChildDocument(true, false, 1); + assertTrue(false); + } catch (UnsupportedOperationException x) { + } + } + + public void testChildDocumentCP() { + internalTestChildDocument(false, true, 1); + } + + public void testChildDocumentRepeatedPC() { + try { + internalTestChildDocument(true, false, 5); + assertTrue(false); + } catch (UnsupportedOperationException x) { + } + } + + public void testChildDocumentRepeatedCP() { + internalTestChildDocument(false, true, 5); + } +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/DocumentTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/DocumentTest.java new file mode 100644 index 00000000000..db56e3f45f6 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/DocumentTest.java @@ -0,0 +1,471 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests; + +import junit.awtui.TestRunner; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.BadPositionCategoryException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.Position; + + +public class DocumentTest extends TestCase { + + private Document fDocument; + + + public DocumentTest(String name) { + super(name); + } + + + public static void main(String args[]) { + String a[] = { "org.eclipse.jface.text.test.DocumentTest"}; + TestRunner.main(a); + } + + protected void checkPositions(Position[] expected) { + + try { + + Position[] actual= fDocument.getPositions(IDocument.DEFAULT_CATEGORY); + assertTrue("invalid number of positions", actual.length == expected.length); + + for (int i= 0; i < expected.length; i++) { + assertEquals(print(actual[i]) + " != " + print(expected[i]), expected[i], actual[i]); + } + + } catch (BadPositionCategoryException x) { + assertTrue("BadPositionCategoryException thrown", false); + } + + } + + protected void checkPositions(Position[] expected, Position[] actual) { + + assertTrue("invalid number of positions", expected.length == actual.length); + + for (int i= 0; i < expected.length; i++) { + assertEquals(print(actual[i]) + " != " + print(expected[i]), expected[i], actual[i]); + } + + } + + protected String print(Position p) { + return "[" + p.getOffset() + "," + p.getLength() + "]"; + } + + protected void setUp() { + + fDocument= new Document(); + + String text= + "package TestPackage;\n" + + "/*\n" + + "* comment\n" + + "*/\n" + + " public class Class {\n" + + " // comment1\n" + + " public void method1() {\n" + + " }\n" + + " // comment2\n" + + " public void method2() {\n" + + " }\n" + + " }\n"; + + fDocument.set(text); + + try { + + fDocument.addPosition(new Position( 0, 20)); + fDocument.addPosition(new Position( 21, 15)); + fDocument.addPosition(new Position( 38, 111)); + fDocument.addPosition(new Position( 61, 12)); + fDocument.addPosition(new Position( 75, 27)); + fDocument.addPosition(new Position(105, 12)); + fDocument.addPosition(new Position(119, 27)); + + } catch (BadLocationException x) { + assertTrue("initilization failed", false); + } + } + + public static Test suite() { + return new TestSuite(DocumentTest.class); + } + + protected void tearDown () { + fDocument= null; + } + + public void testDelete1() { + + try { + + fDocument.replace(21, 16, null); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 0), + new Position( 22, 111), + new Position( 45, 12), + new Position( 59, 27), + new Position( 89, 12), + new Position(103, 27) + }; + + checkPositions(positions); + } + + public void testEditScript1() { + + // 1. step + + try { + + fDocument.replace(0, fDocument.getLength(), null); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 0) + }; + + checkPositions(positions); + + + // 2. step + try { + + fDocument.replace(0, 0, "\t"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + positions= new Position[] { + new Position( 1, 0) + }; + + checkPositions(positions); + + } + + public void testFindPositions() { + + try { + + fDocument.addPosition(new Position( 21, 13)); + fDocument.addPosition(new Position( 0, 19)); + fDocument.addPosition(new Position( 21, 14)); + fDocument.addPosition(new Position( 21, 16)); + fDocument.addPosition(new Position( 0, 0)); + fDocument.addPosition(new Position( 104, 1)); + fDocument.addPosition(new Position( 120, 1)); + fDocument.addPosition(new Position( 119, 1)); + + } catch (BadLocationException x) { + assertTrue("initilization failed", false); + } + + + Position[] positions= new Position[] { + new Position( 0, 0), + new Position( 0, 19), + new Position( 0, 20), + new Position( 21, 16), + new Position( 21, 14), + new Position( 21, 13), + new Position( 21, 15), + new Position( 38, 111), + new Position( 61, 12), + new Position( 75, 27), + new Position(104, 1), + new Position(105, 12), + new Position(119, 1), + new Position(119, 27), + new Position(120, 1) + }; + + checkPositions(positions); + + } + + public void testInsert1() { + + try { + + fDocument.replace(0, 0, "//comment\n"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 10, 20), + new Position( 31, 15), + new Position( 48, 111), + new Position( 71, 12), + new Position( 85, 27), + new Position(115, 12), + new Position(129, 27) + }; + + checkPositions(positions); + } + + public void testInsert2() { + + try { + + fDocument.replace(61, 0, "//comment\n"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 15), + new Position( 38, 121), + new Position( 71, 12), + new Position( 85, 27), + new Position(115, 12), + new Position(129, 27) + }; + + checkPositions(positions); + } + + public void testInsert3() { + + try { + + fDocument.replace(101, 0, "//comment\n"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 15), + new Position( 38, 121), + new Position( 61, 12), + new Position( 75, 37), + new Position(115, 12), + new Position(129, 27) + }; + + checkPositions(positions); + } + + public void testInsert4() { + + try { + + fDocument.replace(20, 0, "// comment"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + System.out.print(fDocument.get()); + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 31, 15), + new Position( 48, 111), + new Position( 71, 12), + new Position( 85, 27), + new Position(115, 12), + new Position(129, 27) + }; + + checkPositions(positions); + } + + public void testReplace1() { + + try { + + fDocument.replace(8, 11, "pkg1"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 13), + new Position( 14, 15), + new Position( 31, 111), + new Position( 54, 12), + new Position( 68, 27), + new Position( 98, 12), + new Position(112, 27) + }; + + checkPositions(positions); + } + + public void testReplace2() { + + try { + + fDocument.replace(21, 16, "//comment\n"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 10), + new Position( 32, 111), + new Position( 55, 12), + new Position( 69, 27), + new Position( 99, 12), + new Position(113, 27) + }; + + checkPositions(positions); + } + + public void testReplace3() { + + Position[] actual= new Position[] { + new Position(0, 150), + }; + + try { + + fDocument.addPosition(actual[0]); + fDocument.replace(0, 150, "xxxxxxxxxx"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] expected= new Position[] { + new Position(0, 10) + }; + + checkPositions(expected, actual); + } + + public void testReplace4() { + + try { + + fDocument.replace(19, 1, "xxxxx;"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 25), + new Position( 26, 15), + new Position( 43, 111), + new Position( 66, 12), + new Position( 80, 27), + new Position(110, 12), + new Position(124, 27) + }; + + checkPositions(positions); + } + + public void testAppend() { + + Position[] actual= new Position[] { + new Position(0, 2), + }; + + try { + + fDocument.replace(0, 150, null); + fDocument.replace(fDocument.getLength(), 0, "xx"); + fDocument.addPosition(actual[0]); + fDocument.replace(fDocument.getLength(), 0, "xxxxxxxx"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] expected= new Position[] { + new Position(0, 2) + }; + + checkPositions(expected, actual); + } + + + public void testShiftLeft() { + + try { + + fDocument.replace(73, 1, null); + fDocument.replace(98, 1, null); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 15), + new Position( 38, 109), + new Position( 61, 12), + new Position( 74, 26), + new Position(103, 12), + new Position(117, 27) + }; + + checkPositions(positions); + } + + public void testShiftRight() { + + try { + + fDocument.replace( 73, 0, "\t"); + fDocument.replace(100, 0, "\t"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 0, 20), + new Position( 21, 15), + new Position( 38, 113), + new Position( 61, 12), + new Position( 76, 28), + new Position(107, 12), + new Position(121, 27) + }; + + checkPositions(positions); + } +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/GapTextTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/GapTextTest.java new file mode 100644 index 00000000000..b66080b050f --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/GapTextTest.java @@ -0,0 +1,202 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests; + +import junit.awtui.TestRunner; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.jface.text.GapTextStore; + + +public class GapTextTest extends TestCase { + + static class GapText extends GapTextStore { + + public GapText() { + super(5, 10); + } + + String getText() { + return super.getContentAsString(); + } + + int getGapStart() { + return super.getGapStartIndex(); + } + + int getGapEnd() { + return super.getGapEndIndex(); + } + + int getRawLength() { + return super.getContentAsString().length(); + } + } + + private GapText fText; + + + public GapTextTest(String name) { + super(name); + } + + public static void main(String args[]) { + String a[] = { "org.eclipse.jface.ui.text.test.GapTextTest" }; + TestRunner.main(a); + } + + protected String printGap() { + return "[" + fText.getGapStart() + "," + fText.getGapEnd() + "]"; + } + + protected void setUp() { + + fText= new GapText(); + fText.set("xxxxx"); + } + + public static Test suite() { + return new TestSuite(GapTextTest.class); + } + + protected void tearDown () { + fText= null; + } + + public void testGetText1() { + + assertTrue("invalid gap", fText.getGapStart() == -1 && fText.getGapEnd() == -1); + + String[] expected= { + "xyxxxx", + "xyxyxxx", + "xyxyxyxx", + "xyxyxyxyx", + "xyxyxyxyxy" + }; + + for (int i= 1; i < 5; i++) { + fText.replace(2 * i - 1, 0, "y"); + String txt= fText.get(0, fText.getLength()); + assertEquals("invalid text \'" + txt + "\' returned, should be \'" + expected[i - 1] + "\'", expected[i - 1], txt); + } + + } + + public void testGetText2() { + + assertTrue("invalid gap", fText.getGapStart() == -1 && fText.getGapEnd() == -1); + + String[] expected= { + "yxxxxx", + "yxyxxxx", + "yxyxyxxx", + "yxyxyxyxx", + "yxyxyxyxyx" + }; + + for (int i= 1; i < 5; i++) { + fText.replace(2 * (i - 1), 0, "y"); + String txt= fText.get(0, fText.getLength()); + assertEquals("invalid text \'" + txt + "\' returned, should be \'" + expected[i - 1] + "\'", expected[i - 1], txt); + } + + } + + public void testInsert() { + + assertTrue("invalid gap", fText.getGapStart() == -1 && fText.getGapEnd() == -1); + fText.replace(2, 0, "y"); + assertTrue("invalid gap:" + printGap(), fText.getGapStart() == 3 && fText.getGapEnd() == 13); + + + for (int i= 1; i <= 5; i++) { + fText.replace(2 + i, 0, "y"); + assertTrue("invalid gap:" + printGap(), fText.getGapStart() == (3 + i) && fText.getGapEnd() == 13); + } + + fText.replace(8, 0, "y"); + assertTrue("invalid gap:" + printGap(), fText.getGapStart() == 9 && fText.getGapEnd() == 19); + } + + public void testRemoveGapOverlapping() { + + assertTrue("invalid gap", fText.getGapStart() == -1 && fText.getGapEnd() == -1); + fText.replace(2, 2, null); + assertTrue("invalid gap: " + printGap(), fText.getGapStart() == 2 && fText.getGapEnd() == 12); + + fText.replace(1, 2, null); + assertTrue("invalid gap: " + printGap(), fText.getGapStart() == 1 && fText.getGapEnd() == 13); + } + + public void testRemoveGapOverlapping2() { + + assertTrue("invalid gap", fText.getGapStart() == -1 && fText.getGapEnd() == -1); + fText.replace(0, 0, "aaaaazzzzzyyyyy"); + assertTrue("invalid gap:" + printGap(), fText.getGapStart() == 15 && fText.getGapEnd() == 25); + assertEquals("aaaaazzzzzyyyyyxxxxx", fText.get(0, fText.getLength())); + + + fText.replace(5, 12, null); + assertTrue("invalid gap:" + printGap(), fText.getGapStart() == 5 && fText.getGapEnd() == 27); + assertEquals("aaaaaxxx", fText.get(0, fText.getLength())); + } + + public void testRemoveRemoteFromGap() { + + assertTrue("invalid gap", fText.getGapStart() == -1 && fText.getGapEnd() == -1); + fText.replace(0, 0, "aaaaazzzzzyyyyy"); + assertTrue("invalid gap:" + printGap(), fText.getGapStart() == 15 && fText.getGapEnd() == 25); + assertEquals("aaaaazzzzzyyyyyxxxxx", fText.get(0, fText.getLength())); + + // before gap + fText.replace(5, 2, null); + assertTrue("invalid gap:" + printGap(), fText.getGapStart() == 5 && fText.getGapEnd() == 15); + assertEquals("aaaaazzzyyyyyxxxxx", fText.get(0, fText.getLength())); + + // after gap + fText.replace(7, 10, null); + assertTrue("invalid gap:" + printGap(), fText.getGapStart() == 7 && fText.getGapEnd() == 17); + assertEquals("aaaaazzx", fText.get(0, fText.getLength())); + + } + + public void testRemoveAtLeftGapEdge() { + assertTrue("invalid gap", fText.getGapStart() == -1 && fText.getGapEnd() == -1); + fText.replace(2, 0, "xxxxx"); + assertTrue("invalid gap: " + printGap(), fText.getGapStart() == 7 && fText.getGapEnd() == 17); + fText.replace(6, 1, null); + assertTrue("invalid gap: " + printGap(), fText.getGapStart() == 6 && fText.getGapEnd() == 17); + } + + public void testRemoveAtRightGapEdge() { + assertTrue("invalid gap", fText.getGapStart() == -1 && fText.getGapEnd() == -1); + fText.replace(2, 0, "xxxxx"); + assertTrue("invalid gap: " + printGap(), fText.getGapStart() == 7 && fText.getGapEnd() == 17); + fText.replace(7, 1, null); + assertTrue("invalid gap: " + printGap(), fText.getGapStart() == 7 && fText.getGapEnd() == 18); + } + + public void testReplace() { + + assertTrue("invalid gap", fText.getGapStart() == -1 && fText.getGapEnd() == -1); + fText.replace(2, 2, "yy"); + assertTrue("invalid gap:" + printGap(), fText.getGapStart() == 4 && fText.getGapEnd() == 14); + + fText.replace(4, 1, "yyyyyyyyyy"); + assertTrue("invalid gap:" + printGap(), fText.getGapStart() == 14 && fText.getGapEnd() == 24); + + fText.replace(14, 0, "yyy"); + assertTrue("invalid gap:" + printGap(), fText.getGapStart() == 17 && fText.getGapEnd() == 24); + } +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/LineTrackerTest3.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/LineTrackerTest3.java new file mode 100644 index 00000000000..e968ff2b077 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/LineTrackerTest3.java @@ -0,0 +1,420 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests; + +import junit.awtui.TestRunner; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.ConfigurableLineTracker; +import org.eclipse.jface.text.GapTextStore; +import org.eclipse.jface.text.ILineTracker; +import org.eclipse.jface.text.IRegion; + +public class LineTrackerTest3 extends TestCase { + + private GapTextStore fText; + private ILineTracker fTracker; + + + public LineTrackerTest3(String name) { + super(name); + } + + public static void main(String args[]) { + String a[] = { "org.eclipse.jface.text.test.LineTrackerTest3"}; + TestRunner.main(a); + } + + protected int getLineOffset(int line, int[] lines) { + int offset= 0; + for (int i= 0; i < line; i++) + offset += (lines[i] + 1); + return offset; + } + + protected void checkLines(int[] lines) { + + assertTrue("invalid number of line", fTracker.getNumberOfLines() == lines.length); + + for (int i= 0; i < lines.length; i++) { + + try { + + IRegion line= fTracker.getLineInformation(i); + + assertTrue("line: " + i + " length=" + line.getLength() + " should be:" + lines[i], line.getLength() == lines[i]); + + int expected= getLineOffset(i, lines); + assertTrue("line: " + i + " offset=" + line.getOffset() + " should be:" + expected, line.getOffset() == expected); + + } catch (BadLocationException x) { + assertTrue(false); + } + } + } + + protected void setUp() { + + fText= new GapTextStore(50, 300); + fTracker= new ConfigurableLineTracker(new String[] { "\n" }); + fText.set("x\nx\nx\nx\nx\n"); + fTracker.set("x\nx\nx\nx\nx\n"); + } + + public static Test suite() { + return new TestSuite(LineTrackerTest3.class); + } + + protected void tearDown () { + fTracker= null; + fText= null; + } + + public void testEditScript1() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(0, fText.getLength(), "x"); + fText.replace(0, fText.getLength(), "x"); + + checkLines(new int[] { 1 }); + + fTracker.replace(1, 0, "y"); + fText.replace(1, 0, "y"); + + checkLines(new int[] { 2 }); + + fTracker.replace(2, 0, "z"); + fText.replace(2, 0, "z"); + + checkLines(new int[] { 3 }); + + fTracker.replace(3, 0, "\n"); + fText.replace(3, 0, "\n"); + + checkLines(new int[] { 3, 0 }); + + + fTracker.replace(4, 0, "x"); + fText.replace(4, 0, "x"); + + checkLines(new int[] { 3, 1 }); + + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + + } + + public void testEmptyLines() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(0, 10, null); + fText.replace(0, 10, null); + checkLines(new int[] { 0 }); + + fTracker.replace(0, 0, "\n\n\n\n\n"); + fText.replace(0, 0, "\n\n\n\n\n"); + checkLines(new int[] { 0, 0, 0, 0, 0, 0 }); + + for (int i= 0; i < 6; i++) { + int no= fTracker.getLineNumberOfOffset(i); + assertTrue("invalid line number " + no + " reported instead of " + i, no == i); + } + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testInsert1() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(3, 0, "yyyy"); + fText.replace(3, 0, "yyyy"); + checkLines(new int[] { 1, 5, 1, 1, 1, 0 }); + + fTracker.replace(9, 0, "y\n"); + fText.replace(9, 0, "y\n"); + checkLines(new int[] { 1, 5, 2, 0, 1, 1, 0 }); + + fTracker.replace(11, 0, "y\n"); + fText.replace(11, 0, "y\n"); + checkLines(new int[] { 1, 5, 2, 1, 0, 1, 1, 0 }); + + fTracker.replace(13, 0, "y"); + fText.replace(13, 0, "y"); + checkLines(new int[] { 1, 5, 2, 1, 1, 1, 1, 0 }); + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testInsert2() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(3, 0, "yyyy"); + fText.replace(3, 0, "yyyy"); + checkLines(new int[] { 1, 5, 1, 1, 1, 0 }); + + fTracker.replace(9, 0, "y\ny\ny"); + fText.replace(9, 0, "y\ny\ny"); + checkLines(new int[] { 1, 5, 2, 1, 1, 1, 1, 0 }); + + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testLinesNumbers() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(0, 10, "\na\nbb\nccc\ndddd\neeeee\n" ); + fText.replace(0, 10, "\na\nbb\nccc\ndddd\neeeee\n" ); + checkLines(new int[] { 0, 1, 2, 3, 4, 5, 0 }); + + int offset= 0; + for (int i= 0; i < 5; i++) { + for (int j= 0; j < i; j++) { + int no= fTracker.getLineNumberOfOffset(offset + j); + assertTrue("invalid line number " + no + " reported instead of " + i, no == i); + } + offset += (i + 1); + } + + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testOffsets() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + for (int i= 0; i < 5; i++) { + IRegion line= fTracker.getLineInformation(i); + int pos= line.getOffset() + line.getLength(); + int offset= (2 * i) + 1; + assertTrue("invalid line end offset " + pos + " for line " + i + " should be " + offset, offset == pos); + } + + for (int i= 0; i < 5; i++) { + int pos= fTracker.getLineOffset(i); + int offset= 2 * i; + assertTrue("invalid line start offset " + pos + " for line " + i + " should be " + offset, pos == offset); + } + + for (int i= 0; i < 10; i++) { + int line= fTracker.getLineNumberOfOffset(i); + double l= Math.floor(i/2); + assertTrue("invalid line number " + line + " for position " + i + " should be " + l, l == line); + } + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testRemove() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(3, 1, null); + fText.replace(3, 1, null); + checkLines(new int[] { 1, 2, 1, 1, 0 }); + + fTracker.replace(6, 1, null); + fText.replace(6, 1, null); + checkLines(new int[] { 1, 2, 2, 0 }); + + fTracker.replace(3, 5, null); + fText.replace(3, 4, null); + checkLines(new int[] { 1, 1 }); + + fTracker.replace(0, 3, null); + fText.replace(0, 3, null); + checkLines(new int[] { 0 }); + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testReplace() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(0, fText.getLength(), "\tx\n\tx\n\tx\n\tx\n\tx\n"); + fText.replace(0, fText.getLength(), "\tx\n\tx\n\tx\n\tx\n\tx\n"); + + checkLines(new int[] { 2, 2, 2, 2, 2, 0 }); + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + + } + + public void testReplace2() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(0, fText.getLength(), "x"); + fText.replace(0, fText.getLength(), "x"); + + checkLines(new int[] { 1 }); + + fTracker.replace(0, fText.getLength(), "x\nx\nx\n"); + fText.replace(0, fText.getLength(), "x\nx\nx\n"); + + checkLines(new int[] { 1, 1, 1, 0 }); + + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + + } + + public void testReplace3() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(1, 1, "\n"); + fText.replace(1, 1, "\n"); + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + + } + + public void testReplace4() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + int lines= fTracker.getNumberOfLines(); + IRegion previous= fTracker.getLineInformation(0); + for (int i= 1; i < lines; i++) { + int lastLineEnd= previous.getOffset() + previous.getLength(); + int lineStart= fTracker.getLineInformation(i).getOffset(); + fTracker.replace(lastLineEnd, lineStart - lastLineEnd, "\n"); + fText.replace(lastLineEnd, lineStart - lastLineEnd, "\n"); + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + previous= fTracker.getLineInformation(i); + } + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + + } + + + public void testShiftLeft() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(0, fText.getLength(), "\tx\n\tx\n\tx\n\tx\n\tx\n"); + fText.replace(0, fText.getLength(), "\tx\n\tx\n\tx\n\tx\n\tx\n"); + checkLines(new int[] { 2, 2, 2, 2, 2, 0 }); + + for (int i= 0; i < 5; i++) { + int pos= fTracker.getLineOffset(i); + fTracker.replace(pos, 1, null); + fText.replace(pos, 1, null); + } + + + String txt= fText.get(0, fText.getLength()); + assertEquals("invalid text", "x\nx\nx\nx\nx\n", txt); + + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testShiftRight() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + for (int i= 0; i < 5; i++) { + int pos= fTracker.getLineOffset(i); + fTracker.replace(pos, 0, "\t"); + fText.replace(pos, 0, "\t"); + } + + checkLines(new int[] { 2, 2, 2, 2, 2, 0 }); + + String txt= fText.get(0, fText.getLength()); + assertEquals("invalid text", "\tx\n\tx\n\tx\n\tx\n\tx\n", txt); + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testMultipleNewlines() { + fText= new GapTextStore(50, 300); + fTracker= new ConfigurableLineTracker(new String[] { "\n" }); + fText.set("x\n\nx\nx\n\nx\nx\n"); + fTracker.set("x\n\nx\nx\n\nx\nx\n"); + + checkLines(new int[] { 1, 0, 1, 1, 0, 1, 1, 0 }); + try { + int line= fTracker.getLineNumberOfOffset(8); + assertTrue(line == 5); + } catch (BadLocationException e) { + assertTrue("bad location", false); + } + } +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/LineTrackerTest4.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/LineTrackerTest4.java new file mode 100644 index 00000000000..da6bfe4d8a0 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/LineTrackerTest4.java @@ -0,0 +1,352 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests; + +import junit.awtui.TestRunner; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.ConfigurableLineTracker; +import org.eclipse.jface.text.GapTextStore; +import org.eclipse.jface.text.ILineTracker; +import org.eclipse.jface.text.IRegion; + + + +public class LineTrackerTest4 extends TestCase { + + private GapTextStore fText; + private ILineTracker fTracker; + + + public LineTrackerTest4(String name) { + super(name); + } + + public static void main(String args[]) { + String a[] = { "org.eclipse.jface.text.test.LineTrackerTest4" }; + TestRunner.main(a); + } + + protected int getLineOffset(int line, int[] lines) { + int offset= 0; + for (int i= 0; i < line; i++) + offset += (lines[i] + 2); + return offset; + } + + protected void checkLines(int[] lines) { + + assertTrue("invalid number of line", fTracker.getNumberOfLines() == lines.length); + + for (int i= 0; i < lines.length; i++) { + + try { + + IRegion line= fTracker.getLineInformation(i); + + assertTrue("line: " + i + " length=" + line.getLength() + " should be:" + lines[i], line.getLength() == lines[i]); + + int expected= getLineOffset(i, lines); + assertTrue("line: " + i + " offset=" + line.getOffset() + " should be:" + expected, line.getOffset() == expected); + + } catch (BadLocationException x) { + assertTrue(false); + } + } + } + + protected void setUp() { + + fText= new GapTextStore(50, 300); + fTracker= new ConfigurableLineTracker(new String[] { "\r\n" }); + fText.set("x\r\nx\r\nx\r\nx\r\nx\r\n"); + fTracker.set("x\r\nx\r\nx\r\nx\r\nx\r\n"); + } + + public static Test suite() { + return new TestSuite(LineTrackerTest4.class); + } + + protected void tearDown () { + fTracker= null; + fText= null; + } + + public void testEditScript1() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(0, fText.getLength(), "x"); + fText.replace(0, fText.getLength(), "x"); + + checkLines(new int[] { 1 }); + + fTracker.replace(1, 0, "y"); + fText.replace(1, 0, "y"); + + checkLines(new int[] { 2 }); + + fTracker.replace(2, 0, "z"); + fText.replace(2, 0, "z"); + + checkLines(new int[] { 3 }); + + fTracker.replace(3, 0, "\r\n"); + fText.replace(3, 0, "\r\n"); + + checkLines(new int[] { 3, 0 }); + + + fTracker.replace(5, 0, "x"); + fText.replace(5, 0, "x"); + + checkLines(new int[] { 3, 1 }); + + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + + } + + public void testEmptyLines() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(0, 15, null); + fText.replace(0, 15, null); + checkLines(new int[] { 0 }); + + fTracker.replace(0, 0, "\r\n\r\n\r\n\r\n\r\n"); + fText.replace(0, 0, "\r\n\r\n\r\n\r\n\r\n"); + checkLines(new int[] { 0, 0, 0, 0, 0, 0 }); + + for (int i= 0; i < 10; i++) { + int no= fTracker.getLineNumberOfOffset(i); + double l= Math.floor(i/2); + assertTrue("invalid line number " + no + " for position " + i + " should be " + l, l == no); + } + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testInsert1() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(4, 0, "yyyy"); + fText.replace(4, 0, "yyyy"); + checkLines(new int[] { 1, 5, 1, 1, 1, 0 }); + + fTracker.replace(11, 0, "y\r\n"); + fText.replace(11, 0, "y\r\n"); + checkLines(new int[] { 1, 5, 2, 0, 1, 1, 0 }); + + fTracker.replace(14, 0, "y\r\n"); + fText.replace(14, 0, "y\r\n"); + checkLines(new int[] { 1, 5, 2, 1, 0, 1, 1, 0 }); + + fTracker.replace(17, 0, "y"); + fText.replace(17, 0, "y"); + checkLines(new int[] { 1, 5, 2, 1, 1, 1, 1, 0 }); + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testInsert2() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(4, 0, "yyyy"); + fText.replace(4, 0, "yyyy"); + checkLines(new int[] { 1, 5, 1, 1, 1, 0 }); + + fTracker.replace(11, 0, "y\r\ny\r\ny"); + fText.replace(11, 0, "y\r\ny\r\ny"); + checkLines(new int[] { 1, 5, 2, 1, 1, 1, 1, 0 }); + + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testLinesNumbers() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(0, 15, "\r\na\r\nbb\r\nccc\r\ndddd\r\neeeee\r\n" ); + fText.replace(0, 10, "\na\nbb\nccc\ndddd\neeeee\n" ); + checkLines(new int[] { 0, 1, 2, 3, 4, 5, 0 }); + + int offset= 0; + for (int i= 0; i < 5; i++) { + for (int j= 0; j <= i; j++) { + int no= fTracker.getLineNumberOfOffset(offset + j); + assertTrue("invalid line number " + no + " reported instead of " + i, no == i); + } + offset += (i + 2); + } + + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testOffsets() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + for (int i= 0; i < 5; i++) { + IRegion line= fTracker.getLineInformation(i); + int pos= line.getOffset() + line.getLength() + 1; + int offset= (3 * i) + 2; + assertTrue("invalid line end offset " + pos + " for line " + i + " should be " + offset, offset == pos); + } + + for (int i= 0; i < 5; i++) { + int pos= fTracker.getLineOffset(i); + int offset= 3 * i; + assertTrue("invalid line start offset " + pos + " for line " + i + " should be " + offset, pos == offset); + } + + for (int i= 0; i < 15; i++) { + int line= fTracker.getLineNumberOfOffset(i); + double l= Math.floor(i/3); + assertTrue("invalid line number " + line + " for position " + i + " should be " + l, l == line); + } + + int lastLine= fTracker.getLineNumberOfOffset(fText.getLength()); + assertTrue("invalid last line number " + lastLine, 5 == lastLine); + + int offset= fTracker.getLineOffset(lastLine); + assertTrue("invalid last line start offset " + offset, fText.getLength() == offset); + + int length= fTracker.getLineLength(lastLine); + assertTrue("invalid last line end offset " + (offset + length -1), 0 == length); + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testRemove() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(4, 2, null); + fText.replace(4, 2, null); + checkLines(new int[] { 1, 2, 1, 1, 0 }); + + fTracker.replace(8, 2, null); + fText.replace(8, 2, null); + checkLines(new int[] { 1, 2, 2, 0 }); + + fTracker.replace(4, 7, null); + fText.replace(4, 7, null); + checkLines(new int[] { 1, 1 }); + + fTracker.replace(0, 4, null); + fText.replace(0, 4, null); + checkLines(new int[] { 0 }); + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testReplace() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(0, fText.getLength(), "\tx\r\n\tx\r\n\tx\r\n\tx\r\n\tx\r\n"); + fText.replace(0, fText.getLength(), "\tx\r\n\tx\r\n\tx\r\n\tx\r\n\tx\r\n"); + + checkLines(new int[] { 2, 2, 2, 2, 2, 0 }); + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + + } + + public void testShiftLeft() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + fTracker.replace(0, fText.getLength(), "\tx\r\n\tx\r\n\tx\r\n\tx\r\n\tx\r\n"); + fText.replace(0, fText.getLength(), "\tx\r\n\tx\r\n\tx\r\n\tx\r\n\tx\r\n"); + checkLines(new int[] { 2, 2, 2, 2, 2, 0 }); + + for (int i= 0; i < 5; i++) { + int pos= fTracker.getLineOffset(i); + fTracker.replace(pos, 1, null); + fText.replace(pos, 1, null); + } + + + String txt= fText.get(0, fText.getLength()); + assertEquals("invalid text", "x\r\nx\r\nx\r\nx\r\nx\r\n", txt); + + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } + + public void testShiftRight() { + + try { + + checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); + + for (int i= 0; i < 5; i++) { + int pos= fTracker.getLineOffset(i); + fTracker.replace(pos, 0, "\t"); + fText.replace(pos, 0, "\t"); + } + + checkLines(new int[] { 2, 2, 2, 2, 2, 0 }); + + String txt= fText.get(0, fText.getLength()); + assertEquals("invalid text", "\tx\r\n\tx\r\n\tx\r\n\tx\r\n\tx\r\n", txt); + + } catch (BadLocationException e) { + assertTrue("BadLocationException", false); + } + } +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/PositionUpdatingCornerCasesTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/PositionUpdatingCornerCasesTest.java new file mode 100644 index 00000000000..2e9028de661 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/PositionUpdatingCornerCasesTest.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests; + +import junit.awtui.TestRunner; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.BadPositionCategoryException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.Position; + + + +public class PositionUpdatingCornerCasesTest extends TestCase { + + private Document fDocument; + + + public PositionUpdatingCornerCasesTest(String name) { + super(name); + } + + + public static void main(String args[]) { + String a[] = { "org.eclipse.jface.text.test.PositionUpdatingCornerCasesTest"}; + TestRunner.main(a); + } + + protected void checkPositions(Position[] expected) { + + try { + + Position[] actual= fDocument.getPositions(IDocument.DEFAULT_CATEGORY); + assertTrue("invalid number of positions", actual.length == expected.length); + + for (int i= 0; i < expected.length; i++) { + assertEquals(print(actual[i]) + " != " + print(expected[i]), expected[i], actual[i]); + } + + } catch (BadPositionCategoryException x) { + assertTrue("BadPositionCategoryException thrown", false); + } + + } + + protected String print(Position p) { + return "[" + p.getOffset() + "," + p.getLength() + "]"; + } + + protected void setUp() { + + fDocument= new Document("x-x-x-x-x-x-x-x-x-x-x"); + + try { + fDocument.addPosition(new Position( 0, 0)); + fDocument.addPosition(new Position( 0, 1)); + fDocument.addPosition(new Position( 5, 0)); + fDocument.addPosition(new Position( 5, 3)); + } catch (BadLocationException x) { + assertTrue("initilization failed", false); + } + } + + public static Test suite() { + return new TestSuite(PositionUpdatingCornerCasesTest.class); + } + + protected void tearDown () { + fDocument= null; + } + + public void testInsert() { + + try { + + fDocument.replace(0, 0, "yy"); + + } catch (BadLocationException x) { + assertTrue("BadLocationException thrown", false); + } + + Position[] positions= new Position[] { + new Position( 2, 1), + new Position( 2, 0), + new Position( 7, 3), + new Position( 7, 0) + }; + + checkPositions(positions); + } +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/TextEditTests.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/TextEditTests.java new file mode 100644 index 00000000000..e1d4bd4435b --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/TextEditTests.java @@ -0,0 +1,1041 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.text.edits.CopySourceEdit; +import org.eclipse.text.edits.CopyTargetEdit; +import org.eclipse.text.edits.DeleteEdit; +import org.eclipse.text.edits.InsertEdit; +import org.eclipse.text.edits.MalformedTreeException; +import org.eclipse.text.edits.MoveSourceEdit; +import org.eclipse.text.edits.MoveTargetEdit; +import org.eclipse.text.edits.MultiTextEdit; +import org.eclipse.text.edits.RangeMarker; +import org.eclipse.text.edits.ReplaceEdit; +import org.eclipse.text.edits.TextEdit; +import org.eclipse.text.edits.TextEditCopier; +import org.eclipse.text.edits.UndoEdit; + +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; + +public class TextEditTests extends TestCase { + + private static final Class THIS= TextEditTests.class; + + private IDocument fDocument; + private MultiTextEdit fRoot; + + public TextEditTests(String name) { + super(name); + } + + public static Test allTests() { + return new TestSuite(THIS); + } + + public static Test suite() { + TestSuite result= new TestSuite(THIS); + if (false) { // For hot code replace when debugging test cases + result.addTestSuite(THIS); + result.addTestSuite(THIS); + result.addTestSuite(THIS); + result.addTestSuite(THIS); + result.addTestSuite(THIS); + result.addTestSuite(THIS); + } + return result; + } + + protected void setUp() throws Exception { + fDocument= new Document("0123456789"); + fRoot= new MultiTextEdit(); + } + + protected void tearDown() throws Exception { + fRoot= null; + fRoot= null; + } + + public void testOverlap1() throws Exception { + // [ [ ] ] + fRoot.addChild(new ReplaceEdit(0, 2, "01")); + boolean exception= false; + try { + fRoot.addChild(new ReplaceEdit(1, 2, "12")); + } catch (MalformedTreeException e) { + exception= true; + } + assertTrue(exception); + } + + public void testOverlap2() throws Exception { + // [[ ] ] + fRoot.addChild(new ReplaceEdit(0, 2, "01")); + boolean exception= false; + try { + fRoot.addChild(new ReplaceEdit(0, 1, "0")); + } catch (MalformedTreeException e) { + exception= true; + } + assertTrue(exception); + } + + public void testOverlap3() throws Exception { + // [ [ ]] + fRoot.addChild(new ReplaceEdit(0, 2, "01")); + boolean exception= false; + try { + fRoot.addChild(new ReplaceEdit(1, 1, "1")); + } catch (MalformedTreeException e) { + exception= true; + } + assertTrue(exception); + } + + public void testOverlap4() throws Exception { + // [ [ ] ] + fRoot.addChild(new ReplaceEdit(0, 3, "012")); + boolean exception= false; + try { + fRoot.addChild(new ReplaceEdit(1, 1, "1")); + } catch (MalformedTreeException e) { + exception= true; + } + assertTrue(exception); + } + + public void testOverlap5() throws Exception { + // [ [] ] + fRoot.addChild(new ReplaceEdit(0, 3, "012")); + boolean exception= false; + try { + fRoot.addChild(new InsertEdit(1, "xx")); + } catch (MalformedTreeException e) { + exception= true; + } + assertTrue(exception); + } + + public void testOverlap6() throws Exception { + // [ [] ] + fRoot.addChild(new ReplaceEdit(0, 3, "012")); + boolean exception= false; + try { + fRoot.addChild(new InsertEdit(2, "xx")); + } catch (MalformedTreeException e) { + exception= true; + } + assertTrue(exception); + } + + public void testOverlap7() throws Exception { + MoveSourceEdit source= new MoveSourceEdit(2, 5); + MoveTargetEdit target= new MoveTargetEdit(3, source); + fRoot.addChild(source); + boolean exception= false; + try { + fRoot.addChild(target); + } catch (MalformedTreeException e) { + exception= true; + } + assertTrue(exception); + } + + public void testOverlap8() throws Exception { + MoveSourceEdit source= new MoveSourceEdit(2, 5); + MoveTargetEdit target= new MoveTargetEdit(6, source); + fRoot.addChild(source); + boolean exception= false; + try { + fRoot.addChild(target); + } catch (MalformedTreeException e) { + exception= true; + } + assertTrue(exception); + } + + public void testOverlap9() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(3, 1); + MoveTargetEdit t1= new MoveTargetEdit(7, s1); + MoveSourceEdit s2= new MoveSourceEdit(2, 3); + MoveTargetEdit t2= new MoveTargetEdit(8, s2); + fRoot.addChild(s1); + fRoot.addChild(t1); + boolean exception= false; + try { + fRoot.addChild(s2); + fRoot.addChild(t2); + } catch (MalformedTreeException e) { + exception= true; + } + assertTrue(exception); + } + + public void testUnconnected1() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(3, 1); + boolean exception= false; + try { + fRoot.addChild(s1); + fRoot.apply(fDocument); + } catch (MalformedTreeException e) { + exception= true; + } + assertTrue(exception); + } + + public void testBufferLength() throws Exception { + MultiTextEdit edit= new MultiTextEdit(0, fDocument.getLength() + 1); + boolean exception= false; + try { + fRoot.addChild(edit); + fRoot.apply(fDocument); + } catch (MalformedTreeException e) { + exception= true; + assertTrue(exception); + } + } + + public void testCopy1() throws Exception { + MultiTextEdit root= new MultiTextEdit(); + TextEdit e1= new InsertEdit(2, "yy"); + TextEdit e2= new ReplaceEdit(2, 3, "3456"); + root.addChild(e1); + root.addChild(e2); + List org= flatten(root); + TextEditCopier copier= new TextEditCopier(root); + List copy= flatten(copier.perform()); + compare(copier, org, copy); + } + + public void testCopy2() throws Exception { + MultiTextEdit root= new MultiTextEdit(); + CopySourceEdit s1= new CopySourceEdit(5, 2); + CopyTargetEdit t1= new CopyTargetEdit(8, s1); + CopySourceEdit s2= new CopySourceEdit(5, 2); + CopyTargetEdit t2= new CopyTargetEdit(2, s2); + s1.addChild(s2); + root.addChild(s1); + root.addChild(t1); + root.addChild(t2); + List org= flatten(root); + TextEditCopier copier= new TextEditCopier(root); + List copy= flatten(copier.perform()); + compare(copier, org, copy); + } + + private List flatten(TextEdit edit) { + List result= new ArrayList(); + flatten(result, edit); + return result; + } + + private static void flatten(List result, TextEdit edit) { + result.add(edit); + TextEdit[] children= edit.getChildren(); + for (int i= 0; i < children.length; i++) { + flatten(result, children[i]); + } + } + + private static void compare(TextEditCopier copier, List org, List copy) { + assertTrue("Same length", org.size() == copy.size()); + for (Iterator iter= copy.iterator(); iter.hasNext();) { + TextEdit edit= (TextEdit)iter.next(); + assertTrue("Original is part of copy list", !org.contains(edit)); + if (edit instanceof MoveSourceEdit) { + MoveSourceEdit source= (MoveSourceEdit)edit; + assertTrue("Target edit isn't a copy", copy.contains(source.getTargetEdit())); + assertTrue("Traget edit is a original", !org.contains(source.getTargetEdit())); + } else if (edit instanceof MoveTargetEdit) { + MoveTargetEdit target= (MoveTargetEdit)edit; + assertTrue("Source edit isn't a copy", copy.contains(target.getSourceEdit())); + assertTrue("Source edit is a original", !org.contains(target.getSourceEdit())); + } else if (edit instanceof CopySourceEdit) { + CopySourceEdit source= (CopySourceEdit)edit; + assertTrue("Target edit isn't a copy", copy.contains(source.getTargetEdit())); + assertTrue("Traget edit is a original", !org.contains(source.getTargetEdit())); + } else if (edit instanceof CopyTargetEdit) { + CopyTargetEdit target= (CopyTargetEdit)edit; + assertTrue("Source edit isn't a copy", copy.contains(target.getSourceEdit())); + assertTrue("Source edit is a original", !org.contains(target.getSourceEdit())); + } + } + } + + public void testInsert1() throws Exception { + // [][ ] + TextEdit e1= new InsertEdit(2, "yy"); + TextEdit e2= new ReplaceEdit(2, 3, "3456"); + fRoot.addChild(e1); + fRoot.addChild(e2); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(fRoot, 2, 6); + assertEquals(e1, 2, 2); + assertEquals(e2, 4, 4); + assertEquals("Buffer content", "01yy345656789", fDocument.get()); + doUndoRedo(undo, "01yy345656789"); + } + + public void testInsert2() throws Exception { + // [][] + TextEdit e1= new InsertEdit(2, "yy"); + TextEdit e2= new InsertEdit(2, "xx"); + fRoot.addChild(e1); + fRoot.addChild(e2); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(fRoot, 2, 4); + assertEquals(e1, 2, 2); + assertEquals(e2, 4, 2); + assertEquals("Buffer content", "01yyxx23456789", fDocument.get()); + doUndoRedo(undo, "01yyxx23456789"); + } + + public void testInsert3() throws Exception { + // [ ][][ ] + TextEdit e1= new ReplaceEdit(0, 2, "011"); + TextEdit e2= new InsertEdit(2, "xx"); + TextEdit e3= new ReplaceEdit(2, 2, "2"); + fRoot.addChild(e1); + fRoot.addChild(e2); + fRoot.addChild(e3); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(fRoot, 0, 6); + assertEquals(e1, 0, 3); + assertEquals(e2, 3, 2); + assertEquals(e3, 5, 1); + assertEquals("Buffer content", "011xx2456789", fDocument.get()); + doUndoRedo(undo, "011xx2456789"); + } + + public void testInsert4() throws Exception { + TextEdit e1= new InsertEdit(0, "xx"); + fRoot.addChild(e1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer length", 12, fDocument.getLength()); + assertEquals(fRoot, 0, 2); + assertEquals(e1, 0, 2); + assertEquals("Buffer content", "xx0123456789", fDocument.get()); + doUndoRedo(undo, "xx0123456789"); + } + + public void testInsert5() throws Exception { + TextEdit e1= new InsertEdit(10, "xx"); + fRoot.addChild(e1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer length", 12, fDocument.getLength()); + assertEquals(fRoot, 10, 2); + assertEquals(e1, 10, 2); + assertEquals("Buffer content", "0123456789xx", fDocument.get()); + doUndoRedo(undo, "0123456789xx"); + } + + public void testInsertReplace1() throws Exception { + TextEdit e1= new ReplaceEdit(2, 1, "y"); + TextEdit e2= new InsertEdit(2, "xx"); + fRoot.addChild(e1); + fRoot.addChild(e2); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(fRoot, 2, 3); + assertEquals(e1, 4, 1); + assertEquals(e2, 2, 2); + assertEquals("Buffer content", "01xxy3456789", fDocument.get()); + doUndoRedo(undo, "01xxy3456789"); + } + + public void testDelete1() throws Exception { + TextEdit e1= new DeleteEdit(3, 1); + fRoot.addChild(e1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(fRoot, 3, 0); + assertEquals(e1, 3, 0); + assertEquals("Buffer content", "012456789", fDocument.get()); + doUndoRedo(undo, "012456789"); + } + + public void testDelete2() throws Exception { + TextEdit e1= new DeleteEdit(4, 1); + TextEdit e2= new DeleteEdit(3, 1); + TextEdit e3= new DeleteEdit(5, 1); + fRoot.addChild(e1); + fRoot.addChild(e2); + fRoot.addChild(e3); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(fRoot, 3, 0); + assertEquals(e1, 3, 0); + assertEquals(e2, 3, 0); + assertEquals(e3, 3, 0); + assertEquals("Buffer content", "0126789", fDocument.get()); + doUndoRedo(undo, "0126789"); + } + + public void testDelete3() throws Exception { + TextEdit e1= new InsertEdit(3, "x"); + TextEdit e2= new DeleteEdit(3, 1); + fRoot.addChild(e1); + fRoot.addChild(e2); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(fRoot, 3, 1); + assertEquals(e1, 3, 1); + assertEquals(e2, 4, 0); + assertEquals("Buffer content", "012x456789", fDocument.get()); + doUndoRedo(undo, "012x456789"); + } + + public void testDeleteWithChildren() throws Exception { + TextEdit e1= new DeleteEdit(2, 6); + MultiTextEdit e2= new MultiTextEdit(3, 3); + e1.addChild(e2); + TextEdit e3= new ReplaceEdit(3, 1, "xx"); + TextEdit e4= new ReplaceEdit(5, 1, "yy"); + e2.addChild(e3); + e2.addChild(e4); + fRoot.addChild(e1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "0189", fDocument.get()); + assertEquals(fRoot, 2, 0); + assertEquals(e1, 2, 0); + assertTrue(e2.isDeleted()); + assertTrue(e3.isDeleted()); + assertTrue(e4.isDeleted()); + doUndoRedo(undo, "0189"); + } + + public void testTreeUpdate1() throws Exception { + MultiTextEdit m1= new MultiTextEdit(); + TextEdit e1= new InsertEdit(2, "aa"); + TextEdit e2= new InsertEdit(4, "bb"); + m1.addChild(e1); + m1.addChild(e2); + MultiTextEdit m2= new MultiTextEdit(); + TextEdit e3= new InsertEdit(6, "cc"); + TextEdit e4= new InsertEdit(8, "dd"); + m2.addChild(e3); + m2.addChild(e4); + fRoot.addChild(m1); + fRoot.addChild(m2); + assertEquals(m1, 2, 2); + assertEquals(m2, 6, 2); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "01aa23bb45cc67dd89", fDocument.get()); + assertEquals(e1, 2, 2); + assertEquals(e2, 6, 2); + assertEquals(e3, 10, 2); + assertEquals(e4, 14, 2); + assertEquals(m1, 2, 6); + assertEquals(m2, 10, 6); + assertEquals(fRoot, 2, 14); + doUndoRedo(undo, "01aa23bb45cc67dd89"); + } + + public void testMove1() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(2, 2); + MoveTargetEdit t1= new MoveTargetEdit(5, s1); + fRoot.addChild(s1); + fRoot.addChild(t1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "0142356789", fDocument.get()); + assertEquals(s1, 2, 0); + assertEquals(t1, 3, 2); + doUndoRedo(undo, "0142356789"); + } + + public void testMove2() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(5, 2); + MoveTargetEdit t1= new MoveTargetEdit(2, s1); + fRoot.addChild(s1); + fRoot.addChild(t1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "0156234789", fDocument.get()); + assertEquals(s1, 7, 0); + assertEquals(t1, 2, 2); + doUndoRedo(undo, "0156234789"); + } + + public void testMove3() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(2, 2); + MoveTargetEdit t1= new MoveTargetEdit(7, s1); + TextEdit e2= new ReplaceEdit(4, 1, "x"); + fRoot.addChild(s1); + fRoot.addChild(t1); + fRoot.addChild(e2); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "01x5623789", fDocument.get()); + assertEquals(s1, 2, 0); + assertEquals(t1, 5, 2); + assertEquals(e2, 2, 1); + doUndoRedo(undo, "01x5623789"); + } + + public void testMove4() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(7, 2); + MoveTargetEdit t1= new MoveTargetEdit(2, s1); + TextEdit e2= new ReplaceEdit(5, 1, "x"); + fRoot.addChild(s1); + fRoot.addChild(t1); + fRoot.addChild(e2); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "0178234x69", fDocument.get()); + assertEquals(s1, 9, 0); + assertEquals(t1, 2, 2); + assertEquals(e2, 7, 1); + doUndoRedo(undo, "0178234x69"); + } + + public void testMove5() throws Exception { + // Move onto itself + MoveSourceEdit s1= new MoveSourceEdit(2, 1); + MoveTargetEdit t1= new MoveTargetEdit(3, s1); + TextEdit e2= new ReplaceEdit(2, 1, "x"); + s1.addChild(e2); + fRoot.addChild(s1); + fRoot.addChild(t1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(s1, 2, 0); + assertEquals(t1, 2, 1); + assertEquals(e2, 2, 1); + assertEquals("Buffer content", "01x3456789", fDocument.get()); + doUndoRedo(undo, "01x3456789"); + } + + public void testMove6() throws Exception { + // Move onto itself + MoveSourceEdit s1= new MoveSourceEdit(2, 1); + MoveTargetEdit t1= new MoveTargetEdit(2, s1); + TextEdit e2= new ReplaceEdit(2, 1, "x"); + s1.addChild(e2); + fRoot.addChild(s1); + fRoot.addChild(t1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(s1, 3, 0); + assertEquals(t1, 2, 1); + assertEquals(e2, 2, 1); + assertEquals("Buffer content", "01x3456789", fDocument.get()); + doUndoRedo(undo,"01x3456789"); + } + + public void testMove7() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(2, 3); + MoveTargetEdit t1= new MoveTargetEdit(7, s1); + TextEdit e2= new ReplaceEdit(3, 1, "x"); + s1.addChild(e2); + fRoot.addChild(s1); + fRoot.addChild(t1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "01562x4789", fDocument.get()); + assertEquals(s1, 2, 0); + assertEquals(t1, 4, 3); + assertEquals(e2, 5, 1); + doUndoRedo(undo, "01562x4789"); + } + + public void testMove8() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(5, 3); + MoveTargetEdit t1= new MoveTargetEdit(1, s1); + TextEdit e2= new ReplaceEdit(6, 1, "x"); + s1.addChild(e2); + fRoot.addChild(s1); + fRoot.addChild(t1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "05x7123489", fDocument.get()); + assertEquals(s1, 8, 0); + assertEquals(t1, 1, 3); + assertEquals(e2, 2, 1); + doUndoRedo(undo, "05x7123489"); + } + + public void testMove9() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(1, 3); + MoveTargetEdit t1= new MoveTargetEdit(5, s1); + + MoveSourceEdit s2= new MoveSourceEdit(1, 1); + MoveTargetEdit t2= new MoveTargetEdit(3, s2); + s1.addChild(s2); + s1.addChild(t2); + + fRoot.addChild(s1); + fRoot.addChild(t1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(s1, 1, 0); + assertEquals(t1, 2, 3); + + assertEquals(s2, 2, 0); + assertEquals(t2, 3, 1); + assertEquals("Buffer content", "0421356789", fDocument.get()); + doUndoRedo(undo, "0421356789"); + } + + public void testMove10() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(2, 2); + MoveTargetEdit t1= new MoveTargetEdit(8, s1); + MoveSourceEdit s2= new MoveSourceEdit(5, 2); + MoveTargetEdit t2= new MoveTargetEdit(1, s2); + + fRoot.addChild(s1); + fRoot.addChild(t1); + fRoot.addChild(s2); + fRoot.addChild(t2); + + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(s1, 4, 0); + assertEquals(t1, 6, 2); + assertEquals(s2, 5, 0); + assertEquals(t2, 1, 2); + assertEquals("Buffer content", "0561472389", fDocument.get()); + doUndoRedo(undo, "0561472389"); + } + + public void testMoveWithRangeMarker() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(2, 2); + MoveTargetEdit t1= new MoveTargetEdit(5, s1); + + RangeMarker marker= new RangeMarker(2, 2); + s1.addChild(marker); + + fRoot.addChild(s1); + fRoot.addChild(t1); + + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "0142356789", fDocument.get()); + assertEquals(s1, 2, 0); + assertEquals(t1, 3, 2); + assertEquals(marker, 3, 2); + doUndoRedo(undo, "0142356789"); + } + + public void testMoveWithTargetDelete() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(2, 3); + MoveTargetEdit t1= new MoveTargetEdit(7, s1); + TextEdit e2= new DeleteEdit(6, 2); + e2.addChild(t1); + fRoot.addChild(s1); + fRoot.addChild(e2); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "01589", fDocument.get()); + assertEquals(s1, 2, 0); + assertTrue(t1.isDeleted()); + assertEquals(e2, 3, 0); + doUndoRedo(undo, "01589"); + } + + public void testMoveUpWithSourceDelete() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(5, 2); + MoveTargetEdit t1= new MoveTargetEdit(2, s1); + + TextEdit d1= new DeleteEdit(5, 2); + d1.addChild(s1); + + RangeMarker marker= new RangeMarker(5, 2); + s1.addChild(marker); + + fRoot.addChild(d1); + fRoot.addChild(t1); + + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "0156234789", fDocument.get()); + assertEquals(t1, 2, 2); + assertEquals(marker, 2, 2); + assertTrue(s1.isDeleted()); + assertEquals(d1, 7, 0); + doUndoRedo(undo, "0156234789"); + } + + public void testMoveDown() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(2, 2); + TextEdit i1= new InsertEdit(5, "x"); + MoveTargetEdit t1= new MoveTargetEdit(7, s1); + TextEdit d1= new DeleteEdit(9, 1); + + RangeMarker m1= new RangeMarker(2, 2); + s1.addChild(m1); + + fRoot.addChild(s1); + fRoot.addChild(i1); + fRoot.addChild(t1); + fRoot.addChild(d1); + + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "014x562378", fDocument.get()); + assertEquals(s1, 2, 0); + assertEquals(i1, 3, 1); + assertEquals(t1, 6, 2); + assertEquals(m1, 6, 2); + assertEquals(d1, 10, 0); + doUndoRedo(undo, "014x562378"); + } + + public void testMoveUp() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(7, 2); + MoveTargetEdit t1= new MoveTargetEdit(2, s1); + TextEdit i1= new InsertEdit(5, "x"); + TextEdit d1= new DeleteEdit(9, 1); + + RangeMarker m1= new RangeMarker(7, 2); + s1.addChild(m1); + + fRoot.addChild(s1); + fRoot.addChild(i1); + fRoot.addChild(t1); + fRoot.addChild(d1); + + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "0178234x56", fDocument.get()); + assertEquals(s1, 10, 0); + assertEquals(i1, 7, 1); + assertEquals(t1, 2, 2); + assertEquals(m1, 2, 2); + assertEquals(d1, 10, 0); + doUndoRedo(undo, "0178234x56"); + } + + public void testMoveDownWithSourceDelete() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(2, 2); + MoveTargetEdit t1= new MoveTargetEdit(7, s1); + + TextEdit d1= new DeleteEdit(2, 2); + d1.addChild(s1); + + RangeMarker m1= new RangeMarker(2, 2); + s1.addChild(m1); + + fRoot.addChild(t1); + fRoot.addChild(d1); + + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "0145623789", fDocument.get()); + assertEquals(d1, 2, 0); + assertTrue(s1.isDeleted()); + assertEquals(t1, 5, 2); + assertEquals(m1, 5, 2); + doUndoRedo(undo, "0145623789"); + } + + public void testMoveUpWithInnerMark() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(7, 2); + MoveTargetEdit t1= new MoveTargetEdit(2, s1); + TextEdit m= new ReplaceEdit(4, 1, "yy"); + fRoot.addChild(t1); + fRoot.addChild(m); + fRoot.addChild(s1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "017823yy569", fDocument.get()); + assertEquals(s1, 10, 0); + assertEquals(t1, 2, 2); + assertEquals(m, 6, 2); + doUndoRedo(undo, "017823yy569"); + } + + public void testMoveDownWithInnerMark() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(2, 2); + MoveTargetEdit t1= new MoveTargetEdit(7, s1); + TextEdit m= new ReplaceEdit(4, 1, "yy"); + fRoot.addChild(t1); + fRoot.addChild(m); + fRoot.addChild(s1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "01yy5623789", fDocument.get()); + assertEquals(s1, 2, 0); + assertEquals(t1, 6, 2); + assertEquals(m, 2, 2); + doUndoRedo(undo, "01yy5623789"); + } + + public void testMoveUpWithParentMark() throws Exception { + RangeMarker m= new RangeMarker(2, 6); + MoveSourceEdit s1= new MoveSourceEdit(4, 2); + MoveTargetEdit t1= new MoveTargetEdit(3, s1); + m.addChild(s1); + m.addChild(t1); + fRoot.addChild(m); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "0124536789", fDocument.get()); + assertEquals(m, 2, 6); + assertEquals(t1, 3, 2); + assertEquals(s1, 6, 0); + doUndoRedo(undo, "0124536789"); + } + + public void testMoveDownWithParentMark() throws Exception { + RangeMarker m= new RangeMarker(2, 6); + MoveSourceEdit s1= new MoveSourceEdit(2, 2); + MoveTargetEdit t1= new MoveTargetEdit(5, s1); + m.addChild(s1); + m.addChild(t1); + fRoot.addChild(m); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals("Buffer content", "0142356789", fDocument.get()); + assertEquals(m, 2, 6); + assertEquals(t1, 3, 2); + assertEquals(s1, 2, 0); + doUndoRedo(undo, "0142356789"); + } + + public void testNestedMoveSource() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(1, 5); + MoveSourceEdit s2= new MoveSourceEdit(2, 3); + MoveSourceEdit s3= new MoveSourceEdit(3, 1); + s1.addChild(s2); + s2.addChild(s3); + MoveTargetEdit t1= new MoveTargetEdit(9, s1); + MoveTargetEdit t2= new MoveTargetEdit(8, s2); + MoveTargetEdit t3= new MoveTargetEdit(7, s3); + fRoot.addChild(s1); + fRoot.addChild(t1); + fRoot.addChild(t2); + fRoot.addChild(t3); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(s1, 1, 0); + assertEquals(s2, 8, 0); + assertEquals(s3, 5, 0); + assertEquals(t1, 7, 2); + assertEquals(t2, 4, 2); + assertEquals(t3, 2, 1); + String result= "0637248159"; + assertEquals("Buffer content", result, fDocument.get()); + doUndoRedo(undo, result); + } + + public void testNestedMoveTarget() throws Exception { + MoveSourceEdit s1= new MoveSourceEdit(1, 2); + MoveSourceEdit s2= new MoveSourceEdit(5, 3); + MoveTargetEdit t1= new MoveTargetEdit(6, s1); + MoveTargetEdit t2= new MoveTargetEdit(9, s2); + s2.addChild(t1); + fRoot.addChild(s1); + fRoot.addChild(s2); + fRoot.addChild(t2); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(s1, 1, 0); + assertEquals(s2, 3, 0); + assertEquals(t1, 5, 2); + assertEquals(t2, 4, 5); + String result= "0348512679"; + assertEquals("Buffer content", result, fDocument.get()); + doUndoRedo(undo, result); + } + + public void testCopyDown() throws Exception { + CopySourceEdit s1= new CopySourceEdit(2, 3); + CopyTargetEdit t1= new CopyTargetEdit(8, s1); + + fRoot.addChild(s1); + fRoot.addChild(t1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(s1, 2, 3); + assertEquals(t1, 8, 3); + String result= "0123456723489"; + assertEquals("Buffer content", result, fDocument.get()); + doUndoRedo(undo, result); + } + + public void testCopyUp() throws Exception { + CopySourceEdit s1= new CopySourceEdit(7, 2); + CopyTargetEdit t1= new CopyTargetEdit(3, s1); + + fRoot.addChild(s1); + fRoot.addChild(t1); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(s1, 9, 2); + assertEquals(t1, 3, 2); + String result= "012783456789"; + assertEquals("Buffer content", result, fDocument.get()); + doUndoRedo(undo, result); + } + + public void testDoubleCopy() throws Exception { + CopySourceEdit s1= new CopySourceEdit(5, 2); + CopyTargetEdit t1= new CopyTargetEdit(8, s1); + CopySourceEdit s2= new CopySourceEdit(5, 2); + CopyTargetEdit t2= new CopyTargetEdit(2, s2); + s1.addChild(s2); + + fRoot.addChild(s1); + fRoot.addChild(t1); + fRoot.addChild(t2); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(s1, 7, 2); + assertEquals(t1, 10, 2); + assertEquals(s2, 7, 2); + assertEquals(t2, 2, 2); + String result= "01562345675689"; + assertEquals("Buffer content", result, fDocument.get()); + doUndoRedo(undo, result); + } + + public void testNestedCopySource() throws Exception { + CopySourceEdit s1= new CopySourceEdit(1, 5); + CopySourceEdit s2= new CopySourceEdit(2, 3); + CopySourceEdit s3= new CopySourceEdit(3, 1); + s1.addChild(s2); + s2.addChild(s3); + CopyTargetEdit t1= new CopyTargetEdit(9, s1); + CopyTargetEdit t2= new CopyTargetEdit(8, s2); + CopyTargetEdit t3= new CopyTargetEdit(7, s3); + fRoot.addChild(s1); + fRoot.addChild(t1); + fRoot.addChild(t2); + fRoot.addChild(t3); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(s1, 1, 5); + assertEquals(s2, 2, 3); + assertEquals(s3, 3, 1); + assertEquals(t1, 13, 5); + assertEquals(t2, 9, 3); + assertEquals(t3, 7, 1); + String result= "0123456372348123459"; + assertEquals("Buffer content", result, fDocument.get()); + doUndoRedo(undo, result); + } + + public void testNestedCopyTarget() throws Exception { + CopySourceEdit s1= new CopySourceEdit(1, 2); + CopySourceEdit s2= new CopySourceEdit(5, 3); + CopyTargetEdit t1= new CopyTargetEdit(6, s1); + CopyTargetEdit t2= new CopyTargetEdit(9, s2); + s2.addChild(t1); + fRoot.addChild(s1); + fRoot.addChild(s2); + fRoot.addChild(t2); + UndoEdit undo= fRoot.apply(fDocument); + assertEquals(s1, 1, 2); + assertEquals(s2, 5, 5); + assertEquals(t1, 6, 2); + assertEquals(t2, 11, 5); + String result= "01234512678512679"; + assertEquals("Buffer content", result, fDocument.get()); + doUndoRedo(undo, result); + } + + public void testSwap1() throws Exception { + IDocument document= new Document("foo(1, 2), 3"); + + MultiTextEdit root= new MultiTextEdit(); + { + CopySourceEdit innerRoot= new CopySourceEdit(0, 9); + + TextEdit e1= new ReplaceEdit(0, 9, ""); + e1.addChild(innerRoot); + CopyTargetEdit t1= new CopyTargetEdit(11, innerRoot); + + TextEdit e2= new ReplaceEdit(11, 1, ""); + CopySourceEdit s2= new CopySourceEdit(11, 1); + e2.addChild(s2); + CopyTargetEdit t2= new CopyTargetEdit(0, s2); + + root.addChild(e1); + root.addChild(t2); + root.addChild(e2); + root.addChild(t1); + } + + root.apply(document); + + String result= "3, foo(1, 2)"; + assertEquals("Buffer content", result, document.get()); + } + + public void testSwap2() throws Exception { + IDocument document= new Document("foo(1, 2), 3"); + + MultiTextEdit root= new MultiTextEdit(); + { + TextEdit e1= new ReplaceEdit(4, 1, ""); + CopySourceEdit s1= new CopySourceEdit(4, 1); + e1.addChild(s1); + CopyTargetEdit t1= new CopyTargetEdit(7, s1); + + TextEdit e2= new ReplaceEdit(7, 1, ""); + CopySourceEdit s2= new CopySourceEdit(7, 1); + e2.addChild(s2); + CopyTargetEdit t2= new CopyTargetEdit(4, s2); + + root.addChild(e1); + root.addChild(t2); + root.addChild(e2); + root.addChild(t1); + } + + root.apply(document); + + String result= "foo(2, 1), 3"; + assertEquals("Buffer content", result, document.get()); + } + + public void testSwap2InSwap1() throws Exception { + IDocument document= new Document("foo(1, 2), 3"); + + CopySourceEdit innerRoot= new CopySourceEdit(0, 9); + { + TextEdit e1= new ReplaceEdit(4, 1, ""); + CopySourceEdit s1= new CopySourceEdit(4, 1); + e1.addChild(s1); + CopyTargetEdit t1= new CopyTargetEdit(7, s1); + + TextEdit e2= new ReplaceEdit(7, 1, ""); + CopySourceEdit s2= new CopySourceEdit(7, 1); + e2.addChild(s2); + CopyTargetEdit t2= new CopyTargetEdit(4, s2); + + innerRoot.addChild(e1); + innerRoot.addChild(t2); + innerRoot.addChild(e2); + innerRoot.addChild(t1); + } + MultiTextEdit root= new MultiTextEdit(); + { + TextEdit e1= new ReplaceEdit(0, 9, ""); + e1.addChild(innerRoot); + CopyTargetEdit t1= new CopyTargetEdit(11, innerRoot); + + TextEdit e2= new ReplaceEdit(11, 1, ""); + CopySourceEdit s2= new CopySourceEdit(11, 1); + e2.addChild(s2); + CopyTargetEdit t2= new CopyTargetEdit(0, s2); + + root.addChild(e1); + root.addChild(t2); + root.addChild(e2); + root.addChild(t1); + } + + root.apply(document); + + String result= "3, foo(2, 1)"; + assertEquals("Buffer content", result, document.get()); + } + + private void doUndoRedo(UndoEdit undo, String redoResult) throws Exception { + UndoEdit redo= undo.apply(fDocument); + assertBufferContent(); + undo= redo.apply(fDocument); + assertEquals("Buffer content redo", redoResult, fDocument.get()); + undo.apply(fDocument); + assertBufferContent(); + } + + private void assertEquals(TextEdit edit, int offset, int length) { + assertEquals("Offset", offset, edit.getOffset()); + assertEquals("Length", length, edit.getLength()); + } + + private void assertBufferContent() { + assertEquals("Buffer content restored", "0123456789", fDocument.get()); + } +} + diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/link/ExclusivePositionUpdaterTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/link/ExclusivePositionUpdaterTest.java new file mode 100644 index 00000000000..5348813c5e3 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/link/ExclusivePositionUpdaterTest.java @@ -0,0 +1,128 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests.link; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.Position; +import org.eclipse.jface.text.link.ExclusivePositionUpdater; + +public class ExclusivePositionUpdaterTest extends TestCase { + + private ExclusivePositionUpdater fUpdater; + private static final String CATEGORY= "testcategory"; + private Position fPos; + private IDocument fDoc; + /* + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + fUpdater= new ExclusivePositionUpdater(CATEGORY); + fDoc= new Document("ccccccccccccccccccccccccccccccccccccccccccccc"); + fPos= new Position(5, 5); + fDoc.addPositionUpdater(fUpdater); + fDoc.addPositionCategory(CATEGORY); + fDoc.addPosition(CATEGORY, fPos); + } + + public void testDeleteAfter() throws BadLocationException { + fDoc.replace(20, 2, null); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testAddAfter() throws BadLocationException { + fDoc.replace(20, 0, "yy"); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testDeleteBefore() throws BadLocationException { + fDoc.replace(2, 2, null); + Assert.assertEquals(3, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testAddBefore() throws BadLocationException { + fDoc.replace(2, 0, "yy"); + Assert.assertEquals(7, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testAddRightBefore() throws BadLocationException { + fDoc.replace(5, 0, "yy"); + Assert.assertEquals(7, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testDeleteRightBefore() throws BadLocationException { + fDoc.replace(5, 2, ""); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(3, fPos.length); + } + + public void testAddRightAfter() throws BadLocationException { + fDoc.replace(10, 0, "yy"); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testDeleteRightAfter() throws BadLocationException { + fDoc.replace(10, 2, ""); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testAddWithin() throws BadLocationException { + fDoc.replace(6, 0, "yy"); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(7, fPos.length); + } + + public void testDeleteWithin() throws BadLocationException { + fDoc.replace(6, 2, ""); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(3, fPos.length); + } + + public void testReplaceLeftBorder() throws BadLocationException { + fDoc.replace(4, 2, "yy"); + Assert.assertEquals(6, fPos.offset); + Assert.assertEquals(4, fPos.length); + } + + public void testReplaceRightBorder() throws BadLocationException { + fDoc.replace(9, 2, "yy"); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(4, fPos.length); + } + + public void testDeleteOverRightBorder() throws BadLocationException { + fDoc.replace(9, 2, ""); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(4, fPos.length); + } + + public void testDeleted() throws BadLocationException { + fDoc.replace(4, 7, null); + Assert.assertTrue(fPos.isDeleted); + } + + public void testReplaced() throws BadLocationException { + fDoc.replace(4, 7, "yyyyyyy"); + Assert.assertTrue(fPos.isDeleted); + } + +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/link/InclusivePositionUpdaterTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/link/InclusivePositionUpdaterTest.java new file mode 100644 index 00000000000..6fd987c1662 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/link/InclusivePositionUpdaterTest.java @@ -0,0 +1,142 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests.link; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.Position; +import org.eclipse.jface.text.link.InclusivePositionUpdater; + +public class InclusivePositionUpdaterTest extends TestCase { + + private InclusivePositionUpdater fUpdater; + private static final String CATEGORY= "testcategory"; + private Position fPos; + private IDocument fDoc; + /* + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + fUpdater= new InclusivePositionUpdater(CATEGORY); + fDoc= new Document("ccccccccccccccccccccccccccccccccccccccccccccc"); + fPos= new Position(5, 5); + fDoc.addPositionUpdater(fUpdater); + fDoc.addPositionCategory(CATEGORY); + fDoc.addPosition(CATEGORY, fPos); + } + + /* + * @see junit.framework.TestCase#tearDown() + */ + protected void tearDown() throws Exception { + fDoc.removePositionUpdater(fUpdater); + fDoc.removePositionCategory(CATEGORY); + } + + public void testDeleteAfter() throws BadLocationException { + fDoc.replace(20, 2, null); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testAddAfter() throws BadLocationException { + fDoc.replace(20, 0, "yy"); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testDeleteBefore() throws BadLocationException { + fDoc.replace(2, 2, null); + Assert.assertEquals(3, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testAddBefore() throws BadLocationException { + fDoc.replace(2, 0, "yy"); + Assert.assertEquals(7, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testAddRightBefore() throws BadLocationException { + fDoc.replace(5, 0, "yy"); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(7, fPos.length); + } + + public void testDeleteAtOffset() throws BadLocationException { + fDoc.replace(5, 2, ""); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(3, fPos.length); + } + + public void testDeleteRightBefore() throws BadLocationException { + fDoc.replace(3, 2, ""); + Assert.assertEquals(3, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testAddRightAfter() throws BadLocationException { + fDoc.replace(10, 0, "yy"); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(7, fPos.length); + } + + public void testDeleteRightAfter() throws BadLocationException { + fDoc.replace(10, 2, ""); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(5, fPos.length); + } + + public void testAddWithin() throws BadLocationException { + fDoc.replace(6, 0, "yy"); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(7, fPos.length); + } + + public void testDeleteWithin() throws BadLocationException { + fDoc.replace(6, 2, ""); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(3, fPos.length); + } + + public void testReplaceLeftBorder() throws BadLocationException { + fDoc.replace(4, 2, "yy"); + Assert.assertEquals(4, fPos.offset); + Assert.assertEquals(6, fPos.length); + } + + public void testReplaceRightBorder() throws BadLocationException { + fDoc.replace(9, 2, "yy"); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(6, fPos.length); + } + + public void testDeleteOverRightBorder() throws BadLocationException { + fDoc.replace(9, 2, ""); + Assert.assertEquals(5, fPos.offset); + Assert.assertEquals(4, fPos.length); + } + + public void testDeleted() throws BadLocationException { + fDoc.replace(4, 7, null); + Assert.assertTrue(fPos.isDeleted); + } + + public void testReplaced() throws BadLocationException { + fDoc.replace(4, 7, "yyyyyyy"); + Assert.assertTrue(fPos.isDeleted); + } + +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedEnvironmentTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedEnvironmentTest.java new file mode 100644 index 00000000000..550289bb003 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedEnvironmentTest.java @@ -0,0 +1,679 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests.link; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import junit.framework.TestCase; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.link.ILinkedListener; +import org.eclipse.jface.text.link.LinkedEnvironment; +import org.eclipse.jface.text.link.LinkedPosition; +import org.eclipse.jface.text.link.LinkedPositionGroup; + + +public class LinkedEnvironmentTest extends TestCase { + + private List fPositions= new LinkedList(); + + private List fDocumentMap= new ArrayList(); + + public void testUpdate() throws BadLocationException { + IDocument doc1= new Document(GARTEN1); + + // set up linked mode + LinkedPositionGroup group1= new LinkedPositionGroup(); + createLinkedPositions(group1, doc1, "MARGARETE"); + LinkedEnvironment env= new LinkedEnvironment(); + env.addGroup(group1); + env.forceInstall(); + + // edit the document + doc1.replace(1, 9, "GRETCHEN"); + + assertEquals(group1, "GRETCHEN"); + assertUnchanged(group1); + } + + public void testUpdateTwoGroups() throws BadLocationException { + IDocument doc1= new Document(GARTEN1); + + // set up linked mode + LinkedPositionGroup group1= new LinkedPositionGroup(); + createLinkedPositions(group1, doc1, "MARGARETE"); + + LinkedPositionGroup group2= new LinkedPositionGroup(); + createLinkedPositions(group2, doc1, "FAUST"); + + LinkedEnvironment env= new LinkedEnvironment(); + env.addGroup(group1); + env.addGroup(group2); + + env.forceInstall(); + + + // edit the document + doc1.replace(7, 3, "INE"); + + assertEquals(group1, "MARGARINE"); + assertEquals(group2, "FAUST"); + assertUnchanged(group1, group2); + } + + public void testUpdateMultipleGroups() throws BadLocationException { + IDocument doc1= new Document(GARTEN1); + + // set up linked mode + LinkedPositionGroup group1= new LinkedPositionGroup(); + createLinkedPositions(group1, doc1, "MARGARETE"); + + LinkedPositionGroup group2= new LinkedPositionGroup(); + createLinkedPositions(group2, doc1, "FAUST"); + + LinkedEnvironment env= new LinkedEnvironment(); + env.addGroup(group1); + env.addGroup(group2); + + env.forceInstall(); + + + // edit the document + doc1.replace(7, 3, "INE"); + doc1.replace(42, 1, ""); + doc1.replace(44, 2, "GE"); + + assertEquals(group1, "MARGARINE"); + assertEquals(group2, "AUGE"); + assertUnchanged(group1, group2); + } + + public void testUpdateMultiDocument() throws BadLocationException { + IDocument doc1= new Document(GARTEN1); + IDocument doc2= new Document(GARTEN2); + + // set up linked mode + LinkedPositionGroup group1= new LinkedPositionGroup(); + createLinkedPositions(group1, doc1, "MARGARETE"); + createLinkedPositions(group1, doc2, "MARGARETE"); + + LinkedPositionGroup group2= new LinkedPositionGroup(); + createLinkedPositions(group2, doc1, "FAUST"); + createLinkedPositions(group2, doc2, "FAUST"); + + LinkedEnvironment env= new LinkedEnvironment(); + env.addGroup(group1); + env.addGroup(group2); + + env.forceInstall(); + + + // edit the document + doc1.replace(7, 3, "INE"); + doc1.replace(42, 1, ""); + doc1.replace(44, 2, "GE"); + + assertEquals(group1, "MARGARINE"); + assertEquals(group2, "AUGE"); + assertUnchanged(group1, group2); + + } + + public void testAddCompatibleGroups() throws BadLocationException { + IDocument doc1= new Document(GARTEN1); + + // set up linked mode + LinkedPositionGroup group1= new LinkedPositionGroup(); + createLinkedPositions(group1, doc1, "MARGARETE"); + + LinkedPositionGroup group2= new LinkedPositionGroup(); + createLinkedPositions(group2, doc1, "FAUST"); + + LinkedEnvironment env= new LinkedEnvironment(); + try { + env.addGroup(group1); + env.addGroup(group2); + } catch (BadLocationException e) { + assertFalse(true); + } + assertUnchanged(group1, group2); + + } + + public void testAddIncompatibleGroups() throws BadLocationException { + IDocument doc1= new Document(GARTEN1); + + // set up linked mode + LinkedPositionGroup group1= new LinkedPositionGroup(); + createLinkedPositions(group1, doc1, "MARGARETE"); + + LinkedPositionGroup group2= new LinkedPositionGroup(); + createLinkedPositions(group2, doc1, "MARGA"); + + LinkedEnvironment env= new LinkedEnvironment(); + try { + env.addGroup(group1); + env.addGroup(group2); + } catch (BadLocationException e) { + return; + } + assertFalse(true); + } + + public void testAddNullGroup() throws BadLocationException { + LinkedEnvironment env= new LinkedEnvironment(); + try { + env.addGroup(null); + } catch (IllegalArgumentException e) { + return; + } + + assertFalse(true); + } + + public void testAddGroupWhenSealed() throws BadLocationException { + IDocument doc1= new Document(GARTEN1); + + // set up linked mode + LinkedPositionGroup group1= new LinkedPositionGroup(); + createLinkedPositions(group1, doc1, "MARGARETE"); + LinkedEnvironment env= new LinkedEnvironment(); + env.addGroup(group1); + env.forceInstall(); + + LinkedPositionGroup group2= new LinkedPositionGroup(); + createLinkedPositions(group2, doc1, "FAUST"); + try { + env.addGroup(group2); + } catch (IllegalStateException e) { + return; + } + + assertFalse(true); + } + + public void testDoubleInstall() throws BadLocationException { + IDocument doc1= new Document(GARTEN1); + + // set up linked mode + LinkedPositionGroup group1= new LinkedPositionGroup(); + createLinkedPositions(group1, doc1, "MARGARETE"); + LinkedEnvironment env= new LinkedEnvironment(); + env.addGroup(group1); + + env.forceInstall(); + + try { + env.forceInstall(); + } catch (IllegalStateException e) { + return; + } + + assertFalse(true); + } + + public void testEmptyInstall() throws BadLocationException { + LinkedEnvironment env= new LinkedEnvironment(); + + try { + env.forceInstall(); + } catch (IllegalStateException e) { + return; + } + + assertFalse(true); + } + + public void testNestedUpdate() throws BadLocationException { + IDocument doc1= new Document(GARTEN1); + + // set up linked mode + LinkedPositionGroup group1= new LinkedPositionGroup(); + createLinkedPositions(group1, doc1, "MARGARETE"); + + LinkedPositionGroup group2= new LinkedPositionGroup(); + createLinkedPositions(group2, doc1, "FAUST"); + + LinkedEnvironment env= new LinkedEnvironment(); + env.addGroup(group1); + env.addGroup(group2); + + env.forceInstall(); + + // second level + + LinkedPositionGroup group1_2= new LinkedPositionGroup(); + group1_2.createPosition(doc1, 7, 3); + + + LinkedEnvironment childEnv= new LinkedEnvironment(); + childEnv.addGroup(group1_2); + childEnv.forceInstall(); + + assertTrue(childEnv.isNested()); + assertFalse(env.isNested()); + + + // edit the document + doc1.replace(7, 3, "INE"); + + assertEquals(group1_2, "INE"); + assertEquals(group1, "MARGARINE"); + assertEquals(group2, "FAUST"); + assertUnchanged(group1, group2); + } + + public void testNestedForceInstall() throws BadLocationException { + IDocument doc1= new Document(GARTEN1); + + // set up linked mode + LinkedPositionGroup group1= new LinkedPositionGroup(); + createLinkedPositions(group1, doc1, "MARGARETE"); + + LinkedPositionGroup group2= new LinkedPositionGroup(); + createLinkedPositions(group2, doc1, "FAUST"); + + LinkedEnvironment env= new LinkedEnvironment(); + env.addGroup(group1); + env.addGroup(group2); + + final boolean[] isExit= { false } ; + env.addLinkedListener(new LinkedAdapter() { + public void left(LinkedEnvironment environment, int flags) { + isExit[0]= true; + } + }); + + env.forceInstall(); + + + // second level + + LinkedPositionGroup group1_2= new LinkedPositionGroup(); + + group1_2.createPosition(doc1, 12, 3); + + LinkedEnvironment childEnv= new LinkedEnvironment(); + childEnv.addGroup(group1_2); + childEnv.forceInstall(); + + assertFalse(childEnv.isNested()); + assertTrue(isExit[0]); + + + // edit the document + doc1.replace(12, 3, "INE"); + + assertEquals(group1_2, "INE"); + } + + public void testNestedTryInstall() throws BadLocationException { + IDocument doc1= new Document(GARTEN1); + + // set up linked mode + LinkedPositionGroup group1= new LinkedPositionGroup(); + createLinkedPositions(group1, doc1, "MARGARETE"); + + LinkedPositionGroup group2= new LinkedPositionGroup(); + createLinkedPositions(group2, doc1, "FAUST"); + + LinkedEnvironment env= new LinkedEnvironment(); + env.addGroup(group1); + env.addGroup(group2); + env.forceInstall(); + + + // second level + + LinkedPositionGroup group1_2= new LinkedPositionGroup(); + group1_2.createPosition(doc1, 12, 3); + + LinkedEnvironment childEnv= new LinkedEnvironment(); + childEnv.addGroup(group1_2); + + final boolean[] isExit= { false } ; + env.addLinkedListener(new LinkedAdapter() { + public void left(LinkedEnvironment environment, int flags) { + isExit[0]= true; + } + }); + + assertFalse(childEnv.tryInstall()); + assertFalse(childEnv.isNested()); + + + // edit the document + doc1.replace(7, 3, "INE"); + + assertEquals(group1, "MARGARINE"); + assertUnchanged(group1, group2); + } + + private void assertEquals(LinkedPositionGroup group, String expected) throws BadLocationException { + LinkedPosition[] positions= group.getPositions(); + for (int i= 0; i < positions.length; i++) { + LinkedPosition pos= positions[i]; + assertEquals(expected, pos.getContent()); + } + } + + private void assertUnchanged(LinkedPositionGroup actual1) throws BadLocationException { + assertUnchanged(actual1, new LinkedPositionGroup()); + } + + private void assertUnchanged(LinkedPositionGroup actual1, LinkedPositionGroup actual2) throws BadLocationException { + LinkedPosition[] exp= (LinkedPosition[]) fPositions.toArray(new LinkedPosition[0]); + LinkedPosition[] act1= actual1.getPositions(); + LinkedPosition[] act2= actual2.getPositions(); + LinkedPosition[] act= new LinkedPosition[act1.length + act2.length]; + System.arraycopy(act1, 0, act, 0, act1.length); + System.arraycopy(act2, 0, act, act1.length, act2.length); + Arrays.sort(act, new PositionComparator()); + Arrays.sort(exp, new PositionComparator()); + + assertEquals(exp.length, act.length); + + LinkedPosition e_prev= null, a_prev= null; + for (int i= 0; i <= exp.length; i++) { + LinkedPosition e_next= i == exp.length ? null : exp[i]; + LinkedPosition a_next= i == exp.length ? null : act[i]; + + IDocument e_doc= e_prev != null ? e_prev.getDocument() : e_next.getDocument(); + if (e_next != null && e_next.getDocument() != e_doc) { + // split at document boundaries + assertEquals(getContentBetweenPositions(e_prev, null), getContentBetweenPositions(a_prev, null)); + assertEquals(getContentBetweenPositions(null, e_next), getContentBetweenPositions(null, a_next)); + } else { + assertEquals(getContentBetweenPositions(e_prev, e_next), getContentBetweenPositions(a_prev, a_next)); + } + + e_prev= e_next; + a_prev= a_next; + } + } + + private String getContentBetweenPositions(LinkedPosition p1, LinkedPosition p2) throws BadLocationException { + if (p1 == null && p2 == null) + return null; + if (p1 == null) + p1= new LinkedPosition(p2.getDocument(), 0, 0); + + if (p2 == null) + p2= new LinkedPosition(p1.getDocument(), p1.getDocument().getLength(), 0); + + IDocument document= p1.getDocument(); + + int offset= p1.getOffset() + p1.getLength(); + int length= p2.getOffset() - offset; + + return document.get(offset, length); + } + + + /** + * {@inheritDoc} + */ + protected void setUp() throws Exception { + fPositions.clear(); + fDocumentMap.clear(); + } + + /** + * Returns a test group on a copy of the document + */ + private void createLinkedPositions(LinkedPositionGroup group, IDocument doc, String substring) throws BadLocationException { + String text= doc.get(); + + IDocument original= getOriginal(doc); + if (original == null) { + original= new Document(text); + putOriginal(doc, original); + } + + + for (int offset= text.indexOf(substring); offset != -1; offset= text.indexOf(substring, offset + 1)) { + group.createPosition(doc, offset, substring.length()); + fPositions.add(new LinkedPosition(original, offset, substring.length())); + } + + } + + private void putOriginal(IDocument doc, IDocument original) { + fDocumentMap.add(new IDocument[] { doc, original }); + } + + private IDocument getOriginal(IDocument doc) { + for (Iterator it = fDocumentMap.iterator(); it.hasNext(); ) { + IDocument[] docs = (IDocument[]) it.next(); + if (docs[0] == doc) + return docs[1]; + } + return null; + } + + private static final String GARTEN1= + " MARGARETE:\n" + + " Versprich mir, Heinrich!\n" + + " \n" + + " FAUST:\n" + + " Was ich kann!\n" + + " \n" + + " MARGARETE:\n" + + " Nun sag, wie hast du\'s mit der Religion?\n" + + " Du bist ein herzlich guter Mann,\n" + + " Allein ich glaub, du hältst nicht viel davon.\n" + + " \n" + + " FAUST:\n" + + " Laß das, mein Kind! Du fühlst, ich bin dir gut;\n" + + " Für meine Lieben ließ\' ich Leib und Blut,\n" + + " Will niemand sein Gefühl und seine Kirche rauben.\n" + + " \n" + + " MARGARETE:\n" + + " Das ist nicht recht, man muß dran glauben.\n" + + " \n" + + " FAUST:\n" + + " Muß man?\n" + + " \n" + + " MARGARETE:\n" + + " Ach! wenn ich etwas auf dich konnte! Du ehrst auch nicht die heil\'gen Sakramente.\n" + + " \n" + + " FAUST:\n" + + " Ich ehre sie.\n" + + " \n" + + " MARGARETE:\n" + + " Doch ohne Verlangen. Zur Messe, zur Beichte bist du lange nicht gegangen.\n" + + " Glaubst du an Gott?\n" + + " \n" + + " FAUST:\n" + + " Mein Liebchen, wer darf sagen: Ich glaub an Gott?\n" + + " Magst Priester oder Weise fragen,\n" + + " Und ihre Antwort scheint nur Spott\n" + + " Über den Frager zu sein.\n" + + " \n" + + " MARGARETE:\n" + + " So glaubst du nicht?\n" + + " \n" + + " FAUST:\n" + + " Mißhör mich nicht, du holdes Angesicht!\n" + + " Wer darf ihn nennen?\n" + + " Und wer bekennen:\n" + + " »Ich glaub ihn!«?\n" + + " Wer empfinden,\n" + + " Und sich unterwinden\n" + + " Zu sagen: »Ich glaub ihn nicht!«?\n" + + " Der Allumfasser,\n" + + " Der Allerhalter,\n" + + " Faßt und erhält er nicht\n" + + " Dich, mich, sich selbst?\n" + + " Wölbt sich der Himmel nicht da droben?\n" + + " Liegt die Erde nicht hier unten fest?\n" + + " Und steigen freundlich blickend\n" + + " Ewige Sterne nicht herauf?\n" + + " Schau ich nicht Aug in Auge dir,\n" + + " Und drängt nicht alles\n" + + " Nach Haupt und Herzen dir,\n" + + " Und webt in ewigem Geheimnis\n" + + " Unsichtbar sichtbar neben dir?\n" + + " Erfüll davon dein Herz, so groß es ist,\n" + + " Und wenn du ganz in dem Gefühle selig bist,\n" + + " Nenn es dann, wie du willst,\n" + + " Nenn\'s Glück! Herz! Liebe! Gott\n" + + " Ich habe keinen Namen\n" + + " Dafür! Gefühl ist alles;\n" + + " Name ist Schall und Rauch,\n" + + " Umnebelnd Himmelsglut.\n"; + + private static final String GARTEN2= + " MARGARETE:\n" + + " Das ist alles recht schön und gut;\n" + + " Ungefähr sagt das der Pfarrer auch,\n" + + " Nur mit ein bißchen andern Worten.\n" + + " \n" + + " FAUST:\n" + + " Es sagen\'s allerorten\n" + + " Alle Herzen unter dem himmlischen Tage,\n" + + " Jedes in seiner Sprache;\n" + + " Warum nicht ich in der meinen?\n" + + " \n" + + " MARGARETE:\n" + + " Wenn man\'s so hört, möcht\'s leidlich scheinen,\n" + + " Steht aber doch immer schief darum;\n" + + " Denn du hast kein Christentum.\n" + + " \n" + + " FAUST:\n" + + " Liebs Kind!\n" + + " \n" + + " MARGARETE:\n" + + " Es tut mir lange schon weh, Daß ich dich in der Gesellschaft seh.\n" + + " \n" + + " FAUST:\n" + + " Wieso?\n" + + " \n" + + " MARGARETE:\n" + + " Der Mensch, den du da bei dir hast, Ist mir in tiefer innrer Seele verhaßt;\n" + + " Es hat mir in meinem Leben\n" + + " So nichts einen Stich ins Herz gegeben\n" + + " Als des Menschen widrig Gesicht.\n" + + " \n" + + " FAUST:\n" + + " Liebe Puppe, fürcht ihn nicht!\n" + + " \n" + + " MARGARETE:\n" + + " Seine Gegenwart bewegt mir das Blut.\n" + + " Ich bin sonst allen Menschen gut;\n" + + " Aber wie ich mich sehne, dich zu schauen,\n" + + " Hab ich vor dem Menschen ein heimlich Grauen,\n" + + " Und halt ihn für einen Schelm dazu!\n" + + " Gott verzeih mir\'s, wenn ich ihm unrecht tu!\n" + + " \n" + + " FAUST:\n" + + " Es muß auch solche Käuze geben.\n" + + " \n" + + " MARGARETE:\n" + + " Wollte nicht mit seinesgleichen leben!\n" + + " Kommt er einmal zur Tür herein,\n" + + " Sieht er immer so spöttisch drein\n" + + " Und halb ergrimmt;\n" + + " Man sieht, daß er an nichts keinen Anteil nimmt;\n" + + " Es steht ihm an der Stirn geschrieben,\n" + + " Daß er nicht mag eine Seele lieben.\n" + + " Mir wird\'s so wohl in deinem Arm,\n" + + " So frei, so hingegeben warm,\n" + + " Und seine Gegenwart schnürt mir das Innre zu.\n" + + " \n" + + " FAUST:\n" + + " Du ahnungsvoller Engel du!\n" + + " \n" + + " MARGARETE:\n" + + " Das übermannt mich so sehr,\n" + + " Daß, wo er nur mag zu uns treten,\n" + + " Mein ich sogar, ich liebte dich nicht mehr.\n" + + " Auch, wenn er da ist, könnt ich nimmer beten,\n" + + " Und das frißt mir ins Herz hinein;\n" + + " Dir, Heinrich, muß es auch so sein.\n" + + " \n" + + " FAUST:\n" + + " Du hast nun die Antipathie!\n" + + " \n" + + " MARGARETE:\n" + + " Ich muß nun fort.\n" + + " \n" + + " FAUST:\n" + + " Ach kann ich nie Ein Stündchen ruhig dir am Busen hängen\n" + + " Und Brust an Brust und Seel in Seele drängen?\n" + + " \n" + + " MARGARETE:\n" + + " Ach wenn ich nur alleine schlief!\n" + + " Ich ließ dir gern heut nacht den Riegel offen;\n" + + " Doch meine Mutter schläft nicht tief,\n" + + " Und würden wir von ihr betroffen,\n" + + " Ich wär gleich auf der Stelle tot!\n" + + " \n" + + " FAUST:\n" + + " Du Engel, das hat keine Not.\n" + + " Hier ist ein Fläschchen!\n" + + " Drei Tropfen nur In ihren Trank umhüllen\n" + + " Mit tiefem Schlaf gefällig die Natur.\n" + + " \n" + + " MARGARETE:\n" + + " Was tu ich nicht um deinetwillen?\n" + + " Es wird ihr hoffentlich nicht schaden!\n" + + " \n" + + " FAUST:\n" + + " Würd ich sonst, Liebchen, dir es raten?\n" + + " \n" + + " MARGARETE:\n" + + " Seh ich dich, bester Mann, nur an,\n" + + " Weiß nicht, was mich nach deinem Willen treibt,\n" + + " Ich habe schon so viel für dich getan,\n" + + " Daß mir zu tun fast nichts mehr übrigbleibt."; + + private class LinkedAdapter implements ILinkedListener { + public void left(LinkedEnvironment environment, int flags) {} + public void suspend(LinkedEnvironment environment) {} + public void resume(LinkedEnvironment environment, int flags) {} + } + + public class PositionComparator implements Comparator { + + public int compare(Object o1, Object o2) { + LinkedPosition p1= (LinkedPosition) o1; + LinkedPosition p2= (LinkedPosition) o2; + + IDocument d1= p1.getDocument(); + IDocument d2= p2.getDocument(); + + if (d1 == d2) + // sort by offset inside the same document + return p1.getOffset() - p2.getOffset(); + else + return getIndex(d1) - getIndex(d2); + } + + private int getIndex(IDocument doc) { + int i= 0; + for (Iterator it= fDocumentMap.iterator(); it.hasNext(); i++) { + IDocument[] docs= (IDocument[]) it.next(); + if (docs[0] == doc || docs[1] == doc) + return i; + } + return -1; + } + } + +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedPositionGroupTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedPositionGroupTest.java new file mode 100644 index 00000000000..269b21a2103 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedPositionGroupTest.java @@ -0,0 +1,172 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests.link; + +import junit.framework.TestCase; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.link.LinkedEnvironment; +import org.eclipse.jface.text.link.LinkedPosition; +import org.eclipse.jface.text.link.LinkedPositionGroup; + + +public class LinkedPositionGroupTest extends TestCase { + + public void testIsEmtpy() { + LinkedPositionGroup group= new LinkedPositionGroup(); + assertTrue(group.isEmtpy()); + } + + public void testIsNotEmtpy() throws BadLocationException { + LinkedPositionGroup group= new LinkedPositionGroup(); + group.addPosition(new LinkedPosition(new Document(), 0, 0)); + assertFalse(group.isEmtpy()); + } + + public void testGetPositions() throws BadLocationException { + LinkedPositionGroup group= new LinkedPositionGroup(); + group.addPosition(new LinkedPosition(new Document(), 0, 0)); + group.addPosition(new LinkedPosition(new Document(), 0, 0)); + assertEquals(2, group.getPositions().length); + } + + public void testAddPosition() throws BadLocationException { + LinkedPositionGroup group= new LinkedPositionGroup(); + LinkedPosition p= new LinkedPosition(new Document(), 0, 0); + group.addPosition(p); + assertSame(p, group.getPositions()[0]); + } + + public void testAddIllegalState() throws BadLocationException { + LinkedPositionGroup group= new LinkedPositionGroup(); + LinkedEnvironment env= new LinkedEnvironment(); + env.addGroup(group); + + LinkedPosition p= new LinkedPosition(new Document(), 0, 0); + try { + group.addPosition(p); + } catch (IllegalStateException e) { + return; + } + + assertFalse(true); + } + + public void testAddBadLocation() throws BadLocationException { + LinkedPositionGroup group= new LinkedPositionGroup(); + IDocument doc= new Document(GARTEN); + group.addPosition(new LinkedPosition(doc, 1, 9)); + try { + group.addPosition(new LinkedPosition(doc, 3, 9)); + } catch (BadLocationException e) { + return; + } + + assertFalse(true); + } + + public void testAddEqualContent() { + LinkedPositionGroup group= new LinkedPositionGroup(); + IDocument doc= new Document(GARTEN); + try { + group.addPosition(new LinkedPosition(doc, 1, 9)); + group.addPosition(new LinkedPosition(doc, 68, 9)); + } catch (BadLocationException e) { + assertFalse(true); + } + } + + public void testAddNotEqualContent() { + LinkedPositionGroup group= new LinkedPositionGroup(); + IDocument doc= new Document(GARTEN); + try { + group.addPosition(new LinkedPosition(doc, 1, 9)); + group.addPosition(new LinkedPosition(doc, 68, 10)); + } catch (BadLocationException e) { + return; + } + assertFalse(true); + } + + private static final String GARTEN= + " MARGARETE:\n" + + " Versprich mir, Heinrich!\n" + + " \n" + + " FAUST:\n" + + " Was ich kann!\n" + + " \n" + + " MARGARETE:\n" + + " Nun sag, wie hast du\'s mit der Religion?\n" + + " Du bist ein herzlich guter Mann,\n" + + " Allein ich glaub, du hältst nicht viel davon.\n" + + " \n" + + " FAUST:\n" + + " Laß das, mein Kind! Du fühlst, ich bin dir gut;\n" + + " Für meine Lieben ließ\' ich Leib und Blut,\n" + + " Will niemand sein Gefühl und seine Kirche rauben.\n" + + " \n" + + " MARGARETE:\n" + + " Das ist nicht recht, man muß dran glauben.\n" + + " \n" + + " FAUST:\n" + + " Muß man?\n" + + " \n" + + " MARGARETE:\n" + + " Ach! wenn ich etwas auf dich konnte! Du ehrst auch nicht die heil\'gen Sakramente.\n" + + " \n" + + " FAUST:\n" + + " Ich ehre sie.\n" + + " \n" + + " MARGARETE:\n" + + " Doch ohne Verlangen. Zur Messe, zur Beichte bist du lange nicht gegangen.\n" + + " Glaubst du an Gott?\n" + + " \n" + + " FAUST:\n" + + " Mein Liebchen, wer darf sagen: Ich glaub an Gott?\n" + + " Magst Priester oder Weise fragen,\n" + + " Und ihre Antwort scheint nur Spott\n" + + " Über den Frager zu sein.\n" + + " \n" + + " MARGARETE:\n" + + " So glaubst du nicht?\n" + + " \n" + + " FAUST:\n" + + " Mißhör mich nicht, du holdes Angesicht!\n" + + " Wer darf ihn nennen?\n" + + " Und wer bekennen:\n" + + " »Ich glaub ihn!«?\n" + + " Wer empfinden,\n" + + " Und sich unterwinden\n" + + " Zu sagen: »Ich glaub ihn nicht!«?\n" + + " Der Allumfasser,\n" + + " Der Allerhalter,\n" + + " Faßt und erhält er nicht\n" + + " Dich, mich, sich selbst?\n" + + " Wölbt sich der Himmel nicht da droben?\n" + + " Liegt die Erde nicht hier unten fest?\n" + + " Und steigen freundlich blickend\n" + + " Ewige Sterne nicht herauf?\n" + + " Schau ich nicht Aug in Auge dir,\n" + + " Und drängt nicht alles\n" + + " Nach Haupt und Herzen dir,\n" + + " Und webt in ewigem Geheimnis\n" + + " Unsichtbar sichtbar neben dir?\n" + + " Erfüll davon dein Herz, so groß es ist,\n" + + " Und wenn du ganz in dem Gefühle selig bist,\n" + + " Nenn es dann, wie du willst,\n" + + " Nenn\'s Glück! Herz! Liebe! Gott\n" + + " Ich habe keinen Namen\n" + + " Dafür! Gefühl ist alles;\n" + + " Name ist Schall und Rauch,\n" + + " Umnebelnd Himmelsglut.\n"; +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedPositionTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedPositionTest.java new file mode 100644 index 00000000000..bd0cd84584d --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedPositionTest.java @@ -0,0 +1,312 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests.link; + +import junit.framework.TestCase; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.DocumentEvent; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.Position; +import org.eclipse.jface.text.link.LinkedPosition; +import org.eclipse.jface.text.link.LinkedPositionGroup; + + +public class LinkedPositionTest extends TestCase { + + public void setUp() { + fDoc= new Document(GARTEN1); + fPos= new LinkedPosition(fDoc, 3, 10); + } + + public void testCreate() { + new LinkedPosition(fDoc, 1, 9); + new LinkedPosition(new Document(), 123, 234); + } + + public void testNullCreate() { + try { + new LinkedPosition(null, 1, 9); + } catch (Throwable e) { + return; + } + assertFalse(true); + } + + /* + * Class to test for boolean includes(int) + */ + public void testIncludesint() { + assertEquals(true, fPos.includes(3)); + assertEquals(true, fPos.includes(6)); + assertEquals(true, fPos.includes(13)); + assertEquals(false, fPos.includes(2)); + assertEquals(false, fPos.includes(15)); + } + + public void testGetDocument() { + assertEquals(fDoc, fPos.getDocument()); + } + + /* + * Class to test for boolean overlapsWith(LinkedPosition) + */ + public void testOverlapsWithLinkedPosition() { + LinkedPosition pos= new LinkedPosition(fDoc, 0, 2); + assertEquals(false, fPos.overlapsWith(pos)); + + pos= new LinkedPosition(fDoc, 0, 3); + assertEquals(false, fPos.overlapsWith(pos)); + + pos= new LinkedPosition(fDoc, 1, 4); + assertEquals(true, fPos.overlapsWith(pos)); + + pos= new LinkedPosition(fDoc, 3, 5); + assertEquals(true, fPos.overlapsWith(pos)); + + pos= new LinkedPosition(fDoc, 5, 7); + assertEquals(true, fPos.overlapsWith(pos)); + + pos= new LinkedPosition(fDoc, 7, 6); + assertEquals(true, fPos.overlapsWith(pos)); + + pos= new LinkedPosition(fDoc, 7, 7); + assertEquals(true, fPos.overlapsWith(pos)); + + pos= new LinkedPosition(fDoc, 13, 1); + assertEquals(false, fPos.overlapsWith(pos)); + + pos= new LinkedPosition(fDoc, 14, 4); + assertEquals(false, fPos.overlapsWith(pos)); + } + + /* + * Class to test for boolean includes(DocumentEvent) + */ + public void testIncludesDocumentEvent() { + DocumentEvent de= new DocumentEvent(fDoc, 0, 2, "ignore"); + assertEquals(false, fPos.includes(de)); + + de= new DocumentEvent(fDoc, 0, 3, "ignore"); + assertEquals(false, fPos.includes(de)); + + de= new DocumentEvent(fDoc, 1, 4, "ignore"); + assertEquals(false, fPos.includes(de)); + + de= new DocumentEvent(fDoc, 3, 5, "ignore"); + assertEquals(true, fPos.includes(de)); + + de= new DocumentEvent(fDoc, 5, 7, "ignore"); + assertEquals(true, fPos.includes(de)); + + de= new DocumentEvent(fDoc, 7, 6, "ignore"); + assertEquals(true, fPos.includes(de)); + + de= new DocumentEvent(fDoc, 7, 7, "ignore"); + assertEquals(false, fPos.includes(de)); + + de= new DocumentEvent(fDoc, 13, 1, "ignore"); + assertEquals(false, fPos.includes(de)); + + de= new DocumentEvent(fDoc, 14, 4, "ignore"); + assertEquals(false, fPos.includes(de)); + } + + /* + * Class to test for boolean includes(LinkedPosition) + */ + public void testIncludesLinkedPosition() { + LinkedPosition pos= new LinkedPosition(fDoc, 0, 2); + assertEquals(false, fPos.includes(pos)); + + pos= new LinkedPosition(fDoc, 0, 3); + assertEquals(false, fPos.includes(pos)); + + pos= new LinkedPosition(fDoc, 1, 4); + assertEquals(false, fPos.includes(pos)); + + pos= new LinkedPosition(fDoc, 3, 5); + assertEquals(true, fPos.includes(pos)); + + pos= new LinkedPosition(fDoc, 5, 7); + assertEquals(true, fPos.includes(pos)); + + pos= new LinkedPosition(fDoc, 7, 6); + assertEquals(true, fPos.includes(pos)); + + pos= new LinkedPosition(fDoc, 7, 7); + assertEquals(false, fPos.includes(pos)); + + pos= new LinkedPosition(fDoc, 13, 1); + assertEquals(false, fPos.includes(pos)); + + pos= new LinkedPosition(fDoc, 14, 4); + assertEquals(false, fPos.includes(pos)); + } + + public void testGetContent() throws BadLocationException { + LinkedPosition p= new LinkedPosition(fDoc, 1, 9); + assertEquals("MARGARETE", p.getContent()); + + p= new LinkedPosition(fDoc, 42, 5); + assertEquals("FAUST", p.getContent()); + + fDoc.replace(42, 2, ""); + assertEquals("UST:\n", p.getContent()); // not linked! + + fDoc.set(GARTEN1); + assertEquals("FAUST", p.getContent()); + } + + public void testBadLocationContent() { + LinkedPosition p= new LinkedPosition(new Document(), 23, 3); + try { + p.getContent(); + } catch (BadLocationException e) { + return; + } + assertFalse(true); + + p= new LinkedPosition(fDoc, 23, 3); + fDoc.set(""); + try { + p.getContent(); + } catch (BadLocationException e) { + return; + } + assertFalse(true); + } + + public void testGetSequenceNumber() { + LinkedPosition p= new LinkedPosition(fDoc, 1, 9); + assertEquals(LinkedPositionGroup.NO_STOP, p.getSequenceNumber()); + + p= new LinkedPosition(fDoc, 1, 9, 18); + assertEquals(18, p.getSequenceNumber()); + } + + public void testSetSequenceNumber() { + fPos.setSequenceNumber(28); + assertEquals(28, fPos.getSequenceNumber()); + } + + public void testEquals() { + LinkedPosition p1= new LinkedPosition(fDoc, 1, 9); + LinkedPosition p2= new LinkedPosition(fDoc, 1, 9); + + assertEquals(p1, p2); + } + + public void testNotEquals() { + LinkedPosition p1= new LinkedPosition(fDoc, 1, 9); + LinkedPosition p2= new LinkedPosition(fDoc, 1, 10); + + assertFalse(p1.equals(p2)); + } + + public void testNotEqualsPosition() { + LinkedPosition p1= new LinkedPosition(fDoc, 1, 9); + Position p2= new Position(1, 9); + + assertFalse(p1.equals(p2)); + } + + public void testNotEqualsPositionSymmetry() { +// LinkedPosition p1= new LinkedPosition(fDoc, 1, 9); +// Position p2= new Position(1, 9); +// +// // breaking equals contract +// assertFalse(p2.equals(p1)); + } + + public void testNotEqualsDifferentDoc() { + LinkedPosition p1= new LinkedPosition(fDoc, 1, 9); + IDocument doc= new Document(); + LinkedPosition p2= new LinkedPosition(doc, 1, 9); + + assertFalse(p1.equals(p2)); + } + + private static final String GARTEN1= + " MARGARETE:\n" + + " Versprich mir, Heinrich!\n" + + " \n" + + " FAUST:\n" + + " Was ich kann!\n" + + " \n" + + " MARGARETE:\n" + + " Nun sag, wie hast du\'s mit der Religion?\n" + + " Du bist ein herzlich guter Mann,\n" + + " Allein ich glaub, du hältst nicht viel davon.\n" + + " \n" + + " FAUST:\n" + + " Laß das, mein Kind! Du fühlst, ich bin dir gut;\n" + + " Für meine Lieben ließ\' ich Leib und Blut,\n" + + " Will niemand sein Gefühl und seine Kirche rauben.\n" + + " \n" + + " MARGARETE:\n" + + " Das ist nicht recht, man muß dran glauben.\n" + + " \n" + + " FAUST:\n" + + " Muß man?\n" + + " \n" + + " MARGARETE:\n" + + " Ach! wenn ich etwas auf dich konnte! Du ehrst auch nicht die heil\'gen Sakramente.\n" + + " \n" + + " FAUST:\n" + + " Ich ehre sie.\n" + + " \n" + + " MARGARETE:\n" + + " Doch ohne Verlangen. Zur Messe, zur Beichte bist du lange nicht gegangen.\n" + + " Glaubst du an Gott?\n" + + " \n" + + " FAUST:\n" + + " Mein Liebchen, wer darf sagen: Ich glaub an Gott?\n" + + " Magst Priester oder Weise fragen,\n" + + " Und ihre Antwort scheint nur Spott\n" + + " Über den Frager zu sein.\n" + + " \n" + + " MARGARETE:\n" + + " So glaubst du nicht?\n" + + " \n" + + " FAUST:\n" + + " Mißhör mich nicht, du holdes Angesicht!\n" + + " Wer darf ihn nennen?\n" + + " Und wer bekennen:\n" + + " »Ich glaub ihn!«?\n" + + " Wer empfinden,\n" + + " Und sich unterwinden\n" + + " Zu sagen: »Ich glaub ihn nicht!«?\n" + + " Der Allumfasser,\n" + + " Der Allerhalter,\n" + + " Faßt und erhält er nicht\n" + + " Dich, mich, sich selbst?\n" + + " Wölbt sich der Himmel nicht da droben?\n" + + " Liegt die Erde nicht hier unten fest?\n" + + " Und steigen freundlich blickend\n" + + " Ewige Sterne nicht herauf?\n" + + " Schau ich nicht Aug in Auge dir,\n" + + " Und drängt nicht alles\n" + + " Nach Haupt und Herzen dir,\n" + + " Und webt in ewigem Geheimnis\n" + + " Unsichtbar sichtbar neben dir?\n" + + " Erfüll davon dein Herz, so groß es ist,\n" + + " Und wenn du ganz in dem Gefühle selig bist,\n" + + " Nenn es dann, wie du willst,\n" + + " Nenn\'s Glück! Herz! Liebe! Gott\n" + + " Ich habe keinen Namen\n" + + " Dafür! Gefühl ist alles;\n" + + " Name ist Schall und Rauch,\n" + + " Umnebelnd Himmelsglut.\n"; + private IDocument fDoc; + private LinkedPosition fPos; +} |