Beispiel #1
0
        public void ConfigManager_WhenFileDoesntExistYet_WillCreateItInitially()
        {
            string fileName = "autocreated".TestRunUniqueName(Toml.FileExtension);

            try
            {
                // Act
                const int ExpectedIntValue = 3;
                var       cfg = new SingleLevelConfig()
                {
                    IntValue = ExpectedIntValue
                };
                Config.CreateAs()
                .MappedToType(() => cfg)
                .StoredAs(store => store.File(fileName))
                .Initialize();

                // Assert
                File.Exists(fileName).Should().Be(true);
                var read = Toml.ReadFile <SingleLevelConfig>(fileName);
                read.IntValue.Should().Be(ExpectedIntValue);
            }
            finally
            {
                TryDeleteFile(fileName);
            }
        }
Beispiel #2
0
        public void WhenValueChangedInProcess_TheNewConfigGetsAutoSaved()
        {
            string filePath = nameof(this.WhenValueChangedInProcess_TheNewConfigGetsAutoSaved) + Guid.NewGuid() + Toml.FileExtension;

            try
            {
                // Arrange
                const int ExpectedNewValue = 1;
                var       f = new SingleLevelConfig();
                Toml.WriteFile(f, filePath);
                var beforeChangeValue = f.IntValue;
                var cfg = Config.CreateAs()
                          .MappedToType(() => new SingleLevelConfig())
                          .StoredAs(store => store.File(filePath))
                          .Initialize();

                // Act
                cfg.Set(c => c.IntValue, ExpectedNewValue);

                // Assert
                var onDisk = Toml.ReadFile <SingleLevelConfig>(filePath);
                beforeChangeValue.Should().NotBe(onDisk.IntValue, "otherwise the file had the same value as after the change and we would test nothing here");
                onDisk.IntValue.Should().Be(ExpectedNewValue);
            }
            finally
            {
                TryDeleteFile(filePath);
            }
        }
Beispiel #3
0
        public void WhenValueChangedInProcess_TheNewConfigGetsAutoSaved()
        {
            string filePath = nameof(this.WhenValueChangedInProcess_TheNewConfigGetsAutoSaved) + Guid.NewGuid() + Toml.FileExtension;

            try
            {
                // Arrange
                const int ExpectedNewValue = 1;
                var f = new SingleLevelConfig();
                Toml.WriteFile(f, filePath);
                var beforeChangeValue = f.IntValue;
                var cfg = Config.Create(() => new SingleLevelConfig(), ConfigSource.CreateFileSource(filePath));

                // Act
                cfg.Set(c => c.IntValue, ExpectedNewValue);

                // Assert
                var onDisk = Toml.ReadFile<SingleLevelConfig>(filePath);
                beforeChangeValue.Should().NotBe(onDisk.IntValue, "otherwise the file had the same value as after the change and we would test nothing here");
                onDisk.IntValue.Should().Be(ExpectedNewValue);
            }
            finally
            {
                TryDeleteFile(filePath);
            }
        }
Beispiel #4
0
        public void ConfigManager_WhenFileDoesntExistYet_WillCreateItInitially()
        {
            string fileName = "autocreated".TestRunUniqueName(Toml.FileExtension);

            try
            {
                // Act
                const int ExpectedIntValue = 3;
                var cfg = new SingleLevelConfig() { IntValue = ExpectedIntValue };
                Config.Create(() => cfg, fileName);

                // Assert
                File.Exists(fileName).Should().Be(true);
                var read = Toml.ReadFile<SingleLevelConfig>(fileName);
                read.IntValue.Should().Be(ExpectedIntValue);
            }
            finally
            {
                TryDeleteFile(fileName);
            }
        }