Skip to content

Commit aab1d89

Browse files
committed
some tests for document editing -- this is a work in progress that mostly documents the current behaviour rather than testing the desired behaviour (at least partly as we don't always know what the desired behaviour should be).
1 parent c50f45b commit aab1d89

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package gate.corpora;
2+
3+
import static gate.Utils.*;
4+
5+
import gate.Annotation;
6+
import gate.Document;
7+
import gate.Factory;
8+
import gate.Gate;
9+
import junit.framework.Test;
10+
import junit.framework.TestCase;
11+
import junit.framework.TestSuite;
12+
13+
/**
14+
* Editing documents in GATE is a bit of a nightmare given all the possible ways
15+
* an edit could interact with existing annotations already covering the text.
16+
* This test suite attempts to test some of these. In some cases assert
17+
* statements have been commented out where behaviour doesn't match what we
18+
* would expect (i.e. the test is broken) so that the tests continue to pass
19+
* while we find the time to work through all options properly.
20+
*
21+
* This class should currently serve more like documentation of how things
22+
* currently are rather than tests to prove it works as it should; at least
23+
* partly as we are yet to properly define how some things should work!
24+
*/
25+
public class TestDocumentEdit extends TestCase {
26+
27+
public TestDocumentEdit(String name) {
28+
super(name);
29+
}
30+
31+
@Override
32+
public void setUp() throws Exception {
33+
if (!Gate.isInitialised())
34+
Gate.init();
35+
}
36+
37+
public static Test suite() {
38+
return new TestSuite(TestDocumentEdit.class);
39+
}
40+
41+
public void testEdit() throws Exception {
42+
43+
// let's start with a simple document...
44+
Document doc = Factory.newDocument("I am a happy person");
45+
46+
// and annotation the emotion word
47+
Integer id = doc.getAnnotations().add(7L, 12L, "Emotion", Factory.newFeatureMap());
48+
49+
// check the annotation we added exists at the right span
50+
Annotation emotion = doc.getAnnotations().get(id);
51+
assertNotNull(emotion);
52+
assertEquals("happy", stringFor(doc, emotion));
53+
assertEquals(7, emotion.getStartNode().getOffset().longValue());
54+
assertEquals(12, emotion.getEndNode().getOffset().longValue());
55+
56+
// we replace the entire span with new text. Currently we know this causes the
57+
// annotation to disappaear so that's what we test for, although it may actually
58+
// be preferable to keep the annotation in a case such as this
59+
doc.edit(7L, 12L, new DocumentContentImpl("sad"));
60+
assertEquals("I am a sad person", doc.getContent().toString());
61+
emotion = doc.getAnnotations().get(id);
62+
assertNull(emotion);
63+
64+
// add the annotation back over the emotion word and check it's correct
65+
id = doc.getAnnotations().add(7L, 10L, "Emotion", Factory.newFeatureMap());
66+
emotion = doc.getAnnotations().get(id);
67+
assertNotNull(emotion);
68+
assertEquals("sad", stringFor(doc, emotion));
69+
assertEquals(7, emotion.getStartNode().getOffset().longValue());
70+
assertEquals(10, emotion.getEndNode().getOffset().longValue());
71+
72+
// now replace the end of the span (i.e. don't replace the first character).
73+
// This annotion end should adjust to fit the new text
74+
doc.edit(8L, 10L, new DocumentContentImpl("miley"));
75+
assertEquals("I am a smiley person", doc.getContent().toString());
76+
emotion = doc.getAnnotations().get(id);
77+
assertNotNull(emotion);
78+
// unfortunately the end of the annotation is now in completely the wrong place.
79+
// This is because the node at the end of the annotation is moved to be at the
80+
// beginning of the edit (a special case where the firstNode is moved) prior to
81+
// then being repositioned correctly. That first move means the repositioning is
82+
// then wrong, so we comment out the asserts for now
83+
// assertEquals("smiley",stringFor(doc,emotion)); assertEquals(7,
84+
// emotion.getStartNode().getOffset().longValue()); assertEquals(13,
85+
// emotion.getEndNode().getOffset().longValue());
86+
87+
// now we replace the beginning of the span of the annotation (i.e. not the last
88+
// character). In this case I'd expect the annotation to remain and adjust (just
89+
// as it did with editing the end of the span) but...
90+
doc.edit(7L, 12L, new DocumentContentImpl("happ"));
91+
assertEquals("I am a happy person", doc.getContent().toString());
92+
emotion = doc.getAnnotations().get(id);
93+
// ...instead it disappears entirely. This is again because the firstNode is moved
94+
// to the beginning of the span, this time that results in a zero length
95+
// annotation which is then removed, before the end position is corrected, so
96+
// again we comment out the assert statements.
97+
// assertNotNull(emotion); assertEquals("happy",stringFor(doc,emotion));
98+
// assertEquals(7, emotion.getStartNode().getOffset().longValue());
99+
// assertEquals(12, emotion.getEndNode().getOffset().longValue());
100+
}
101+
}

0 commit comments

Comments
 (0)