From aa218da8aa3bf61463d84e478b1ed498d7d964cb Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Tue, 31 Oct 2023 18:01:44 +0100 Subject: [PATCH] JAL-4313 Test and patch annots trimming --- src/jalview/datamodel/AlignmentAnnotation.java | 10 ++-- .../datamodel/AlignmentAnnotationTests.java | 52 +++++++++++++++++++- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/jalview/datamodel/AlignmentAnnotation.java b/src/jalview/datamodel/AlignmentAnnotation.java index 0e0239a..e832c2f 100755 --- a/src/jalview/datamodel/AlignmentAnnotation.java +++ b/src/jalview/datamodel/AlignmentAnnotation.java @@ -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) diff --git a/test/jalview/datamodel/AlignmentAnnotationTests.java b/test/jalview/datamodel/AlignmentAnnotationTests.java index 6ffbfa9..2948f83 100644 --- a/test/jalview/datamodel/AlignmentAnnotationTests.java +++ b/test/jalview/datamodel/AlignmentAnnotationTests.java @@ -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; + } } -- 1.7.10.2