Advertisement
Dennisaa

FootballClub01

Oct 10th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.59 KB | None | 0 0
  1. using System;
  2. using FootballClub.UI.Wrapper;
  3.  
  4. namespace FootballClub.UI {
  5.     public class ClubWrapper : ModelWrapper<Model.FootballClub> {
  6.  
  7.         public ClubWrapper(Model.FootballClub model) : base(model) {
  8.  
  9.         }
  10.         public string Name {
  11.             get { return GetValue<string>(); }
  12.             set { SetValue(value); }
  13.         }
  14.  
  15.         public int Id {
  16.             get { return GetValue<int>(); }
  17.             set { SetValue(value); }
  18.         }
  19.  
  20.         public DateTime DateOfIncorporation {
  21.             get { return GetValue<DateTime>(); }
  22.             set { SetValue(value); }
  23.         }
  24.  
  25.         public AddressWrapper Address { get; }
  26.     }
  27. }
  28. --
  29. using FootballClub.Model;
  30.  
  31. namespace FootballClub.UI.Wrapper {
  32.     public class AddressWrapper : ModelWrapper<Address> {
  33.  
  34.         public AddressWrapper(Address model) : base(model) {
  35.  
  36.         }
  37.  
  38.         public string FirstLine {
  39.             get {
  40.                 return GetValue<string>();
  41.             }
  42.             set { SetValue(value); }
  43.         }
  44.     }
  45. }
  46.  
  47. --
  48. using System;
  49. using System.Runtime.CompilerServices;
  50. using FootballClub.UI.ViewModel;
  51.  
  52. namespace FootballClub.UI.Wrapper {
  53.     public class ModelWrapper<T> : Observable {
  54.  
  55.         public ModelWrapper(T model) {
  56.             if (model == null) {
  57.                 throw new ArgumentNullException("ModelWrapper: 1");
  58.             }
  59.             Model = model;
  60.         }
  61.  
  62.         public T Model { get; }
  63.  
  64.         internal TValue GetValue<TValue>([CallerMemberName] string PropertyName = null) {
  65.             var propertyInfo = Model.GetType().GetProperty(PropertyName);
  66.             return (TValue)(propertyInfo.GetValue(Model));
  67.         }
  68.  
  69.         internal void SetValue<TValue>(TValue value, [CallerMemberName] string PropertyName = null) {
  70.             var propertyInfo = Model.GetType().GetProperty(PropertyName);
  71.             var currentValue = propertyInfo.GetValue(Model);
  72.             if (Equals(currentValue, value)) return;
  73.             propertyInfo.SetValue(Model, value);
  74.             OnPropertyChanged(PropertyName);
  75.         }
  76.     }
  77. }
  78.  
  79. --
  80. using System;
  81. using FootballClub.Model;
  82. using Microsoft.VisualStudio.TestTools.UnitTesting;
  83.  
  84. namespace FootballClub.UnitTests {
  85.     [TestClass]
  86.     public class BasicTests {
  87.  
  88.         private Model.FootballClub _footballClub;
  89.  
  90.         [TestInitialize]
  91.         public void Initialize() {
  92.             _footballClub = new Model.FootballClub {
  93.                 Id = 101,
  94.                 Name = "Tottenham Hotspur",
  95.                 DateOfIncorporation = new DateTime(1897, 1, 1),
  96.                 Address = new Address { FirstLine = "White Hart Lane", City = "London", PostCode = "N17 6QN", County = "Middlesex", Country = "UK" },
  97.                 Emails = new System.Collections.Generic.List<Email> { new Email { EmailAddress = "tottenham@spurs.com" } }
  98.             };
  99.  
  100.         }
  101.         [TestMethod]
  102.         public void WrapperContainsReferenceToModel() {
  103.             var wrapper = new UI.ClubWrapper(_footballClub);
  104.             Assert.AreEqual(_footballClub, wrapper.Model);
  105.         }
  106.  
  107.         [TestMethod]
  108.         [ExpectedException(typeof(ArgumentNullException))]
  109.         public void ModelPassedToWrapperMustNotBeNull() {
  110.             var wrapper = new UI.ClubWrapper(null);
  111.         }
  112.  
  113.         [TestMethod]
  114.         public void SimpleNamePropertyIsGotViaWrapper() {
  115.             _footballClub.Name = "Arsenal";
  116.             var wrapper = new UI.ClubWrapper(_footballClub);
  117.             Assert.AreEqual(_footballClub.Name, wrapper.Name);
  118.         }
  119.  
  120.         [TestMethod]
  121.         public void SimpleIdPropertyIsGotViaWrapper() {
  122.             _footballClub.Id = 2020;
  123.             var wrapper = new UI.ClubWrapper(_footballClub);
  124.             Assert.AreEqual(_footballClub.Id, wrapper.Id);
  125.         }
  126.  
  127.         [TestMethod]
  128.         public void SimpleDateOfIncorporationPropertyIsGotViaWrapper() {
  129.             _footballClub.DateOfIncorporation = new DateTime(1822,5,5);
  130.             var wrapper = new UI.ClubWrapper(_footballClub);
  131.             Assert.AreEqual(_footballClub.DateOfIncorporation, wrapper.DateOfIncorporation);
  132.         }
  133.  
  134.         [TestMethod]
  135.         public void SimpleNamePropertyIsSetViaWrapper() {
  136.             var wrapper = new UI.ClubWrapper(_footballClub);
  137.             wrapper.Name = "Arsenal";
  138.             Assert.AreEqual("Arsenal", _footballClub.Name);
  139.         }
  140.  
  141.         [TestMethod]
  142.         public void SimpleIdPropertyIsSetViaWrapper() {
  143.             var wrapper = new UI.ClubWrapper(_footballClub);
  144.             wrapper.Id = 3030;
  145.             Assert.AreEqual(wrapper.Id, _footballClub.Id);
  146.         }
  147.  
  148.         [TestMethod]
  149.         public void SimpleDateOfIncorporationPropertyIsSetViaWrapper() {
  150.             var wrapper = new UI.ClubWrapper(_footballClub);
  151.             wrapper.DateOfIncorporation = new DateTime(1842, 5, 5);
  152.             Assert.AreEqual(wrapper.DateOfIncorporation, _footballClub.DateOfIncorporation);
  153.         }
  154.     }
  155. }
  156.  
  157. --
  158. using System;
  159. using FootballClub.Model;
  160. using Microsoft.VisualStudio.TestTools.UnitTesting;
  161. using System.ComponentModel;
  162.  
  163. namespace FootballClub.UnitTests {
  164.     [TestClass]
  165.     public class PropertyNotificationTests {
  166.        
  167.         private Model.FootballClub _footballClub;
  168.  
  169.         [TestInitialize]
  170.         public void Initialize() {
  171.             _footballClub = new Model.FootballClub {
  172.                 Id = 101,
  173.                 Name = "Tottenham Hotspur",
  174.                 DateOfIncorporation = new DateTime(1897, 1, 1),
  175.                 Address = new Address { FirstLine = "White Hart Lane", City = "London", PostCode = "N17 6QN", County = "Middlesex", Country = "UK" },
  176.                 Emails = new System.Collections.Generic.List<Email> { new Email { EmailAddress = "tottenham@spurs.com" } }
  177.             };
  178.  
  179.         }
  180.         [TestMethod]
  181.         public void NameEventFiresOnPropertyChange() {
  182.             bool fired = false;
  183.  
  184.             var wrapper = new UI.ClubWrapper(_footballClub);
  185.             wrapper.PropertyChanged += (s, e) => {
  186.                 fired = true;
  187.             };
  188.             wrapper.Name = "Darlington FC";
  189.             Assert.IsTrue(fired, "[Name event was not called]");
  190.         }
  191.  
  192.         [TestMethod]
  193.         public void NameEventDoesNotFiresIfRemainsAsWas() {
  194.             bool fired = false;
  195.  
  196.             var wrapper = new UI.ClubWrapper(_footballClub);
  197.             wrapper.PropertyChanged += (s, e) => {
  198.                 fired = true;
  199.             };
  200.             wrapper.Name = "Tottenham Hotspur";
  201.             Assert.IsFalse(fired, "[Property change event was called]");
  202.         }
  203.     }
  204. }
  205.  
  206. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement