Exemplo n.º 1
0
        private void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton <IFileIOService, FileIOService>();
            services.AddSingleton <IStoragePathService, WindowsStoragePathService>();
            services.AddSingleton <IPrinterConfigurationManagerService, PrinterConfigurationManagerService>(sp =>
            {
                var service = new PrinterConfigurationManagerService(
                    sp.GetService <IStoragePathService>(),
                    sp.GetService <IFileIOService>());
                service.Load();
                return(service);
            });
            services.AddTransient <PrinterConfigurationView>();
            services.AddTransient <PrintersConfigurationView>();
            services.AddScoped <IPrinterControllerService, MarlinPrinterControllerService>();
            services.AddScoped <IPrinterPacketParser, MarlinPrinterPacketParser>(x =>
            {
                var options = new MarlinPrinterPacketParserOptions()
                {
                };

                return(new MarlinPrinterPacketParser(options));
            });
            services.AddSingleton <ISerialPortAdapter, SerialPortAdapter <WrappedSerialPort> >();
            services.AddTransient <ICommandValidatorService, CommandValidatorService>();
            services.AddTransient <ICommandsDefinitionLoaderService, CommandsDefinitionLoaderService>(x =>
            {
                var service = new CommandsDefinitionLoaderService();
                service.Load("Config\\MarlinCommands.json");
                return(service);
            });
        }
        public void Given2Printers_WhenClear_ThenAllPrintersRemovedFromConfig()
        {
            // Arrange
            var mockFileIOService = new Mock <IFileIOService>();

            mockFileIOService.Setup(x => x.Exists(
                                        It.IsAny <string>()))
            .Returns(true);

            mockFileIOService.Setup(x => x.ReadAllText(
                                        It.IsAny <string>()))
            .Returns("{\"Printers\":[{},{}]}");

            var sut = new PrinterConfigurationManagerService(
                new WindowsStoragePathService(),
                mockFileIOService.Object);

            sut.Load();

            // Act
            sut.Clear();

            // Assert
            Assert.Empty(sut.Config.Printers);
        }
        public void Given1Printer_WhenSave_ThenAllPrintersRemovedFromConfig()
        {
            // Arrange
            var windowsStoragePathService = new WindowsStoragePathService();
            var mockFileIOService         = new Mock <IFileIOService>();

            mockFileIOService.Setup(x => x.Exists(
                                        It.IsAny <string>()))
            .Returns(true);

            mockFileIOService.Setup(x => x.ReadAllText(
                                        It.IsAny <string>()))
            .Returns("{\"Printers\":[{}]}");

            var sut = new PrinterConfigurationManagerService(
                windowsStoragePathService,
                mockFileIOService.Object);

            sut.Load();

            // Act
            sut.Save();

            // Assert
            var configJson = JsonConvert.SerializeObject(sut.Config);

            mockFileIOService.Verify(x => x.WriteAllText(
                                         It.Is <string>(x => x == windowsStoragePathService.UserAppConfigPrinterConfigurationsFilePath),
                                         It.Is <string>(x => x == configJson)), Times.Once);
        }
        public void GivenConfigNotExists_WhenLoad_ThenNoPrintersAreLoaded()
        {
            // Arrange
            var windowsStoragePathService = new WindowsStoragePathService();
            var mockFileIOService         = new Mock <IFileIOService>();

            mockFileIOService.Setup(x => x.Exists(
                                        It.IsAny <string>()))
            .Returns(false);

            var sut = new PrinterConfigurationManagerService(
                windowsStoragePathService,
                mockFileIOService.Object);

            sut.Load();

            // Act
            var result = sut.Load();

            // Assert
            Assert.False(result);
        }
        public void GivenPrinterConfigurationModel_WhenAdd_ThenPrinterAddedToConfig()
        {
            // Arrange
            var mockFileIOService = new Mock <IFileIOService>();
            var config            = new PrinterConfigurationModel()
            {
                Name     = Guid.NewGuid().ToString(),
                Port     = "com6",
                BaudRate = 100,
                BedXSize = 101,
                BedYSize = 102,
                BedZSize = 103,
                PrintableAreaMarginBack  = 104,
                PrintableAreaMarginFront = 105,
                PrintableAreaMarginLeft  = 106,
                PrintableAreaMarginRight = 107
            };

            mockFileIOService.Setup(x => x.Exists(
                                        It.IsAny <string>()))
            .Returns(true);

            mockFileIOService.Setup(x => x.ReadAllText(
                                        It.IsAny <string>()))
            .Returns("{}");

            var sut = new PrinterConfigurationManagerService(
                new WindowsStoragePathService(),
                mockFileIOService.Object);

            sut.Load();

            // Act
            sut.Add(config);

            // Assert
            Assert.Single(sut.Config.Printers);
            Assert.Equal(config, sut.Config.Printers.First());
        }