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.

LuExpressionUtilsTests.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using Luticate2.Auth.Utils.Business.Utils;
  5. using Xunit;
  6. namespace Luticate2.Auth.Tests.Business.Utils
  7. {
  8. public class TestDbo1
  9. {
  10. public string MyString { get; set; }
  11. public TestDbo2 TestDbo2 { get; set; }
  12. }
  13. public class TestDbo2
  14. {
  15. public string MyOtherString { get; set; }
  16. public int MyInt { get; set; }
  17. public TestDbo2 RecursiveObject { get; set; }
  18. }
  19. public class LuExpressionUtilsTests
  20. {
  21. [Fact]
  22. public void GetSingleMemberFromExpression1()
  23. {
  24. var memberInfo = LuExpressionUtils.GetSingleMemberFromExpression((Expression<Func<TestDbo1, string>>)(x => x.MyString));
  25. Assert.NotNull(memberInfo);
  26. Assert.Equal("MyString", memberInfo.Name);
  27. }
  28. [Fact]
  29. public void GetSingleMemberFromExpression2()
  30. {
  31. var memberInfo = LuExpressionUtils.GetSingleMemberFromExpression((Expression<Func<TestDbo1, TestDbo2>>)(x => x.TestDbo2));
  32. Assert.NotNull(memberInfo);
  33. Assert.Equal("TestDbo2", memberInfo.Name);
  34. }
  35. [Fact]
  36. public void GetSingleMemberFromExpression3()
  37. {
  38. var memberInfo = LuExpressionUtils.GetSingleMemberFromExpression((Expression<Func<TestDbo1, TestDbo1>>)(x => x));
  39. Assert.Null(memberInfo);
  40. }
  41. [Fact]
  42. public void GetSingleMemberFromExpression4()
  43. {
  44. var memberInfo = LuExpressionUtils.GetSingleMemberFromExpression((Expression<Func<TestDbo1, int>>)(x => 42));
  45. Assert.Null(memberInfo);
  46. }
  47. [Fact]
  48. public void GetSingleMemberFromExpression5()
  49. {
  50. var memberInfo = LuExpressionUtils.GetSingleMemberFromExpression((Expression<Func<TestDbo1, int>>)(x => x.TestDbo2.MyInt));
  51. Assert.Null(memberInfo);
  52. }
  53. [Fact]
  54. public void GetSingleMemberFromExpression6()
  55. {
  56. var memberInfo = LuExpressionUtils.GetSingleMemberFromExpression((Expression<Func<TestDbo1, int>>)(x => x.TestDbo2.RecursiveObject.MyInt));
  57. Assert.Null(memberInfo);
  58. }
  59. [Fact]
  60. public void GetMembersFromExpression1()
  61. {
  62. var memberInfo = LuExpressionUtils.GetMembersFromExpression<TestDbo1, string>(x => x.MyString);
  63. Assert.NotNull(memberInfo);
  64. var memberInfosString = string.Join(".", memberInfo.Select(x => x.Name));
  65. Assert.Equal("MyString", memberInfosString);
  66. }
  67. [Fact]
  68. public void GetMembersFromExpression2()
  69. {
  70. var memberInfo = LuExpressionUtils.GetMembersFromExpression<TestDbo1, TestDbo2>(x => x.TestDbo2);
  71. Assert.NotNull(memberInfo);
  72. var memberInfosString = string.Join(".", memberInfo.Select(x => x.Name));
  73. Assert.Equal("TestDbo2", memberInfosString);
  74. }
  75. [Fact]
  76. public void GetMembersFromExpression3()
  77. {
  78. var memberInfo = LuExpressionUtils.GetMembersFromExpression<TestDbo1, TestDbo1>(x => x);
  79. Assert.Null(memberInfo);
  80. }
  81. [Fact]
  82. public void GetMembersFromExpression4()
  83. {
  84. var memberInfo = LuExpressionUtils.GetMembersFromExpression<TestDbo1, int>(x => 42);
  85. Assert.Null(memberInfo);
  86. }
  87. [Fact]
  88. public void GetMembersFromExpression5()
  89. {
  90. var memberInfo = LuExpressionUtils.GetMembersFromExpression<TestDbo1, int>(x => x.TestDbo2.MyInt);
  91. Assert.NotNull(memberInfo);
  92. var memberInfosString = string.Join(".", memberInfo.Select(x => x.Name));
  93. Assert.Equal("TestDbo2.MyInt", memberInfosString);
  94. }
  95. [Fact]
  96. public void GetMembersFromExpression6()
  97. {
  98. var memberInfo = LuExpressionUtils.GetMembersFromExpression<TestDbo1, int>(x => x.TestDbo2.RecursiveObject.MyInt);
  99. Assert.NotNull(memberInfo);
  100. var memberInfosString = string.Join(".", memberInfo.Select(x => x.Name));
  101. Assert.Equal("TestDbo2.RecursiveObject.MyInt", memberInfosString);
  102. }
  103. }
  104. }