Пример #1
0
 internal PersistenceBuilder(Type baseType,
                             ReadOnlyDictionary <int, ReadOnlyCollection <HandlerInformation> > handlers,
                             SortedSet <string> namespaces, RockOptions options)
     : base(baseType, handlers, namespaces, options, new PersistenceNameGenerator(baseType),
            new PersistenceMethodInformationBuilder(namespaces, baseType), new PersistenceTypeNameGenerator(namespaces), false)
 {
 }
Пример #2
0
        public static void Create()
        {
            var options = new RockOptions();

            using var repository = new RockRepository(options);
            Assert.That(repository.Options, Is.SameAs(options));
        }
Пример #3
0
 public RockAssembly(Assembly assembly, RockOptions options)
     : base()
 {
     this.assembly = assembly;
     this.options  = options;
     this.Result   = this.Generate();
 }
Пример #4
0
        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
        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);
        }
    }
Пример #6
0
 private StashRocks(RockOptions globalOptions)
     : base(new StashboxContainer(config => config.WithUnknownTypeResolution()))
 {
     this.globalOptions = globalOptions;
     this.repository    = new ConcurrentDictionary <Type, object>();
     base.Container.RegisterResolver(new RocksResolver(base.RequestedTypes));
 }
Пример #7
0
 internal InMemoryBuilder(Type baseType,
                          ReadOnlyDictionary <int, ReadOnlyCollection <HandlerInformation> > handlers,
                          SortedSet <string> namespaces, RockOptions options, bool isMake)
     : base(baseType, handlers, namespaces, options, new InMemoryNameGenerator(),
            new InMemoryMethodInformationBuilder(namespaces, handlers),
            new InMemoryTypeNameGenerator(namespaces), isMake)
 {
 }
Пример #8
0
        private static IRock <T> NewRock <T>(RockOptions options, bool isMake)
            where T : class
        {
            var tType = typeof(T);

            // Can assume only sealed typed with the right constructor passed the .Validate() test.
            return(tType.IsSealed ? new AssemblyRock <T>() as IRock <T> :
                   new InMemoryRock <T>(options, isMake) as IRock <T>);
        }
Пример #9
0
 private static void AssertOptions(RockOptions options,
                                   OptimizationSetting level, CodeFileOption codeFile, SerializationOption serialization,
                                   CachingOption caching, AllowWarning allowWarnings, string codeFileDirectory)
 {
     Assert.That(options.Optimization, Is.EqualTo(level), nameof(options.Optimization));
     Assert.That(options.CodeFile, Is.EqualTo(codeFile), nameof(options.CodeFile));
     Assert.That(options.Serialization, Is.EqualTo(serialization), nameof(options.Serialization));
     Assert.That(options.Caching, Is.EqualTo(caching), nameof(options.Caching));
     Assert.That(options.AllowWarning, Is.EqualTo(allowWarnings), nameof(options.AllowWarning));
     Assert.That(options.CodeFileDirectory, Is.EqualTo(codeFileDirectory), nameof(options.CodeFileDirectory));
 }
Пример #10
0
        public MetadataReferenceCacheBenchmark()
        {
            var options = new RockOptions(caching: CachingOption.GenerateNewVersion);

            this.iaRock = Rock.Create <IA>(options);
            this.iaRock.Handle(_ => _.Foo());
            this.ibRock = Rock.Create <IB>(options);
            this.ibRock.Handle(_ => _.Foo());
            this.icRock = Rock.Create <IC>(options);
            this.icRock.Handle(_ => _.Foo());
            this.idRock = Rock.Create <ID>(options);
            this.idRock.Handle(_ => _.Foo());
        }
Пример #11
0
        /// <summary>
        /// Creates a simple dummy object and registers it into the container.
        /// </summary>
        /// <typeparam name="TService">The type of the mock.</typeparam>
        /// <param name="options">The options of the mock.</param>
        /// <returns>The created mock object.</returns>
        public TService Make <TService>(RockOptions options = null) where TService : class
        {
            if (base.Container.IsRegistered <TService>())
            {
                return(base.Container.Resolve <TService>());
            }

            options = options ?? this.globalOptions;

            var mock = options == null?Rock.Make <TService>() : Rock.Make <TService>(options);

            base.Container.RegisterInstanceAs(mock);
            return(mock);
        }
Пример #12
0
        /// <summary>
        /// Creates a mock object and registers it into the container.
        /// </summary>
        /// <typeparam name="TService">The type of the mock.</typeparam>
        /// <param name="options">The options of the mock.</param>
        /// <returns>The created mock object.</returns>
        public IRock <TService> Mock <TService>(RockOptions options = null) where TService : class
        {
            if (this.repository.TryGetValue(typeof(TService), out object value))
            {
                return((IRock <TService>)value);
            }

            options = options ?? this.globalOptions;

            var mock = options == null?Rock.Create <TService>() : Rock.Create <TService>(options);

            this.repository.AddOrUpdate(typeof(TService), mock, (key, val) => mock);
            return(mock);
        }
Пример #13
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);

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

            this.Mock = assembly.GetType($"{baseType.Namespace}.{builder.TypeName}");
        }
Пример #14
0
 internal Builder(Type baseType,
                  ReadOnlyDictionary <int, ReadOnlyCollection <HandlerInformation> > handlers,
                  SortedSet <string> namespaces, RockOptions options, NameGenerator generator,
                  TInformationBuilder informationBuilder, TypeNameGenerator typeNameGenerator,
                  bool isMake)
 {
     this.BaseType           = baseType;
     this.IsUnsafe           = this.BaseType.IsUnsafeToMock();
     this.Handlers           = handlers;
     this.Namespaces         = namespaces;
     this.Options            = options;
     this.NameGenerator      = generator;
     this.InformationBuilder = informationBuilder;
     this.TypeName           = typeNameGenerator.Generate(baseType);
     this.IsMake             = isMake;
     this.Tree = this.MakeTree();
 }
Пример #15
0
        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);
        }
Пример #16
0
        public static IRock <T> Create <T>(RockOptions options)
            where T : class
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var tType   = typeof(T);
            var message = tType.Validate(options.Serialization,
                                         tType.IsSealed ? new PersistenceNameGenerator(tType) as NameGenerator :
                                         new InMemoryNameGenerator() as NameGenerator);

            if (!string.IsNullOrWhiteSpace(message))
            {
                throw new ValidationException(message);
            }

            return(Rock.NewRock <T>(options, false));
        }
Пример #17
0
 internal InMemoryRock(RockOptions options, bool isMake) =>
 (this.options, this.isMake) = (options, isMake);
Пример #18
0
 public RockRepository(RockOptions options) =>
 this.Options = options ?? throw new ArgumentNullException(nameof(options));
Пример #19
0
 /// <summary>
 /// Creates a <see cref="StashRocks"/> instance.
 /// </summary>
 /// <param name="globalOptions">The global options of the created mocks.</param>
 /// <returns>The <see cref="StashRocks"/> instance.</returns>
 public static StashRocks Create(RockOptions globalOptions = null) =>
 new StashRocks(globalOptions);
Пример #20
0
 internal CacheKey(Type type, RockOptions options) =>
 (this.type, this.options) = (type, options);