AlertPrototypeTests.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.IO;
  3. using Content.Shared.Alert;
  4. using NUnit.Framework;
  5. using Robust.Shared.IoC;
  6. using Robust.Shared.Serialization.Manager;
  7. using Robust.Shared.Serialization.Markdown.Mapping;
  8. using Robust.Shared.Utility;
  9. using YamlDotNet.RepresentationModel;
  10. namespace Content.Tests.Shared.Alert
  11. {
  12. [TestFixture, TestOf(typeof(AlertPrototype))]
  13. public sealed class AlertPrototypeTests : ContentUnitTest
  14. {
  15. private const string Prototypes = @"
  16. - type: alert
  17. id: HumanHealth
  18. category: Health
  19. icons:
  20. - /Textures/Interface/Alerts/Human/human.rsi/human0.png
  21. - /Textures/Interface/Alerts/Human/human.rsi/human1.png
  22. - /Textures/Interface/Alerts/Human/human.rsi/human2.png
  23. - /Textures/Interface/Alerts/Human/human.rsi/human3.png
  24. - /Textures/Interface/Alerts/Human/human.rsi/human4.png
  25. - /Textures/Interface/Alerts/Human/human.rsi/human5.png
  26. - /Textures/Interface/Alerts/Human/human.rsi/human6.png
  27. name: Health
  28. description: ""[color=green]Green[/color] good. [color=red]Red[/color] bad.""
  29. minSeverity: 0
  30. maxSeverity: 6";
  31. [OneTimeSetUp]
  32. public void OneTimeSetUp()
  33. {
  34. IoCManager.Resolve<ISerializationManager>().Initialize();
  35. }
  36. [Test]
  37. public void TestAlertKey()
  38. {
  39. Assert.That(new AlertKey("HumanHealth", null), Is.Not.EqualTo(AlertKey.ForCategory("Health")));
  40. Assert.That((new AlertKey(null, "Health")), Is.EqualTo(AlertKey.ForCategory("Health")));
  41. Assert.That((new AlertKey("Buckled", "Health")), Is.EqualTo(AlertKey.ForCategory("Health")));
  42. }
  43. [TestCase(0, "/Textures/Interface/Alerts/Human/human.rsi/human0.png")]
  44. [TestCase(1, "/Textures/Interface/Alerts/Human/human.rsi/human1.png")]
  45. [TestCase(6, "/Textures/Interface/Alerts/Human/human.rsi/human6.png")]
  46. public void GetsIconPath(short? severity, string expected)
  47. {
  48. var alert = GetTestPrototype();
  49. Assert.That(alert.GetIcon(severity), Is.EqualTo(new SpriteSpecifier.Texture(new (expected))));
  50. }
  51. [TestCase(null, "/Textures/Interface/Alerts/Human/human.rsi/human0.png")]
  52. [TestCase(7, "/Textures/Interface/Alerts/Human/human.rsi/human1.png")]
  53. public void GetsIconPathThrows(short? severity, string expected)
  54. {
  55. var alert = GetTestPrototype();
  56. try
  57. {
  58. alert.GetIcon(severity);
  59. }
  60. catch (ArgumentException)
  61. {
  62. Assert.Pass();
  63. }
  64. catch (Exception e)
  65. {
  66. Assert.Fail($"Unexpected exception: {e}");
  67. }
  68. }
  69. private AlertPrototype GetTestPrototype()
  70. {
  71. using TextReader stream = new StringReader(Prototypes);
  72. var yamlStream = new YamlStream();
  73. yamlStream.Load(stream);
  74. var document = yamlStream.Documents[0];
  75. var rootNode = (YamlSequenceNode) document.RootNode;
  76. var proto = (YamlMappingNode) rootNode[0];
  77. var serMan = IoCManager.Resolve<ISerializationManager>();
  78. return serMan.Read<AlertPrototype>(new MappingDataNode(proto));
  79. }
  80. }
  81. }