|
| 1 | +using JetBrains.Annotations; |
| 2 | +using Terminal.Gui.Drawing; |
| 3 | + |
| 4 | +namespace ViewsTests.Markdown; |
| 5 | + |
| 6 | +// Copilot |
| 7 | + |
| 8 | +[TestSubject (typeof (Terminal.Gui.Views.Markdown))] |
| 9 | +public class MarkdownRenderToAnsiTests |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public void RenderToAnsi_BasicMarkdown_ReturnsNonEmptyAnsi () |
| 13 | + { |
| 14 | + Terminal.Gui.Views.Markdown view = new () { Text = "# Hello\n\nWorld" }; |
| 15 | + |
| 16 | + string result = view.RenderToAnsi (); |
| 17 | + |
| 18 | + Assert.NotEmpty (result); |
| 19 | + // ANSI escape sequences start with ESC [ |
| 20 | + Assert.Contains ("\x1b[", result); |
| 21 | + // The text content should be present |
| 22 | + Assert.Contains ("Hello", result); |
| 23 | + Assert.Contains ("World", result); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void RenderToAnsi_WithMarkdownParameter_OverridesText () |
| 28 | + { |
| 29 | + Terminal.Gui.Views.Markdown view = new () { Text = "Original" }; |
| 30 | + |
| 31 | + string result = view.RenderToAnsi ("**Override**"); |
| 32 | + |
| 33 | + Assert.Contains ("Override", result); |
| 34 | + Assert.DoesNotContain ("Original", result); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void RenderToAnsi_EmptyText_ReturnsEmpty () |
| 39 | + { |
| 40 | + Terminal.Gui.Views.Markdown view = new (); |
| 41 | + |
| 42 | + string result = view.RenderToAnsi (""); |
| 43 | + |
| 44 | + Assert.Equal (string.Empty, result); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void RenderToAnsi_NullMarkdownUsesInstanceText () |
| 49 | + { |
| 50 | + Terminal.Gui.Views.Markdown view = new () { Text = "# Title" }; |
| 51 | + |
| 52 | + string result = view.RenderToAnsi (null); |
| 53 | + |
| 54 | + Assert.Contains ("Title", result); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void RenderToAnsi_WidthAffectsWrapping () |
| 59 | + { |
| 60 | + string longLine = "This is a very long line that should be wrapped when the width is narrow enough to force wrapping behavior."; |
| 61 | + Terminal.Gui.Views.Markdown view = new () { Text = longLine }; |
| 62 | + |
| 63 | + string narrow = view.RenderToAnsi (width: 20); |
| 64 | + string wide = view.RenderToAnsi (width: 200); |
| 65 | + |
| 66 | + // Narrow output should have more newlines (more lines due to wrapping) |
| 67 | + int narrowNewlines = narrow.Split ('\n').Length; |
| 68 | + int wideNewlines = wide.Split ('\n').Length; |
| 69 | + Assert.True (narrowNewlines > wideNewlines, $"Narrow ({narrowNewlines} lines) should have more lines than wide ({wideNewlines} lines)"); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void RenderToAnsi_WithSyntaxHighlighter_ProducesOutput () |
| 74 | + { |
| 75 | + Terminal.Gui.Views.Markdown view = new () |
| 76 | + { |
| 77 | + Text = "```csharp\nint x = 42;\n```", |
| 78 | + SyntaxHighlighter = new TextMateSyntaxHighlighter () |
| 79 | + }; |
| 80 | + |
| 81 | + string result = view.RenderToAnsi (); |
| 82 | + |
| 83 | + Assert.NotEmpty (result); |
| 84 | + Assert.Contains ("42", result); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void RenderToAnsi_SmallWidth_ClampsToMinimum () |
| 89 | + { |
| 90 | + Terminal.Gui.Views.Markdown view = new () { Text = "Hello" }; |
| 91 | + |
| 92 | + // Width below MIN_WRAP_WIDTH (4) should not throw |
| 93 | + string result = view.RenderToAnsi (width: 1); |
| 94 | + |
| 95 | + Assert.NotEmpty (result); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void RenderToAnsi_DoesNotMutateInstance () |
| 100 | + { |
| 101 | + Terminal.Gui.Views.Markdown view = new () { Text = "# Original" }; |
| 102 | + |
| 103 | + _ = view.RenderToAnsi ("# Different"); |
| 104 | + |
| 105 | + // Original text should be unchanged |
| 106 | + Assert.Equal ("# Original", view.Text); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public void RenderToAnsi_UseThemeBackground_False_ProducesOutput () |
| 111 | + { |
| 112 | + Terminal.Gui.Views.Markdown view = new () |
| 113 | + { |
| 114 | + Text = "# Hello", |
| 115 | + UseThemeBackground = false |
| 116 | + }; |
| 117 | + |
| 118 | + string result = view.RenderToAnsi (); |
| 119 | + |
| 120 | + Assert.NotEmpty (result); |
| 121 | + Assert.Contains ("Hello", result); |
| 122 | + } |
| 123 | +} |
0 commit comments