JAL-4313 Test and patch annots trimming
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Tue, 31 Oct 2023 17:01:44 +0000 (18:01 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Tue, 31 Oct 2023 17:01:44 +0000 (18:01 +0100)
src/jalview/datamodel/AlignmentAnnotation.java
test/jalview/datamodel/AlignmentAnnotationTests.java

index 0e0239a..e832c2f 100755 (executable)
@@ -854,15 +854,15 @@ public class AlignmentAnnotation
     }
     if (startRes >= annotations.length)
     {
-      startRes = annotations.length - 1;
+      startRes = annotations.length;
     }
-    if (endRes >= annotations.length)
+    if (endRes < 0)
     {
-      endRes = annotations.length - 1;
+      endRes = -1;
     }
-    if (annotations == null)
+    if (endRes >= annotations.length)
     {
-      return;
+      endRes = annotations.length - 1;
     }
     Annotation[] temp = new Annotation[endRes - startRes + 1];
     if (startRes < annotations.length)
index 6ffbfa9..2948f83 100644 (file)
@@ -31,8 +31,7 @@ import org.testng.annotations.Test;
 import static jalview.datamodel.Annotation.EMPTY_ANNOTATION;
 import static jalview.testutils.Matchers.matchesAnnotations;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.Matchers.*;
 import static org.testng.Assert.assertNull;
 import static org.testng.AssertJUnit.assertEquals;
 
@@ -585,4 +584,53 @@ public class AlignmentAnnotationTests
         matchesAnnotations(new Annotation(1), new Annotation(2),
             new Annotation(3), new Annotation(8), new Annotation(9)));
   }
+
+  @Test(groups = "Functional")
+  public void testMakeVisibleAnnotation_TruncateNegativeStart()
+  {
+    Annotation[] annots = annotsRange(8);
+    AlignmentAnnotation ann = new AlignmentAnnotation("label", "desc", annots);
+    HiddenColumns hc = new HiddenColumns();
+    ann.makeVisibleAnnotation(-5, 1, hc);
+    assertThat(ann.annotations,
+        matchesAnnotations(new Annotation(0), new Annotation(1)));
+  }
+
+  @Test(groups = "Functional")
+  public void testMakeVisibleAnnotation_TruncateNegativeStartAndEnd()
+  {
+    Annotation[] annots = annotsRange(8);
+    AlignmentAnnotation ann = new AlignmentAnnotation("label", "desc", annots);
+    HiddenColumns hc = new HiddenColumns();
+    ann.makeVisibleAnnotation(-5, -2, hc);
+    assertThat(ann.annotations, is(emptyArray()));
+  }
+
+  @Test(groups = "Functional")
+  public void testMakeVisibleAnnotation_TruncateEndBeyondSize()
+  {
+    Annotation[] annots = annotsRange(8);
+    AlignmentAnnotation ann = new AlignmentAnnotation("label", "desc", annots);
+    HiddenColumns hc = new HiddenColumns();
+    ann.makeVisibleAnnotation(6, 10, hc);
+    assertThat(ann.annotations, matchesAnnotations(new Annotation(6), new Annotation(7)));
+  }
+
+  @Test(groups = "Functional")
+  public void testMakeVisibleAnnotation_TruncateStartAndEndBeyondSize()
+  {
+    Annotation[] annots = annotsRange(4);
+    AlignmentAnnotation ann = new AlignmentAnnotation("label", "desc", annots);
+    HiddenColumns hc = new HiddenColumns();
+    ann.makeVisibleAnnotation(6, 10, hc);
+    assertThat(ann.annotations, is(emptyArray()));
+  }
+
+  static Annotation[] annotsRange(int stop)
+  {
+    Annotation[] annotations = new Annotation[stop];
+    for (int i = 0; i < stop; i++)
+      annotations[i] = new Annotation(i);
+    return annotations;
+  }
 }