public void SharedSourceFiles_Methods() { string projectPath = null; string outputPath = null; TestHelper.GetProjectPaths("SFT", out projectPath, out outputPath); string clientProjectPath = CodeGenHelper.ClientClassLibProjectPath(projectPath); List <string> sourceFiles = CodeGenHelper.ClientClassLibSourceFiles(clientProjectPath); ConsoleLogger logger = new ConsoleLogger(); FilenameMap filenameMap = new FilenameMap(); using (SourceFileLocationService locationService = new SourceFileLocationService(new[] { new PdbSourceFileProviderFactory(/*symbolSearchPath*/ null, logger) }, filenameMap)) { SharedSourceFiles ssf = new SharedSourceFiles(locationService, filenameMap, sourceFiles); int[] fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateMethodKey(typeof(TestValidator).GetMethod("IsValid"))); Assert.IsNotNull(fileIds, "Expected TestValidator.IsValid to have non-null file ID's because it is shared"); Assert.AreEqual(1, fileIds.Length, "Expected TestValidator.IsValid to be found in exactly one file"); Assert.IsTrue(filenameMap[fileIds[0]].Contains("TestValidator.linked.cs"), "Expected TestValidator.IsValid to be in TestValidator.linked.cs"); fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateMethodKey(typeof(TestEntity).GetMethod("ServerAndClientMethod"))); Assert.IsNotNull(fileIds, "Expected TestEntity.ServerAndClientMethod to have non-null file ID's because it is shared"); Assert.AreEqual(1, fileIds.Length, "Expected TestEntity.ServerAndClientMethod to be found in exactly one file"); Assert.IsTrue(filenameMap[fileIds[0]].Contains("TestEntity.linked.cs"), "Expected TestEntity.ServerAndClientMethod to be in TestEntity.linked.cs"); fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateMethodKey(typeof(TestEntity).GetMethod("ServerMethod"))); Assert.IsNull(fileIds, "Expected TestEntity.ServerMethod to have null file ids"); fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateMethodKey(typeof(TestValidatorServer).GetMethod("IsValid"))); Assert.IsNull(fileIds, "Expected TestValidatorServer.IsValid to have null file ids"); } }
public void CodeMemberKey_Method_Key_Bad_ParameterTypes() { // Right number of parameters, wrong type CodeMemberKey key = CodeMemberKey.CreateMethodKey(typeof(string).AssemblyQualifiedName, "Contains", new[] { typeof(int).AssemblyQualifiedName }); Assert.IsNotNull(key, "CreateTypeKey using name failed"); Assert.AreEqual(key.TypeName, typeof(string).AssemblyQualifiedName, "TypeName property get different than ctor"); Type t = key.Type; Assert.AreEqual(typeof(string), t, "CodeMemberKey.Type failed to load type from name"); MethodBase methodBase = key.MethodBase; Assert.IsNull(methodBase, "CodeMemberKey.MethodBase should have failed to resolve."); // Wrong number of parameters key = CodeMemberKey.CreateMethodKey(typeof(string).AssemblyQualifiedName, "Contains", new[] { typeof(string).AssemblyQualifiedName, typeof(string).AssemblyQualifiedName }); Assert.IsNotNull(key, "CreateTypeKey using name failed"); Assert.AreEqual(key.TypeName, typeof(string).AssemblyQualifiedName, "TypeName property get different than ctor"); t = key.Type; Assert.AreEqual(typeof(string), t, "CodeMemberKey.Type failed to load type from name"); methodBase = key.MethodBase; Assert.IsNull(methodBase, "CodeMemberKey.MethodBase should have failed to resolve."); // Bogus parameter type name key = CodeMemberKey.CreateMethodKey(typeof(string).AssemblyQualifiedName, "Contains", new[] { "NotAType" }); Assert.IsNotNull(key, "CreateTypeKey using name failed"); Assert.AreEqual(key.TypeName, typeof(string).AssemblyQualifiedName, "TypeName property get different than ctor"); t = key.Type; Assert.AreEqual(typeof(string), t, "CodeMemberKey.Type failed to load type from name"); methodBase = key.MethodBase; Assert.IsNull(methodBase, "CodeMemberKey.MethodBase should have failed to resolve."); }
public CodeMemberShareKind GetMethodShareKind(string typeName, string methodName, IEnumerable <string> parameterTypeNames) { CodeMemberKey key = CodeMemberKey.CreateMethodKey(typeName, methodName, parameterTypeNames == null ? new string[0] : parameterTypeNames.ToArray()); if (this._sharedMethods.Contains(key)) { return(CodeMemberShareKind.SharedByReference); } if (this.IsMethodSharedInFile(key.MethodBase)) { return(CodeMemberShareKind.SharedBySource); } return(CodeMemberShareKind.NotShared); }
public CodeMemberShareKind GetMethodShareKind(MethodBase methodBase) { CodeMemberKey key = CodeMemberKey.CreateMethodKey(methodBase); if (this._sharedMethods.Contains(key) || IsSharedFrameworkType(methodBase.DeclaringType)) { return(CodeMemberShareKind.SharedByReference); } if (this.IsMethodSharedInFile(methodBase)) { return(CodeMemberShareKind.SharedBySource); } return(CodeMemberShareKind.NotShared); }
public MockSharedCodeService(IEnumerable <Type> sharedTypes, IEnumerable <MethodBase> sharedMethods, IEnumerable <string> sharedFiles) { if (sharedTypes == null) { sharedTypes = Enumerable.Empty <Type>(); } if (sharedMethods == null) { sharedMethods = Enumerable.Empty <MethodBase>(); } if (sharedFiles == null) { sharedFiles = Enumerable.Empty <string>(); } foreach (Type t in sharedTypes) { Assert.IsNotNull(t, "Test error -- null passed in for a shared type"); } foreach (MethodInfo m in sharedMethods) { Assert.IsNotNull(m, "Test error -- null passed in for a shared method"); } this._sharedTypes = new HashSet <Type>(sharedTypes); foreach (MethodBase m in sharedMethods) { this._sharedMethods.Add(CodeMemberKey.CreateMethodKey((m))); } this._sharedFiles = new HashSet <string>(sharedFiles); // Open up a real PDB based location service if have shared files if (this._sharedFiles.Any()) { this._pdbSourceFileLocationService = new SourceFileLocationService(new[] { new PdbSourceFileProviderFactory(/*symbolSearchPath*/ null, /*logger*/ null) }, new FilenameMap()); } }
public void CodeMemberKey_Method_Key() { // Form the name based key CodeMemberKey key = CodeMemberKey.CreateMethodKey(typeof(object).AssemblyQualifiedName, "Equals", new [] { typeof(object).AssemblyQualifiedName }); Assert.IsNotNull(key, "CreateTypeKey using name failed"); Assert.AreEqual(key.TypeName, typeof(object).AssemblyQualifiedName, "TypeName property get different than ctor"); Type t = key.Type; Assert.AreEqual(typeof(object), t, "CodeMemberKey.Type failed to load type from name"); MethodBase methodBase = key.MethodBase; Assert.IsNotNull(methodBase, "CodeMemberKey.MethodBase failed to get real method info."); Assert.AreEqual("Equals", methodBase.Name, "incorrect method info name"); // Form the type-based key CodeMemberKey key2 = CodeMemberKey.CreateMethodKey(typeof(object).GetMethod("Equals", new[] { typeof(object) })); Assert.IsNotNull(key2, "CreateTypeKey using property failed"); Assert.AreEqual(key2.TypeName, typeof(object).AssemblyQualifiedName, "TypeName property get different than ctor"); t = key2.Type; Assert.AreEqual(typeof(object), t, "CodeMemberKey.Type failed to load type from name"); methodBase = key2.MethodBase; Assert.IsNotNull(methodBase, "CodeMemberKey.MethodBase failed to get real method info."); Assert.AreEqual("Equals", methodBase.Name, "incorrect method info name"); // These keys should be the same Assert.AreEqual(key.GetHashCode(), key2.GetHashCode(), "CodeMemberKeys from property should have yielded same hash"); Assert.AreEqual(key, key2); // Ensure null is acceptable for parameter types CodeMemberKey key3 = CodeMemberKey.CreateMethodKey(typeof(object).AssemblyQualifiedName, "GetHashCode", null); Assert.IsNotNull(key3, "CreateTypeKey using name failed"); Assert.AreEqual(key3.TypeName, typeof(object).AssemblyQualifiedName, "TypeName property get different than ctor"); t = key3.Type; Assert.AreEqual(typeof(object), t, "CodeMemberKey.Type failed to load type from name"); methodBase = key3.MethodBase; Assert.IsNotNull(methodBase, "CodeMemberKey.MethodBase failed to get real method info."); Assert.AreEqual("GetHashCode", methodBase.Name, "incorrect method info name"); }
private static string GetSharedMethodLocation(SharedAssemblies sa, Type type, string methodName, Type[] parameterTypes) { var key = CodeMemberKey.CreateMethodKey(type.AssemblyQualifiedName, methodName, parameterTypes.Select(t => t.AssemblyQualifiedName).ToArray()); return(sa.GetSharedAssemblyPath(key)); }