TestRunnerCallbacks.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.TestTools.TestRunner.Api;
  4. using UnityEngine;
  5. namespace Microsoft.Unity.VisualStudio.Editor.Testing
  6. {
  7. internal class TestRunnerCallbacks : ICallbacks
  8. {
  9. private string Serialize<TContainer, TSource, TAdaptor>(
  10. TSource source,
  11. Func<TSource, int, TAdaptor> createAdaptor,
  12. Func<TSource, IEnumerable<TSource>> children,
  13. Func<TAdaptor[], TContainer> container)
  14. {
  15. var adaptors = new List<TAdaptor>();
  16. void AddAdaptor(TSource item, int parentIndex)
  17. {
  18. var index = adaptors.Count;
  19. adaptors.Add(createAdaptor(item, parentIndex));
  20. foreach (var child in children(item))
  21. AddAdaptor(child, index);
  22. }
  23. AddAdaptor(source, -1);
  24. return JsonUtility.ToJson(container(adaptors.ToArray()));
  25. }
  26. private string Serialize(ITestAdaptor testAdaptor)
  27. {
  28. return Serialize(
  29. testAdaptor,
  30. (a, parentIndex) => new TestAdaptor(a, parentIndex),
  31. (a) => a.Children,
  32. (r) => new TestAdaptorContainer { TestAdaptors = r });
  33. }
  34. private string Serialize(ITestResultAdaptor testResultAdaptor)
  35. {
  36. return Serialize(
  37. testResultAdaptor,
  38. (a, parentIndex) => new TestResultAdaptor(a, parentIndex),
  39. (a) => a.Children,
  40. (r) => new TestResultAdaptorContainer { TestResultAdaptors = r });
  41. }
  42. public void RunFinished(ITestResultAdaptor testResultAdaptor)
  43. {
  44. VisualStudioIntegration.BroadcastMessage(Messaging.MessageType.RunFinished, Serialize(testResultAdaptor));
  45. }
  46. public void RunStarted(ITestAdaptor testAdaptor)
  47. {
  48. VisualStudioIntegration.BroadcastMessage(Messaging.MessageType.RunStarted, Serialize(testAdaptor));
  49. }
  50. public void TestFinished(ITestResultAdaptor testResultAdaptor)
  51. {
  52. VisualStudioIntegration.BroadcastMessage(Messaging.MessageType.TestFinished, Serialize(testResultAdaptor));
  53. }
  54. public void TestStarted(ITestAdaptor testAdaptor)
  55. {
  56. VisualStudioIntegration.BroadcastMessage(Messaging.MessageType.TestStarted, Serialize(testAdaptor));
  57. }
  58. private static string TestModeName(TestMode testMode)
  59. {
  60. switch (testMode)
  61. {
  62. case TestMode.EditMode: return "EditMode";
  63. case TestMode.PlayMode: return "PlayMode";
  64. }
  65. throw new ArgumentOutOfRangeException();
  66. }
  67. internal void TestListRetrieved(TestMode testMode, ITestAdaptor testAdaptor)
  68. {
  69. // TestListRetrieved format:
  70. // TestMode:Json
  71. var value = TestModeName(testMode) + ":" + Serialize(testAdaptor);
  72. VisualStudioIntegration.BroadcastMessage(Messaging.MessageType.TestListRetrieved, value);
  73. }
  74. }
  75. }