private CompilationVerifier CompileAndVerify(string source, string expectedOutput, IEnumerable<MetadataReference> references = null, TestEmitters emitOptions = TestEmitters.All, CSharpCompilationOptions compOptions = null) { SynchronizationContext.SetSynchronizationContext(null); var compilation = this.CreateCompilation(source, references: references, compOptions: compOptions); return base.CompileAndVerify(compilation, expectedOutput: expectedOutput, emitOptions: emitOptions); }
private CompilationVerifier CompileAndVerify(string source, string expectedOutput = null, IEnumerable<MetadataReference> references = null, TestEmitters emitOptions = TestEmitters.All, CSharpCompilationOptions options = null) { references = (references != null) ? references.Concat(s_asyncRefs) : s_asyncRefs; return base.CompileAndVerify(source, expectedOutput: expectedOutput, additionalRefs: references, options: options, emitOptions: emitOptions); }
internal CompilationVerifier CompileAndVerifyFieldMarshal(string source, Func <string, PEAssembly, TestEmitters, byte[]> getExpectedBlob, bool isField = true, TestEmitters emitters = TestEmitters.All) { return(CompileAndVerify(source, emitters: emitters, options: CompilationOptionsReleaseDll, assemblyValidator: (assembly, options) => MarshalAsMetadataValidator(assembly, getExpectedBlob, options, isField))); }
internal CompilationVerifier CompileAndVerifyFieldMarshal(string source, Dictionary <string, byte[]> expectedBlobs, bool isField = true, TestEmitters emitters = TestEmitters.All) { return(CompileAndVerifyFieldMarshal( source, (s, _omitted1, _omitted2) => { Assert.True(expectedBlobs.ContainsKey(s), "Expecting marshalling blob for " + (isField ? "field " : "parameter ") + s); return expectedBlobs[s]; }, isField, emitters)); }
internal CompilationVerifier CompileAndVerify( Compilation compilation, IEnumerable <ResourceDescription> manifestResources = null, IEnumerable <ModuleData> dependencies = null, TestEmitters emitters = TestEmitters.All, Action <IModuleSymbol, TestEmitters> sourceSymbolValidator = null, Action <PEAssembly, TestEmitters> assemblyValidator = null, Action <IModuleSymbol, TestEmitters> symbolValidator = null, SignatureDescription[] expectedSignatures = null, string expectedOutput = null, bool collectEmittedAssembly = true, bool verify = true) { Assert.NotNull(compilation); Assert.True(expectedOutput == null || (compilation.Options.OutputKind == OutputKind.ConsoleApplication || compilation.Options.OutputKind == OutputKind.WindowsApplication), "Compilation must be executable if output is expected."); if (verify) { // Unsafe code might not verify, so don't try. var csharpOptions = compilation.Options as CSharp.CSharpCompilationOptions; verify = (csharpOptions == null || !csharpOptions.AllowUnsafe); } if (sourceSymbolValidator != null) { var module = compilation.Assembly.Modules.First(); sourceSymbolValidator(module, emitters); } if (s_emitters.IsDefaultOrEmpty) { throw new InvalidOperationException( @"You must specify at least one Emitter. Example app.config: <?xml version=""1.0"" encoding=""utf-8""?> <configuration> <roslyn.unittests> <emit> <method assembly=""SomeAssembly"" type=""SomeClass"" name= ""SomeEmitMethod"" /> </emit> </roslyn.unittests> </configuration>"); } CompilationVerifier result = null; foreach (var emit in s_emitters) { var verifier = emit(this, compilation, dependencies, emitters, manifestResources, expectedSignatures, expectedOutput, assemblyValidator, symbolValidator, collectEmittedAssembly, verify); if (result == null) { result = verifier; } else { // only one emitter should return a verifier Assert.Null(verifier); } } // If this fails, it means that more that all emmiters failed to return a validator // (i.e. none thought that they were applicable for the given input parameters). Assert.NotNull(result); return(result); }
/// <summary> /// Used to validate metadata blobs emitted for MarshalAs. /// </summary> internal static void MarshalAsMetadataValidator(PEAssembly assembly, Func <string, PEAssembly, TestEmitters, byte[]> getExpectedBlob, TestEmitters emitOptions, bool isField = true) { var metadataReader = assembly.GetMetadataReader(); // no custom attributes should be emitted on parameters, fields or methods: foreach (var ca in metadataReader.CustomAttributes) { Assert.NotEqual("MarshalAsAttribute", GetAttributeName(metadataReader, ca)); } int expectedMarshalCount = 0; if (isField) { // fields foreach (var fieldDef in metadataReader.FieldDefinitions) { var field = metadataReader.GetFieldDefinition(fieldDef); string fieldName = metadataReader.GetString(field.Name); byte[] expectedBlob = getExpectedBlob(fieldName, assembly, emitOptions); if (expectedBlob != null) { BlobHandle descriptor = metadataReader.GetFieldDefinition(fieldDef).GetMarshallingDescriptor(); Assert.False(descriptor.IsNil, "Expecting record in FieldMarshal table"); Assert.NotEqual(0, (int)(field.Attributes & FieldAttributes.HasFieldMarshal)); expectedMarshalCount++; byte[] actualBlob = metadataReader.GetBlobBytes(descriptor); AssertEx.Equal(expectedBlob, actualBlob); } else { Assert.Equal(0, (int)(field.Attributes & FieldAttributes.HasFieldMarshal)); } } } else { // parameters foreach (var methodHandle in metadataReader.MethodDefinitions) { var methodDef = metadataReader.GetMethodDefinition(methodHandle); string memberName = metadataReader.GetString(methodDef.Name); foreach (var paramHandle in methodDef.GetParameters()) { var paramRow = metadataReader.GetParameter(paramHandle); string paramName = metadataReader.GetString(paramRow.Name); byte[] expectedBlob = getExpectedBlob(memberName + ":" + paramName, assembly, emitOptions); if (expectedBlob != null) { Assert.NotEqual(0, (int)(paramRow.Attributes & ParameterAttributes.HasFieldMarshal)); expectedMarshalCount++; BlobHandle descriptor = metadataReader.GetParameter(paramHandle).GetMarshallingDescriptor(); Assert.False(descriptor.IsNil, "Expecting record in FieldMarshal table"); byte[] actualBlob = metadataReader.GetBlobBytes(descriptor); AssertEx.Equal(expectedBlob, actualBlob); } else { Assert.Equal(0, (int)(paramRow.Attributes & ParameterAttributes.HasFieldMarshal)); } } } } Assert.Equal(expectedMarshalCount, metadataReader.GetTableRowCount(TableIndex.FieldMarshal)); }
private CompilationVerifier CompileAndVerify(string source, string expectedOutput = null, IEnumerable <MetadataReference> references = null, TestEmitters emitOptions = TestEmitters.All, CSharpCompilationOptions options = null) { references = (references != null) ? references.Concat(s_asyncRefs) : s_asyncRefs; return(base.CompileAndVerify(source, expectedOutput: expectedOutput, additionalRefs: references, options: options, emitOptions: emitOptions)); }