Esempio n. 1
0
        /// <summary>
        /// Uses Reflection to verify that the specified member signatures are present in emitted metadata
        /// </summary>
        /// <param name="appDomainHost">Unit test AppDomain host</param>
        /// <param name="expectedSignatures">Baseline signatures - use the Signature() factory method to create instances of SignatureDescription</param>
        public static void VerifyMemberSignatures(
            HostedRuntimeEnvironment appDomainHost, params SignatureDescription[] expectedSignatures)
        {
            Assert.NotNull(expectedSignatures);
            Assert.NotEmpty(expectedSignatures);

            var succeeded = true;
            var expected  = new List <string>();
            var actual    = new List <string>();

            foreach (var signature in expectedSignatures)
            {
                List <string> actualSignatures  = null;
                var           expectedSignature = signature.ExpectedSignature;

                if (!VerifyMemberSignatureHelper(
                        appDomainHost, signature.FullyQualifiedTypeName, signature.MemberName,
                        ref expectedSignature, out actualSignatures))
                {
                    succeeded = false;
                }

                expected.Add(expectedSignature);
                actual.AddRange(actualSignatures);
            }

            if (!succeeded)
            {
                TriggerSignatureMismatchFailure(expected, actual);
            }
        }
            public void Emit(string expectedOutput, IEnumerable <ResourceDescription> manifestResources, bool emitPdb, bool peVerify, SignatureDescription[] expectedSignatures)
            {
                bool doExecute = expectedOutput != null;

                using (var testEnvironment = new HostedRuntimeEnvironment(dependencies))
                {
                    string mainModuleName = Emit(testEnvironment, manifestResources, emitPdb);

                    allModuleData = testEnvironment.GetAllModuleData();

                    if (peVerify)
                    {
                        testEnvironment.PeVerify();
                    }

                    if (expectedSignatures != null)
                    {
                        MetadataSignatureUnitTestHelper.VerifyMemberSignatures(testEnvironment, expectedSignatures);
                    }

                    if (doExecute)
                    {
                        testEnvironment.Execute(mainModuleName, expectedOutput);
                    }
                }
            }
        /// <summary>
        /// Uses Reflection to verify that the specified member signatures are present in emitted metadata
        /// </summary>
        /// <param name="appDomainHost">Unit test AppDomain host</param>
        /// <param name="expectedSignatures">Baseline signatures - use the Signature() factory method to create instances of SignatureDescription</param>
        public static void VerifyMemberSignatures(
            HostedRuntimeEnvironment appDomainHost, params SignatureDescription[] expectedSignatures)
        {
            Assert.NotNull(expectedSignatures);
            Assert.NotEmpty(expectedSignatures);

            var succeeded = true;
            var expected = new List<string>();
            var actual = new List<string>();

            foreach (var signature in expectedSignatures)
            {
                List<string> actualSignatures = null;
                var expectedSignature = signature.ExpectedSignature;

                if (!VerifyMemberSignatureHelper(
                    appDomainHost, signature.FullyQualifiedTypeName, signature.MemberName,
                    ref expectedSignature, out actualSignatures))
                {
                    succeeded = false;
                }

                expected.Add(expectedSignature);
                actual.AddRange(actualSignatures);
            }

            if (!succeeded)
            {
                TriggerSignatureMismatchFailure(expected, actual);
            }
        }
            public void Emit(string expectedOutput, IEnumerable<ResourceDescription> manifestResources, bool emitPdb, bool peVerify, SignatureDescription[] expectedSignatures)
            {
                bool doExecute = expectedOutput != null;

                using (var testEnvironment = new HostedRuntimeEnvironment(dependencies))
                {
                    string mainModuleName = Emit(testEnvironment, manifestResources, emitPdb);

                    allModuleData = testEnvironment.GetAllModuleData();

                    if (peVerify)
                    {
                        testEnvironment.PeVerify();
                    }

                    if (expectedSignatures != null)
                    {
                        MetadataSignatureUnitTestHelper.VerifyMemberSignatures(testEnvironment, expectedSignatures);
                    }

                    if (doExecute)
                    {
                        testEnvironment.Execute(mainModuleName, expectedOutput);
                    }
                }
            }
Esempio n. 5
0
        /// <summary>
        /// Uses Reflection to verify that the specified member signature is present in emitted metadata
        /// </summary>
        /// <param name="appDomainHost">Unit test AppDomain host</param>
        /// <param name="fullyQualifiedTypeName">
        /// Fully qualified type name for member
        /// Names must be in format recognized by reflection
        /// e.g. MyType&lt;T&gt;.MyNestedType&lt;T, U&gt; => MyType`1+MyNestedType`2
        /// </param>
        /// <param name="memberName">
        /// Name of member on specified type whose signature needs to be verified
        /// Names must be in format recognized by reflection
        /// e.g. For explicitly implemented member - I1&lt;string&gt;.Method => I1&lt;System.String&gt;.Method
        /// </param>
        /// <param name="expectedSignature">
        /// Baseline string for signature of specified member
        /// Skip this argument to get an error message that shows all available signatures for specified member
        /// This argument is passed by reference and it will be updated with a formatted form of the baseline signature for error reporting purposes
        /// </param>
        /// <param name="actualSignatures">List of found signatures matching member name</param>
        /// <returns>True if a matching member signature was found, false otherwise</returns>
        private static bool VerifyMemberSignatureHelper(
            HostedRuntimeEnvironment appDomainHost, string fullyQualifiedTypeName, string memberName,
            ref string expectedSignature, out List <string> actualSignatures)
        {
            Assert.False(string.IsNullOrWhiteSpace(fullyQualifiedTypeName), "'fullyQualifiedTypeName' can't be null or empty");
            Assert.False(string.IsNullOrWhiteSpace(memberName), "'memberName' can't be null or empty");

            var retVal              = true; actualSignatures = new List <string>();
            var signatures          = appDomainHost.GetMemberSignaturesFromMetadata(fullyQualifiedTypeName, memberName);
            var signatureAssertText = "Signature(\"" + fullyQualifiedTypeName + "\", \"" + memberName + "\", \"{0}\"),";

            if (!string.IsNullOrWhiteSpace(expectedSignature))
            {
                expectedSignature = expectedSignature.Replace("\"", "\\\"");
            }
            expectedSignature = string.Format(signatureAssertText, expectedSignature);

            if (signatures.Count > 1)
            {
                var found = false;
                foreach (var signature in signatures)
                {
                    var actualSignature = signature.Replace("\"", "\\\"");
                    actualSignature = string.Format(signatureAssertText, actualSignature);

                    if (actualSignature == expectedSignature)
                    {
                        actualSignatures.Clear();
                        actualSignatures.Add(actualSignature);
                        found = true; break;
                    }
                    else
                    {
                        actualSignatures.Add(actualSignature);
                    }
                }
                if (!found)
                {
                    retVal = false;
                }
            }
            else if (signatures.Count == 1)
            {
                var actualSignature = signatures.First().Replace("\"", "\\\"");
                actualSignature = string.Format(signatureAssertText, actualSignature);
                actualSignatures.Add(actualSignature);

                if (expectedSignature != actualSignature)
                {
                    retVal = false;
                }
            }
            else
            {
                retVal = false;
            }

            return(retVal);
        }
        /// <summary>
        /// Uses Reflection to verify that the specified member signature is present in emitted metadata
        /// </summary>
        /// <param name="appDomainHost">Unit test AppDomain host</param>
        /// <param name="fullyQualifiedTypeName">
        /// Fully qualified type name for member
        /// Names must be in format recognized by reflection
        /// e.g. MyType&lt;T&gt;.MyNestedType&lt;T, U&gt; => MyType`1+MyNestedType`2
        /// </param>
        /// <param name="memberName">
        /// Name of member on specified type whose signature needs to be verified
        /// Names must be in format recognized by reflection
        /// e.g. For explicitly implemented member - I1&lt;string&gt;.Method => I1&lt;System.String&gt;.Method
        /// </param>
        /// <param name="expectedSignature">
        /// Baseline string for signature of specified member
        /// Skip this argument to get an error message that shows all available signatures for specified member
        /// This argument is passed by reference and it will be updated with a formatted form of the baseline signature for error reporting purposes
        /// </param>
        /// <param name="actualSignatures">List of found signatures matching member name</param>
        /// <returns>True if a matching member signature was found, false otherwise</returns>
        private static bool VerifyMemberSignatureHelper(
            HostedRuntimeEnvironment appDomainHost, string fullyQualifiedTypeName, string memberName,
            ref string expectedSignature, out List<string> actualSignatures)
        {
            Assert.False(string.IsNullOrWhiteSpace(fullyQualifiedTypeName), "'fullyQualifiedTypeName' can't be null or empty");
            Assert.False(string.IsNullOrWhiteSpace(memberName), "'memberName' can't be null or empty");

            var retVal = true; actualSignatures = new List<string>();
            var signatures = appDomainHost.GetMemberSignaturesFromMetadata(fullyQualifiedTypeName, memberName);
            var signatureAssertText = "Signature(\"" + fullyQualifiedTypeName + "\", \"" + memberName + "\", \"{0}\"),";

            if (!string.IsNullOrWhiteSpace(expectedSignature))
            {
                expectedSignature = expectedSignature.Replace("\"", "\\\"");
            }
            expectedSignature = string.Format(signatureAssertText, expectedSignature);

            if (signatures.Count > 1)
            {
                var found = false;
                foreach (var signature in signatures)
                {
                    var actualSignature = signature.Replace("\"", "\\\"");
                    actualSignature = string.Format(signatureAssertText, actualSignature);

                    if (actualSignature == expectedSignature)
                    {
                        actualSignatures.Clear();
                        actualSignatures.Add(actualSignature);
                        found = true; break;
                    }
                    else
                    {
                        actualSignatures.Add(actualSignature);
                    }
                }
                if (!found)
                {
                    retVal = false;
                }
            }
            else if (signatures.Count == 1)
            {
                var actualSignature = signatures.First().Replace("\"", "\\\"");
                actualSignature = string.Format(signatureAssertText, actualSignature);
                actualSignatures.Add(actualSignature);

                if (expectedSignature != actualSignature)
                {
                    retVal = false;
                }
            }
            else
            {
                retVal = false;
            }

            return retVal;
        }
 // TODO(tomat): Fold into CompileAndVerify.
 // Replace bool verify parameter with string[] expectedPeVerifyOutput. If null, no verification. If empty verify have to succeed. Otherwise compare errors.
 public void EmitAndVerify(params string[] expectedPeVerifyOutput)
 {
     using (var testEnvironment = new HostedRuntimeEnvironment(_dependencies))
     {
         string   mainModuleName = Emit(testEnvironment, null);
         string[] actualOutput   = testEnvironment.PeVerifyModules(new[] { mainModuleName }, throwOnError: false);
         Assert.Equal(expectedPeVerifyOutput, actualOutput);
     }
 }
            private string Emit(HostedRuntimeEnvironment testEnvironment, IEnumerable <ResourceDescription> manifestResources, bool emitPdb)
            {
                testEnvironment.Emit(compilation, manifestResources, emitPdb);

                diagnostics         = testEnvironment.GetDiagnostics();
                EmittedAssemblyData = testEnvironment.GetMainImage();
                testData            = testEnvironment.GetCompilationTestData();

                return(compilation.Assembly.Identity.GetDisplayName());
            }
            private string Emit(HostedRuntimeEnvironment testEnvironment, IEnumerable<ResourceDescription> manifestResources)
            {
                testEnvironment.Emit(_compilation, manifestResources);

                _diagnostics = testEnvironment.GetDiagnostics();
                EmittedAssemblyData = testEnvironment.GetMainImage();
                EmittedAssemblyPdb = testEnvironment.GetMainPdb();
                _testData = testEnvironment.GetCompilationTestData();

                return _compilation.Assembly.Identity.GetDisplayName();
            }
 // TODO(tomat): Fold into CompileAndVerify. 
 // Replace bool verify parameter with string[] expectedPeVerifyOutput. If null, no verification. If empty verify have to succeed. Otherwise compare errors.
 public void EmitAndVerify(params string[] expectedPeVerifyOutput)
 {
     using (var testEnvironment = new HostedRuntimeEnvironment(_dependencies))
     {
         string mainModuleName = Emit(testEnvironment, null);
         string[] actualOutput = testEnvironment.PeVerifyModules(new[] { mainModuleName }, throwOnError: false);
         Assert.Equal(expectedPeVerifyOutput, actualOutput);
     }
 }