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) { }
public static void Create() { var options = new RockOptions(); using var repository = new RockRepository(options); Assert.That(repository.Options, Is.SameAs(options)); }
public RockAssembly(Assembly assembly, RockOptions options) : base() { this.assembly = assembly; this.options = options; this.Result = this.Generate(); }
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); }
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); } }
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)); }
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) { }
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>); }
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)); }
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()); }
/// <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); }
/// <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); }
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}"); }
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(); }
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); }
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)); }
internal InMemoryRock(RockOptions options, bool isMake) => (this.options, this.isMake) = (options, isMake);
public RockRepository(RockOptions options) => this.Options = options ?? throw new ArgumentNullException(nameof(options));
/// <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);
internal CacheKey(Type type, RockOptions options) => (this.type, this.options) = (type, options);