|
| 1 | +// CoPilot - claude-opus-4.6 |
| 2 | + |
| 3 | +using Terminal.Gui.Document; |
| 4 | +using Terminal.Gui.Text.Indentation; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Terminal.Gui.Editor.Tests.Indentation; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Tests for <see cref="DefaultIndentationStrategy" /> — the AvaloniaEdit-derived strategy |
| 11 | +/// that copies leading whitespace from the previous line. |
| 12 | +/// </summary> |
| 13 | +public class DefaultIndentationStrategyTests |
| 14 | +{ |
| 15 | + [Fact] |
| 16 | + public void IndentLine_Copies_Spaces_From_Previous_Line () |
| 17 | + { |
| 18 | + TextDocument doc = new (" hello\n"); |
| 19 | + DefaultIndentationStrategy strategy = new (); |
| 20 | + |
| 21 | + DocumentLine line2 = doc.GetLineByNumber (2); |
| 22 | + strategy.IndentLine (doc, line2); |
| 23 | + |
| 24 | + Assert.Equal (" hello\n ", doc.Text); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void IndentLine_Copies_Tab_From_Previous_Line () |
| 29 | + { |
| 30 | + TextDocument doc = new ("\thello\n"); |
| 31 | + DefaultIndentationStrategy strategy = new (); |
| 32 | + |
| 33 | + DocumentLine line2 = doc.GetLineByNumber (2); |
| 34 | + strategy.IndentLine (doc, line2); |
| 35 | + |
| 36 | + Assert.Equal ("\thello\n\t", doc.Text); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void IndentLine_Copies_Mixed_Whitespace_From_Previous_Line () |
| 41 | + { |
| 42 | + TextDocument doc = new ("\t hello\n"); |
| 43 | + DefaultIndentationStrategy strategy = new (); |
| 44 | + |
| 45 | + DocumentLine line2 = doc.GetLineByNumber (2); |
| 46 | + strategy.IndentLine (doc, line2); |
| 47 | + |
| 48 | + Assert.Equal ("\t hello\n\t ", doc.Text); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void IndentLine_NoOp_On_First_Line () |
| 53 | + { |
| 54 | + TextDocument doc = new ("hello"); |
| 55 | + DefaultIndentationStrategy strategy = new (); |
| 56 | + |
| 57 | + DocumentLine line1 = doc.GetLineByNumber (1); |
| 58 | + strategy.IndentLine (doc, line1); |
| 59 | + |
| 60 | + Assert.Equal ("hello", doc.Text); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void IndentLine_NoOp_When_Previous_Line_Has_No_Indentation () |
| 65 | + { |
| 66 | + TextDocument doc = new ("hello\n"); |
| 67 | + DefaultIndentationStrategy strategy = new (); |
| 68 | + |
| 69 | + DocumentLine line2 = doc.GetLineByNumber (2); |
| 70 | + strategy.IndentLine (doc, line2); |
| 71 | + |
| 72 | + Assert.Equal ("hello\n", doc.Text); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void IndentLine_Replaces_Existing_Whitespace_On_Target_Line () |
| 77 | + { |
| 78 | + TextDocument doc = new (" hello\n world"); |
| 79 | + DefaultIndentationStrategy strategy = new (); |
| 80 | + |
| 81 | + DocumentLine line2 = doc.GetLineByNumber (2); |
| 82 | + strategy.IndentLine (doc, line2); |
| 83 | + |
| 84 | + Assert.Equal (" hello\n world", doc.Text); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void IndentLines_Does_Nothing () |
| 89 | + { |
| 90 | + TextDocument doc = new ("hello\nworld\n"); |
| 91 | + DefaultIndentationStrategy strategy = new (); |
| 92 | + |
| 93 | + // IndentLines is a no-op for DefaultIndentationStrategy |
| 94 | + strategy.IndentLines (doc, 1, 2); |
| 95 | + |
| 96 | + Assert.Equal ("hello\nworld\n", doc.Text); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public void IndentLine_Throws_On_Null_Document () |
| 101 | + { |
| 102 | + DefaultIndentationStrategy strategy = new (); |
| 103 | + |
| 104 | + Assert.Throws<ArgumentNullException> (() => strategy.IndentLine (null!, null!)); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void IndentLine_Throws_On_Null_Line () |
| 109 | + { |
| 110 | + TextDocument doc = new ("hello"); |
| 111 | + DefaultIndentationStrategy strategy = new (); |
| 112 | + |
| 113 | + Assert.Throws<ArgumentNullException> (() => strategy.IndentLine (doc, null!)); |
| 114 | + } |
| 115 | +} |
0 commit comments