1 /*
  2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.skara.webrev;
 24 
 25 import org.junit.jupiter.api.Test;
 26 import org.openjdk.skara.vcs.Hunk;
 27 import org.openjdk.skara.vcs.Range;
 28 
 29 import java.util.List;
 30 
 31 import static org.junit.jupiter.api.Assertions.assertEquals;
 32 
 33 public class HunkCoalescerTest {
 34 
 35     @Test
 36     void testOverlappingContextMerge() {
 37         var sourceContent = List.of(
 38                 "A",
 39                 "",
 40                 "",
 41                 "",
 42                 "",
 43                 "",
 44                 "",
 45                 "",
 46                 "",
 47                 "B"
 48         );
 49 
 50         var targetContent = List.of(
 51                 "C",
 52                 "",
 53                 "",
 54                 "",
 55                 "",
 56                 "",
 57                 "",
 58                 "",
 59                 "",
 60                 "D"
 61         );
 62 
 63         var hunk1 = new Hunk(new Range(1, 1), List.of("A"), new Range(1, 1), List.of("C"));
 64         var hunk2 = new Hunk(new Range(10, 1), List.of("B"), new Range(10, 1), List.of("D"));
 65 
 66         var coalescer = new HunkCoalescer(5, sourceContent, targetContent);
 67         var groups = coalescer.coalesce(List.of(hunk1, hunk2));
 68 
 69         assertEquals(1, groups.size());
 70         var group = groups.get(0);
 71         assertEquals(10, group.header().source().count());
 72         assertEquals(10, group.header().target().count());
 73 
 74         assertEquals(2, group.hunks().size());
 75         var contextHunk1 = group.hunks().get(0);
 76         assertEquals(8, contextHunk1.contextAfter().sourceLines().size());
 77         assertEquals(8, contextHunk1.contextAfter().destinationLines().size());
 78     }
 79 
 80     @Test
 81     void testContextOverlapsContent() {
 82         var sourceContent = List.of(
 83                 "A",
 84                 "",
 85                 "",
 86                 "B"
 87         );
 88 
 89         var targetContent = List.of(
 90                 "C",
 91                 "",
 92                 "",
 93                 "D"
 94         );
 95 
 96         var hunk1 = new Hunk(new Range(1, 1), List.of("A"), new Range(1, 1), List.of("C"));
 97         var hunk2 = new Hunk(new Range(4, 1), List.of("B"), new Range(4, 1), List.of("D"));
 98 
 99         var coalescer = new HunkCoalescer(5, sourceContent, targetContent);
100         var groups = coalescer.coalesce(List.of(hunk1, hunk2));
101 
102         assertEquals(1, groups.size());
103         var group = groups.get(0);
104         assertEquals(4, group.header().source().count());
105         assertEquals(4, group.header().target().count());
106 
107         assertEquals(2, group.hunks().size());
108         var contextHunk1 = group.hunks().get(0);
109         assertEquals(2, contextHunk1.contextAfter().sourceLines().size());
110         assertEquals(2, contextHunk1.contextAfter().destinationLines().size());
111     }
112 }