Пример #1
0
		public void FixtureSetUp()
		{
			// use "IncludeInternalMembers" so that Cecil results match C# parser results
			CecilLoader loader = new CecilLoader() { IncludeInternalMembers = true };
			IUnresolvedAssembly asm = loader.LoadAssemblyFile(typeof(TestCase.SimplePublicClass).Assembly.Location);
			compilation = new SimpleCompilation(asm, CecilLoaderTests.Mscorlib);
		}
Пример #2
0
		ICompilation CreateCompilation()
		{
			var solutionSnapshot = new TestableSolutionSnapshot(msbuildProject);
			msbuildProject.SetProjectContent(projectContent);
			ICompilation compilation = new SimpleCompilation(solutionSnapshot, projectContent, projectContent.AssemblyReferences);
			solutionSnapshot.AddCompilation(projectContent, compilation);
			return compilation;
		}
		public void ConstructorsAreReportedAsJsonConstructors() {
			var compilation = new SimpleCompilation(new CSharpProjectContent());
			var er = new MockErrorReporter(true);
			var md = new MetadataImporter(er, compilation, new CompilerOptions());
			Assert.That(er.AllMessages, Is.Empty, "Prepare should not generate errors");

			var t = CreateType(compilation);

			var c = md.GetConstructorSemantics(DefaultResolvedMethod.GetDummyConstructor(compilation, t));
			Assert.That(c.Type, Is.EqualTo(ConstructorScriptSemantics.ImplType.Json));
		}
Пример #4
0
		public void FixtureSetUp()
		{
			try {
				// use "IncludeInternalMembers" so that Cecil results match C# parser results
				IkvmLoader loader = new IkvmLoader() { IncludeInternalMembers = true };
				IUnresolvedAssembly asm = loader.LoadAssemblyFile(typeof(TestCase.SimplePublicClass).Assembly.Location);
				compilation = new SimpleCompilation(asm, IkvmLoaderTests.Mscorlib);
			} catch (Exception e) {
				Console.WriteLine(e);
				throw;
			}
		}
		public void TransparentIdentiferIsValidJavascriptIdentifierStartingWithDollar() {
			var compilation = new SimpleCompilation(new CSharpProjectContent());
			var er = new MockErrorReporter(true);
			var md = new MetadataImporter(er, compilation, new CompilerOptions());
			Assert.That(er.AllMessages, Is.Empty, "Prepare should not generate errors");

			var t = CreateType(compilation, new[] { "<>Identifier" });

			var c = md.GetPropertySemantics(t.GetProperties().Single());
			Assert.That(c.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
			Assert.That(c.FieldName, Is.EqualTo("$Identifier"));
		}
Пример #6
0
		public void TypeParameterDerivingFromOtherTypeParameterDoesNotInheritReferenceConstraint()
		{
			// class C<T, U> where T : class where U : T
			var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");
			c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T") { HasReferenceTypeConstraint = true });
			c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U") {
			                     	Constraints = { new TypeParameterReference(EntityType.TypeDefinition, 0) }
			                     });
			
			ITypeDefinition resolvedC = new SimpleCompilation(CecilLoaderTests.Mscorlib).MainAssembly.GetTypeDefinition(c);
			// At runtime, we might have T=System.ValueType and U=int, so C# can't inherit the 'class' constraint
			// from one type parameter to another.
			Assert.AreEqual(true, resolvedC.TypeParameters[0].IsReferenceType);
			Assert.IsNull(resolvedC.TypeParameters[1].IsReferenceType);
		}
		public void AnonymousTypePropertyNamesAreNotMinimized() {
			var compilation = new SimpleCompilation(new CSharpProjectContent());
			var er = new MockErrorReporter(true);
			var md = new MetadataImporter(er, compilation, new CompilerOptions());
			Assert.That(er.AllMessages, Is.Empty, "Prepare should not generate errors");

			var t = CreateType(compilation);

			var p1 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "prop1"));
			Assert.That(p1.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
			Assert.That(p1.FieldName, Is.EqualTo("prop1"));

			var p2 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "Prop2"));
			Assert.That(p2.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
			Assert.That(p2.FieldName, Is.EqualTo("Prop2"));
		}
		public void PropertiesAreImplementedAsFieldsWithTheSameName() {
			var compilation = new SimpleCompilation(new CSharpProjectContent());
			var er = new MockErrorReporter(true);
			var s = new AttributeStore(compilation, er);
			var md = new MetadataImporter(er, compilation, s, new CompilerOptions());
			Assert.That(er.AllMessages, Is.Empty, "Prepare should not generate errors");

			var t = CreateType(compilation);

			var p1 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "prop1"));
			Assert.That(p1.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
			Assert.That(p1.FieldName, Is.EqualTo("prop1"));

			var p2 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "Prop2"));
			Assert.That(p2.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
			Assert.That(p2.FieldName, Is.EqualTo("Prop2"));
		}
Пример #9
0
		public void TypeParameterDerivingFromOtherTypeParameterInheritsEffectiveBaseClass()
		{
			// class C<T, U> where T : List<string> where U : T
			var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");
			c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T") {
			                     	Constraints = { typeof(List<string>).ToTypeReference() }
			                     });
			c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U") {
			                     	Constraints = { new TypeParameterReference(EntityType.TypeDefinition, 0) }
			                     });
			
			ITypeDefinition resolvedC = new SimpleCompilation(CecilLoaderTests.Mscorlib).MainAssembly.GetTypeDefinition(c);
			Assert.AreEqual(true, resolvedC.TypeParameters[0].IsReferenceType);
			Assert.AreEqual(true, resolvedC.TypeParameters[1].IsReferenceType);
			Assert.AreEqual("System.Collections.Generic.List`1[[System.String]]", resolvedC.TypeParameters[0].EffectiveBaseClass.ReflectionName);
			Assert.AreEqual("System.Collections.Generic.List`1[[System.String]]", resolvedC.TypeParameters[1].EffectiveBaseClass.ReflectionName);
		}
        public void PropertiesAreImplementedAsFieldsWithTheSameName()
        {
            var compilation = new SimpleCompilation(new CSharpProjectContent());
            var md = new MetadataImporter.ScriptSharpMetadataImporter(false);
            var er = new MockErrorReporter(true);
            md.Prepare(new ITypeDefinition[0], compilation.MainAssembly, er);
            Assert.That(er.AllMessagesText, Is.Empty, "Prepare should not generate errors");

            var t = CreateType(compilation);

            var p1 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "prop1"));
            Assert.That(p1.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
            Assert.That(p1.FieldName, Is.EqualTo("prop1"));

            var p2 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "Prop2"));
            Assert.That(p2.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
            Assert.That(p2.FieldName, Is.EqualTo("Prop2"));
        }
Пример #11
0
		public virtual ICompilation CreateCompilation()
		{
			var solutionSnapshot = new DefaultSolutionSnapshot();
			ICompilation compilation = new SimpleCompilation(solutionSnapshot, this, assemblyReferences);
			solutionSnapshot.AddCompilation(this, compilation);
			return compilation;
		}
Пример #12
0
		public void ExpansiveInheritance()
		{
			var a = new DefaultUnresolvedTypeDefinition(string.Empty, "A");
			var b = new DefaultUnresolvedTypeDefinition(string.Empty, "B");
			// interface A<in U>
			a.Kind = TypeKind.Interface;
			a.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "U") { Variance = VarianceModifier.Contravariant });
			// interface B<X> : A<A<B<X>>> { }
			b.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "X"));
			b.BaseTypes.Add(new ParameterizedTypeReference(
				a, new[] { new ParameterizedTypeReference(
					a, new [] { new ParameterizedTypeReference(
						b, new [] { new TypeParameterReference(EntityType.TypeDefinition, 0) }
					) } ) }));
			
			ICompilation compilation = new SimpleCompilation(CecilLoaderTests.Mscorlib);
			ITypeDefinition resolvedA = compilation.MainAssembly.GetTypeDefinition(a);
			ITypeDefinition resolvedB = compilation.MainAssembly.GetTypeDefinition(b);
			
			IType type1 = new ParameterizedType(resolvedB, new[] { compilation.FindType(KnownTypeCode.Double) });
			IType type2 = new ParameterizedType(resolvedA, new [] { new ParameterizedType(resolvedB, new[] { compilation.FindType(KnownTypeCode.String) }) });
			Assert.IsFalse(conversions.ImplicitConversion(type1, type2));
		}
Пример #13
0
		public void FindRedirectedType()
		{
			var compilationWithSystemCore = new SimpleCompilation(systemCore.Value, mscorlib.Value);
			
			var typeRef = ReflectionHelper.ParseReflectionName("System.Func`2, System.Core");
			ITypeDefinition c = typeRef.Resolve(compilationWithSystemCore.TypeResolveContext).GetDefinition();
			Assert.IsNotNull(c, "System.Func<,> not found");
			Assert.AreEqual("mscorlib", c.ParentAssembly.AssemblyName);
		}
		static ICompilation GetSystemWebDom (TargetRuntime runtime, TargetFramework targetFramework)
		{
			string file = runtime.AssemblyContext.GetAssemblyNameForVersion (sysWebAssemblyName, targetFramework);
			if (string.IsNullOrEmpty (file))
				throw new Exception ("System.Web assembly name not found for framework " + targetFramework.Id);
			file = runtime.AssemblyContext.GetAssemblyLocation (file, targetFramework);
			if (string.IsNullOrEmpty (file))
				throw new Exception ("System.Web assembly file not found for framework " + targetFramework.Id);
			var dom = new SimpleCompilation (TypeSystemService.LoadAssemblyContext (runtime, targetFramework, file), new IAssemblyReference[] {
				TypeSystemService.LoadAssemblyContext (runtime, targetFramework, typeof (object).Assembly.Location)
			});
			if (dom == null)
				throw new Exception ("System.Web parse database not found for framework " + targetFramework.Id + " file '" + file + "'");
			return dom;
		}
		protected IType Resolve (ITreeNavigator treeBuilder, IUnresolvedTypeDefinition type)
		{
			var mainAssembly = GetMainAssembly (treeBuilder);
			if (mainAssembly != null) {
				var simpleCompilation = new SimpleCompilation (mainAssembly);
				return type.Resolve (new SimpleTypeResolveContext (simpleCompilation.MainAssembly));
			}
			// TODO: roslyn port ?
			// var project = (Project)treeBuilder.GetParentDataItem (typeof(Project), true);
			// var ctx = TypeSystemService.GetCompilation (project);
			// return ctx.MainAssembly.GetTypeDefinition (type.Namespace, type.Name, type.TypeParameters.Count);
			return null;
		}
		protected ITypeResolveContext GetContext (ITreeNavigator treeBuilder)
		{
			var mainAssembly = GetMainAssembly (treeBuilder);
			if (mainAssembly != null) {
				var simpleCompilation = new SimpleCompilation (mainAssembly);
				return new SimpleTypeResolveContext (simpleCompilation.MainAssembly);
			}
			// TODO: roslyn port ?
			// var project = (Project)treeBuilder.GetParentDataItem (typeof(Project), true);
			// var compilation = TypeSystemService.GetCompilation (project);
			// return new SimpleTypeResolveContext (compilation.MainAssembly);
			return null;
		}
		protected IMember Resolve (ITreeNavigator treeBuilder, IUnresolvedMember member)
		{
			var mainAssembly = GetMainAssembly (treeBuilder);
			if (mainAssembly != null) {
				var simpleCompilation = new SimpleCompilation (mainAssembly);
				return member.CreateResolved (new SimpleTypeResolveContext (simpleCompilation.MainAssembly));
			}
			var project = (Project)treeBuilder.GetParentDataItem (typeof(Project), true);
			var ctx = TypeSystemService.GetCompilation (project);
			return member.CreateResolved (ctx.TypeResolveContext);
		}
Пример #18
0
 public void CorlibIDStrings()
 {
     var list = new SimpleCompilation(CecilLoaderTests.Mscorlib).FindType(typeof(List<>)).GetDefinition();
     Assert.AreEqual("T:System.Collections.Generic.List`1",
                     IdStringProvider.GetIdString(list));
     Assert.AreEqual("M:System.Collections.Generic.List`1.Add(`0)",
                     IdStringProvider.GetIdString(list.Methods.Single(m => m.Name == "Add")));
     Assert.AreEqual("M:System.Collections.Generic.List`1.AddRange(System.Collections.Generic.IEnumerable{`0})",
                     IdStringProvider.GetIdString(list.Methods.Single(m => m.Name == "AddRange")));
     Assert.AreEqual("M:System.Collections.Generic.List`1.ConvertAll``1(System.Converter{`0,``0})",
                     IdStringProvider.GetIdString(list.Methods.Single(m => m.Name == "ConvertAll")));
 }