PageRenderTime 1225ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/HartAnalyzer/HartAnalyzer.Services.UnitTest/_HartCommunicationService/PortState.cs

#
C# | 89 lines | 75 code | 14 blank | 0 comment | 4 complexity | 1f0fce1d9031aef45c414dc85308a0cc MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq.Expressions;
  5. using Communication.Hart;
  6. using FluentAssertions;
  7. using Moq;
  8. using NUnit.Framework;
  9. namespace HartAnalyzer.Services.UnitTest._HartCommunicationService
  10. {
  11. [TestFixture]
  12. public class PortState
  13. {
  14. [Test]
  15. public void DefaultPortStateShouldBeClosed()
  16. {
  17. var mock = new Mock<IHartCommunication>();
  18. var service = new HartCommunicationService(mock.Object);
  19. service.PortState.Should().Be(Services.PortState.Closed);
  20. }
  21. [Test]
  22. public async void ShouldSetPortStateFromOpeningToOpened()
  23. {
  24. var mock = new Mock<IHartCommunication>();
  25. mock.Setup(item => item.Open()).Returns(OpenResult.Opened);
  26. var service = new HartCommunicationService(mock.Object);
  27. var states = new List<Services.PortState>();
  28. service.LogChanges(() => service.PortState, states);
  29. await service.OpenAsync();
  30. states.Count.Should().Be(2);
  31. states[0].Should().Be(Services.PortState.Opening);
  32. states[1].Should().Be(Services.PortState.Opened);
  33. }
  34. [Test]
  35. public async void ShouldSetPortStateFromOpenedToClosed()
  36. {
  37. var mock = new Mock<IHartCommunication>();
  38. mock.Setup(item => item.Close()).Returns(CloseResult.Closed);
  39. var service = new HartCommunicationService(mock.Object) {PortState = Services.PortState.Opened};
  40. var states = new Queue<Services.PortState>();
  41. states.Enqueue(Services.PortState.Closing);
  42. states.Enqueue(Services.PortState.Closed);
  43. service.LogChanges(() => service.PortState, states);
  44. await service.CloseAsync();
  45. states.Count.Should().Be(0);
  46. }
  47. }
  48. public static class UnitTestHelper
  49. {
  50. public static void LogChanges<TNotify, TProperty>(this TNotify notifyObject, Expression<Func<TProperty>> property, List<TProperty> history)
  51. where TNotify : INotifyPropertyChanged
  52. {
  53. var name = ((MemberExpression)property.Body).Member.Name;
  54. notifyObject.PropertyChanged += (sender, args) =>
  55. {
  56. if (name != args.PropertyName)
  57. return;
  58. var compile = property.Compile();
  59. history.Add(compile());
  60. };
  61. }
  62. public static void LogChanges<TNotify, TProperty>(this TNotify notifyObject, Expression<Func<TProperty>> property, Queue<TProperty> expects)
  63. where TNotify : INotifyPropertyChanged
  64. {
  65. var name = ((MemberExpression)property.Body).Member.Name;
  66. notifyObject.PropertyChanged += (sender, args) =>
  67. {
  68. if (name != args.PropertyName)
  69. return;
  70. var compile = property.Compile();
  71. Assert.That(compile(), Is.EqualTo(expects.Dequeue()));
  72. };
  73. }
  74. }
  75. }