Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using FootballClub.UI.Wrapper;
- namespace FootballClub.UI {
- public class ClubWrapper : ModelWrapper<Model.FootballClub> {
- public ClubWrapper(Model.FootballClub model) : base(model) {
- }
- public string Name {
- get { return GetValue<string>(); }
- set { SetValue(value); }
- }
- public int Id {
- get { return GetValue<int>(); }
- set { SetValue(value); }
- }
- public DateTime DateOfIncorporation {
- get { return GetValue<DateTime>(); }
- set { SetValue(value); }
- }
- public AddressWrapper Address { get; }
- }
- }
- --
- using FootballClub.Model;
- namespace FootballClub.UI.Wrapper {
- public class AddressWrapper : ModelWrapper<Address> {
- public AddressWrapper(Address model) : base(model) {
- }
- public string FirstLine {
- get {
- return GetValue<string>();
- }
- set { SetValue(value); }
- }
- }
- }
- --
- using System;
- using System.Runtime.CompilerServices;
- using FootballClub.UI.ViewModel;
- namespace FootballClub.UI.Wrapper {
- public class ModelWrapper<T> : Observable {
- public ModelWrapper(T model) {
- if (model == null) {
- throw new ArgumentNullException("ModelWrapper: 1");
- }
- Model = model;
- }
- public T Model { get; }
- internal TValue GetValue<TValue>([CallerMemberName] string PropertyName = null) {
- var propertyInfo = Model.GetType().GetProperty(PropertyName);
- return (TValue)(propertyInfo.GetValue(Model));
- }
- internal void SetValue<TValue>(TValue value, [CallerMemberName] string PropertyName = null) {
- var propertyInfo = Model.GetType().GetProperty(PropertyName);
- var currentValue = propertyInfo.GetValue(Model);
- if (Equals(currentValue, value)) return;
- propertyInfo.SetValue(Model, value);
- OnPropertyChanged(PropertyName);
- }
- }
- }
- --
- using System;
- using FootballClub.Model;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- namespace FootballClub.UnitTests {
- [TestClass]
- public class BasicTests {
- private Model.FootballClub _footballClub;
- [TestInitialize]
- public void Initialize() {
- _footballClub = new Model.FootballClub {
- Id = 101,
- Name = "Tottenham Hotspur",
- DateOfIncorporation = new DateTime(1897, 1, 1),
- Address = new Address { FirstLine = "White Hart Lane", City = "London", PostCode = "N17 6QN", County = "Middlesex", Country = "UK" },
- Emails = new System.Collections.Generic.List<Email> { new Email { EmailAddress = "tottenham@spurs.com" } }
- };
- }
- [TestMethod]
- public void WrapperContainsReferenceToModel() {
- var wrapper = new UI.ClubWrapper(_footballClub);
- Assert.AreEqual(_footballClub, wrapper.Model);
- }
- [TestMethod]
- [ExpectedException(typeof(ArgumentNullException))]
- public void ModelPassedToWrapperMustNotBeNull() {
- var wrapper = new UI.ClubWrapper(null);
- }
- [TestMethod]
- public void SimpleNamePropertyIsGotViaWrapper() {
- _footballClub.Name = "Arsenal";
- var wrapper = new UI.ClubWrapper(_footballClub);
- Assert.AreEqual(_footballClub.Name, wrapper.Name);
- }
- [TestMethod]
- public void SimpleIdPropertyIsGotViaWrapper() {
- _footballClub.Id = 2020;
- var wrapper = new UI.ClubWrapper(_footballClub);
- Assert.AreEqual(_footballClub.Id, wrapper.Id);
- }
- [TestMethod]
- public void SimpleDateOfIncorporationPropertyIsGotViaWrapper() {
- _footballClub.DateOfIncorporation = new DateTime(1822,5,5);
- var wrapper = new UI.ClubWrapper(_footballClub);
- Assert.AreEqual(_footballClub.DateOfIncorporation, wrapper.DateOfIncorporation);
- }
- [TestMethod]
- public void SimpleNamePropertyIsSetViaWrapper() {
- var wrapper = new UI.ClubWrapper(_footballClub);
- wrapper.Name = "Arsenal";
- Assert.AreEqual("Arsenal", _footballClub.Name);
- }
- [TestMethod]
- public void SimpleIdPropertyIsSetViaWrapper() {
- var wrapper = new UI.ClubWrapper(_footballClub);
- wrapper.Id = 3030;
- Assert.AreEqual(wrapper.Id, _footballClub.Id);
- }
- [TestMethod]
- public void SimpleDateOfIncorporationPropertyIsSetViaWrapper() {
- var wrapper = new UI.ClubWrapper(_footballClub);
- wrapper.DateOfIncorporation = new DateTime(1842, 5, 5);
- Assert.AreEqual(wrapper.DateOfIncorporation, _footballClub.DateOfIncorporation);
- }
- }
- }
- --
- using System;
- using FootballClub.Model;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using System.ComponentModel;
- namespace FootballClub.UnitTests {
- [TestClass]
- public class PropertyNotificationTests {
- private Model.FootballClub _footballClub;
- [TestInitialize]
- public void Initialize() {
- _footballClub = new Model.FootballClub {
- Id = 101,
- Name = "Tottenham Hotspur",
- DateOfIncorporation = new DateTime(1897, 1, 1),
- Address = new Address { FirstLine = "White Hart Lane", City = "London", PostCode = "N17 6QN", County = "Middlesex", Country = "UK" },
- Emails = new System.Collections.Generic.List<Email> { new Email { EmailAddress = "tottenham@spurs.com" } }
- };
- }
- [TestMethod]
- public void NameEventFiresOnPropertyChange() {
- bool fired = false;
- var wrapper = new UI.ClubWrapper(_footballClub);
- wrapper.PropertyChanged += (s, e) => {
- fired = true;
- };
- wrapper.Name = "Darlington FC";
- Assert.IsTrue(fired, "[Name event was not called]");
- }
- [TestMethod]
- public void NameEventDoesNotFiresIfRemainsAsWas() {
- bool fired = false;
- var wrapper = new UI.ClubWrapper(_footballClub);
- wrapper.PropertyChanged += (s, e) => {
- fired = true;
- };
- wrapper.Name = "Tottenham Hotspur";
- Assert.IsFalse(fired, "[Property change event was called]");
- }
- }
- }
- --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement