2022-04-07 16:20:14 +08:00
|
|
|
|
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 { };
|
2023-01-05 10:02:26 +08:00
|
|
|
|
t.ica.ItemA="itemb";
|
2022-04-07 16:20:14 +08:00
|
|
|
|
|
2023-01-05 10:02:26 +08:00
|
|
|
|
Assert.IsTrue(t.ica.ItemA=="itemb");
|
2022-04-07 16:20:14 +08:00
|
|
|
|
|
|
|
|
|
var r = s.CloneTo(t);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotNull(s);
|
|
|
|
|
Assert.IsNotNull(r);
|
|
|
|
|
Assert.IsNotNull(t);
|
2023-01-05 10:02:26 +08:00
|
|
|
|
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");
|
2022-04-07 16:20:14 +08:00
|
|
|
|
Assert.IsTrue(t.ica.Equals(s.ica));
|
|
|
|
|
}
|
2022-07-14 09:01:38 +08:00
|
|
|
|
|
|
|
|
|
[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)));
|
|
|
|
|
}
|
2022-11-28 09:49:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 测试对象为null则会抛出异常
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void ThrowNullExceptionWhenNullTest() {
|
|
|
|
|
var obj = new object();
|
|
|
|
|
obj.ThrowNullExceptionWhenNull();
|
2023-01-05 10:02:26 +08:00
|
|
|
|
obj=null;
|
2022-11-28 09:49:49 +08:00
|
|
|
|
Assert.ThrowsException<ArgumentNullException>(() => obj.ThrowNullExceptionWhenNull());
|
|
|
|
|
}
|
2023-01-05 10:02:26 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 测试对象属性扩展
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void ExtendPropertyTest() {
|
|
|
|
|
var obj = new ExtendPropertyTestClass {
|
|
|
|
|
Id=1,Name="Falcon",Sex=null,Count="count"
|
|
|
|
|
};
|
|
|
|
|
var ep = obj.ExpandProperties();
|
|
|
|
|
Assert.IsNotNull(ep);
|
|
|
|
|
Assert.IsTrue(ep.Count()==4);
|
|
|
|
|
Assert.IsTrue(ep.Select(m => m.Name).Distinct().Count()==4);
|
|
|
|
|
foreach(var p in ep) {
|
|
|
|
|
if(p.Name=="Id") {
|
|
|
|
|
Assert.IsTrue(p.Type==typeof(int));
|
|
|
|
|
Assert.IsTrue(p.Value.Equals(obj.Id));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(p.Name=="Name") {
|
|
|
|
|
Assert.IsTrue(p.Type==typeof(string));
|
|
|
|
|
Assert.IsTrue(p.Value.Equals(obj.Name));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(p.Name=="Sex") {
|
|
|
|
|
Assert.IsTrue(p.Type==typeof(string));
|
|
|
|
|
Assert.IsTrue(p.Value==null);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(p.Name=="Count") {
|
|
|
|
|
Assert.IsTrue(p.Type==typeof(string));
|
|
|
|
|
Assert.IsTrue(p.Value=="count");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 扩展属性测试类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ExtendPropertyTestClass
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public string? Sex { get; set; }
|
|
|
|
|
public string Count { private get; set; }
|
2022-04-07 16:20:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|