using System.IO; using System.Reflection; using ChineseChess.GUI.Contracts.Services; using ChineseChess.GUI.Core.Contracts.Services; using ChineseChess.GUI.Core.Services; using ChineseChess.GUI.Models; using ChineseChess.GUI.Services; using ChineseChess.GUI.ViewModels; using ChineseChess.GUI.Views; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Xunit; namespace ChineseChess.GUI.Tests.XUnit { public class PagesTests { private readonly IHost _host; public PagesTests() { var appLocation = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location); _host = Host.CreateDefaultBuilder() .ConfigureAppConfiguration(c => c.SetBasePath(appLocation)) .ConfigureServices(ConfigureServices) .Build(); } private void ConfigureServices(HostBuilderContext context, IServiceCollection services) { // Core Services services.AddSingleton(); // Services services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); // ViewModels services.AddTransient(); services.AddTransient(); // Configuration services.Configure(context.Configuration.GetSection(nameof(AppConfig))); } // TODO WTS: Add tests for functionality you add to SettingsViewModel. [Fact] public void TestSettingsViewModelCreation() { var vm = _host.Services.GetService(typeof(SettingsViewModel)); Assert.NotNull(vm); } [Fact] public void TestGetSettingsPageType() { if (_host.Services.GetService(typeof(IPageService)) is IPageService pageService) { var pageType = pageService.GetPageType(typeof(SettingsViewModel).FullName); Assert.Equal(typeof(SettingsPage), pageType); } else { Assert.True(false, $"Can't resolve {nameof(IPageService)}"); } } // TODO WTS: Add tests for functionality you add to MainViewModel. [Fact] public void TestMainViewModelCreation() { var vm = _host.Services.GetService(typeof(MainViewModel)); Assert.NotNull(vm); } [Fact] public void TestGetMainPageType() { if (_host.Services.GetService(typeof(IPageService)) is IPageService pageService) { var pageType = pageService.GetPageType(typeof(MainViewModel).FullName); Assert.Equal(typeof(MainPage), pageType); } else { Assert.True(false, $"Can't resolve {nameof(IPageService)}"); } } } }