2022-04-07 16:20:14 +08:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Falcon.SugarApi.Test
|
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class ObjectExtendTest
|
|
|
|
|
{
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void CloneToTest() {
|
|
|
|
|
var s = new SourceClass { };
|
|
|
|
|
var t = new TargetClass { };
|
2023-06-06 13:15:15 +08:00
|
|
|
|
t.ica.ItemA = "itemb";
|
2022-04-07 16:20:14 +08:00
|
|
|
|
|
2023-06-06 13:15:15 +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-06-06 13:15:15 +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-06-06 13:15:15 +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 {
|
2023-06-06 13:15:15 +08:00
|
|
|
|
Id = 1,Name = "Falcon",Sex = null,Count = "count"
|
2023-01-05 10:02:26 +08:00
|
|
|
|
};
|
|
|
|
|
var ep = obj.ExpandProperties();
|
|
|
|
|
Assert.IsNotNull(ep);
|
2023-06-06 13:15:15 +08:00
|
|
|
|
Assert.IsTrue(ep.Count() == 4);
|
|
|
|
|
Assert.IsTrue(ep.Select(m => m.Name).Distinct().Count() == 4);
|
2023-01-05 10:02:26 +08:00
|
|
|
|
foreach(var p in ep) {
|
2023-06-06 13:15:15 +08:00
|
|
|
|
if(p.Name == "Id") {
|
|
|
|
|
Assert.IsTrue(p.Type == typeof(int));
|
2023-01-05 10:02:26 +08:00
|
|
|
|
Assert.IsTrue(p.Value.Equals(obj.Id));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-06-06 13:15:15 +08:00
|
|
|
|
if(p.Name == "Name") {
|
|
|
|
|
Assert.IsTrue(p.Type == typeof(string));
|
2023-01-05 10:02:26 +08:00
|
|
|
|
Assert.IsTrue(p.Value.Equals(obj.Name));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-06-06 13:15:15 +08:00
|
|
|
|
if(p.Name == "Sex") {
|
|
|
|
|
Assert.IsTrue(p.Type == typeof(string));
|
|
|
|
|
Assert.IsTrue(p.Value == null);
|
2023-01-05 10:02:26 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-06-06 13:15:15 +08:00
|
|
|
|
if(p.Name == "Count") {
|
|
|
|
|
Assert.IsTrue(p.Type == typeof(string));
|
|
|
|
|
Assert.IsTrue(p.Value == "count");
|
2023-01-05 10:02:26 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-06 15:43:21 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 是否为空测试
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void IsNullTest() {
|
|
|
|
|
object obj = null;
|
|
|
|
|
Assert.IsTrue(obj.IsNull());
|
|
|
|
|
Assert.IsFalse(obj.IsNotNull());
|
|
|
|
|
|
2023-06-06 13:15:15 +08:00
|
|
|
|
obj = new object();
|
2023-02-06 15:43:21 +08:00
|
|
|
|
Assert.IsTrue(obj.IsNotNull());
|
|
|
|
|
Assert.IsFalse(obj.IsNull());
|
|
|
|
|
|
|
|
|
|
object? obj1 = null;
|
|
|
|
|
Assert.IsTrue(obj1.IsNull());
|
|
|
|
|
Assert.IsFalse(obj1.IsNotNull());
|
|
|
|
|
|
2023-06-06 13:15:15 +08:00
|
|
|
|
obj1 = new object();
|
2023-02-06 15:43:21 +08:00
|
|
|
|
Assert.IsTrue(obj1.IsNotNull());
|
|
|
|
|
Assert.IsFalse(obj1.IsNull());
|
|
|
|
|
}
|
2023-06-06 13:15:15 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取调用堆栈测试
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void GetStackTraceTest() {
|
|
|
|
|
var r = this.GetStackTrace().ToArray();
|
|
|
|
|
Assert.IsNotNull(r);
|
|
|
|
|
foreach(var i in r) {
|
|
|
|
|
Console.WriteLine(i.Name);
|
|
|
|
|
}
|
|
|
|
|
Assert.IsTrue(r.Count() > 0);
|
|
|
|
|
Assert.IsTrue(r[0].Name == "GetStackTrace");
|
|
|
|
|
Assert.IsTrue(r[1].Name == "GetStackTraceTest");
|
|
|
|
|
|
|
|
|
|
var fullCount = this.GetStackTrace(full: true).Count();
|
|
|
|
|
var nFullCount = this.GetStackTrace(full: false).Count();
|
|
|
|
|
Assert.IsTrue(fullCount > nFullCount);
|
|
|
|
|
|
|
|
|
|
}
|
2023-01-05 10:02:26 +08:00
|
|
|
|
}
|
|
|
|
|
/// <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";
|
|
|
|
|
}
|
|
|
|
|
}
|