ExpressionObserverTests.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Collections;
  4. using Avalonia.Markup.Data;
  5. using Avalonia.UnitTests;
  6. using JetBrains.dotMemoryUnit;
  7. using Xunit;
  8. using Xunit.Abstractions;
  9. namespace Avalonia.LeakTests
  10. {
  11. [DotMemoryUnit(FailIfRunWithoutSupport = false)]
  12. public class ExpressionObserverTests
  13. {
  14. public ExpressionObserverTests(ITestOutputHelper atr)
  15. {
  16. DotMemoryUnitTestOutput.SetOutputMethod(atr.WriteLine);
  17. }
  18. [Fact]
  19. public void Should_Not_Keep_Source_Alive_ObservableCollection()
  20. {
  21. Func<ExpressionObserver> run = () =>
  22. {
  23. var source = new { Foo = new AvaloniaList<string> {"foo", "bar"} };
  24. var target = new ExpressionObserver(source, "Foo");
  25. target.Subscribe(_ => { });
  26. return target;
  27. };
  28. var result = run();
  29. dotMemory.Check(memory =>
  30. Assert.Equal(0, memory.GetObjects(where => where.Type.Is<AvaloniaList<string>>()).ObjectsCount));
  31. }
  32. [Fact]
  33. public void Should_Not_Keep_Source_Alive_ObservableCollection_With_DataValidation()
  34. {
  35. Func<ExpressionObserver> run = () =>
  36. {
  37. var source = new { Foo = new AvaloniaList<string> { "foo", "bar" } };
  38. var target = new ExpressionObserver(source, "Foo", true);
  39. target.Subscribe(_ => { });
  40. return target;
  41. };
  42. var result = run();
  43. dotMemory.Check(memory =>
  44. Assert.Equal(0, memory.GetObjects(where => where.Type.Is<AvaloniaList<string>>()).ObjectsCount));
  45. }
  46. [Fact]
  47. public void Should_Not_Keep_Source_Alive_NonIntegerIndexer()
  48. {
  49. Func<ExpressionObserver> run = () =>
  50. {
  51. var source = new { Foo = new NonIntegerIndexer() };
  52. var target = new ExpressionObserver(source, "Foo");
  53. target.Subscribe(_ => { });
  54. return target;
  55. };
  56. var result = run();
  57. dotMemory.Check(memory =>
  58. Assert.Equal(0, memory.GetObjects(where => where.Type.Is<NonIntegerIndexer>()).ObjectsCount));
  59. }
  60. private class NonIntegerIndexer : NotifyingBase
  61. {
  62. private readonly Dictionary<string, string> _storage = new Dictionary<string, string>();
  63. public string this[string key]
  64. {
  65. get
  66. {
  67. return _storage[key];
  68. }
  69. set
  70. {
  71. _storage[key] = value;
  72. RaisePropertyChanged(CommonPropertyNames.IndexerName);
  73. }
  74. }
  75. }
  76. }
  77. }