You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

190 lines
6.2 KiB

using OT.COM.Encryption;
using OT.COM.LogisticsUtil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace TestUnit.OT.COM.LogisticsUtil
{
public class Test_ClassHelper
{
[Theory]
[InlineData("Util", typeof(Util))]
[InlineData("Util1", null)]
public void GetTypeByTypeName(string i_sTypeName, Type i_tExcept)
{
Type tResult = ClassHelper.GetTypeByTypeName(i_sTypeName);
Assert.True(tResult == i_tExcept, $"Cannot create type '{i_sTypeName}'");
}
[Theory]
[InlineData("OT.COM.LogisticsUtil.ValidatorHelper", typeof(ValidatorHelper))]
[InlineData("Util1", null)]
public void GetTypeByFullName(string i_sTypeName, Type i_tExcept)
{
Type tResult = ClassHelper.GetTypeByFullName(i_sTypeName);
Assert.True(tResult == i_tExcept, $"Cannot create type '{i_sTypeName}'");
}
[Theory]
[InlineData("OT.COM.Encryption.AES", typeof(AES))]
[InlineData("Util1", null)]
public void GetInstByFullName(string i_sTypeName, Type i_tExcept)
{
string sMsg = ClassHelper.GetInstByFullName(i_sTypeName, out object oInst, null);
if (i_tExcept != null)
{
Assert.True(sMsg == null, sMsg);
Assert.True(oInst != null && oInst.GetType() == i_tExcept, $"Create '{i_sTypeName}' success but type is not expect '{i_tExcept.FullName}'");
}
else
{
Assert.True(sMsg != null && oInst == null, $"Create '{i_sTypeName}' success but should not happen");
}
}
[Theory]
[InlineData("OT.COM.LogisticsUtil.ValidatorHelper", typeof(ValidatorHelper))]
[InlineData("Util1", null)]
public void GetTypeByFullNameEndWithTerm(string i_sTypeName, Type i_tExcept)
{
Type tResult = ClassHelper.GetTypeByFullNameEndWithTerm(i_sTypeName);
Assert.True(tResult == i_tExcept, $"Cannot create type '{i_sTypeName}'");
}
[Theory]
[InlineData("Encryption.AES", typeof(AES))]
[InlineData("Util1", null)]
public void GetInstByFullNameEndWithTerm(string i_sTypeName, Type i_tExcept)
{
string sMsg = ClassHelper.GetInstByFullNameEndWithTerm(i_sTypeName, out object oInst);
if (i_tExcept != null)
{
Assert.True(sMsg == null, sMsg);
Assert.True(oInst != null && oInst.GetType() == i_tExcept, $"Create '{i_sTypeName}' success but type is not expect '{i_tExcept.FullName}'");
}
else
{
Assert.True(sMsg != null && oInst == null, $"Create '{i_sTypeName}' success but should not happen");
}
}
[Theory]
[InlineData(typeof(AES), typeof(AES))]
[InlineData(null, null)]
public void GetInstByType(Type i_sTypeName, Type i_tExcept)
{
string sMsg = ClassHelper.GetInstByType(i_sTypeName, out object oInst);
if (i_tExcept != null)
{
Assert.True(sMsg == null, sMsg);
Assert.True(oInst != null && oInst.GetType() == i_tExcept, $"Create '{i_sTypeName}' success but type is not expect '{i_tExcept.FullName}'");
}
else
{
Assert.True(sMsg != null && oInst == null, $"Create '{i_sTypeName}' success but should not happen");
}
}
[Theory]
[InlineData("AES", typeof(AES))]
[InlineData("Util1", null)]
public void GetInstByClassName(string i_sTypeName, Type i_tExcept)
{
string sMsg = ClassHelper.GetInstByClassName(i_sTypeName, out object oInst);
if (i_tExcept != null)
{
Assert.True(sMsg == null, sMsg);
Assert.True(oInst != null && oInst.GetType() == i_tExcept, $"Create '{i_sTypeName}' success but type is not expect '{i_tExcept.FullName}'");
}
else
{
Assert.True(sMsg != null && oInst == null, $"Create '{i_sTypeName}' success but should not happen");
}
}
[Theory]
[InlineData(typeof(AES), typeof(AES))]
[InlineData(typeof(int ?), typeof(int))]
public void GetRealType(Type i_sTypeName, Type i_tExcept)
{
Type r = ClassHelper.GetRealType(i_sTypeName);
if (i_tExcept != null)
{
Assert.True(r == i_tExcept, $"Create '{i_sTypeName}' success but type is not expect '{i_tExcept.FullName}'");
}
else
{
Assert.True(r == null, $"Create '{i_sTypeName}' success but should not happen");
}
}
[Theory]
[InlineData(typeof(int), "1", 1 )]
[InlineData(typeof(string), 2, "2")]
public void ConvertValue(Type i_sTypeName, object i_oOrigin, object i_tTarget)
{
object t = ClassHelper.ConvertValue(i_sTypeName, i_oOrigin);
if (i_tTarget != null)
{
Assert.True(t != null && t.ToString() == i_tTarget.ToString(), $"Create '{i_sTypeName}' success but type is not expect '{i_tTarget.ToString()}'");
}
else
{
Assert.True(t == null, $"Create '{i_sTypeName}' success but should not happen");
}
}
public class TestA
{
public int A { get; set; }
}
[Theory]
[InlineData("3", 3)]
public void ConvertValue2(object i_oOrigin, object i_tTarget)
{
PropertyInfo pi = typeof(TestA).GetProperties().First();
object t = ClassHelper.ConvertValue(pi, i_oOrigin);
if (i_tTarget != null)
{
Assert.True(t != null && t.ToString() == i_tTarget.ToString(), $"Create '{pi}' success but type is not expect '{i_tTarget.ToString()}'");
}
else
{
Assert.True(t == null, $"Create '{pi}' success but should not happen");
}
}
}
}