Skip to content

Commit

Permalink
#192 以下の二つのメソッドを追加した。
Browse files Browse the repository at this point in the history
public IList<VirtualNode> AddRange(IEnumerable<VirtualNode> nodes, bool allowOverwrite = false)
public static VirtualDirectory operator +(VirtualDirectory directory, IList<VirtualNode> nodes)
テスト済み。
  • Loading branch information
shimodateakira committed Sep 18, 2024
1 parent 7639b63 commit 7d8c812
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 3 deletions.
47 changes: 47 additions & 0 deletions AkiraNetwork/VirtualStorageLibrary.Test/VirtualDirectoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,31 @@ public void Add_InvalidNodeNameWithRoot_ThrowsArgumentException()
Debug.WriteLine(ex.Message);
}

[TestMethod]
public void AddRange_AddNodes()
{
// Arrange
VirtualStorage<BinaryData> vs = new();
VirtualDirectory dir = vs.GetDirectory("/");

// Act
IList<VirtualNode> addedNodes = dir.AddRange(
[
new VirtualItem<BinaryData>("item1"),
new VirtualItem<BinaryData>("item2"),
new VirtualItem<BinaryData>("item3")
]);

// Assert
Assert.IsTrue(vs.ItemExists("/item1"));
Assert.IsTrue(vs.ItemExists("/item2"));
Assert.IsTrue(vs.ItemExists("/item3"));
Assert.AreEqual(3, addedNodes.Count);
Assert.AreEqual("item1", (string)addedNodes[0].Name);
Assert.AreEqual("item2", (string)addedNodes[1].Name);
Assert.AreEqual("item3", (string)addedNodes[2].Name);
}

[TestMethod]
public void AddItem_AddsNewItemSuccessfully()
{
Expand Down Expand Up @@ -1700,6 +1725,28 @@ public void Operator_Plus_AddsExistingDirectoryToNewDirectory_And_VerifiesIsRefe
Assert.IsFalse(addedTestLink.IsReferencedInStorage);
}

[TestMethod]
public void Operator_Plus_AddsNodesToDirectory()
{
// Arrange
VirtualStorage<BinaryData> vs = new();
VirtualDirectory dir = vs.GetDirectory("/");

// Act
dir +=
[
new VirtualItem<BinaryData>("item1"),
new VirtualItem<BinaryData>("item2"),
new VirtualItem<BinaryData>("item3")
];

// Assert
Assert.AreEqual(3, dir.Count);
Assert.IsTrue(dir.ItemExists("item1"));
Assert.IsTrue(dir.ItemExists("item2"));
Assert.IsTrue(dir.ItemExists("item3"));
}

[TestMethod]
public void AddDirectoryToAnotherDirectory_CreatesClone_And_VerifiesIsReferencedInStorage()
{
Expand Down
71 changes: 71 additions & 0 deletions AkiraNetwork/VirtualStorageLibrary.Test/VirtualStorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11059,6 +11059,77 @@ public void Indexer_Test14()
Debug.WriteLine($"/dir2/link1(ItemData) = {vs.Item["/dir2/link1"].ItemData}");
}

[TestMethod]
[TestCategory("Indexer")]
public void Indexer_Test15()
{
// Arrange
VirtualStorage<string> vs = new();
VirtualItem<string> item1 = new("item1", "pen");
VirtualItem<string> item2 = new("item2", "pineapple");
VirtualItem<string> item3 = new("item3", "apple");
vs["/"] += new VirtualDirectory("PPAP");

// Act
vs["/PPAP"] += [item1, item2, item3];

// Debug Out
Debug.WriteLine($"/PPAP/item1 = {vs.Item["/PPAP/item1"].ItemData}");
Debug.WriteLine($"/PPAP/item2 = {vs.Item["/PPAP/item2"].ItemData}");
Debug.WriteLine($"/PPAP/item3 = {vs.Item["/PPAP/item3"].ItemData}");

// Assert
Assert.AreEqual("pen", vs.Item["/PPAP/item1"].ItemData);
Assert.AreEqual("pineapple", vs.Item["/PPAP/item2"].ItemData);
Assert.AreEqual("apple", vs.Item["/PPAP/item3"].ItemData);
}

[TestMethod]
[TestCategory("Indexer")]
public void Indexer_Test16()
{
// Arrange
VirtualStorage<BinaryData> vs = new();
VirtualItem<BinaryData> item1 = new("item1", [1, 2, 3]);
VirtualItem<BinaryData> item2 = new("item2", [4, 5, 6]);
VirtualItem<BinaryData> item3 = new("item3", [7, 8, 9]);
vs["/"] += new VirtualDirectory("dir1");
vs["/"] += new VirtualDirectory("dir2");

// Act
vs["/dir1"] += [item1, item2, item3];

// Debug Out
Debug.WriteLine($"\n/dir1/item1 = {vs.Item["/dir1/item1"].ItemData}");
Debug.WriteLine($"/dir1/item2 = {vs.Item["/dir1/item2"].ItemData}");
Debug.WriteLine($"/dir1/item3 = {vs.Item["/dir1/item3"].ItemData}");

// Act
vs["/dir2"] += [vs.Item["/dir1/item1"], vs.Item["/dir1/item2"], vs.Item["/dir1/item3"]];

// Debug Out
Debug.WriteLine($"\n/dir2/item1 = {vs.Item["/dir2/item1"].ItemData}");
Debug.WriteLine($"/dir2/item2 = {vs.Item["/dir2/item2"].ItemData}");
Debug.WriteLine($"/dir2/item3 = {vs.Item["/dir2/item3"].ItemData}");

// Assert
Assert.AreEqual(new BinaryData([1, 2, 3]), vs.Item["/dir1/item1"].ItemData);
Assert.AreEqual(new BinaryData([4, 5, 6]), vs.Item["/dir1/item2"].ItemData);
Assert.AreEqual(new BinaryData([7, 8, 9]), vs.Item["/dir1/item3"].ItemData);

Assert.AreEqual(new BinaryData([1, 2, 3]), vs.Item["/dir2/item1"].ItemData);
Assert.AreEqual(new BinaryData([4, 5, 6]), vs.Item["/dir2/item2"].ItemData);
Assert.AreEqual(new BinaryData([7, 8, 9]), vs.Item["/dir2/item3"].ItemData);

Assert.AreNotEqual(vs.Item["/dir2/item1"], vs.Item["/dir1/item1"]);
Assert.AreNotEqual(vs.Item["/dir2/item2"], vs.Item["/dir1/item2"]);
Assert.AreNotEqual(vs.Item["/dir2/item3"], vs.Item["/dir1/item3"]);

Assert.AreNotSame(vs.Item["/dir2/item1"].ItemData, vs.Item["/dir1/item1"].ItemData);
Assert.AreNotSame(vs.Item["/dir2/item2"].ItemData, vs.Item["/dir1/item2"].ItemData);
Assert.AreNotSame(vs.Item["/dir2/item3"].ItemData, vs.Item["/dir1/item3"].ItemData);
}

[TestMethod]
[TestCategory("SetNode")]
public void SetNode_Directory()
Expand Down
45 changes: 45 additions & 0 deletions AkiraNetwork/VirtualStorageLibrary/VirtualDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,28 @@ public VirtualNode Add(VirtualNode node, bool allowOverwrite = false)
return node;
}

/// <summary>
/// Adds multiple nodes to the directory.
/// </summary>
/// <param name="nodes">The nodes to add.</param>
/// <param name="allowOverwrite">If true, allows overwriting existing nodes with the same names.</param>
/// <returns>A list of the added nodes. If a node was cloned, the cloned instance is returned in the list.</returns>
/// <exception cref="ArgumentException">Thrown if an invalid node name is specified in any of the nodes.</exception>
/// <exception cref="InvalidOperationException">Thrown if a node with the same name already exists and overwriting is not allowed.</exception>
public IList<VirtualNode> AddRange(IEnumerable<VirtualNode> nodes, bool allowOverwrite = false)
{
var addedNodes = new List<VirtualNode>();

foreach (var node in nodes)
{
// Add the node and collect the returned node (original or cloned)
var addedNode = Add(node, allowOverwrite);
addedNodes.Add(addedNode);
}

return addedNodes;
}

/// <summary>
/// Adds an item with the specified name.
/// </summary>
Expand Down Expand Up @@ -572,6 +594,29 @@ public VirtualSymbolicLink GetSymbolicLink(VirtualNodeName name)
return directory;
}

/// <summary>
/// Overloads the addition operator to add multiple nodes to the directory using AddRange.
/// </summary>
/// <param name="directory">The directory to which the nodes are added.</param>
/// <param name="nodes">The nodes to add.</param>
/// <returns>The directory with the added nodes.</returns>
public static VirtualDirectory operator +(VirtualDirectory directory, IList<VirtualNode> nodes)
{
// Use AddRange to add multiple nodes
var addedNodes = directory.AddRange(nodes, allowOverwrite: false);

// If the directory is referenced in storage, propagate IsReferencedInStorage to each added node
if (directory.IsReferencedInStorage)
{
foreach (var node in addedNodes)
{
SetIsReferencedInStorageRecursively(node, true);
}
}

return directory;
}

/// <summary>
/// Overloads the addition operator to add a node to the directory.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<RootNamespace>AkiraNetwork.VirtualStorageLibrary</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Title>Virtual Storage Library</Title>
<Version>0.9.1.0</Version>
<Version>0.9.2.0</Version>
<Authors>Akira Shimodate</Authors>
<Company>AkiraNetwork</Company>
<Description>VirtualStorageLibrary is a fully in-memory .NET library that provides a tree-structured collection.
Expand Down Expand Up @@ -63,8 +63,8 @@ https://shimodateakira.github.io/VirtualStorageLibrary/index.html
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
<IncludeSymbols>True</IncludeSymbols>
<PackageOutputPath>../../packages</PackageOutputPath>
<AssemblyVersion>0.9.1.0</AssemblyVersion>
<FileVersion>0.9.1.0</FileVersion>
<AssemblyVersion>0.9.2.0</AssemblyVersion>
<FileVersion>0.9.2.0</FileVersion>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\..\Signature\VirtualStorageLibrary.snk</AssemblyOriginatorKeyFile>
<PackageId>AkiraNetwork.VirtualStorageLibrary</PackageId>
Expand Down

0 comments on commit 7d8c812

Please sign in to comment.