View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2009, Johannes E. Schindelin <johannes.schindelin@gmx.de> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.diff;
13  
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertFalse;
16  import static org.junit.Assert.assertSame;
17  import static org.junit.Assert.assertTrue;
18  
19  import org.junit.Test;
20  
21  public class EditTest {
22  	@Test
23  	public void testCreate() {
24  		final Edit e = new Edit(1, 2, 3, 4);
25  		assertEquals(1, e.getBeginA());
26  		assertEquals(2, e.getEndA());
27  		assertEquals(3, e.getBeginB());
28  		assertEquals(4, e.getEndB());
29  	}
30  
31  	@Test
32  	public void testCreateEmpty() {
33  		final Edit e = new Edit(1, 3);
34  		assertEquals(1, e.getBeginA());
35  		assertEquals(1, e.getEndA());
36  		assertEquals(3, e.getBeginB());
37  		assertEquals(3, e.getEndB());
38  		assertTrue("is empty", e.isEmpty());
39  		assertSame(Edit.Type.EMPTY, e.getType());
40  	}
41  
42  	@Test
43  	public void testSwap() {
44  		final Edit e = new Edit(1, 2, 3, 4);
45  		e.swap();
46  		assertEquals(3, e.getBeginA());
47  		assertEquals(4, e.getEndA());
48  		assertEquals(1, e.getBeginB());
49  		assertEquals(2, e.getEndB());
50  	}
51  
52  	@Test
53  	public void testType_Insert() {
54  		final Edit e = new Edit(1, 1, 1, 2);
55  		assertSame(Edit.Type.INSERT, e.getType());
56  		assertFalse("not empty", e.isEmpty());
57  		assertEquals(0, e.getLengthA());
58  		assertEquals(1, e.getLengthB());
59  	}
60  
61  	@Test
62  	public void testType_Delete() {
63  		final Edit e = new Edit(1, 2, 1, 1);
64  		assertSame(Edit.Type.DELETE, e.getType());
65  		assertFalse("not empty", e.isEmpty());
66  		assertEquals(1, e.getLengthA());
67  		assertEquals(0, e.getLengthB());
68  	}
69  
70  	@Test
71  	public void testType_Replace() {
72  		final Edit e = new Edit(1, 2, 1, 4);
73  		assertSame(Edit.Type.REPLACE, e.getType());
74  		assertFalse("not empty", e.isEmpty());
75  		assertEquals(1, e.getLengthA());
76  		assertEquals(3, e.getLengthB());
77  	}
78  
79  	@Test
80  	public void testType_Empty() {
81  		final Edit e = new Edit(1, 1, 2, 2);
82  		assertSame(Edit.Type.EMPTY, e.getType());
83  		assertSame(Edit.Type.EMPTY, new Edit(1, 2).getType());
84  		assertTrue("is empty", e.isEmpty());
85  		assertEquals(0, e.getLengthA());
86  		assertEquals(0, e.getLengthB());
87  	}
88  
89  	@Test
90  	public void testToString() {
91  		final Edit e = new Edit(1, 2, 1, 4);
92  		assertEquals("REPLACE(1-2,1-4)", e.toString());
93  	}
94  
95  	@SuppressWarnings("unlikely-arg-type")
96  	@Test
97  	public void testEquals1() {
98  		final Edit e1 = new Edit(1, 2, 3, 4);
99  		final Edit e2 = new Edit(1, 2, 3, 4);
100 
101 		assertEquals(e1, e1);
102 		assertEquals(e2, e1);
103 		assertEquals(e1, e2);
104 		assertEquals(e1.hashCode(), e2.hashCode());
105 		assertFalse(e1.equals(""));
106 	}
107 
108 	@Test
109 	public void testNotEquals1() {
110 		assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(0, 2, 3, 4)));
111 	}
112 
113 	@Test
114 	public void testNotEquals2() {
115 		assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 0, 3, 4)));
116 	}
117 
118 	@Test
119 	public void testNotEquals3() {
120 		assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 2, 0, 4)));
121 	}
122 
123 	@Test
124 	public void testNotEquals4() {
125 		assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 2, 3, 0)));
126 	}
127 
128 	@Test
129 	public void testExtendA() {
130 		final Edit e = new Edit(1, 2, 1, 1);
131 
132 		e.extendA();
133 		assertEquals(new Edit(1, 3, 1, 1), e);
134 
135 		e.extendA();
136 		assertEquals(new Edit(1, 4, 1, 1), e);
137 	}
138 
139 	@Test
140 	public void testExtendB() {
141 		final Edit e = new Edit(1, 2, 1, 1);
142 
143 		e.extendB();
144 		assertEquals(new Edit(1, 2, 1, 2), e);
145 
146 		e.extendB();
147 		assertEquals(new Edit(1, 2, 1, 3), e);
148 	}
149 
150 	@Test
151 	public void testBeforeAfterCuts() {
152 		final Edit whole = new Edit(1, 8, 2, 9);
153 		final Edit mid = new Edit(4, 5, 3, 6);
154 
155 		assertEquals(new Edit(1, 4, 2, 3), whole.before(mid));
156 		assertEquals(new Edit(5, 8, 6, 9), whole.after(mid));
157 	}
158 }