diff --git a/OpenNest.Core/Plate.cs b/OpenNest.Core/Plate.cs index 2c96dbbf..31f92285 100644 --- a/OpenNest.Core/Plate.cs +++ b/OpenNest.Core/Plate.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using OpenNest.Collections; using OpenNest.Geometry; @@ -141,7 +142,7 @@ namespace OpenNest /// public ClampRail ClampRail { get; private set; } - private static readonly IReadOnlySet NoRemovedClamps = new HashSet(); + private static readonly IReadOnlySet NoRemovedClamps = ImmutableHashSet.Empty; /// /// Rail clamp ids taken off this sheet as a last resort (D8) so a part that could not @@ -183,7 +184,7 @@ namespace OpenNest ClampRail = rail; RemovedClamps = rail == null || removedClamps == null ? NoRemovedClamps - : new HashSet(removedClamps, StringComparer.Ordinal); + : removedClamps.ToImmutableHashSet(StringComparer.Ordinal); ReplaceClampZones(ResolveRailZones()); } diff --git a/OpenNest.Tests/ClampRailTests.cs b/OpenNest.Tests/ClampRailTests.cs index bf04204b..c7f98734 100644 --- a/OpenNest.Tests/ClampRailTests.cs +++ b/OpenNest.Tests/ClampRailTests.cs @@ -382,4 +382,17 @@ public class ClampRailTests Assert.Equal(ClampRail.DefaultRemovalMinutes, plate.ClampRemovalMinutes); Assert.Contains("clamp-8", plate.RemovedClamps); // still listed (C13) } + + // PM 6ded8ac4 (e): the removed set is read-only, even through a downcast, and an + // untouched plate does not share a mutable empty set with every other plate. + [Fact] + public void RemovedClampsCannotBeMutatedThroughTheCollection() + { + var removed = Sheet(48, 120); + removed.SetClampRail(Machine(), new[] { "clamp-3" }); + var untouched = Sheet(48, 120); + + Assert.True(((ICollection)removed.RemovedClamps).IsReadOnly); + Assert.True(((ICollection)untouched.RemovedClamps).IsReadOnly); + } }