DocumentParsingTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #nullable enable
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using Content.Client.Guidebook;
  5. using Content.Client.Guidebook.Richtext;
  6. using Robust.Client.UserInterface;
  7. using Robust.Client.UserInterface.Controls;
  8. namespace Content.IntegrationTests.Tests.Guidebook;
  9. /// <summary>
  10. /// This test checks that an example document string properly gets parsed by the <see cref="DocumentParsingManager"/>.
  11. /// </summary>
  12. [TestFixture]
  13. [TestOf(typeof(DocumentParsingManager))]
  14. public sealed class DocumentParsingTest
  15. {
  16. public string TestDocument = @"multiple
  17. lines
  18. separated by
  19. only single newlines
  20. make a single rich text control
  21. unless there is a double newline. Also
  22. whitespace before newlines are ignored.
  23. <TestControl/>
  24. < TestControl />
  25. <TestControl>
  26. some text with a nested control
  27. <TestControl/>
  28. </TestControl>
  29. <TestControl key1=""value1"" key2=""value2 with spaces"" key3=""value3 with a
  30. newline""/>
  31. <TestControl >
  32. <TestControl k=""<\>\\>=\""=<-_?*3.0//"">
  33. </TestControl>
  34. </TestControl>";
  35. [Test]
  36. public async Task ParseTestDocument()
  37. {
  38. await using var pair = await PoolManager.GetServerClient();
  39. var client = pair.Client;
  40. await client.WaitIdleAsync();
  41. var parser = client.ResolveDependency<DocumentParsingManager>();
  42. Control ctrl = default!;
  43. await client.WaitPost(() =>
  44. {
  45. ctrl = new Control();
  46. Assert.That(parser.TryAddMarkup(ctrl, TestDocument));
  47. });
  48. Assert.That(ctrl.ChildCount, Is.EqualTo(7));
  49. var richText1 = ctrl.GetChild(0) as RichTextLabel;
  50. var richText2 = ctrl.GetChild(1) as RichTextLabel;
  51. Assert.Multiple(() =>
  52. {
  53. Assert.That(richText1, Is.Not.Null);
  54. Assert.That(richText2, Is.Not.Null);
  55. });
  56. // uhh.. WTF. rich text has no means of getting the contents!?!?
  57. // TODO assert text content is correct after fixing that bullshit.
  58. // Assert.That(richText1?.Text, Is.EqualTo("multiple lines separated by only single newlines make a single rich text control"));
  59. // Assert.That(richText2?.Text, Is.EqualTo("unless there is a double newline. Also whitespace before newlines are ignored."));
  60. var test1 = ctrl.GetChild(2) as TestControl;
  61. var test2 = ctrl.GetChild(3) as TestControl;
  62. var test3 = ctrl.GetChild(4) as TestControl;
  63. var test4 = ctrl.GetChild(5) as TestControl;
  64. var test5 = ctrl.GetChild(6) as TestControl;
  65. Assert.Multiple(() =>
  66. {
  67. Assert.That(test1, Is.Not.Null);
  68. Assert.That(test2, Is.Not.Null);
  69. Assert.That(test3, Is.Not.Null);
  70. Assert.That(test4, Is.Not.Null);
  71. Assert.That(test5, Is.Not.Null);
  72. });
  73. Assert.Multiple(() =>
  74. {
  75. Assert.That(test1!.ChildCount, Is.EqualTo(0));
  76. Assert.That(test2!.ChildCount, Is.EqualTo(0));
  77. Assert.That(test3!.ChildCount, Is.EqualTo(2));
  78. Assert.That(test4!.ChildCount, Is.EqualTo(0));
  79. Assert.That(test5!.ChildCount, Is.EqualTo(1));
  80. });
  81. var subText = test3!.GetChild(0) as RichTextLabel;
  82. var subTest = test3.GetChild(1) as TestControl;
  83. #pragma warning disable NUnit2045
  84. Assert.That(subText, Is.Not.Null);
  85. //Assert.That(subText?.Text, Is.EqualTo("some text with a nested control"));
  86. Assert.That(subTest, Is.Not.Null);
  87. Assert.That(subTest?.ChildCount, Is.EqualTo(0));
  88. #pragma warning restore NUnit2045
  89. var subTest2 = test5!.GetChild(0) as TestControl;
  90. Assert.That(subTest2, Is.Not.Null);
  91. Assert.That(subTest2!.ChildCount, Is.EqualTo(0));
  92. Assert.Multiple(() =>
  93. {
  94. Assert.That(test1!.Params, Has.Count.EqualTo(0));
  95. Assert.That(test2!.Params, Has.Count.EqualTo(0));
  96. Assert.That(test3.Params, Has.Count.EqualTo(0));
  97. Assert.That(test4!.Params, Has.Count.EqualTo(3));
  98. Assert.That(test5.Params, Has.Count.EqualTo(0));
  99. Assert.That(subTest2.Params, Has.Count.EqualTo(1));
  100. });
  101. test4!.Params.TryGetValue("key1", out var val);
  102. Assert.That(val, Is.EqualTo("value1"));
  103. test4.Params.TryGetValue("key2", out val);
  104. Assert.That(val, Is.EqualTo("value2 with spaces"));
  105. test4.Params.TryGetValue("key3", out val);
  106. Assert.That(val, Is.EqualTo(@"value3 with a
  107. newline"));
  108. subTest2.Params.TryGetValue("k", out val);
  109. Assert.That(val, Is.EqualTo(@"<>\>=""=<-_?*3.0//"));
  110. await pair.CleanReturnAsync();
  111. }
  112. public sealed class TestControl : Control, IDocumentTag
  113. {
  114. public Dictionary<string, string> Params = default!;
  115. public bool TryParseTag(Dictionary<string, string> param, [NotNullWhen(true)] out Control control)
  116. {
  117. Params = param;
  118. control = this;
  119. return true;
  120. }
  121. }
  122. }