Skip to content

Commit

Permalink
#142 - Rewrite Cloning tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Feb 2, 2019
1 parent d36a14c commit 6f9d2aa
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 140 deletions.
34 changes: 0 additions & 34 deletions test/TestConsole/Asyncing/TestAsync.cs

This file was deleted.

101 changes: 0 additions & 101 deletions test/TestConsole/Cloning/TestCloning.cs

This file was deleted.

6 changes: 1 addition & 5 deletions test/TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Threading.Tasks;
using TestConsole.Behaviors;
using TestConsole.BootstrapTasks;
using TestConsole.Cloning;
using TestConsole.Collections;
using TestConsole.Commands;
using TestConsole.Delegates;
Expand All @@ -19,7 +18,6 @@
using TestConsole.PresentationModels;
using TestConsole.Threading;
using TestConsole.DispatcherExceptions;
using TestConsole.Asyncing;

namespace TestConsole
{
Expand All @@ -41,16 +39,14 @@ private static void Main(string[] args)
//TestMultiLockProvider.Test();
//TestDependency.Test();
//TestAppServices.Test();
//TestCloning.Test();
TestBehaviors.Test();
//TestBehaviors.Test();
//TestObjectSize.Test();
//TestSharpKitCompiler.Test();
//TestLog4net.Test();
//TestLocalization.Test();
//Services.Queries.TestQueries.Test();
//TestDispatcherException.Test();
//TestEventManager.Test();
TestAsync.Test();

Console.ReadKey(true);
}
Expand Down
106 changes: 106 additions & 0 deletions test/UnitTest/TestCloning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Neptuo
{
[TestClass]
public class TestCloning
{
[TestMethod]
public void Memberwise()
{
Person p1 = new Person("John", "Doe", 22);
p1.Address = new Address("Main street", 50, "L.A.");
Person p2 = p1.Clone();

AssertEqual(p1, p2);
}

[TestMethod]
public void Generic()
{
Person p1 = new Person("John", "Doe", 22);
p1.Address = new Address("Main street", 50, "L.A.");
Person p2 = p1.Clone();

ICloneable<object> cloneable = p1 as ICloneable<object>;
Assert.IsNotNull(cloneable);

AssertEqual(p1, p2);
}

private void AssertEqual(Person p1, Person p2)
{
Assert.AreEqual(p1.Name, p2.Name);
Assert.AreEqual(p1.Surname, p2.Surname);
Assert.AreEqual(p1.GetAge(), p2.GetAge());

if (p1.Address != null)
{
if (p2.Address == null)
Assert.Fail("p1 has adress, p2 is missing address.");

Assert.AreEqual(p1.Address.Street, p2.Address.Street);
Assert.AreEqual(p1.Address.City, p2.Address.City);
Assert.AreEqual(p1.Address.HouseNumber, p2.Address.HouseNumber);
}
}


class Person : ICloneable<Person>, ICloneable<object>
{
private int age;

public string Name { get; set; }
public string Surname { get; set; }
public Address Address { get; set; }

public Person(string name, string surname, int age)
{
Name = name;
Surname = surname;
this.age = age;
}

public int GetAge() => age;

public Person Clone()
{
return (Person)MemberwiseClone();
}

public override string ToString()
{
return String.Format("{0} {1} ({2}) [{3}]", Name, Surname, age, Address);
}

object ICloneable<object>.Clone()
{
return (Person)MemberwiseClone();
}
}

class Address
{
public string Street { get; set; }
public int HouseNumber { get; set; }
public string City { get; set; }

public Address(string street, int houseNumber, string city)
{
Street = street;
HouseNumber = houseNumber;
City = city;
}

public override string ToString()
{
return String.Format("{0} {1}, {2}", Street, HouseNumber, City);
}
}
}
}

0 comments on commit 6f9d2aa

Please sign in to comment.