public void UseTransaction_OnlyOneTransactionAllowed() { using (var scenario = SingleConfigFileScenario.Setup(nameof(UseTransaction_OnlyOneTransactionAllowed))) { // Arrange var cfg = scenario.CreateConfig(); cfg.StartTransaction(); // Act Action a = () => cfg.StartTransaction(); // Assert a.ShouldThrow <InvalidOperationException>().WithMessage("*transaction*"); } }
public void SaveSetting_WhenItDoesNotExistYetInConfigFile_GetsCreatedAndSaved() { using (var scenario = SingleConfigFileScenario.Setup(nameof(SaveSetting_WhenItDoesNotExistYetInConfigFile_GetsCreatedAndSaved))) { // Arrange var cfg = Config.CreateAs() .MappedToType(() => new SingleConfigFileScenario.ConfigContent()) .StoredAs(store => store.File(scenario.File)) .Initialize(); // Act cfg.Set(c => c.Sub.Z, 1); // Assert File.ReadAllText(scenario.File).Should().Be("X = 1\r\nY = \"Y\"\r\n\r\n[Sub]\r\nZ = 1\r\n"); } }
public void SaveSetting_WhenItDoesNotExistYetInConfigFileAndTargetExplicitelySpecified_GetsCreatedAndSaved() { using (var scenario = SingleConfigFileScenario.Setup(nameof(SaveSetting_WhenItDoesNotExistYetInConfigFileAndTargetExplicitelySpecified_GetsCreatedAndSaved))) { // Arrange IConfigSource src = null; var cfg = Config.CreateAs() .MappedToType(() => new SingleConfigFileScenario.ConfigContent()) .StoredAs(store => store.File(scenario.File).AccessedBySource("main", out src)) .Initialize(); // Act cfg.Set(c => c.Sub.Z, 1, src); // Assert File.ReadAllText(scenario.File).Should().Be("X = 1\r\nY = \"Y\"\r\n\r\n[Sub]\r\nZ = 1\r\n"); } }
public void UseTransaction_ChangesNotSavedUntilTransactionIsDisposed() { using (var scenario = SingleConfigFileScenario.Setup(nameof(UseTransaction_ChangesNotSavedUntilTransactionIsDisposed))) { // Arrange const int ChangedX = 234; var cfg = scenario.CreateConfig(); int original = cfg.Get(c => c.X); cfg.StartTransaction(); // Act cfg.Set(c => c.X, 2); cfg.Set(c => c.X, 23); cfg.Set(c => c.X, ChangedX); // Assert scenario.ReadFile().X.Should().Be(original); } }
public void UseTransaction_AfterTransactionIsClosed_SaveWillSaveDirectlyAgain() { using (var scenario = SingleConfigFileScenario.Setup(nameof(UseTransaction_AfterTransactionIsClosed_SaveWillSaveDirectlyAgain))) { // Arrange const int ChangedX = 234; var cfg = scenario.CreateConfig(); using (cfg.StartTransaction()) { cfg.Set(c => c.X, 2); cfg.Set(c => c.X, 23); } // Act cfg.Set(c => c.X, ChangedX); // Assert scenario.ReadFile().X.Should().Be(ChangedX); } }
public void UseTransaction_WillApplySettings() { using (var scenario = SingleConfigFileScenario.Setup(nameof(UseTransaction_WillApplySettings))) { // Arrange const int ChangedX = 234; var cfg = scenario.CreateConfig(); // Act using (cfg.StartTransaction()) { cfg.Set(c => c.X, 2); cfg.Set(c => c.X, 23); cfg.Set(c => c.X, ChangedX); } // Assert scenario.ReadFile().X.Should().Be(ChangedX); } }
public void UseTransaction_ExternalChangesAreLoaded() { using (var scenario = SingleConfigFileScenario.Setup(nameof(UseTransaction_ExternalChangesAreLoaded))) { // Arrange var cfg = scenario.CreateConfig(); cfg.StartTransaction(); int originalValue = cfg.Get(c => c.X); const int newValue = 123; Toml.WriteFile(new SingleConfigFileScenario.ConfigContent() { X = newValue }, scenario.File); // Act var v = cfg.Get(c => c.X); // Assert v.Should().Be(originalValue); } }
public void UseTransaction_ExternalChangesNotIncorporatedAndSavedBack() { using (var scenario = SingleConfigFileScenario.Setup(nameof(UseTransaction_ExternalChangesAreLoaded))) { // Arrange var cfg = scenario.CreateConfig(); var trans = cfg.StartTransaction(); const int newXValue = 123; const string newYValue = "New Y"; Toml.WriteFile(new SingleConfigFileScenario.ConfigContent() { X = newXValue }, scenario.File); // Act cfg.Set(c => c.Y, newYValue); Action a = () => trans.Dispose(); // Assert a.ShouldThrow <InvalidOperationException>().WithMessage("*externally*"); } }