/IntegrationTests/Utilities/IntrospectionUtility_ClassTest.cs

http://injectioncop.codeplex.com · C# · 309 lines · 254 code · 42 blank · 13 comment · 6 complexity · 25f7f2479a8c1658019255e5fb2645c1 MD5 · raw file

  1. // Copyright 2012 rubicon informationstechnologie gmbh
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using InjectionCop.IntegrationTests.Parser;
  16. using InjectionCop.Utilities;
  17. using Microsoft.FxCop.Sdk;
  18. using NUnit.Framework;
  19. namespace InjectionCop.IntegrationTests.Utilities
  20. {
  21. [TestFixture]
  22. public class IntrospectionUtility_ClassTest
  23. {
  24. [Test]
  25. public void IsVariable_FieldInMembebinding_IsTrue ()
  26. {
  27. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample>("UsingField");
  28. Block expressionBlock = (Block)sample.Body.Statements[0];
  29. ExpressionStatement expressionStatement = (ExpressionStatement)expressionBlock.Statements[1];
  30. UnaryExpression sampleUnaryExpression = (UnaryExpression)expressionStatement.Expression;
  31. MethodCall sampleMethodCall = (MethodCall) sampleUnaryExpression.Operand;
  32. Expression sampleExpression = sampleMethodCall.Operands[0];
  33. bool fieldIsVariable = IntrospectionUtility.IsVariable(sampleExpression);
  34. Assert.That(fieldIsVariable, Is.True);
  35. }
  36. [Test]
  37. public void IsVariable_Method_IsFalse ()
  38. {
  39. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample>("UsingField");
  40. Block expressionBlock = (Block)sample.Body.Statements[0];
  41. ExpressionStatement expressionStatement = (ExpressionStatement)expressionBlock.Statements[1];
  42. UnaryExpression sampleUnaryExpression = (UnaryExpression)expressionStatement.Expression;
  43. MethodCall sampleMethodCall = (MethodCall) sampleUnaryExpression.Operand;
  44. bool methodIsVariable = IntrospectionUtility.IsVariable(sampleMethodCall);
  45. Assert.That(methodIsVariable, Is.False);
  46. }
  47. [Test]
  48. public void GetVariableName_FieldInMemberbinding_ReturnsName ()
  49. {
  50. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample>("UsingField");
  51. Block expressionBlock = (Block)sample.Body.Statements[0];
  52. ExpressionStatement expressionStatement = (ExpressionStatement)expressionBlock.Statements[1];
  53. UnaryExpression sampleUnaryExpression = (UnaryExpression)expressionStatement.Expression;
  54. MethodCall sampleMethodCall = (MethodCall) sampleUnaryExpression.Operand;
  55. Expression sampleExpression = sampleMethodCall.Operands[0];
  56. string variableName = IntrospectionUtility.GetVariableName(sampleExpression);
  57. Assert.That(variableName, Is.EqualTo("_field"));
  58. }
  59. [Test]
  60. public void GetVariableName_Method_IsNull ()
  61. {
  62. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample>("UsingField");
  63. Block expressionBlock = (Block)sample.Body.Statements[0];
  64. ExpressionStatement expressionStatement = (ExpressionStatement)expressionBlock.Statements[1];
  65. UnaryExpression sampleUnaryExpression = (UnaryExpression)expressionStatement.Expression;
  66. MethodCall sampleMethodCall = (MethodCall) sampleUnaryExpression.Operand;
  67. string variableName = IntrospectionUtility.GetVariableName(sampleMethodCall);
  68. Assert.That(variableName, Is.Null);
  69. }
  70. [Test]
  71. public void IsVariableWithOutParameter_FieldInMemberbinding_IsTrueAndReturnsFieldName ()
  72. {
  73. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample>("UsingField");
  74. Block expressionBlock = (Block)sample.Body.Statements[0];
  75. ExpressionStatement expressionStatement = (ExpressionStatement)expressionBlock.Statements[1];
  76. UnaryExpression sampleUnaryExpression = (UnaryExpression)expressionStatement.Expression;
  77. MethodCall sampleMethodCall = (MethodCall) sampleUnaryExpression.Operand;
  78. Expression sampleExpression = sampleMethodCall.Operands[0];
  79. string variableName;
  80. bool fieldIsVariable = IntrospectionUtility.IsVariable(sampleExpression, out variableName);
  81. bool correctName = variableName == "_field";
  82. Assert.That(fieldIsVariable && correctName, Is.True);
  83. }
  84. [Test]
  85. public void IsVariableWithOutParameter_Method_IsFalseAndReturnsNullAsVariableName ()
  86. {
  87. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample>("UsingField");
  88. Block expressionBlock = (Block)sample.Body.Statements[0];
  89. ExpressionStatement expressionStatement = (ExpressionStatement)expressionBlock.Statements[1];
  90. UnaryExpression sampleUnaryExpression = (UnaryExpression)expressionStatement.Expression;
  91. MethodCall sampleMethodCall = (MethodCall) sampleUnaryExpression.Operand;
  92. string variableName;
  93. bool methodIsVariable = IntrospectionUtility.IsVariable(sampleMethodCall, out variableName);
  94. bool correctName = variableName == null;
  95. Assert.That(!methodIsVariable && correctName, Is.True);
  96. }
  97. [Test]
  98. public void IsField_FieldExpression_ReturnsTrue ()
  99. {
  100. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("FieldAssignment");
  101. Block assignmentBlock = (Block) sample.Body.Statements[0];
  102. AssignmentStatement assignmentStatement = (AssignmentStatement) assignmentBlock.Statements[1];
  103. Expression sampleExpression = assignmentStatement.Target;
  104. bool fieldRecognized = IntrospectionUtility.IsField (sampleExpression);
  105. Assert.That (fieldRecognized, Is.True);
  106. }
  107. [Test]
  108. public void IsField_NonFieldAssignment_ReturnsFalse ()
  109. {
  110. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("NonFieldAssignment");
  111. Block assignmentBlock = (Block) sample.Body.Statements[0];
  112. AssignmentStatement assignmentStatement = (AssignmentStatement) assignmentBlock.Statements[1];
  113. Expression sampleExpression = assignmentStatement.Target;
  114. bool fieldRecognized = IntrospectionUtility.IsField (sampleExpression);
  115. Assert.That (fieldRecognized, Is.False);
  116. }
  117. [Test]
  118. public void GetField_FieldExpression_ReturnsContainedField ()
  119. {
  120. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("FieldAssignment");
  121. Block assignmentBlock = (Block) sample.Body.Statements[0];
  122. AssignmentStatement assignmentStatement = (AssignmentStatement) assignmentBlock.Statements[1];
  123. Expression sampleExpression = assignmentStatement.Target;
  124. Field extractedField = IntrospectionUtility.GetField(sampleExpression);
  125. Member boundMember = ((MemberBinding) sampleExpression).BoundMember;
  126. bool extractionCorrect = extractedField.UniqueKey == boundMember.UniqueKey;
  127. Assert.That (extractionCorrect, Is.True);
  128. }
  129. [Test]
  130. public void GetField_NonFieldAssignment_ReturnsNull ()
  131. {
  132. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("NonFieldAssignment");
  133. Block assignmentBlock = (Block) sample.Body.Statements[0];
  134. AssignmentStatement assignmentStatement = (AssignmentStatement) assignmentBlock.Statements[1];
  135. Expression sampleExpression = assignmentStatement.Target;
  136. Field extractedField = IntrospectionUtility.GetField(sampleExpression);
  137. bool extractionCorrect = extractedField == null;
  138. Assert.That (extractionCorrect, Is.True);
  139. }
  140. [Test]
  141. public void IsPropertyGetter_PropertyGetter_ReturnsTrue ()
  142. {
  143. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("get_AnyProperty");
  144. bool isPropertyGetter = IntrospectionUtility.IsPropertyGetter(sample);
  145. Assert.That (isPropertyGetter, Is.True);
  146. }
  147. [Test]
  148. public void IsPropertyGetter_PropertySetter_ReturnsFalse ()
  149. {
  150. TypeNode objectTypeNode = IntrospectionUtility.TypeNodeFactory<Object>();
  151. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("set_AnyProperty", objectTypeNode);
  152. bool isPropertyGetter = IntrospectionUtility.IsPropertyGetter(sample);
  153. Assert.That (isPropertyGetter, Is.False);
  154. }
  155. [Test]
  156. public void IsPropertyGetter_MethodNamedLikeGetter_ReturnsFalse ()
  157. {
  158. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("get_NonExistingProperty");
  159. bool isPropertyGetter = IntrospectionUtility.IsPropertyGetter (sample);
  160. Assert.That (isPropertyGetter, Is.False);
  161. }
  162. [Test]
  163. public void IsPropertyGetter_MethodWithParameterNamedLikeGetter_ReturnsFalse ()
  164. {
  165. TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory<string>();
  166. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("get_NonExistingProperty", stringTypeNode);
  167. bool isPropertyGetter = IntrospectionUtility.IsPropertyGetter (sample);
  168. Assert.That (isPropertyGetter, Is.False);
  169. }
  170. [Test]
  171. public void IsPropertyGetter_DummyMethod_ReturnsFalse ()
  172. {
  173. TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory<string>();
  174. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("Dummy", stringTypeNode);
  175. bool isPropertyGetter = IntrospectionUtility.IsPropertyGetter (sample);
  176. Assert.That (isPropertyGetter, Is.False);
  177. }
  178. [Test]
  179. public void IsPropertySetter_PropertySetter_ReturnsTrue ()
  180. {
  181. TypeNode objectTypeNode = IntrospectionUtility.TypeNodeFactory<Object>();
  182. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("set_AnyProperty", objectTypeNode);
  183. bool isPropertyGetter = IntrospectionUtility.IsPropertySetter(sample);
  184. Assert.That (isPropertyGetter, Is.True);
  185. }
  186. [Test]
  187. public void IsPropertySetter_DummyMethod_ReturnsFalse ()
  188. {
  189. TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory<string>();
  190. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("Dummy", stringTypeNode);
  191. bool isPropertySetter = IntrospectionUtility.IsPropertySetter (sample);
  192. Assert.That (isPropertySetter, Is.False);
  193. }
  194. [Test]
  195. public void IsPropertySetter_PropertyGetter_ReturnsFalse ()
  196. {
  197. Method sample = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("get_AnyProperty");
  198. bool isPropertyGetter = IntrospectionUtility.IsPropertySetter (sample);
  199. Assert.That (isPropertyGetter, Is.False);
  200. }
  201. [Test]
  202. public void GetNestedType_ExistingNestedType_ReturnsNestedType ()
  203. {
  204. TypeNode parent = IntrospectionUtility.TypeNodeFactory<IntrospectionUtility_ClassSample>();
  205. TypeNode nestedType = IntrospectionUtility.GetNestedType (parent, "NestedClass");
  206. Assert.That (nestedType, Is.Not.Null);
  207. }
  208. [Test]
  209. public void GetNestedType_NonExistingNestedType_ReturnsNestedType ()
  210. {
  211. TypeNode parent = IntrospectionUtility.TypeNodeFactory<IntrospectionUtility_ClassSample>();
  212. TypeNode nestedType = IntrospectionUtility.GetNestedType (parent, "DoesNotExist");
  213. Assert.That (nestedType, Is.Null);
  214. }
  215. [Test]
  216. public void IsVariable_ArrayVariable_ReturnsTrue ()
  217. {
  218. Method sampleMethod = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("ArrayVariableAndIndexer");
  219. Block sampleBlock = (Block) sampleMethod.Body.Statements[0];
  220. AssignmentStatement sampleAssignment = (AssignmentStatement) sampleBlock.Statements[1];
  221. Expression sample = sampleAssignment.Target;
  222. Assert.That (IntrospectionUtility.IsVariable (sample), Is.True);
  223. }
  224. [Test]
  225. public void GetVariableName_ArrayVariable_ReturnsCorrectName ()
  226. {
  227. Method sampleMethod = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("ArrayVariableAndIndexer");
  228. Block sampleBlock = (Block) sampleMethod.Body.Statements[0];
  229. AssignmentStatement sampleAssignment = (AssignmentStatement) sampleBlock.Statements[1];
  230. Expression sample = sampleAssignment.Target;
  231. Assert.That (IntrospectionUtility.GetVariableName (sample), Is.EqualTo("local$0"));
  232. }
  233. [Test]
  234. public void IsVariable_Indexer_ReturnsTrue ()
  235. {
  236. Method sampleMethod = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("ArrayVariableAndIndexer");
  237. Block sampleBlock = (Block) sampleMethod.Body.Statements[0];
  238. AssignmentStatement sampleAssignment = (AssignmentStatement) sampleBlock.Statements[2];
  239. Expression sample = sampleAssignment.Target;
  240. Assert.That (IntrospectionUtility.IsVariable (sample), Is.True);
  241. }
  242. [Test]
  243. public void GetVariableName_Indexer_ReturnsNameOfDeclaringObject ()
  244. {
  245. Method sampleMethod = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("ArrayVariableAndIndexer");
  246. Block sampleBlock = (Block) sampleMethod.Body.Statements[0];
  247. AssignmentStatement sampleAssignment = (AssignmentStatement) sampleBlock.Statements[2];
  248. Expression sample = sampleAssignment.Target;
  249. Assert.That (IntrospectionUtility.GetVariableName (sample), Is.EqualTo("local$0"));
  250. }
  251. [Test]
  252. public void IsCompilerGenerated_UserGeneratedTypeNode_ReturnsFalse ()
  253. {
  254. TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory<IntrospectionUtility_ClassSample>();
  255. Assert.That (IntrospectionUtility.IsCompilerGenerated (sampleTypeNode), Is.False);
  256. }
  257. [Test]
  258. public void IsCompilerGenerated_CompilerGeneratedTypeNode_ReturnsTrue ()
  259. {
  260. Method sampleMethod = TestHelper.GetSample<IntrospectionUtility_ClassSample> ("UsingClosure");
  261. Block sampleBlock = (Block) sampleMethod.Body.Statements[0];
  262. AssignmentStatement sampleAssignment = (AssignmentStatement) sampleBlock.Statements[0];
  263. TypeNode compilerGeneratedTypeNode = sampleAssignment.Source.Type;
  264. Assert.That (IntrospectionUtility.IsCompilerGenerated (compilerGeneratedTypeNode), Is.True);
  265. }
  266. }
  267. }