From 6f9d2aaa7cc385b05e4219175b4b6f55b0708b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Sat, 2 Feb 2019 12:42:39 +0100 Subject: [PATCH] #142 - Rewrite Cloning tests. --- test/TestConsole/Asyncing/TestAsync.cs | 34 -------- test/TestConsole/Cloning/TestCloning.cs | 101 ---------------------- test/TestConsole/Program.cs | 6 +- test/UnitTest/TestCloning.cs | 106 ++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 140 deletions(-) delete mode 100644 test/TestConsole/Asyncing/TestAsync.cs delete mode 100644 test/TestConsole/Cloning/TestCloning.cs create mode 100644 test/UnitTest/TestCloning.cs diff --git a/test/TestConsole/Asyncing/TestAsync.cs b/test/TestConsole/Asyncing/TestAsync.cs deleted file mode 100644 index 8a17fc93..00000000 --- a/test/TestConsole/Asyncing/TestAsync.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Neptuo.Threading.Tasks; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TestConsole.Asyncing -{ - static class TestAsync - { - public static void Test() - { - Schedule(0).Wait(); - Schedule(1).Wait(); - } - - static Task Schedule(int index) - { - return Task.Factory.StartNew(async () => - { - for (int i = 0; i < 5; i++) - await Execute(index, i); - }); - } - - static async Task Execute(int index, int i) - { - Console.WriteLine($"Entering {index} at iteration {i}"); - await Task.Delay(100); - Console.WriteLine($"Leaving {index} at iteration {i}"); - } - } -} diff --git a/test/TestConsole/Cloning/TestCloning.cs b/test/TestConsole/Cloning/TestCloning.cs deleted file mode 100644 index 4cb71b49..00000000 --- a/test/TestConsole/Cloning/TestCloning.cs +++ /dev/null @@ -1,101 +0,0 @@ -using Neptuo; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TestConsole.Cloning -{ - class TestCloning - { - public static void Test() - { - //TestMemberwise(); - TestGeneric(); - } - - static void TestMemberwise() - { - Person p1 = new Person("John", "Doe", 22); - p1.Address = new Address("Main street", 50, "L.A."); - Person p2 = p1.Clone(); - - Console.WriteLine(p1); - Console.WriteLine(p2); - Console.WriteLine("---"); - - p2.Name = "Melisa"; - p2.Address.Street = "Support street"; - - Console.WriteLine(p1); - Console.WriteLine(p2); - Console.WriteLine("---"); - } - - static void TestGeneric() - { - Person p1 = new Person("John", "Doe", 22); - p1.Address = new Address("Main street", 50, "L.A."); - Person p2 = p1.Clone(); - - ICloneable cloneable = p1 as ICloneable; - if(cloneable != null) - Console.WriteLine(cloneable.Clone()); - - - Console.WriteLine(p1); - Console.WriteLine(p2); - } - } - - class Person : ICloneable, ICloneable - { - 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 Person Clone() - { - return (Person)MemberwiseClone(); - } - - public override string ToString() - { - return String.Format("{0} {1} ({2}) [{3}]", Name, Surname, age, Address); - } - - object ICloneable.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); - } - } -} diff --git a/test/TestConsole/Program.cs b/test/TestConsole/Program.cs index 1b5e2f31..bab86fc4 100644 --- a/test/TestConsole/Program.cs +++ b/test/TestConsole/Program.cs @@ -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; @@ -19,7 +18,6 @@ using TestConsole.PresentationModels; using TestConsole.Threading; using TestConsole.DispatcherExceptions; -using TestConsole.Asyncing; namespace TestConsole { @@ -41,8 +39,7 @@ private static void Main(string[] args) //TestMultiLockProvider.Test(); //TestDependency.Test(); //TestAppServices.Test(); - //TestCloning.Test(); - TestBehaviors.Test(); + //TestBehaviors.Test(); //TestObjectSize.Test(); //TestSharpKitCompiler.Test(); //TestLog4net.Test(); @@ -50,7 +47,6 @@ private static void Main(string[] args) //Services.Queries.TestQueries.Test(); //TestDispatcherException.Test(); //TestEventManager.Test(); - TestAsync.Test(); Console.ReadKey(true); } diff --git a/test/UnitTest/TestCloning.cs b/test/UnitTest/TestCloning.cs new file mode 100644 index 00000000..be12392c --- /dev/null +++ b/test/UnitTest/TestCloning.cs @@ -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 cloneable = p1 as ICloneable; + 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, ICloneable + { + 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.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); + } + } + } +}