SearcherItemCollectionEquivalentConstraint.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using JetBrains.Annotations;
  6. using NUnit.Framework.Constraints;
  7. namespace UnityEditor.Searcher.Tests
  8. {
  9. [PublicAPI]
  10. class Is : NUnit.Framework.Is
  11. {
  12. public static SearcherItemCollectionEquivalentConstraint SearcherItemCollectionEquivalent(IEnumerable<SearcherItem> expected)
  13. {
  14. return new SearcherItemCollectionEquivalentConstraint(expected);
  15. }
  16. }
  17. class SearcherItemCollectionEquivalentConstraint : CollectionItemsEqualConstraint
  18. {
  19. readonly List<SearcherItem> m_Expected;
  20. public SearcherItemCollectionEquivalentConstraint(IEnumerable<SearcherItem> expected)
  21. : base(expected)
  22. {
  23. m_Expected = expected.ToList();
  24. }
  25. protected override bool Matches(IEnumerable actual)
  26. {
  27. if (m_Expected == null)
  28. {
  29. Description = "Expected is not a valid collection";
  30. return false;
  31. }
  32. if (!(actual is IEnumerable<SearcherItem> actualCollection))
  33. {
  34. Description = "Actual is not a valid collection";
  35. return false;
  36. }
  37. var actualList = actualCollection.ToList();
  38. if (actualList.Count != m_Expected.Count)
  39. {
  40. Description = $"Collections lengths are not equal. \nExpected length: {m_Expected.Count}, " +
  41. $"\nBut was: {actualList.Count}";
  42. return false;
  43. }
  44. for (var i = 0; i < m_Expected.Count; ++i)
  45. {
  46. var res1 = m_Expected[i].Name;
  47. var res2 = actualList[i].Name;
  48. if (!string.Equals(res1, res2))
  49. {
  50. Description = $"Object at index {i} are not the same.\nExpected: {res1},\nBut was: {res2}";
  51. return false;
  52. }
  53. var constraint = new SearcherItemCollectionEquivalentConstraint(m_Expected[i].Children);
  54. if (constraint.Matches(actualList[i].Children))
  55. continue;
  56. Description = constraint.Description;
  57. return false;
  58. }
  59. return true;
  60. }
  61. }
  62. }