CellTests.cs 5.7 KB

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