ExpressionObserverBuilderTests_Negation.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Reactive.Linq;
  5. using System.Threading.Tasks;
  6. using Avalonia.Data;
  7. using Avalonia.Markup.Parsers;
  8. using Xunit;
  9. namespace Avalonia.Markup.UnitTests.Parsers
  10. {
  11. public class ExpressionObserverBuilderTests_Negation
  12. {
  13. [Fact]
  14. public async Task Should_Negate_0()
  15. {
  16. var data = new { Foo = 0 };
  17. var target = ExpressionObserverBuilder.Build(data, "!Foo");
  18. var result = await target.Take(1);
  19. Assert.True((bool)result);
  20. GC.KeepAlive(data);
  21. }
  22. [Fact]
  23. public async Task Should_Negate_1()
  24. {
  25. var data = new { Foo = 1 };
  26. var target = ExpressionObserverBuilder.Build(data, "!Foo");
  27. var result = await target.Take(1);
  28. Assert.False((bool)result);
  29. GC.KeepAlive(data);
  30. }
  31. [Fact]
  32. public async Task Should_Negate_False_String()
  33. {
  34. var data = new { Foo = "false" };
  35. var target = ExpressionObserverBuilder.Build(data, "!Foo");
  36. var result = await target.Take(1);
  37. Assert.True((bool)result);
  38. GC.KeepAlive(data);
  39. }
  40. [Fact]
  41. public async Task Should_Negate_True_String()
  42. {
  43. var data = new { Foo = "True" };
  44. var target = ExpressionObserverBuilder.Build(data, "!Foo");
  45. var result = await target.Take(1);
  46. Assert.False((bool)result);
  47. GC.KeepAlive(data);
  48. }
  49. [Fact]
  50. public async Task Should_Return_BindingNotification_For_String_Not_Convertible_To_Boolean()
  51. {
  52. var data = new { Foo = "foo" };
  53. var target = ExpressionObserverBuilder.Build(data, "!Foo");
  54. var result = await target.Take(1);
  55. Assert.Equal(
  56. new BindingNotification(
  57. new InvalidCastException($"Unable to convert 'foo' to bool."),
  58. BindingErrorType.Error),
  59. result);
  60. GC.KeepAlive(data);
  61. }
  62. [Fact]
  63. public async Task Should_Return_BindingNotification_For_Value_Not_Convertible_To_Boolean()
  64. {
  65. var data = new { Foo = new object() };
  66. var target = ExpressionObserverBuilder.Build(data, "!Foo");
  67. var result = await target.Take(1);
  68. Assert.Equal(
  69. new BindingNotification(
  70. new InvalidCastException($"Unable to convert 'System.Object' to bool."),
  71. BindingErrorType.Error),
  72. result);
  73. GC.KeepAlive(data);
  74. }
  75. [Fact]
  76. public void SetValue_Should_Return_False_For_Invalid_Value()
  77. {
  78. var data = new { Foo = "foo" };
  79. var target = ExpressionObserverBuilder.Build(data, "!Foo");
  80. target.Subscribe(_ => { });
  81. Assert.False(target.SetValue("bar"));
  82. GC.KeepAlive(data);
  83. }
  84. private class Test
  85. {
  86. public bool Foo { get; set; }
  87. }
  88. }
  89. }