Falcon.SugarApi/Falcon.SugarApi.Test/ObjectExtendTest.cs

77 lines
2.2 KiB
C#

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Falcon.SugarApi;
namespace Falcon.SugarApi.Test
{
[TestClass]
public class ObjectExtendTest
{
[TestMethod]
public void CloneToTest() {
var s = new SourceClass { };
var t = new TargetClass { };
t.ica.ItemA = "itemb";
Assert.IsTrue(t.ica.ItemA == "itemb");
var r = s.CloneTo(t);
Assert.IsNotNull(s);
Assert.IsNotNull(r);
Assert.IsNotNull(t);
Assert.IsTrue(s.ia == t.ia, $"a.id:{s.ia},t.ia:{t.ia}");
Assert.IsTrue(t.ia == 1);
Assert.IsTrue(s.sa == t.sa);
Assert.IsTrue(s.sc == "sc");
Assert.IsTrue(t.sd == "sd");
Assert.IsTrue(s.ica.ItemA == "itema");
Assert.IsTrue(t.ica.ItemA == "itema");
Assert.IsTrue(t.ica.Equals(s.ica));
}
[TestMethod]
public void ChangeTypeTest() {
int s = 1;
var t = s.ChangeType(typeof(int));
Console.WriteLine(t.GetType().FullName);
Assert.IsTrue(t.GetType().Equals(typeof(int)));
}
/// <summary>
/// 测试对象为null则会抛出异常
/// </summary>
[TestMethod]
public void ThrowNullExceptionWhenNullTest() {
var obj = new object();
obj.ThrowNullExceptionWhenNull();
obj = null;
Assert.ThrowsException<ArgumentNullException>(() => obj.ThrowNullExceptionWhenNull());
}
}
public class SourceClass
{
public int ia { get; set; } = 1;
public string sa { get; set; } = "sa";
public string sc { get; set; } = "sc";
public ItemClass ica { get; set; } = new ItemClass();
}
public class TargetClass
{
public int ia { get; set; }
public string sa { get; set; }
public string sd { get; set; } = "sd";
public ItemClass ica { get; set; } = new ItemClass();
}
public class ItemClass
{
public string ItemA { get; set; } = "itema";
}
}