View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.text.diff;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.junit.jupiter.api.AfterEach;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests for the StringsComparator.
30   */
31  class StringsComparatorTest {
32      private static final class ExecutionVisitor<T> implements CommandVisitor<T> {
33  
34          private final StringBuilder v;
35  
36          ExecutionVisitor() {
37              v = new StringBuilder();
38          }
39  
40          public String getString() {
41              return v.toString();
42          }
43  
44          @Override
45          public void visitDeleteCommand(final T object) {
46              // noop
47          }
48  
49          @Override
50          public void visitInsertCommand(final T object) {
51              v.append(object);
52          }
53          @Override
54          public void visitKeepCommand(final T object) {
55              v.append(object);
56          }
57      }
58      private List<String> before;
59      private List<String> after;
60      private int[]        length;
61  
62      private int[]        lcs;
63  
64      @BeforeEach
65      public void setUp() {
66          before = Arrays.asList(
67              "bottle",
68              "nematode knowledge",
69              "",
70              "aa",
71              "prefixed string",
72              "ABCABBA",
73              "glop glop",
74              "coq",
75              "spider-man");
76          after = Arrays.asList(
77              "noodle",
78              "empty bottle",
79              "",
80              "C",
81              "prefix",
82              "CBABAC",
83              "pas glop pas glop",
84              "ane",
85              "klingon");
86          length = new int[] {
87              6,
88              16,
89              0,
90              3,
91              9,
92              5,
93              8,
94              6,
95              13
96          };
97          lcs = new int[] {
98              3,
99              7,
100             0,
101             0,
102             6,
103             4,
104             9,
105             0,
106             2
107         };
108     }
109 
110     @AfterEach
111     public void tearDown() {
112         before = null;
113         after  = null;
114         length = null;
115     }
116 
117     @Test
118     void testExecution() {
119         for (int i = 0; i < before.size(); ++i) {
120             final ExecutionVisitor<Character> ev = new ExecutionVisitor<>();
121             new StringsComparator(before.get(i), after.get(i)).getScript().visit(ev);
122             assertEquals(after.get(i), ev.getString());
123         }
124     }
125 
126     @Test
127     void testLength() {
128         for (int i = 0; i < before.size(); ++i) {
129             final StringsComparator comparator =  new StringsComparator(before.get(i), after.get(i));
130             assertEquals(length[i], comparator.getScript().getModifications());
131         }
132     }
133     @Test
134     void testLongestCommonSubsequence() {
135         for (int i = 0; i < before.size(); ++i) {
136             final StringsComparator comparator =  new StringsComparator(before.get(i), after.get(i));
137             assertEquals(lcs[i], comparator.getScript().getLCSLength());
138         }
139     }
140 }