StringTests.cs 2.0 KB

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