Skip to content

Commit

Permalink
Merge pull request #141 from oliverzick/140-enable-cloning
Browse files Browse the repository at this point in the history
Enable cloning an INI document
  • Loading branch information
oliverzick authored Feb 22, 2025
2 parents f4bfb70 + be06ed8 commit 3217919
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions src/Delizious.Ini.Test/IniDocumentSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,55 @@ public static IEnumerable<object[]> 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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Delizious.Ini/Delizious.Ini.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.10.0</Version>
<Version>1.11.0</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>Delizious.Ini</PackageId>
<Authors>OliverZick</Authors>
Expand Down
21 changes: 21 additions & 0 deletions src/Delizious.Ini/IniDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ public void SaveTo(TextWriter textWriter)
this.iniDocument.SaveTo(textWriter);
}

/// <summary>
/// Clones the INI document and provides a deep copy of this instance.
/// </summary>
/// <returns>
/// A new <see cref="IniDocument"/> instance that is a deep copy of this instance.
/// </returns>
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);
}
}

/// <summary>
/// Enumerates the names of all contained sections.
/// </summary>
Expand Down

0 comments on commit 3217919

Please sign in to comment.