Ejemplo n.º 1
0
 public static Task RunTestAsync(
     this IHostBuilder source,
     TestBotData data,
     Func <TestDiscordClient, Task> func)
 {
     return(RunTestAsync(source, data, null, func));
 }
Ejemplo n.º 2
0
        public TestDiscordClient(TestBotData data, IDiscordEventHandler eventHandler, ITestOutputHelper outputHelper = null)
        {
            _data         = data;
            EventHandler  = eventHandler;
            _outputHelper = outputHelper;

            InitCollection(data.Guilds);
            InitCollection(data.Users);
        }
Ejemplo n.º 3
0
        public static async Task RunTestAsync(
            this IHostBuilder source,
            TestBotData data,
            ITestOutputHelper output,
            Func <TestDiscordClient, Task> func)
        {
            if (!source.IsDiscordBotConfigures())
            {
                source.ConfigureDiscordBot(false);
            }

            source.ConfigureServices(services =>
            {
                if (output != null)
                {
                    services.AddSingleton(output);
                }

                services.Configure <DebugOptions>(options =>
                {
                    options.ThrowOnMessageException = true;
                });

                services.AddSingleton(data);
                services.AddSingleton <IDiscordClient, TestDiscordClient>();
                services.AddSingleton <IBotApplication, VoidBotApplication>();
            });

            var host = source.Build();

            await host.StartAsync();

            try
            {
                var client = (TestDiscordClient)host.Services.GetRequiredService <IDiscordClient>();
                await func(client);
            }
            finally
            {
                await host.StopAsync();
            }
        }
        public static ServiceProvider BuildTestServiceProvider(this IServiceCollection services, TestBotData data = null)
        {
            if (!services.IsRegistered <IDiscordClient>())
            {
                services.AddSingleton <IDiscordClient, TestDiscordClient>();
            }

            if (!services.IsRegistered <TestBotData>())
            {
                services.AddSingleton(data ?? new TestBotData());
            }

            HostExtensions.AddDefaultServices(services);

            if (!services.IsRegistered <IBotApplicationBuilderFactory>())
            {
                services.AddApplicationBuilder(builder =>
                {
                    // Do nothing.
                });
            }

            services.PostConfigure <DiscordOptions>(options =>
            {
                options.Token ??= "INVALID-TOKEN";
            });

            var provider = services.BuildServiceProvider();

            provider.GetRequiredService <IEventManager>().CallAsync(new InitializeEvent()).AsTask().Wait();

            return(provider);
        }
 public static IServiceCollection AddTestClient(this IServiceCollection collection, TestBotData data = null)
 {
     collection.AddSingleton <IDiscordClient, TestDiscordClient>();
     collection.AddSingleton(data ?? new TestBotData());
     return(collection);
 }