示例#1
0
		public void Build()
		{
			var baseType = typeof(IBuilderTest);
			var handlers = new ReadOnlyDictionary<int, ReadOnlyCollection<HandlerInformation>>(
				new Dictionary<int, ReadOnlyCollection<HandlerInformation>>());
			var namespaces = new SortedSet<string> { baseType.Namespace };
			var options = new RockOptions();

			var builder = new InMemoryBuilder(baseType, handlers, namespaces, options, false);
			builder.Build();

			Assert.AreSame(baseType, builder.BaseType, nameof(builder.BaseType));
			Assert.AreSame(handlers, builder.Handlers, nameof(builder.Handlers));
			Assert.AreSame(namespaces, builder.Namespaces, nameof(builder.Namespaces));
			Assert.AreEqual(0, namespaces.Count, nameof(namespaces.Count));

			Assert.AreSame(options, builder.Options, nameof(builder.Options));
			Assert.IsNotNull(builder.Tree, nameof(builder.Tree));
			Assert.IsTrue(!string.IsNullOrWhiteSpace(builder.TypeName), nameof(builder.TypeName));

			var tree = builder.Tree.ToString();

			Assert.IsFalse(tree.StartsWith("#pragma warning disable CS0618"));
			Assert.IsFalse(tree.Contains("#pragma warning disable CS0672"));
			Assert.IsFalse(tree.Contains("#pragma warning restore CS0672"));
			Assert.IsFalse(tree.EndsWith("#pragma warning restore CS0618"));
		}
        public static void Compile()
        {
            var baseType = typeof(IBuilderTest);
            var handlers = new ReadOnlyDictionary <int, ReadOnlyCollection <HandlerInformation> >(
                new Dictionary <int, ReadOnlyCollection <HandlerInformation> >());
            var namespaces = new SortedSet <string> {
                baseType.Namespace !
            };
            var options = new RockOptions();

            var builder = new InMemoryBuilder(baseType, handlers, namespaces, options, false);

            var trees    = new[] { builder.Tree };
            var compiler = new InMemoryCompiler(trees, options.Optimization,
                                                new List <Assembly> {
                baseType.Assembly
            }.AsReadOnly(), builder.IsUnsafe,
                                                options.AllowWarning);
            var assembly = compiler.Compile();

            Assert.That(compiler.Optimization, Is.EqualTo(options.Optimization), nameof(compiler.Optimization));
            Assert.That(compiler.Trees, Is.SameAs(trees), nameof(compiler.Trees));
            Assert.That(assembly, Is.Not.Null, nameof(compiler.Compile));
            Assert.That(
                (from type in assembly.GetTypes()
                 where baseType.IsAssignableFrom(type)
                 select type).Single(), Is.Not.Null);
        }
    }
		public void Compile()
		{
			var baseType = typeof(IBuilderTest);
			var handlers = new ReadOnlyDictionary<int, ReadOnlyCollection<HandlerInformation>>(
				new Dictionary<int, ReadOnlyCollection<HandlerInformation>>());
			var namespaces = new SortedSet<string> { baseType.Namespace };
			var options = new RockOptions();

			var builder = new InMemoryBuilder(baseType, handlers, namespaces, options, false);
			builder.Build();

			var trees = new[] { builder.Tree };
			var compiler = new InMemoryCompiler(trees, options.Optimization, 
				new List<Assembly> { baseType.Assembly }.AsReadOnly(), builder.IsUnsafe,
				options.AllowWarnings);
			compiler.Compile();

			Assert.AreEqual(options.Optimization, compiler.Optimization, nameof(compiler.Optimization));
			Assert.AreSame(trees, compiler.Trees, nameof(compiler.Trees));
			Assert.IsNotNull(compiler.Result, nameof(compiler.Result));
			Assert.IsNotNull(
				(from type in compiler.Result.GetTypes()
				where baseType.IsAssignableFrom(type)
				select type).Single());
		}
        public static void Build()
        {
            var baseType = typeof(IBuilderTest);
            var handlers = new ReadOnlyDictionary <int, ReadOnlyCollection <HandlerInformation> >(
                new Dictionary <int, ReadOnlyCollection <HandlerInformation> >());
            var namespaces = new SortedSet <string> {
                baseType.Namespace !
            };
            var options = new RockOptions();

            var builder = new InMemoryBuilder(baseType, handlers, namespaces, options, false);

            Assert.That(builder.BaseType, Is.SameAs(baseType), nameof(builder.BaseType));
            Assert.That(builder.Handlers, Is.SameAs(handlers), nameof(builder.Handlers));
            Assert.That(builder.Namespaces, Is.SameAs(namespaces), nameof(builder.Namespaces));
            Assert.That(namespaces.Count, Is.EqualTo(0), nameof(namespaces.Count));

            Assert.That(builder.Options, Is.SameAs(options), nameof(builder.Options));
            Assert.That(builder.Tree, Is.Not.Null, nameof(builder.Tree));
            Assert.That(!string.IsNullOrWhiteSpace(builder.TypeName), Is.True, nameof(builder.TypeName));

            var tree = builder.Tree.ToString();

            Assert.That(tree.StartsWith("#pragma warning disable CS0618"), Is.False);
            Assert.That(tree.Contains("#pragma warning disable CS0672"), Is.False);
            Assert.That(tree.Contains("#pragma warning restore CS0672"), Is.False);
            Assert.That(tree.EndsWith("#pragma warning restore CS0618"), Is.False);
        }
示例#5
0
		internal InMemoryMaker(Type baseType,
			ReadOnlyDictionary<int, ReadOnlyCollection<HandlerInformation>> handlers,
			SortedSet<string> namespaces, RockOptions options, bool isMake)
		{
			var builder = new InMemoryBuilder(baseType, handlers, namespaces, options, isMake);
			builder.Build();

			var compiler = new InMemoryCompiler(new List<SyntaxTree> { builder.Tree }, options.Optimization,
				new List<Assembly> { baseType.Assembly }.AsReadOnly(), builder.IsUnsafe, options.AllowWarnings);
			compiler.Compile();

			this.Mock = compiler.Result.GetType($"{baseType.Namespace}.{builder.TypeName}");
		}
示例#6
0
		public void BuildWhenMethodIsObsolete()
		{
			var baseType = typeof(IHaveAnObsoleteMethod);
			var handlers = new ReadOnlyDictionary<int, ReadOnlyCollection<HandlerInformation>>(
				new Dictionary<int, ReadOnlyCollection<HandlerInformation>>());
			var namespaces = new SortedSet<string> { baseType.Namespace };
			var options = new RockOptions();

			var builder = new InMemoryBuilder(baseType, handlers, namespaces, options, false);
			builder.Build();

			var tree = builder.Tree.ToString();

			Assert.IsTrue(tree.StartsWith("#pragma warning disable CS0618"));
			Assert.IsTrue(tree.Contains("#pragma warning disable CS0672"));
			Assert.IsTrue(tree.Contains("#pragma warning restore CS0672"));
			Assert.IsTrue(tree.EndsWith("#pragma warning restore CS0618"));
		}
        public static void BuildWhenPropertyIsObsolete()
        {
            var baseType = typeof(IHaveAnObsoleteProperty);
            var handlers = new ReadOnlyDictionary <int, ReadOnlyCollection <HandlerInformation> >(
                new Dictionary <int, ReadOnlyCollection <HandlerInformation> >());
            var namespaces = new SortedSet <string> {
                baseType.Namespace !
            };
            var options = new RockOptions();

            var builder = new InMemoryBuilder(baseType, handlers, namespaces, options, false);

            var tree = builder.Tree.ToString();

            Assert.That(tree.StartsWith("#pragma warning disable CS0618"), Is.True);
            Assert.That(tree.Contains("#pragma warning disable CS0672"), Is.True);
            Assert.That(tree.Contains("#pragma warning restore CS0672"), Is.True);
            Assert.That(tree.EndsWith("#pragma warning restore CS0618"), Is.True);
        }