| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013, the Dart project authors. | 2 * Copyright (c) 2013, the Dart project authors. |
| 3 * | 3 * |
| 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except | 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except |
| 5 * in compliance with the License. You may obtain a copy of the License at | 5 * in compliance with the License. You may obtain a copy of the License at |
| 6 * | 6 * |
| 7 * http://www.eclipse.org/legal/epl-v10.html | 7 * http://www.eclipse.org/legal/epl-v10.html |
| 8 * | 8 * |
| 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License | 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
| 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express | 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
| 11 * or implied. See the License for the specific language governing permissions a
nd limitations under | 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
| 12 * the License. | 12 * the License. |
| 13 */ | 13 */ |
| 14 package com.google.dart.tools.ui.internal.text.dart; | 14 package com.google.dart.tools.ui.internal.text.dart; |
| 15 | 15 |
| 16 import junit.framework.TestCase; | 16 import junit.framework.TestCase; |
| 17 | 17 |
| 18 public class DartReconcilingRegionTest extends TestCase { | 18 public class DartReconcilingRegionTest extends TestCase { |
| 19 | 19 |
| 20 public void test_add_disjoint_after() throws Exception { | 20 public void test_add_contiguous() throws Exception { |
| 21 DartReconcilingRegion target = new DartReconcilingRegion(3, 2, 6); | 21 DartReconcilingRegion target = new DartReconcilingRegion(5, 12, 25); |
| 22 DartReconcilingRegion result = target.add(10, 7, 20); | 22 DartReconcilingRegion result = target.add(30, 0, 6); |
| 23 assertEquals(3, result.getOffset()); | 23 assertResult(target, new DartReconcilingRegion(30, 0, 6), result); |
| 24 assertEquals(14, result.getOldLength()); | 24 assertEquals(5, result.getOffset()); |
| 25 assertEquals(27, result.getNewLength()); | 25 assertEquals(12, result.getOldLength()); |
| 26 assertEquals(31, result.getNewLength()); |
| 26 assertFalse(result.isEmpty()); | 27 assertFalse(result.isEmpty()); |
| 27 } | 28 } |
| 28 | 29 |
| 29 public void test_add_disjoint_before() throws Exception { | 30 public void test_add_contiguousDelete() throws Exception { |
| 31 DartReconcilingRegion target = new DartReconcilingRegion(5, 12, 25); |
| 32 DartReconcilingRegion result = target.add(3, 2, 0); |
| 33 assertResult(target, new DartReconcilingRegion(3, 2, 0), result); |
| 34 assertEquals(3, result.getOffset()); |
| 35 assertEquals(14, result.getOldLength()); |
| 36 assertEquals(25, result.getNewLength()); |
| 37 assertFalse(result.isEmpty()); |
| 38 } |
| 39 |
| 40 public void test_add_contiguousDelete2() throws Exception { |
| 41 DartReconcilingRegion target = new DartReconcilingRegion(5, 12, 25); |
| 42 DartReconcilingRegion result = target.add(4, 2, 0); |
| 43 assertResult(target, new DartReconcilingRegion(4, 2, 0), result); |
| 44 assertEquals(4, result.getOffset()); |
| 45 assertEquals(13, result.getOldLength()); |
| 46 assertEquals(24, result.getNewLength()); |
| 47 assertFalse(result.isEmpty()); |
| 48 } |
| 49 |
| 50 public void test_add_contiguousReplace() throws Exception { |
| 51 DartReconcilingRegion target = new DartReconcilingRegion(5, 12, 25); |
| 52 DartReconcilingRegion result = target.add(28, 2, 6); |
| 53 assertResult(target, new DartReconcilingRegion(28, 2, 6), result); |
| 54 assertEquals(5, result.getOffset()); |
| 55 assertEquals(12, result.getOldLength()); |
| 56 assertEquals(29, result.getNewLength()); |
| 57 assertFalse(result.isEmpty()); |
| 58 } |
| 59 |
| 60 public void test_add_contiguousReplace2() throws Exception { |
| 61 DartReconcilingRegion target = new DartReconcilingRegion(5, 12, 25); |
| 62 DartReconcilingRegion result = target.add(10, 2, 6); |
| 63 assertResult(target, new DartReconcilingRegion(28, 2, 6), result); |
| 64 assertEquals(5, result.getOffset()); |
| 65 assertEquals(12, result.getOldLength()); |
| 66 assertEquals(29, result.getNewLength()); |
| 67 assertFalse(result.isEmpty()); |
| 68 } |
| 69 |
| 70 public void test_add_contiguousReplaceBefore() throws Exception { |
| 71 DartReconcilingRegion target = new DartReconcilingRegion(5, 12, 25); |
| 72 DartReconcilingRegion result = target.add(3, 2, 6); |
| 73 assertResult(target, new DartReconcilingRegion(3, 2, 6), result); |
| 74 assertEquals(3, result.getOffset()); |
| 75 assertEquals(14, result.getOldLength()); |
| 76 assertEquals(31, result.getNewLength()); |
| 77 assertFalse(result.isEmpty()); |
| 78 } |
| 79 |
| 80 public void test_add_disjointAfter() throws Exception { |
| 81 DartReconcilingRegion target = new DartReconcilingRegion(3, 2, 6); |
| 82 DartReconcilingRegion result = target.add(10, 7, 20); |
| 83 assertNull(result); |
| 84 } |
| 85 |
| 86 public void test_add_disjointBefore() throws Exception { |
| 30 DartReconcilingRegion target = new DartReconcilingRegion(10, 7, 20); | 87 DartReconcilingRegion target = new DartReconcilingRegion(10, 7, 20); |
| 31 DartReconcilingRegion result = target.add(3, 2, 6); | 88 DartReconcilingRegion result = target.add(3, 2, 6); |
| 32 assertEquals(3, result.getOffset()); | 89 assertNull(result); |
| 33 assertEquals(14, result.getOldLength()); | |
| 34 assertEquals(27, result.getNewLength()); | |
| 35 assertFalse(result.isEmpty()); | |
| 36 } | 90 } |
| 37 | 91 |
| 38 public void test_add_empty() throws Exception { | 92 public void test_add_empty() throws Exception { |
| 39 DartReconcilingRegion target = new DartReconcilingRegion(3, 2, 6); | 93 DartReconcilingRegion target = new DartReconcilingRegion(3, 2, 6); |
| 40 DartReconcilingRegion result = target.add(10, 0, 0); | 94 DartReconcilingRegion result = target.add(10, 0, 0); |
| 95 assertResult(target, new DartReconcilingRegion(10, 0, 0), result); |
| 41 assertEquals(3, result.getOffset()); | 96 assertEquals(3, result.getOffset()); |
| 42 assertEquals(2, result.getOldLength()); | 97 assertEquals(2, result.getOldLength()); |
| 43 assertEquals(6, result.getNewLength()); | 98 assertEquals(6, result.getNewLength()); |
| 44 assertFalse(result.isEmpty()); | 99 assertFalse(result.isEmpty()); |
| 45 } | 100 } |
| 46 | 101 |
| 47 public void test_add_overlapping_after() throws Exception { | 102 public void test_add_overlappingAfter() throws Exception { |
| 48 DartReconcilingRegion target = new DartReconcilingRegion(3, 2, 6); | 103 DartReconcilingRegion target = new DartReconcilingRegion(3, 2, 6); |
| 49 DartReconcilingRegion result = target.add(5, 12, 25); | 104 DartReconcilingRegion result = target.add(5, 5, 25); |
| 50 assertEquals(3, result.getOffset()); | 105 assertNull(result); |
| 51 assertEquals(14, result.getOldLength()); | |
| 52 assertEquals(27, result.getNewLength()); | |
| 53 assertFalse(result.isEmpty()); | |
| 54 } | 106 } |
| 55 | 107 |
| 56 public void test_add_overlapping_before() throws Exception { | 108 public void test_add_overlappingBefore() throws Exception { |
| 57 DartReconcilingRegion target = new DartReconcilingRegion(5, 12, 25); | 109 DartReconcilingRegion target = new DartReconcilingRegion(5, 12, 25); |
| 58 DartReconcilingRegion result = target.add(3, 2, 6); | 110 DartReconcilingRegion result = target.add(3, 1, 6); |
| 59 assertEquals(3, result.getOffset()); | 111 assertNull(result); |
| 60 assertEquals(14, result.getOldLength()); | |
| 61 assertEquals(27, result.getNewLength()); | |
| 62 assertFalse(result.isEmpty()); | |
| 63 } | 112 } |
| 64 | 113 |
| 65 public void test_add_to_empty() throws Exception { | 114 public void test_add_toEmpty1() throws Exception { |
| 66 DartReconcilingRegion target = new DartReconcilingRegion(0, 0, 0); | 115 DartReconcilingRegion target = new DartReconcilingRegion(0, 0, 0); |
| 67 DartReconcilingRegion result = target.add(3, 2, 6); | 116 DartReconcilingRegion result = target.add(3, 2, 6); |
| 68 assertEquals(3, result.getOffset()); | 117 assertResult(target, new DartReconcilingRegion(3, 2, 6), result); |
| 69 assertEquals(2, result.getOldLength()); | |
| 70 assertEquals(6, result.getNewLength()); | |
| 71 assertFalse(result.isEmpty()); | |
| 72 | |
| 73 target = new DartReconcilingRegion(10, 0, 0); | |
| 74 result = target.add(3, 2, 6); | |
| 75 assertEquals(3, result.getOffset()); | 118 assertEquals(3, result.getOffset()); |
| 76 assertEquals(2, result.getOldLength()); | 119 assertEquals(2, result.getOldLength()); |
| 77 assertEquals(6, result.getNewLength()); | 120 assertEquals(6, result.getNewLength()); |
| 78 assertFalse(result.isEmpty()); | 121 assertFalse(result.isEmpty()); |
| 79 } | 122 } |
| 80 | 123 |
| 81 public void test_new_empty() { | 124 public void test_add_toEmpty2() throws Exception { |
| 125 DartReconcilingRegion target = new DartReconcilingRegion(10, 0, 0); |
| 126 DartReconcilingRegion result = target.add(3, 2, 6); |
| 127 assertResult(target, new DartReconcilingRegion(3, 2, 6), result); |
| 128 assertEquals(3, result.getOffset()); |
| 129 assertEquals(2, result.getOldLength()); |
| 130 assertEquals(6, result.getNewLength()); |
| 131 assertFalse(result.isEmpty()); |
| 132 } |
| 133 |
| 134 public void test_new_empty1() { |
| 82 DartReconcilingRegion target = new DartReconcilingRegion(0, 0, 0); | 135 DartReconcilingRegion target = new DartReconcilingRegion(0, 0, 0); |
| 83 assertEquals(0, target.getOffset()); | 136 assertEquals(0, target.getOffset()); |
| 84 assertEquals(0, target.getOldLength()); | 137 assertEquals(0, target.getOldLength()); |
| 85 assertEquals(0, target.getNewLength()); | 138 assertEquals(0, target.getNewLength()); |
| 86 assertTrue(target.isEmpty()); | 139 assertTrue(target.isEmpty()); |
| 140 } |
| 87 | 141 |
| 88 target = new DartReconcilingRegion(10, 0, 0); | 142 public void test_new_empty2() { |
| 143 DartReconcilingRegion target = new DartReconcilingRegion(10, 0, 0); |
| 89 assertEquals(10, target.getOffset()); | 144 assertEquals(10, target.getOffset()); |
| 90 assertEquals(0, target.getOldLength()); | 145 assertEquals(0, target.getOldLength()); |
| 91 assertEquals(0, target.getNewLength()); | 146 assertEquals(0, target.getNewLength()); |
| 92 assertTrue(target.isEmpty()); | 147 assertTrue(target.isEmpty()); |
| 93 } | 148 } |
| 94 | 149 |
| 95 public void test_new_insert() { | 150 public void test_new_insert() { |
| 96 DartReconcilingRegion target = new DartReconcilingRegion(10, 0, 20); | 151 DartReconcilingRegion target = new DartReconcilingRegion(10, 0, 20); |
| 97 assertEquals(10, target.getOffset()); | 152 assertEquals(10, target.getOffset()); |
| 98 assertEquals(0, target.getOldLength()); | 153 assertEquals(0, target.getOldLength()); |
| 99 assertEquals(20, target.getNewLength()); | 154 assertEquals(20, target.getNewLength()); |
| 100 assertFalse(target.isEmpty()); | 155 assertFalse(target.isEmpty()); |
| 101 } | 156 } |
| 102 | 157 |
| 103 public void test_new_replace() { | 158 public void test_new_replace() { |
| 104 DartReconcilingRegion target = new DartReconcilingRegion(10, 20, 0); | 159 DartReconcilingRegion target = new DartReconcilingRegion(10, 20, 0); |
| 105 assertEquals(10, target.getOffset()); | 160 assertEquals(10, target.getOffset()); |
| 106 assertEquals(20, target.getOldLength()); | 161 assertEquals(20, target.getOldLength()); |
| 107 assertEquals(0, target.getNewLength()); | 162 assertEquals(0, target.getNewLength()); |
| 108 assertFalse(target.isEmpty()); | 163 assertFalse(target.isEmpty()); |
| 109 } | 164 } |
| 165 |
| 166 private String adjust(String code, DartReconcilingRegion region) { |
| 167 StringBuilder sb = new StringBuilder(); |
| 168 sb.append(code.substring(0, region.getOffset())); |
| 169 for (int count = 0; count < region.getNewLength(); count++) { |
| 170 sb.append('0'); |
| 171 } |
| 172 sb.append(code.substring(region.getOffset() + region.getOldLength())); |
| 173 return sb.toString(); |
| 174 } |
| 175 |
| 176 private void assertResult(DartReconcilingRegion target, DartReconcilingRegion
added, |
| 177 DartReconcilingRegion result) { |
| 178 String code = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 179 String expected = adjust(adjust(code, target), added); |
| 180 String actual = adjust(code, result); |
| 181 assertEquals(expected, actual); |
| 182 } |
| 110 } | 183 } |
| OLD | NEW |