ClassHelperTest.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Masuit.Tools.Reflection;
  2. using System.Collections.Generic;
  3. using Xunit;
  4. namespace Masuit.Tools.Test
  5. {
  6. public class ClassHelperTest
  7. {
  8. [Fact]
  9. public void Can_AddProperty_ReturnNewInstance()
  10. {
  11. // arrange
  12. MyClass myClass = new MyClass();
  13. // act
  14. dynamic newObj = myClass.AddProperty(new List<ClassHelper.CustPropertyInfo>()
  15. {
  16. new ClassHelper.CustPropertyInfo(typeof(string),"Name","张三"),
  17. new ClassHelper.CustPropertyInfo(typeof(int),"Age",20),
  18. }).AddProperty("List", new List<string>());
  19. // act
  20. Assert.Equal("张三", newObj.Name);
  21. Assert.Equal(20, newObj.Age);
  22. Assert.IsType<List<string>>(newObj.List);
  23. }
  24. [Fact]
  25. public void Can_RemoveProperty_ReturnNewInstance()
  26. {
  27. // arrange
  28. MyClass myClass = new MyClass()
  29. {
  30. MyProperty = "aa",
  31. Number = 123
  32. };
  33. // act
  34. object newObj = myClass.DeleteProperty("MyProperty");
  35. // act
  36. int propertyCount = newObj.GetType().GetProperties().Length;
  37. Assert.Equal(1, propertyCount);
  38. }
  39. }
  40. public class MyClass
  41. {
  42. public string MyProperty { get; set; }
  43. public int Number { get; set; }
  44. }
  45. }