| 1 | // This file is generated by Continue |
| 2 | |
| 3 | import { |
| 4 | getRangeInString, |
| 5 | intersection, |
| 6 | maxPosition, |
| 7 | minPosition, |
| 8 | union, |
| 9 | } from "./ranges"; |
| 10 | |
| 11 | interface Position { |
| 12 | line: number; |
| 13 | character: number; |
| 14 | } |
| 15 | |
| 16 | interface Range { |
| 17 | start: Position; |
| 18 | end: Position; |
| 19 | } |
| 20 | |
| 21 | describe("union", () => { |
| 22 | let rangeA: Range; |
| 23 | let rangeB: Range; |
| 24 | |
| 25 | beforeEach(() => { |
| 26 | rangeA = { |
| 27 | start: { line: 1, character: 0 }, |
| 28 | end: { line: 3, character: 5 }, |
| 29 | }; |
| 30 | |
| 31 | rangeB = { |
| 32 | start: { line: 2, character: 3 }, |
| 33 | end: { line: 4, character: 2 }, |
| 34 | }; |
| 35 | }); |
| 36 | |
| 37 | test("should return the union of two overlapping ranges", () => { |
| 38 | const result = union(rangeA, rangeB); |
| 39 | expect(result).toEqual({ |
| 40 | start: { line: 1, character: 0 }, |
| 41 | end: { line: 4, character: 2 }, |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | test("should return the union when ranges are identical", () => { |
| 46 | const result = union(rangeA, rangeA); |
| 47 | expect(result).toEqual(rangeA); |
| 48 | }); |
| 49 | |
| 50 | test("should return the union when ranges do not overlap but are adjacent", () => { |
| 51 | rangeB = { |
| 52 | start: { line: 3, character: 5 }, |
| 53 | end: { line: 4, character: 2 }, |
| 54 | }; |
| 55 | const result = union(rangeA, rangeB); |
| 56 | expect(result).toEqual({ |
| 57 | start: { line: 1, character: 0 }, |
| 58 | end: { line: 4, character: 2 }, |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | test("should return the union when one range is completely within the other", () => { |
| 63 | rangeB = { |
| 64 | start: { line: 2, character: 1 }, |
| 65 | end: { line: 2, character: 4 }, |
| 66 | }; |
| 67 | const result = union(rangeA, rangeB); |
| 68 | expect(result).toEqual(rangeA); |
| 69 | }); |
| 70 | |
| 71 | test("should return the union when ranges do not overlap and are not adjacent", () => { |
| 72 | rangeB = { |
| 73 | start: { line: 4, character: 3 }, |
| 74 | end: { line: 5, character: 1 }, |
| 75 | }; |
| 76 | const result = union(rangeA, rangeB); |
| 77 | expect(result).toEqual({ |
| 78 | start: { line: 1, character: 0 }, |
| 79 | end: { line: 5, character: 1 }, |
| 80 | }); |
| 81 | }); |
| 82 | |
| 83 | test("should handle edge case where start and end lines are the same", () => { |
| 84 | rangeA = { |
| 85 | start: { line: 1, character: 0 }, |
| 86 | end: { line: 1, character: 5 }, |
| 87 | }; |
| 88 | rangeB = { |
| 89 | start: { line: 1, character: 3 }, |
| 90 | end: { line: 1, character: 7 }, |
| 91 | }; |
| 92 | const result = union(rangeA, rangeB); |
| 93 | expect(result).toEqual({ |
| 94 | start: { line: 1, character: 0 }, |
| 95 | end: { line: 1, character: 7 }, |
| 96 | }); |
| 97 | }); |
| 98 | }); |
| 99 | |
| 100 | describe("Position comparison functions", () => { |
| 101 | let pos1: Position; |
| 102 | let pos2: Position; |
| 103 | let pos3: Position; |
| 104 | let pos4: Position; |
| 105 | |
| 106 | beforeAll(() => { |
| 107 | // Setup: Initializing positions |
| 108 | pos1 = { line: 1, character: 5 }; |
| 109 | pos2 = { line: 2, character: 3 }; |
| 110 | pos3 = { line: 1, character: 7 }; |
| 111 | pos4 = { line: 2, character: 3 }; // Same as pos2 to test equality |
| 112 | }); |
| 113 | |
| 114 | describe("maxPosition", () => { |
| 115 | it("should return the position with the greater line number", () => { |
| 116 | expect(maxPosition(pos1, pos2)).toEqual(pos2); |
| 117 | }); |
| 118 | |
| 119 | it("should return the position with the greater character number when lines are equal", () => { |
| 120 | expect(maxPosition(pos1, pos3)).toEqual(pos3); |
| 121 | }); |
| 122 | |
| 123 | it("should return the first position when both positions are equal", () => { |
| 124 | expect(maxPosition(pos2, pos4)).toEqual(pos2); |
| 125 | }); |
| 126 | |
| 127 | it("should handle positions with negative line numbers", () => { |
| 128 | const posNegative = { line: -1, character: 0 }; |
| 129 | expect(maxPosition(posNegative, pos1)).toEqual(pos1); |
| 130 | }); |
| 131 | |
| 132 | it("should handle positions with negative character numbers", () => { |
| 133 | const posNegativeChar = { line: 1, character: -1 }; |
| 134 | expect(maxPosition(posNegativeChar, pos1)).toEqual(pos1); |
| 135 | }); |
| 136 | }); |
| 137 | |
| 138 | describe("minPosition", () => { |
| 139 | it("should return the position with the lesser line number", () => { |
| 140 | expect(minPosition(pos1, pos2)).toEqual(pos1); |
| 141 | }); |
| 142 | |
| 143 | it("should return the position with the lesser character number when lines are equal", () => { |
| 144 | expect(minPosition(pos1, pos3)).toEqual(pos1); |
| 145 | }); |
| 146 | |
| 147 | it("should return the first position when both positions are equal", () => { |
| 148 | expect(minPosition(pos2, pos4)).toEqual(pos2); |
| 149 | }); |
| 150 | |
| 151 | it("should handle positions with negative line numbers", () => { |
| 152 | const posNegative = { line: -1, character: 0 }; |
| 153 | expect(minPosition(posNegative, pos1)).toEqual(posNegative); |
| 154 | }); |
| 155 | |
| 156 | it("should handle positions with negative character numbers", () => { |
| 157 | const posNegativeChar = { line: 1, character: -1 }; |
| 158 | expect(minPosition(posNegativeChar, pos1)).toEqual(posNegativeChar); |
| 159 | }); |
| 160 | }); |
| 161 | }); |
| 162 | |
| 163 | describe.skip("getRangeInString", () => { |
| 164 | let content: string; |
| 165 | |
| 166 | beforeEach(() => { |
| 167 | content = `Line 1 |
| 168 | Line 2 |
| 169 | Line 3 |
| 170 | Line 4 |
| 171 | Line 5`; |
| 172 | }); |
| 173 | |
| 174 | afterEach(() => { |
| 175 | content = ""; |
| 176 | }); |
| 177 | |
| 178 | test("should return substring within the same line", () => { |
| 179 | const range = { |
| 180 | start: { line: 1, character: 0 }, |
| 181 | end: { line: 1, character: 4 }, |
| 182 | }; |
| 183 | expect(getRangeInString(content, range)).toBe("Line"); |
| 184 | }); |
| 185 | |
| 186 | test("should return substring spanning multiple lines", () => { |
| 187 | const range = { |
| 188 | start: { line: 1, character: 2 }, |
| 189 | end: { line: 3, character: 3 }, |
| 190 | }; |
| 191 | const expected = `ne 2 |
| 192 | Line 3 |
| 193 | Line 4`; |
| 194 | expect(getRangeInString(content, range)).toBe(expected); |
| 195 | }); |
| 196 | |
| 197 | test("should handle range that starts and ends at the same character", () => { |
| 198 | const range = { |
| 199 | start: { line: 2, character: 0 }, |
| 200 | end: { line: 2, character: 0 }, |
| 201 | }; |
| 202 | expect(getRangeInString(content, range)).toBe("L"); |
| 203 | }); |
| 204 | |
| 205 | test("should handle range that spans entire content", () => { |
| 206 | const range = { |
| 207 | start: { line: 0, character: 0 }, |
| 208 | end: { line: 4, character: 5 }, |
| 209 | }; |
| 210 | expect(getRangeInString(content, range)).toBe(content); |
| 211 | }); |
| 212 | |
| 213 | test("should handle range that spans to the end of the last line", () => { |
| 214 | const range = { |
| 215 | start: { line: 3, character: 2 }, |
| 216 | end: { line: 4, character: 5 }, |
| 217 | }; |
| 218 | const expected = `ne 4 |
| 219 | Line 5`; |
| 220 | expect(getRangeInString(content, range)).toBe(expected); |
| 221 | }); |
| 222 | |
| 223 | test("should handle empty content", () => { |
| 224 | content = ""; |
| 225 | const range = { |
| 226 | start: { line: 0, character: 0 }, |
| 227 | end: { line: 0, character: 0 }, |
| 228 | }; |
| 229 | expect(getRangeInString(content, range)).toBe(""); |
| 230 | }); |
| 231 | |
| 232 | test("should handle invalid range (start line > end line)", () => { |
| 233 | const range = { |
| 234 | start: { line: 3, character: 0 }, |
| 235 | end: { line: 2, character: 0 }, |
| 236 | }; |
| 237 | expect(getRangeInString(content, range)).toBe(""); |
| 238 | }); |
| 239 | |
| 240 | test("should handle invalid range (start character > end character on same line)", () => { |
| 241 | const range = { |
| 242 | start: { line: 1, character: 5 }, |
| 243 | end: { line: 1, character: 2 }, |
| 244 | }; |
| 245 | expect(getRangeInString(content, range)).toBe(""); |
| 246 | }); |
| 247 | |
| 248 | test("should handle range that starts and ends at the same line but different characters", () => { |
| 249 | const range = { |
| 250 | start: { line: 1, character: 2 }, |
| 251 | end: { line: 1, character: 6 }, |
| 252 | }; |
| 253 | expect(getRangeInString(content, range)).toBe("ne 2"); |
| 254 | }); |
| 255 | |
| 256 | test("should handle range that starts and ends at the same line and same characters", () => { |
| 257 | const range = { |
| 258 | start: { line: 1, character: 2 }, |
| 259 | end: { line: 1, character: 2 }, |
| 260 | }; |
| 261 | expect(getRangeInString(content, range)).toBe("n"); |
| 262 | }); |
| 263 | }); |
| 264 | |
| 265 | describe("intersection", () => { |
| 266 | let rangeA: Range; |
| 267 | let rangeB: Range; |
| 268 | |
| 269 | beforeEach(() => { |
| 270 | rangeA = { |
| 271 | start: { line: 1, character: 0 }, |
| 272 | end: { line: 3, character: 5 }, |
| 273 | }; |
| 274 | |
| 275 | rangeB = { |
| 276 | start: { line: 2, character: 2 }, |
| 277 | end: { line: 4, character: 0 }, |
| 278 | }; |
| 279 | }); |
| 280 | |
| 281 | test("returns correct intersection for overlapping ranges", () => { |
| 282 | const result = intersection(rangeA, rangeB); |
| 283 | expect(result).toEqual({ |
| 284 | start: { line: 2, character: 2 }, |
| 285 | end: { line: 3, character: 5 }, |
| 286 | }); |
| 287 | }); |
| 288 | |
| 289 | test("returns null for non-overlapping ranges", () => { |
| 290 | rangeA = { |
| 291 | start: { line: 1, character: 0 }, |
| 292 | end: { line: 2, character: 0 }, |
| 293 | }; |
| 294 | |
| 295 | rangeB = { |
| 296 | start: { line: 3, character: 0 }, |
| 297 | end: { line: 4, character: 0 }, |
| 298 | }; |
| 299 | |
| 300 | const result = intersection(rangeA, rangeB); |
| 301 | expect(result).toBeNull(); |
| 302 | }); |
| 303 | |
| 304 | // TODO |
| 305 | test.skip("returns correct intersection for single line overlap", () => { |
| 306 | rangeA = { |
| 307 | start: { line: 1, character: 0 }, |
| 308 | end: { line: 1, character: 5 }, |
| 309 | }; |
| 310 | |
| 311 | rangeB = { |
| 312 | start: { line: 1, character: 3 }, |
| 313 | end: { line: 2, character: 0 }, |
| 314 | }; |
| 315 | |
| 316 | const result = intersection(rangeA, rangeB); |
| 317 | expect(result).toEqual({ |
| 318 | start: { line: 1, character: 3 }, |
| 319 | end: { line: 1, character: 5 }, |
| 320 | }); |
| 321 | }); |
| 322 | |
| 323 | test("returns null for single line non-overlapping ranges", () => { |
| 324 | rangeA = { |
| 325 | start: { line: 1, character: 0 }, |
| 326 | end: { line: 1, character: 2 }, |
| 327 | }; |
| 328 | |
| 329 | rangeB = { |
| 330 | start: { line: 1, character: 3 }, |
| 331 | end: { line: 1, character: 5 }, |
| 332 | }; |
| 333 | |
| 334 | const result = intersection(rangeA, rangeB); |
| 335 | expect(result).toBeNull(); |
| 336 | }); |
| 337 | |
| 338 | test("returns correct intersection when one range is fully within another", () => { |
| 339 | rangeA = { |
| 340 | start: { line: 1, character: 0 }, |
| 341 | end: { line: 4, character: 5 }, |
| 342 | }; |
| 343 | |
| 344 | rangeB = { |
| 345 | start: { line: 2, character: 2 }, |
| 346 | end: { line: 3, character: 3 }, |
| 347 | }; |
| 348 | |
| 349 | const result = intersection(rangeA, rangeB); |
| 350 | expect(result).toEqual({ |
| 351 | start: { line: 2, character: 2 }, |
| 352 | end: { line: 3, character: 3 }, |
| 353 | }); |
| 354 | }); |
| 355 | |
| 356 | test("returns correct intersection when ranges touch at the edge", () => { |
| 357 | rangeA = { |
| 358 | start: { line: 1, character: 0 }, |
| 359 | end: { line: 2, character: 0 }, |
| 360 | }; |
| 361 | |
| 362 | rangeB = { |
| 363 | start: { line: 2, character: 0 }, |
| 364 | end: { line: 3, character: 0 }, |
| 365 | }; |
| 366 | |
| 367 | const result = intersection(rangeA, rangeB); |
| 368 | expect(result).toEqual({ |
| 369 | start: { line: 2, character: 0 }, |
| 370 | end: { line: 2, character: 0 }, |
| 371 | }); |
| 372 | }); |
| 373 | }); |