diff --git a/src/Promote.NuGet.Commands/Core/DistinctQueue.cs b/src/Promote.NuGet.Commands/Core/DistinctQueue.cs index 0255c50..df909ea 100644 --- a/src/Promote.NuGet.Commands/Core/DistinctQueue.cs +++ b/src/Promote.NuGet.Commands/Core/DistinctQueue.cs @@ -15,12 +15,14 @@ public DistinctQueue() _enqueuedItems = new HashSet(); } - public DistinctQueue(IEnumerable items) + public DistinctQueue(IEnumerable items) : this() { if (items == null) throw new ArgumentNullException(nameof(items)); - _queue = new Queue(items); - _enqueuedItems = new HashSet(); + foreach (var item in items) + { + Enqueue(item); + } } public bool Enqueue(T item) diff --git a/tests/Promote.NuGet.Commands.Tests/Core/DistinctQueueTests.cs b/tests/Promote.NuGet.Commands.Tests/Core/DistinctQueueTests.cs index f23f3f8..9fdac35 100644 --- a/tests/Promote.NuGet.Commands.Tests/Core/DistinctQueueTests.cs +++ b/tests/Promote.NuGet.Commands.Tests/Core/DistinctQueueTests.cs @@ -95,4 +95,37 @@ public void Create_queue_with_items() queue.TryDequeue(out result).Should().BeFalse(); result.Should().BeNull(); } + + [Test] + public void Ctor_keeps_only_unique_initial_items() + { + var queue = new DistinctQueue(new[] { "str_1", "str_2", "str_1" }); + + queue.TryDequeue(out var result).Should().BeTrue(); + result.Should().Be("str_1"); + + queue.TryDequeue(out result).Should().BeTrue(); + result.Should().Be("str_2"); + + queue.TryDequeue(out result).Should().BeFalse(); + result.Should().BeNull(); + } + + [Test] + public void Cannot_enqueue_items_added_by_ctor() + { + var queue = new DistinctQueue(new[] { "str_1", "str_2" }); + + queue.TryDequeue(out var result).Should().BeTrue(); + result.Should().Be("str_1"); + + queue.Enqueue("str_1").Should().BeFalse(); + queue.Enqueue("str_2").Should().BeFalse(); + + queue.TryDequeue(out result).Should().BeTrue(); + result.Should().Be("str_2"); + + queue.TryDequeue(out result).Should().BeFalse(); + result.Should().BeNull(); + } } \ No newline at end of file