You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. package org.eclipse.jgit.diff;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertSame;
  15. import static org.junit.Assert.assertTrue;
  16. import org.junit.Test;
  17. public class EditTest {
  18. @Test
  19. public void testCreate() {
  20. final Edit e = new Edit(1, 2, 3, 4);
  21. assertEquals(1, e.getBeginA());
  22. assertEquals(2, e.getEndA());
  23. assertEquals(3, e.getBeginB());
  24. assertEquals(4, e.getEndB());
  25. }
  26. @Test
  27. public void testCreateEmpty() {
  28. final Edit e = new Edit(1, 3);
  29. assertEquals(1, e.getBeginA());
  30. assertEquals(1, e.getEndA());
  31. assertEquals(3, e.getBeginB());
  32. assertEquals(3, e.getEndB());
  33. assertTrue("is empty", e.isEmpty());
  34. assertSame(Edit.Type.EMPTY, e.getType());
  35. }
  36. @Test
  37. public void testSwap() {
  38. final Edit e = new Edit(1, 2, 3, 4);
  39. e.swap();
  40. assertEquals(3, e.getBeginA());
  41. assertEquals(4, e.getEndA());
  42. assertEquals(1, e.getBeginB());
  43. assertEquals(2, e.getEndB());
  44. }
  45. @Test
  46. public void testType_Insert() {
  47. final Edit e = new Edit(1, 1, 1, 2);
  48. assertSame(Edit.Type.INSERT, e.getType());
  49. assertFalse("not empty", e.isEmpty());
  50. assertEquals(0, e.getLengthA());
  51. assertEquals(1, e.getLengthB());
  52. }
  53. @Test
  54. public void testType_Delete() {
  55. final Edit e = new Edit(1, 2, 1, 1);
  56. assertSame(Edit.Type.DELETE, e.getType());
  57. assertFalse("not empty", e.isEmpty());
  58. assertEquals(1, e.getLengthA());
  59. assertEquals(0, e.getLengthB());
  60. }
  61. @Test
  62. public void testType_Replace() {
  63. final Edit e = new Edit(1, 2, 1, 4);
  64. assertSame(Edit.Type.REPLACE, e.getType());
  65. assertFalse("not empty", e.isEmpty());
  66. assertEquals(1, e.getLengthA());
  67. assertEquals(3, e.getLengthB());
  68. }
  69. @Test
  70. public void testType_Empty() {
  71. final Edit e = new Edit(1, 1, 2, 2);
  72. assertSame(Edit.Type.EMPTY, e.getType());
  73. assertSame(Edit.Type.EMPTY, new Edit(1, 2).getType());
  74. assertTrue("is empty", e.isEmpty());
  75. assertEquals(0, e.getLengthA());
  76. assertEquals(0, e.getLengthB());
  77. }
  78. @Test
  79. public void testToString() {
  80. final Edit e = new Edit(1, 2, 1, 4);
  81. assertEquals("REPLACE(1-2,1-4)", e.toString());
  82. }
  83. @SuppressWarnings("unlikely-arg-type")
  84. @Test
  85. public void testEquals1() {
  86. final Edit e1 = new Edit(1, 2, 3, 4);
  87. final Edit e2 = new Edit(1, 2, 3, 4);
  88. assertEquals(e1, e1);
  89. assertEquals(e2, e1);
  90. assertEquals(e1, e2);
  91. assertEquals(e1.hashCode(), e2.hashCode());
  92. assertFalse(e1.equals(""));
  93. }
  94. @Test
  95. public void testNotEquals1() {
  96. assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(0, 2, 3, 4)));
  97. }
  98. @Test
  99. public void testNotEquals2() {
  100. assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 0, 3, 4)));
  101. }
  102. @Test
  103. public void testNotEquals3() {
  104. assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 2, 0, 4)));
  105. }
  106. @Test
  107. public void testNotEquals4() {
  108. assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 2, 3, 0)));
  109. }
  110. @Test
  111. public void testExtendA() {
  112. final Edit e = new Edit(1, 2, 1, 1);
  113. e.extendA();
  114. assertEquals(new Edit(1, 3, 1, 1), e);
  115. e.extendA();
  116. assertEquals(new Edit(1, 4, 1, 1), e);
  117. }
  118. @Test
  119. public void testExtendB() {
  120. final Edit e = new Edit(1, 2, 1, 1);
  121. e.extendB();
  122. assertEquals(new Edit(1, 2, 1, 2), e);
  123. e.extendB();
  124. assertEquals(new Edit(1, 2, 1, 3), e);
  125. }
  126. @Test
  127. public void testBeforeAfterCuts() {
  128. final Edit whole = new Edit(1, 8, 2, 9);
  129. final Edit mid = new Edit(4, 5, 3, 6);
  130. assertEquals(new Edit(1, 4, 2, 3), whole.before(mid));
  131. assertEquals(new Edit(5, 8, 6, 9), whole.after(mid));
  132. }
  133. }