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 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")); }
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")); }