diff --git a/CHANGELOG.md b/CHANGELOG.md index e552f47..13a9f97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.11.0] - 2025-02-22 +### Added +- Enable cloning an INI document ([#140](https://github.com/oliverzick/Delizious-Ini/issues/140)) + ## [1.10.0] - 2025-02-17 ### Added - Enable configuration of the regular expression pattern for a section name in an INI document ([#138](https://github.com/oliverzick/Delizious-Ini/issues/138)) diff --git a/README.md b/README.md index 945ff96..11dfb25 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ ## What? Delizious Ini is an easy to use .NET Standard library entirely written in C# for reading and writing of INI data. +## New features in version 1.11.0 +* Cloning an INI document + ## Features Delizious Ini provides the following features: * Intuitive API design applying [Domain-driven design (DDD)](https://en.wikipedia.org/wiki/Domain-driven_design) diff --git a/src/Delizious.Ini.Test/IniDocumentSpec.cs b/src/Delizious.Ini.Test/IniDocumentSpec.cs index 917fee2..4bbc13a 100644 --- a/src/Delizious.Ini.Test/IniDocumentSpec.cs +++ b/src/Delizious.Ini.Test/IniDocumentSpec.cs @@ -421,6 +421,55 @@ public static IEnumerable Saves_the_ini_document_to_text_writer_test_c } } + [TestClass] + public sealed class Clone + { + private static IniDocumentConfiguration Configuration => IniDocumentConfiguration.Loose; + + [TestMethod] + public void Cloned_instance_is_not_the_same_as_the_original_instance() + { + var original = Make.SampleTarget(Configuration); + + var clone = original.Clone(); + + Assert.AreNotSame(original, clone); + } + + [TestMethod] + public void Cloned_instance_represents_same_content_as_the_original_instance() + { + var original = Make.SampleTarget(Configuration); + + var clone = original.Clone(); + + using var originalWriter = new StringWriter(); + original.SaveTo(originalWriter); + + using var cloneWriter = new StringWriter(); + clone.SaveTo(cloneWriter); + + Assert.AreEqual(originalWriter.ToString(), cloneWriter.ToString()); + } + + [TestMethod] + public void Changes_in_cloned_instance_do_not_affect_the_original_instance() + { + var original = Make.SampleTarget(Configuration); + + var clone = original.Clone(); + clone.WriteProperty("CloneSection", "CloneProperty", "Clone value"); + + using var originalWriter = new StringWriter(); + original.SaveTo(originalWriter); + + using var cloneWriter = new StringWriter(); + clone.SaveTo(cloneWriter); + + Assert.AreNotEqual(originalWriter.ToString(), cloneWriter.ToString()); + } + } + [TestClass] public sealed class EnumerateSections { diff --git a/src/Delizious.Ini/Delizious.Ini.csproj b/src/Delizious.Ini/Delizious.Ini.csproj index ef6464f..1323074 100644 --- a/src/Delizious.Ini/Delizious.Ini.csproj +++ b/src/Delizious.Ini/Delizious.Ini.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 1.10.0 + 1.11.0 True Delizious.Ini OliverZick diff --git a/src/Delizious.Ini/IniDocument.cs b/src/Delizious.Ini/IniDocument.cs index bcb145e..0aa0853 100644 --- a/src/Delizious.Ini/IniDocument.cs +++ b/src/Delizious.Ini/IniDocument.cs @@ -101,6 +101,27 @@ public void SaveTo(TextWriter textWriter) this.iniDocument.SaveTo(textWriter); } + /// + /// Clones the INI document and provides a deep copy of this instance. + /// + /// + /// A new instance that is a deep copy of this instance. + /// + public IniDocument Clone() + { + using (var stream = new MemoryStream()) + using (var writer = new StreamWriter(stream)) + using (var reader = new StreamReader(stream)) + { + this.SaveTo(writer); + writer.Flush(); + + stream.Seek(0, SeekOrigin.Begin); + + return LoadFrom(reader, this.configuration); + } + } + /// /// Enumerates the names of all contained sections. ///