| 1 | using CascadeIDE.Services; |
| 2 | using Xunit; |
| 3 | |
| 4 | namespace CascadeIDE.Tests; |
| 5 | |
| 6 | public class EditorTextCoordinateUtilitiesTests |
| 7 | { |
| 8 | [Fact] |
| 9 | public void LineColumnToOffset_FirstLineFirstColumn_ReturnsZero() |
| 10 | { |
| 11 | Assert.Equal(0, EditorTextCoordinateUtilities.LineColumnToOffset("abc", 1, 1)); |
| 12 | } |
| 13 | |
| 14 | [Fact] |
| 15 | public void LineColumnToOffset_SecondLine_FirstColumn_AccountsForNewline() |
| 16 | { |
| 17 | var text = "a\nbc"; |
| 18 | Assert.Equal(2, EditorTextCoordinateUtilities.LineColumnToOffset(text, 2, 1)); |
| 19 | } |
| 20 | |
| 21 | [Fact] |
| 22 | public void LineColumnToOffset_ColumnPastLineEnd_ClampsToEndPlusOne() |
| 23 | { |
| 24 | // line "ab" has length 2; column is clamped to lineLen+1 → offset after the line |
| 25 | Assert.Equal(2, EditorTextCoordinateUtilities.LineColumnToOffset("ab", 1, 3)); |
| 26 | Assert.Equal(2, EditorTextCoordinateUtilities.LineColumnToOffset("ab", 1, 100)); |
| 27 | } |
| 28 | |
| 29 | [Fact] |
| 30 | public void LineColumnToOffset_InvalidLineOrColumn_ReturnsMinusOne() |
| 31 | { |
| 32 | Assert.Equal(-1, EditorTextCoordinateUtilities.LineColumnToOffset("a", 0, 1)); |
| 33 | Assert.Equal(-1, EditorTextCoordinateUtilities.LineColumnToOffset("a", 1, 0)); |
| 34 | Assert.Equal(-1, EditorTextCoordinateUtilities.LineColumnToOffset("a", 2, 1)); |
| 35 | } |
| 36 | |
| 37 | [Fact] |
| 38 | public void PathsReferToSameFile_SamePathDifferentCase_WhenFullPathWorks() |
| 39 | { |
| 40 | var dir = Path.Combine(Path.GetTempPath(), "cascade_path_" + Guid.NewGuid().ToString("N")); |
| 41 | Directory.CreateDirectory(dir); |
| 42 | try |
| 43 | { |
| 44 | var a = Path.Combine(dir, "file.txt"); |
| 45 | File.WriteAllText(a, "x"); |
| 46 | var b = Path.Combine(dir.ToUpperInvariant(), "FILE.TXT"); |
| 47 | Assert.True(EditorTextCoordinateUtilities.PathsReferToSameFile(a, b)); |
| 48 | } |
| 49 | finally |
| 50 | { |
| 51 | try { Directory.Delete(dir, true); } catch { /* ignore */ } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | [Fact] |
| 56 | public void PathsReferToSameFile_DifferentPaths_ReturnsFalse() |
| 57 | { |
| 58 | Assert.False(EditorTextCoordinateUtilities.PathsReferToSameFile( |
| 59 | Path.Combine("a", "x.txt"), |
| 60 | Path.Combine("b", "x.txt"))); |
| 61 | } |
| 62 | } |
| 63 | |