PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ABB.SrcML.Data.Test/TestHelper.cs

https://github.com/nkcsgexi/SrcML.NET
C# | 201 lines | 149 code | 21 blank | 31 comment | 76 complexity | 42617a7e1acc66398780b1c2a7b1ef7a MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using NUnit.Framework;
  7. using System.Collections.ObjectModel;
  8. namespace ABB.SrcML.Data.Test {
  9. public static class TestHelper {
  10. public static void VerifyPrefixValues(IEnumerable<string> expected, NamedScopeUse use) {
  11. var prefix = use;
  12. Collection<string> prefixes = new Collection<string>();
  13. do {
  14. prefixes.Add(prefix.Name);
  15. prefix = prefix.ChildScopeUse;
  16. } while(prefix != null);
  17. CollectionAssert.AreEqual(expected as IEnumerable, prefixes as IEnumerable);
  18. }
  19. public static bool ScopesAreEqual(Scope a, Scope b) {
  20. if(a == b) { return true; }
  21. //Assert.AreEqual(a.GetType(), b.GetType());
  22. if (a.GetType() != b.GetType()) { return false; }
  23. return TestEquality((dynamic)a, (dynamic)b);
  24. }
  25. private static bool TestEquality(Scope a, Scope b) {
  26. //Assert.IsTrue(CollectionsAreEqual(a.ChildScopes.ToList(), b.ChildScopes.ToList(), ScopesAreEqual));
  27. //Assert.IsTrue(CollectionsAreEqual(a.MethodCalls.ToList(), b.MethodCalls.ToList(), MethodCallsAreEqual));
  28. //Assert.IsTrue(CollectionsAreEqual(a.DeclaredVariables.ToList(), b.DeclaredVariables.ToList(), VariableDeclarationsAreEqual));
  29. //Assert.IsTrue(CollectionsAreEqual(a.Locations.ToList(), b.Locations.ToList(), LocationsAreEqual));
  30. //Assert.AreEqual(a.ProgrammingLanguage, b.ProgrammingLanguage);
  31. return CollectionsAreEqual(a.ChildScopes.ToList(), b.ChildScopes.ToList(), ScopesAreEqual) &&
  32. CollectionsAreEqual(a.MethodCalls.ToList(), b.MethodCalls.ToList(), MethodCallsAreEqual) &&
  33. CollectionsAreEqual(a.DeclaredVariables.ToList(), b.DeclaredVariables.ToList(), VariableDeclarationsAreEqual) &&
  34. CollectionsAreEqual(a.Locations.ToList(), b.Locations.ToList(), LocationsAreEqual) &&
  35. a.ProgrammingLanguage == b.ProgrammingLanguage;
  36. }
  37. private static bool TestEquality(NamedScope a, NamedScope b) {
  38. //Assert.AreEqual(a.Name, b.Name);
  39. ////Accessibility isn't undone right now, so don't check it
  40. ////Assert.AreEqual(a.Accessibility, b.Accessibility);
  41. //Assert.IsTrue(CollectionsAreEqual(a.ParentScopeCandidates, b.ParentScopeCandidates, NamedScopeUsesAreEqual));
  42. //Assert.IsTrue(NamedScopeUsesAreEqual(a.UnresolvedParentScopeInUse, b.UnresolvedParentScopeInUse));
  43. //return TestEquality((Scope)a, (Scope)b);
  44. return a.Name == b.Name &&
  45. CollectionsAreEqual(a.ParentScopeCandidates, b.ParentScopeCandidates, NamedScopeUsesAreEqual) &&
  46. NamedScopeUsesAreEqual(a.UnresolvedParentScopeInUse, b.UnresolvedParentScopeInUse) &&
  47. TestEquality((Scope)a, (Scope)b);
  48. }
  49. private static bool TestEquality(NamespaceDefinition a, NamespaceDefinition b) {
  50. //Assert.AreEqual(a.IsAnonymous, b.IsAnonymous);
  51. //return TestEquality((NamedScope)a, (NamedScope)b);
  52. return a.IsAnonymous == b.IsAnonymous &&
  53. TestEquality((NamedScope)a, (NamedScope)b);
  54. }
  55. private static bool TestEquality(TypeDefinition a, TypeDefinition b) {
  56. //Assert.AreEqual(a.IsPartial, b.IsPartial);
  57. //Assert.AreEqual(a.Kind, b.Kind);
  58. //Assert.IsTrue(CollectionsAreEqual(a.ParentTypes, b.ParentTypes, TypeUsesAreEqual));
  59. //return TestEquality((NamedScope)a, (NamedScope)b);
  60. return a.IsPartial == b.IsPartial &&
  61. a.Kind == b.Kind &&
  62. CollectionsAreEqual(a.ParentTypes, b.ParentTypes, TypeUsesAreEqual) &&
  63. TestEquality((NamedScope)a, (NamedScope)b);
  64. }
  65. private static bool TestEquality(MethodDefinition a, MethodDefinition b) {
  66. //Assert.AreEqual(a.IsConstructor, b.IsConstructor);
  67. //Assert.AreEqual(a.IsDestructor, b.IsDestructor);
  68. //Assert.IsTrue(OrderedCollectionsAreEqual(a.Parameters, b.Parameters, ParameterDeclarationsAreEqual));
  69. //return TestEquality((NamedScope)a, (NamedScope)b);
  70. return a.IsConstructor == b.IsConstructor &&
  71. a.IsDestructor == b.IsDestructor &&
  72. OrderedCollectionsAreEqual(a.Parameters, b.Parameters, ParameterDeclarationsAreEqual) &&
  73. TestEquality((NamedScope)a, (NamedScope)b);
  74. }
  75. public static bool VariableDeclarationsAreEqual(VariableDeclaration a, VariableDeclaration b) {
  76. if(a == b) { return true; }
  77. return a.Accessibility == b.Accessibility &&
  78. LocationsAreEqual(a.Location, b.Location) &&
  79. a.Name == b.Name &&
  80. TypeUsesAreEqual(a.VariableType, b.VariableType);
  81. }
  82. public static bool ParameterDeclarationsAreEqual(ParameterDeclaration a, ParameterDeclaration b) {
  83. if(a == b) { return true; }
  84. //we intentially don't test parameter names, because those may differ between signatures
  85. //we also intentionally ignore the parameter type locations
  86. var variableTypesEqual = a.VariableType.Name == b.VariableType.Name &&
  87. NamedScopeUsesAreEqual(a.VariableType.Prefix, b.VariableType.Prefix);
  88. return variableTypesEqual &&
  89. CollectionsAreEqual(a.Locations, b.Locations, LocationsAreEqual);
  90. }
  91. public static bool TypeUsesAreEqual(TypeUse a, TypeUse b) {
  92. if(a == b) { return true; }
  93. return LocationsAreEqual(a.Location, b.Location) &&
  94. NamedScopeUsesAreEqual(a.Prefix, b.Prefix) &&
  95. a.Name == b.Name &&
  96. IResolvesToTypesAreEqual(a.CallingObject, b.CallingObject);
  97. }
  98. public static bool AliasesAreEqual(Alias a, Alias b) {
  99. if(a == b) { return true; }
  100. return LocationsAreEqual(a.Location, b.Location) &&
  101. NamedScopeUsesAreEqual(a.ImportedNamedScope, b.ImportedNamedScope) &&
  102. NamedScopeUsesAreEqual(a.ImportedNamespace, b.ImportedNamespace);
  103. }
  104. public static bool LocationsAreEqual(SrcMLLocation a, SrcMLLocation b) {
  105. if(a == b) { return true; }
  106. return a.IsReference == b.IsReference &&
  107. a.StartingColumnNumber == b.StartingColumnNumber &&
  108. a.SourceFileName == b.SourceFileName &&
  109. a.StartingLineNumber == b.StartingLineNumber &&
  110. a.XPath == b.XPath;
  111. }
  112. public static bool NamedScopeUsesAreEqual(NamedScopeUse a, NamedScopeUse b) {
  113. if(a == b) { return true; }
  114. return a.Name == b.Name &&
  115. LocationsAreEqual(a.Location, b.Location) &&
  116. a.ProgrammingLanguage == b.ProgrammingLanguage &&
  117. NamedScopeUsesAreEqual(a.ChildScopeUse, b.ChildScopeUse);
  118. }
  119. public static bool MethodCallsAreEqual(MethodCall a, MethodCall b) {
  120. if(a == b) { return true; }
  121. return CollectionsAreEqual(a.Arguments, b.Arguments, IResolvesToTypesAreEqual) &&
  122. IResolvesToTypesAreEqual(a.CallingObject, b.CallingObject) &&
  123. a.IsConstructor == b.IsConstructor &&
  124. a.IsDestructor == b.IsDestructor &&
  125. LocationsAreEqual(a.Location, b.Location) &&
  126. a.Name == b.Name;
  127. }
  128. public static bool IResolvesToTypesAreEqual(IResolvesToType a, IResolvesToType b) {
  129. //TODO: reimplement this using proper OO-ish design
  130. if(a == b) { return true; }
  131. if(a == null || b == null) { return false; }
  132. var aType = a.GetType();
  133. if(aType != b.GetType()) { return false; }
  134. if(aType.Name == "VariableUse") {
  135. return VariableUsesAreEqual((VariableUse)a, (VariableUse)b);
  136. } else if(aType.Name == "MethodCall") {
  137. return MethodCallsAreEqual((MethodCall)a, (MethodCall)b);
  138. } else if(aType.Name == "TypeUse") {
  139. return TypeUsesAreEqual((TypeUse)a, (TypeUse)b);
  140. } else if(aType.Name == "LiteralUse") {
  141. return LiteralUsesAreEqual((LiteralUse)a, (LiteralUse)b);
  142. }
  143. return false;
  144. }
  145. public static bool VariableUsesAreEqual(VariableUse a, VariableUse b) {
  146. if(a == b) { return true; }
  147. return a.Name == b.Name &&
  148. LocationsAreEqual(a.Location, b.Location) &&
  149. IResolvesToTypesAreEqual(a.CallingObject, b.CallingObject);
  150. }
  151. public static bool LiteralUsesAreEqual(LiteralUse a, LiteralUse b) {
  152. if(a == b) { return true; }
  153. return a.Kind == b.Kind &&
  154. TypeUsesAreEqual(a, b);
  155. }
  156. /// <summary>
  157. /// Checks whether two collections have the same contents. The ordering is ignored.
  158. /// </summary>
  159. private static bool CollectionsAreEqual<T>(ICollection<T> a, ICollection<T> b, Func<T, T, bool> equalityComparer) {
  160. if(a == b) { return true; }
  161. bool equal = a.Count == b.Count;
  162. foreach(var aMember in a) {
  163. equal = equal && b.Any(bMember => equalityComparer(aMember, bMember));
  164. }
  165. foreach(var bMember in b) {
  166. equal = equal && a.Any(aMember => equalityComparer(bMember, aMember));
  167. }
  168. return equal;
  169. }
  170. /// <summary>
  171. /// Checks whether two collections have the same contents in the same order.
  172. /// </summary>
  173. private static bool OrderedCollectionsAreEqual<T>(ICollection<T> a, ICollection<T> b, Func<T, T, bool> equalityComparer) {
  174. if(a == b) { return true; }
  175. bool equal = a.Count == b.Count;
  176. equal = a.Zip(b, equalityComparer).Aggregate(equal, (current, result) => current && result);
  177. return equal;
  178. }
  179. }
  180. }