Falcon.SugarApi/Falcon.SugarApi.Test/ProxyTest.cs
2024-10-24 11:25:04 +08:00

69 lines
1.9 KiB
C#

using Falcon.SugarApi.Proxy;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Falcon.SugarApi.Test
{
/// <summary>
/// 代理类测试
/// </summary>
[TestClass]
public class ProxyTest
{
/// <summary>
/// 基础功能测试
/// </summary>
[TestMethod]
public void BaseTest() {
var obj = new TestObject { Name = "tom" };
//普通代理测试
var pobj = new ProxyObj(obj);
pobj.SetAction("Name","123");
Assert.AreEqual(obj.Name,"ProxyObj run:123");
Assert.IsTrue(pobj.GetFunc("Name").ToString() == "ProxyObj run:123");
pobj.Invoke("SetName","jack");
Assert.IsTrue(obj.Name == "jack");
//泛型代理测试
var pobj1 = new ProxyObj1(obj);
pobj1.SetAction("Name","123");
Assert.AreEqual(obj.Name,"ProxyObj1 run:123");
Assert.IsTrue(pobj1.GetFunc("Name").ToString() == "ProxyObj1 run:123");
pobj1.Invoke("SetName","jack1");
Assert.IsTrue(obj.Name == "jack1");
}
class TestObject
{
public string Name { get; set; }
public void SetName(string newName) {
this.Name = newName;
}
}
class ProxyObj:ProxyBase
{
public ProxyObj(object target) : base(target) {
}
public override void SetAction(string prop,object? value) {
value = "ProxyObj run:" + value;
base.SetAction(prop,value);
}
}
class ProxyObj1:Proxy<TestObject>
{
public ProxyObj1(TestObject target) : base(target) {
}
public override void SetAction(string prop,object? value) {
value = "ProxyObj1 run:" + value;
base.SetAction(prop,value);
}
}
}
}