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"));
		}
Example #2
0
		public RockAssembly(Assembly assembly, RockOptions options, string currentDirectory)
		{
			this.assembly = assembly;
			this.options = options;
			this.currentDirectory = currentDirectory;
			this.Result = this.Generate();
		}
		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());
		}
Example #4
0
		public void CreateWithDefaults()
		{
			var options = new RockOptions();
			Assert.AreEqual(OptimizationSetting.Release, options.Optimization, nameof(options.Optimization));
			Assert.AreEqual(CodeFileOptions.None, options.CodeFile, nameof(options.CodeFile));
			Assert.AreEqual(SerializationOptions.NotSupported, options.Serialization, nameof(options.Serialization));
			Assert.AreEqual(CachingOptions.UseCache, options.Caching, nameof(options.Caching));
			Assert.AreEqual(AllowWarnings.No, options.AllowWarnings, nameof(options.AllowWarnings));
			Assert.AreEqual(4, options.GetHashCode(), nameof(options.GetHashCode));
		}
Example #5
0
		public void CreateWithCachingOptionsGenerateNewVersion()
		{
			var options = new RockOptions(caching: CachingOptions.GenerateNewVersion);
			Assert.AreEqual(OptimizationSetting.Release, options.Optimization, nameof(options.Optimization));
			Assert.AreEqual(CodeFileOptions.None, options.CodeFile, nameof(options.CodeFile));
			Assert.AreEqual(SerializationOptions.NotSupported, options.Serialization, nameof(options.Serialization));
			Assert.AreEqual(CachingOptions.GenerateNewVersion, options.Caching, nameof(options.Caching));
			Assert.AreEqual(AllowWarnings.No, options.AllowWarnings, nameof(options.AllowWarnings));
			Assert.AreEqual(12, options.GetHashCode(), nameof(options.GetHashCode));
		}
Example #6
0
		public void CreateWithOptimizationLevelDebug()
		{
			var options = new RockOptions(level: OptimizationSetting.Debug);
			Assert.AreEqual(OptimizationSetting.Debug, options.Optimization, nameof(options.Optimization));
			Assert.AreEqual(CodeFileOptions.None, options.CodeFile, nameof(options.CodeFile));
			Assert.AreEqual(SerializationOptions.NotSupported, options.Serialization, nameof(options.Serialization));
			Assert.AreEqual(CachingOptions.UseCache, options.Caching, nameof(options.Caching));
			Assert.AreEqual(AllowWarnings.No, options.AllowWarnings, nameof(options.AllowWarnings));
			Assert.AreEqual(0, options.GetHashCode(), nameof(options.GetHashCode));
		}
Example #7
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}");
		}
		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"));
		}
Example #9
0
		public RockAssembly(Assembly assembly, RockOptions options)
			: this(assembly, options, Environment.CurrentDirectory)
		{ }