private static void LazyRegistrationsCanBeResolved(ITypeRegistrar registrar) { // Given var instance = new MockService(); var factoryCalled = false; registrar.RegisterLazy(typeof(IMockService), () => { factoryCalled = true; return(instance); }); var resolver = registrar.Build(); // When var actual = resolver.Resolve(typeof(IMockService)); // Then if (!factoryCalled) { throw new TestFailedException( "Expected the factory to be called, to resolve the lazy registration."); } if (!ReferenceEquals(actual, instance)) { throw new TestFailedException( "Expected the resolver to return exactly the result of the lazy-registered factory."); } }
public Task <int> Execute(IConfiguration configuration, IEnumerable <string> args) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } _registrar.RegisterInstance(typeof(IConfiguration), configuration); // Create the command model. var model = CommandModelBuilder.Build(configuration); _registrar.RegisterInstance(typeof(CommandModel), model); _registrar.RegisterDependencies(model); // Parse and map the model against the arguments. var parser = new CommandTreeParser(model); var parsedResult = parser.Parse(args); _registrar.RegisterInstance(typeof(CommandTreeParserResult), parsedResult); // Currently the root? if (parsedResult.Tree == null) { // Display help. ConsoleRenderer.Render( HelpWriter.Write(model), configuration.Settings.Console); return(Task.FromResult(0)); } // Get the command to execute. var leaf = parsedResult.Tree.GetLeafCommand(); if (leaf.Command.IsBranch || leaf.ShowHelp) { // Branches can't be executed. Show help. ConsoleRenderer.Render( HelpWriter.WriteCommand(model, leaf.Command), configuration.Settings.Console); return(Task.FromResult(leaf.ShowHelp ? 0 : 1)); } // Register the arguments with the container. _registrar.RegisterInstance(typeof(IRemainingArguments), parsedResult.Remaining); // Create the resolver and the context. var resolver = new TypeResolverAdapter(_registrar.Build()); var context = new CommandContext(parsedResult.Remaining, leaf.Command.Name, leaf.Command.Data); // Execute the command tree. return(Execute(leaf, parsedResult.Tree, context, resolver, configuration)); }
public int Execute(IConfiguration configuration, IEnumerable <string> args) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (configuration.Commands.Count == 0) { throw new CommandAppException("No commands have been configured."); } // Create the command model. var model = CommandModelBuilder.Build(configuration); // Parse and map the model against the arguments. var parser = new CommandTreeParser(model, new CommandOptionAttribute("-h|--help")); var(tree, remaining) = parser.Parse(args); // Currently the root? if (tree == null) { // Display help. HelpWriter.Write(model); return(0); } // Get the command to execute. var leaf = tree.GetLeafCommand(); if (leaf.Command.IsProxy || leaf.ShowHelp) { // Proxy's can't be executed. Show help. HelpWriter.Write(model, leaf.Command); return(0); } // Register the arguments with the container. var arguments = new Arguments(remaining); _registrar?.RegisterInstance(typeof(IArguments), arguments); // Create the resolver. var resolver = new TypeResolverAdapter(_registrar?.Build()); // Execute the command tree. return(Execute(leaf, tree, remaining, resolver)); }
private static void InstanceRegistrationsCanBeResolved(ITypeRegistrar registrar) { // Given var instance = new MockService(); registrar.RegisterInstance(typeof(IMockService), instance); var resolver = registrar.Build(); // When var actual = resolver.Resolve(typeof(IMockService)); // Then if (!ReferenceEquals(actual, instance)) { throw new TestFailedException( "Expected the resolver to resolve exactly the registered instance."); } }
private static void RegistrationsCanBeResolved(ITypeRegistrar registrar) { // Given registrar.Register(typeof(IMockService), typeof(MockService)); var resolver = registrar.Build(); // When var actual = resolver.Resolve(typeof(IMockService)); // Then if (actual == null) { throw new TestFailedException( $"Expected the resolver to resolve an instance of {nameof(MockService)}. Actually resolved null."); } if (actual is not MockService) { throw new TestFailedException( $"Expected the resolver to resolve an instance of {nameof(MockService)}. Actually resolved {actual.GetType().Name}."); } }
private static void ResolvingNotRegisteredServiceReturnsNull(ITypeRegistrar registrar) { // Given no registration var resolver = registrar.Build(); try { // When var actual = resolver.Resolve(typeof(IMockService)); // Then if (actual != null) { throw new TestFailedException( $"Expected the resolver to resolve null, since no service was registered. Actually resolved {actual.GetType().Name}."); } } catch (Exception ex) { throw new TestFailedException( $"Expected the resolver not to throw, but caught {ex.GetType().Name}.", ex); } }
public Task <int> Execute(IConfiguration configuration, IEnumerable <string> args) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } _registrar.RegisterInstance(typeof(IConfiguration), configuration); _registrar.RegisterInstance(typeof(IAnsiConsole), configuration.Settings.Console.GetConsole()); // Create the command model. var model = CommandModelBuilder.Build(configuration); _registrar.RegisterInstance(typeof(CommandModel), model); _registrar.RegisterDependencies(model); // No default command? if (model.DefaultCommand == null) { // Got at least one argument? var firstArgument = args.FirstOrDefault(); if (firstArgument != null) { // Asking for version? Kind of a hack, but it's alright. // We should probably make this a bit better in the future. if (firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase) || firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase)) { var console = configuration.Settings.Console.GetConsole(); console.WriteLine(VersionHelper.GetVersion(Assembly.GetEntryAssembly())); return(Task.FromResult(0)); } } } // Parse and map the model against the arguments. var parser = new CommandTreeParser(model, configuration.Settings); var parsedResult = parser.Parse(args); _registrar.RegisterInstance(typeof(CommandTreeParserResult), parsedResult); // Currently the root? if (parsedResult.Tree == null) { // Display help. configuration.Settings.Console.SafeRender(HelpWriter.Write(model)); return(Task.FromResult(0)); } // Get the command to execute. var leaf = parsedResult.Tree.GetLeafCommand(); if (leaf.Command.IsBranch || leaf.ShowHelp) { // Branches can't be executed. Show help. configuration.Settings.Console.SafeRender(HelpWriter.WriteCommand(model, leaf.Command)); return(Task.FromResult(leaf.ShowHelp ? 0 : 1)); } // Register the arguments with the container. _registrar.RegisterInstance(typeof(IRemainingArguments), parsedResult.Remaining); // Create the resolver and the context. var resolver = new TypeResolverAdapter(_registrar.Build()); var context = new CommandContext(parsedResult.Remaining, leaf.Command.Name, leaf.Command.Data); // Execute the command tree. return(Execute(leaf, parsedResult.Tree, context, resolver, configuration)); }