StringTests.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. namespace Terminal.Gui.TextTests;
  2. #nullable enable
  3. public class StringTests
  4. {
  5. [Fact]
  6. public void TestGetColumns_Empty ()
  7. {
  8. var str = string.Empty;
  9. Assert.Equal (0, str.GetColumns ());
  10. }
  11. [Theory]
  12. [InlineData ("a", 1)]
  13. [InlineData ("á", 1)]
  14. [InlineData ("ab", 2)]
  15. [InlineData ("áé", 2)]
  16. [InlineData ("abc", 3)]
  17. [InlineData ("áéí", 3)]
  18. [InlineData ("abcd", 4)]
  19. public void TestGetColumns_MultiRune (string str, int expected) { Assert.Equal (expected, str.GetColumns ()); }
  20. // Test non-BMP codepoints
  21. // Face with Tears of Joy Emoji (😂), Unicode U+1F602 is 2 columns wide
  22. [Theory]
  23. [InlineData ("😂", 2)]
  24. [InlineData ("😂😂", 4)]
  25. public void TestGetColumns_MultiRune_NonBMP (string str, int expected) { Assert.Equal (expected, str.GetColumns ()); }
  26. // Test known wide codepoints
  27. [Theory]
  28. [InlineData ("🙂", 2)]
  29. [InlineData ("a🙂", 3)]
  30. [InlineData ("🙂a", 3)]
  31. [InlineData ("👨‍👩‍👦‍👦", 8)]
  32. [InlineData ("👨‍👩‍👦‍👦🙂", 10)]
  33. [InlineData ("👨‍👩‍👦‍👦🙂a", 11)]
  34. [InlineData ("👨‍👩‍👦‍👦a🙂", 11)]
  35. [InlineData ("👨‍👩‍👦‍👦👨‍👩‍👦‍👦", 16)]
  36. [InlineData ("山", 2)] // The character for "mountain" in Chinese/Japanese/Korean (山), Unicode U+5C71
  37. [InlineData ("山🙂", 4)] // The character for "mountain" in Chinese/Japanese/Korean (山), Unicode U+5C71
  38. //[InlineData ("\ufe20\ufe21", 2)] // Combining Ligature Left Half ︠ - U+fe20 -https://github.com/microsoft/terminal/blob/main/src/types/unicode_width_overrides.xml
  39. // // Combining Ligature Right Half - U+fe21 -https://github.com/microsoft/terminal/blob/main/src/types/unicode_width_overrides.xml
  40. public void TestGetColumns_MultiRune_WideBMP (string str, int expected) { Assert.Equal (expected, str.GetColumns ()); }
  41. [Fact]
  42. public void TestGetColumns_Null ()
  43. {
  44. string? str = null;
  45. Assert.Equal (0, str!.GetColumns ());
  46. }
  47. [Fact]
  48. public void TestGetColumns_SingleRune ()
  49. {
  50. var str = "a";
  51. Assert.Equal (1, str.GetColumns ());
  52. }
  53. }