Exemple #1
0
        public void SharedSourceFiles_Types()
        {
            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.CreateTypeKey(typeof(TestEntity)));
                Assert.IsNotNull(fileIds, "Expected TestEntity to have non-null file ID's because it is shared");
                Assert.AreEqual(2, fileIds.Length, "Expected TestEntity to be found in exactly 2 files");
                foreach (int i in fileIds)
                {
                    string file = filenameMap[i];
                    Assert.IsTrue(file.Contains("TestEntity.linked.cs") || file.Contains("TestEntity.reverse.linked.cs"), "Expected exactly these 2 files to be shared");
                }

                fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateTypeKey(typeof(TestValidator)));
                Assert.IsNotNull(fileIds, "Expected TestValidator to have non-null file ID's because it is shared");
                Assert.AreEqual(1, fileIds.Length, "Expected TestValidator to be found in exactly one file");
                Assert.IsTrue(filenameMap[fileIds[0]].Contains("TestValidator.linked.cs"), "expected this to be the sole shared file for TestValidator");

                fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateTypeKey(typeof(TestValidatorServer)));
                Assert.IsNull(fileIds, "Expected TestValidatorServer to have no shared file ids");
            }
        }
Exemple #2
0
        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_Property_Key()
        {
            // Form the name based key
            CodeMemberKey key = CodeMemberKey.CreatePropertyKey(typeof(TestEntity).AssemblyQualifiedName, "TheValue");

            Assert.IsNotNull(key, "CreateTypeKey using name failed");
            Assert.AreEqual(key.TypeName, typeof(TestEntity).AssemblyQualifiedName, "TypeName property get different than ctor");
            Type t = key.Type;

            Assert.AreEqual(typeof(TestEntity), t, "CodeMemberKey.Type failed to load type from name");
            PropertyInfo propInfo = key.PropertyInfo;

            Assert.IsNotNull(propInfo, "CodeMemberKey.PropertyInfo failed to get real property info.");
            Assert.AreEqual("TheValue", propInfo.Name, "incorrect property info name");

            // Form the type-based key
            CodeMemberKey key2 = CodeMemberKey.CreatePropertyKey(typeof(TestEntity).GetProperty("TheValue"));

            Assert.IsNotNull(key2, "CreateTypeKey using property failed");
            Assert.AreEqual(key2.TypeName, typeof(TestEntity).AssemblyQualifiedName, "TypeName property get different than ctor");
            t = key2.Type;
            Assert.AreEqual(typeof(TestEntity), t, "CodeMemberKey.Type failed to load type from name");
            propInfo = key2.PropertyInfo;
            Assert.IsNotNull(propInfo, "CodeMemberKey.PropertyInfo failed to get real property info.");
            Assert.AreEqual("TheValue", propInfo.Name, "incorrect property 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);
        }
        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 GetPropertyShareKind(string typeName, string propertyName)
        {
            CodeMemberKey key          = CodeMemberKey.CreatePropertyKey(typeName, propertyName);
            PropertyInfo  propertyInfo = key.PropertyInfo;

            if (propertyInfo == null)
            {
                return(CodeMemberShareKind.NotShared);
            }
            MethodBase method = propertyInfo.GetGetMethod();

            return(GetMethodShareKind(typeName, method.Name, new string[0]));
        }
        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 bool IsTypeSharedInFile(string typeName)
        {
            if (this._pdbSourceFileLocationService == null)
            {
                return(false);
            }
            Type t = CodeMemberKey.CreateTypeKey(typeName).Type;

            if (t != null)
            {
                IEnumerable <string> files = this._pdbSourceFileLocationService.GetFilesForType(t);
                foreach (string file in files)
                {
                    if (this.IsFileShared(file))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        public void CodeMemberKey_Type_Key()
        {
            // Form the name based key
            CodeMemberKey key = CodeMemberKey.CreateTypeKey(typeof(TestEntity).AssemblyQualifiedName);

            Assert.IsNotNull(key, "CreateTypeKey using name failed");
            Assert.AreEqual(key.TypeName, typeof(TestEntity).AssemblyQualifiedName, "TypeName property get different than ctor");
            Type t = key.Type;

            Assert.AreEqual(typeof(TestEntity), t, "CodeMemberKey.Type failed to load type from name");

            // Form the type-based key
            CodeMemberKey key2 = CodeMemberKey.CreateTypeKey(typeof(TestEntity));

            Assert.IsNotNull(key2, "CreateTypeKey using type failed");
            Assert.AreEqual(key2.TypeName, typeof(TestEntity).AssemblyQualifiedName, "TypeName property get different than ctor");
            t = key2.Type;
            Assert.AreEqual(typeof(TestEntity), t, "CodeMemberKey.Type failed to load type from name");

            // These keys should be the same
            Assert.AreEqual(key.GetHashCode(), key2.GetHashCode(), "CodeMemberKeys from type should have yielded same hash");
            Assert.AreEqual(key, key2);
        }
Exemple #12
0
        private static string GetSharedPropertyLocation(SharedAssemblies sa, string fullName, string propertyName)
        {
            var key = CodeMemberKey.CreatePropertyKey(fullName, propertyName);

            return(sa.GetSharedAssemblyPath(key));
        }
Exemple #13
0
        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));
        }
Exemple #14
0
        private static string GetSharedTypeLocation(SharedAssemblies sa, Type type)
        {
            var key = CodeMemberKey.CreateTypeKey(type.FullName);

            return(sa.GetSharedAssemblyPath(key));
        }