CellTests.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System.Text;
  2. namespace DrawingTests;
  3. public class CellTests
  4. {
  5. [Fact]
  6. public void Constructor_Defaults ()
  7. {
  8. var c = new Cell ();
  9. Assert.True (c is { });
  10. Assert.Empty (c.Runes);
  11. Assert.Null (c.Attribute);
  12. Assert.False (c.IsDirty);
  13. Assert.Null (c.Grapheme);
  14. }
  15. [Theory]
  16. [InlineData (null, new uint [] { })]
  17. [InlineData ("", new uint [] { })]
  18. [InlineData ("a", new uint [] { 0x0061 })]
  19. [InlineData ("👩‍❤️‍💋‍👨", new uint [] { 0x1F469, 0x200D, 0x2764, 0xFE0F, 0x200D, 0x1F48B, 0x200D, 0x1F468 })]
  20. [InlineData ("æ", new uint [] { 0x00E6 })]
  21. [InlineData ("a︠", new uint [] { 0x0061, 0xFE20 })]
  22. [InlineData ("e︡", new uint [] { 0x0065, 0xFE21 })]
  23. [InlineData ("🇵🇹", new uint [] { 0x1F1F5, 0x1F1F9 })]
  24. public void Runes_From_Grapheme (string? grapheme, uint [] expected)
  25. {
  26. // Arrange
  27. var c = new Cell { Grapheme = grapheme! };
  28. // Act
  29. Rune [] runes = expected.Select (u => new Rune (u)).ToArray ();
  30. // Assert
  31. Assert.Equal (grapheme, c.Grapheme);
  32. Assert.Equal (runes, c.Runes);
  33. }
  34. [Fact]
  35. public void Equals_False ()
  36. {
  37. var c1 = new Cell ();
  38. var c2 = new Cell
  39. {
  40. Grapheme = "a", Attribute = new (Color.Red)
  41. };
  42. Assert.False (c1.Equals (c2));
  43. Assert.False (c2.Equals (c1));
  44. c1.Grapheme = "a";
  45. c1.Attribute = new ();
  46. Assert.Equal (c1.Grapheme, c2.Grapheme);
  47. Assert.False (c1.Equals (c2));
  48. Assert.False (c2.Equals (c1));
  49. }
  50. [Fact]
  51. public void Set_Text_With_Invalid_Grapheme_Throws ()
  52. {
  53. Assert.Throws<InvalidOperationException> (() => new Cell { Grapheme = "ab" });
  54. Assert.Throws<InvalidOperationException> (() => new Cell { Grapheme = "\u0061\u0062" }); // ab
  55. }
  56. [Theory]
  57. [MemberData (nameof (ToStringTestData))]
  58. public void ToString_Override (string text, Attribute? attribute, string expected)
  59. {
  60. var c = new Cell (attribute, true, text);
  61. string result = c.ToString ();
  62. Assert.Equal (expected, result);
  63. }
  64. public static IEnumerable<object? []> ToStringTestData ()
  65. {
  66. yield return ["", null, "[\"\":]"];
  67. yield return ["a", null, "[\"a\":]"];
  68. yield return ["\t", null, "[\"\\t\":]"];
  69. yield return ["\r", null, "[\"\\r\":]"];
  70. yield return ["\n", null, "[\"\\n\":]"];
  71. yield return ["\r\n", null, "[\"\\r\\n\":]"];
  72. yield return ["\f", null, "[\"\\f\":]"];
  73. yield return ["\v", null, "[\"\\v\":]"];
  74. yield return ["\x1B", null, "[\"\\u001B\":]"];
  75. yield return ["\\", new Attribute (Color.Blue), "[\"\\\":[Blue,Blue,None]]"];
  76. yield return ["😀", null, "[\"😀\":]"];
  77. yield return ["👨‍👩‍👦‍👦", null, "[\"👨‍👩‍👦‍👦\":]"];
  78. yield return ["A", new Attribute (Color.Red) { Style = TextStyle.Blink }, "[\"A\":[Red,Red,Blink]]"];
  79. yield return ["\U0001F469\u200D\u2764\uFE0F\u200D\U0001F48B\u200D\U0001F468", null, "[\"👩‍❤️‍💋‍👨\":]"];
  80. yield return ["\uD83C\uDDF5\uD83C\uDDF9", null, "[\"🇵🇹\":]"];
  81. }
  82. [Fact]
  83. public void Graphemes_Decomposed_Normalize ()
  84. {
  85. Cell c1 = new ()
  86. {
  87. // 'e' + '◌́' COMBINING ACUTE ACCENT (U+0301)
  88. Grapheme = "e\u0301" // visually "é"
  89. };
  90. Cell c2 = new ()
  91. {
  92. // NFC single code point (U+00E9)
  93. Grapheme = "é"
  94. };
  95. // Validation
  96. Assert.Equal ("é", c1.Grapheme); // Proper normalized grapheme
  97. Assert.Equal (c1.Grapheme, c2.Grapheme);
  98. Assert.Equal (c1.Runes.Count, c2.Runes.Count);
  99. Assert.Equal (new (0x00E9), c2.Runes [0]);
  100. }
  101. [Fact]
  102. public void Cell_IsDirty_Flag_Works ()
  103. {
  104. var c = new Cell ();
  105. Assert.False (c.IsDirty);
  106. c.IsDirty = true;
  107. Assert.True (c.IsDirty);
  108. c.IsDirty = false;
  109. Assert.False (c.IsDirty);
  110. }
  111. [Theory]
  112. [InlineData ("\uFDD0", false)]
  113. [InlineData ("\uFDEF", false)]
  114. [InlineData ("\uFFFE", true)]
  115. [InlineData ("\uFFFF", false)]
  116. [InlineData ("\U0001FFFE", false)]
  117. [InlineData ("\U0001FFFF", false)]
  118. [InlineData ("\U0010FFFE", false)]
  119. [InlineData ("\U0010FFFF", false)]
  120. public void IsNormalized_ArgumentException (string text, bool throws)
  121. {
  122. try
  123. {
  124. bool normalized = text.IsNormalized (NormalizationForm.FormC);
  125. Assert.True (normalized);
  126. Assert.False (throws);
  127. }
  128. catch (ArgumentException)
  129. {
  130. Assert.True (throws);
  131. }
  132. Assert.Null (Record.Exception (() => new Cell { Grapheme = text }));
  133. }
  134. [Fact]
  135. public void Surrogate_Normalize_Throws_And_Cell_Setter_Throws ()
  136. {
  137. // Create the lone high surrogate at runtime (safe)
  138. string s = new string ((char)0xD800, 1);
  139. // Confirm the runtime string actually contains the surrogate
  140. Assert.Equal (0xD800, s [0]);
  141. // Normalize should throw
  142. Assert.Throws<ArgumentException> (() => s.Normalize (NormalizationForm.FormC));
  143. // And if your Grapheme setter normalizes, assignment should throw as well
  144. Assert.Throws<ArgumentException> (() => new Cell () { Grapheme = s });
  145. // Create the lone low surrogate at runtime (safe)
  146. s = new string ((char)0xDC00, 1);
  147. // Confirm the runtime string actually contains the surrogate
  148. Assert.Equal (0xDC00, s [0]);
  149. // Normalize should throw
  150. Assert.Throws<ArgumentException> (() => s.Normalize (NormalizationForm.FormC));
  151. // And if your Grapheme setter normalizes, assignment should throw as well
  152. Assert.Throws<ArgumentException> (() => new Cell () { Grapheme = s });
  153. }
  154. }