PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/tests/Helpers/ReflectionUtilFixture.cs

https://github.com/sflanker/commandline
C# | 156 lines | 104 code | 26 blank | 26 comment | 3 complexity | b4ffaf2f9f0870f1a65e975d554d85cc MD5 | raw file
Possible License(s): Apache-2.0
  1. #region License
  2. //
  3. // Command Line Library: ReflectionUtilFixture.cs
  4. //
  5. // Author:
  6. // Giacomo Stelluti Scala (gsscoder@gmail.com)
  7. //
  8. // Copyright (C) 2005 - 2013 Giacomo Stelluti Scala
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. //
  28. #endregion
  29. #region Using Directives
  30. using System;
  31. using System.Collections.Generic;
  32. using System.ComponentModel;
  33. using System.Reflection;
  34. using CommandLine.Helpers;
  35. using Xunit;
  36. using FluentAssertions;
  37. using CommandLine.Text;
  38. using CommandLine.Core;
  39. #endregion
  40. namespace CommandLine.Tests
  41. {
  42. public class ReflectionUtilFixture
  43. {
  44. #region Mock Objects
  45. private class MockAttribute : Attribute
  46. {
  47. }
  48. private class AnotherMockAttribute : Attribute
  49. {
  50. }
  51. private class MockWithValueAttribute : Attribute
  52. {
  53. public string StringValue = String.Empty;
  54. }
  55. private class MockObject
  56. {
  57. public MockObject()
  58. {
  59. IntField = 0;
  60. }
  61. [Mock]
  62. public string StringField {get;set;}
  63. [Mock]
  64. public bool BooleanField {get;set;}
  65. [AnotherMock]
  66. public int IntField { get; set; }
  67. [Mock]
  68. public void DoNothing()
  69. {
  70. }
  71. }
  72. private class AnotherMockObject
  73. {
  74. public AnotherMockObject()
  75. {
  76. X = 0;
  77. Y = 0;
  78. Z = 0;
  79. }
  80. [MockWithValue(StringValue="applied to X")]
  81. public long X { get; set; }
  82. [MockWithValue(StringValue="applied to Y")]
  83. public long Y { get; set; }
  84. [MockWithValue(StringValue="applied to Z")]
  85. public long Z { get; set; }
  86. }
  87. #endregion
  88. [Fact]
  89. public void Get_fields_by_attribute()
  90. {
  91. var target = new MockObject();
  92. var list = ReflectionUtil.RetrievePropertyList<MockAttribute>(target);
  93. list.Should().HaveCount(n => n == 2);
  94. list[0].Left.Name.Should().Be("StringField");
  95. list[1].Left.Name.Should().Be("BooleanField");
  96. PrintFieldList<MockAttribute>(list);
  97. var anotherList = ReflectionUtil.RetrievePropertyList<AnotherMockAttribute>(target);
  98. anotherList.Should().HaveCount(n => n == 1);
  99. anotherList[0].Left.Name.Should().Be("IntField");
  100. PrintFieldList<AnotherMockAttribute>(anotherList);
  101. }
  102. [Fact]
  103. public void Get_method_by_attribute()
  104. {
  105. var target = new MockObject();
  106. var pair = ReflectionUtil.RetrieveMethod<MockAttribute>(target);
  107. pair.Should().NotBeNull();
  108. pair.Left.Name.Should().Be("DoNothing");
  109. }
  110. [Fact]
  111. public void Get_fields_attribute_list()
  112. {
  113. var list = ReflectionUtil.RetrievePropertyAttributeList<MockWithValueAttribute>(new AnotherMockObject());
  114. list.Should().NotBeNull();
  115. list.Should().HaveCount(n => n == 3);
  116. list[0].StringValue.Should().Be("applied to X");
  117. list[1].StringValue.Should().Be("applied to Y");
  118. list[2].StringValue.Should().Be("applied to Z");
  119. }
  120. private static void PrintFieldList<TAttribute>(IList<Pair<PropertyInfo, TAttribute>> list)
  121. where TAttribute : Attribute
  122. {
  123. Console.WriteLine("Attribute: {0}", list[0].Right.GetType());
  124. foreach (var pair in list)
  125. {
  126. Console.WriteLine("\tField: {0}", pair.Left.Name);
  127. }
  128. }
  129. }
  130. }