diff --git a/SabreTools.DatItems.Test/MachineTests.cs b/SabreTools.DatItems.Test/MachineTests.cs new file mode 100644 index 00000000..e49b5f31 --- /dev/null +++ b/SabreTools.DatItems.Test/MachineTests.cs @@ -0,0 +1,87 @@ +using Xunit; + +namespace SabreTools.DatItems.Test +{ + public class MachineTests + { + #region Clone + + [Fact] + public void CloneTest() + { + Machine item = new Machine(); + item.SetFieldValue(Models.Metadata.Machine.NameKey, "name"); + + object clone = item.Clone(); + Machine? actual = clone as Machine; + Assert.NotNull(actual); + Assert.Equal("name", actual.GetStringFieldValue(Models.Metadata.Machine.NameKey)); + } + + #endregion + + #region GetInternalClone + + [Fact] + public void GetInternalCloneTest() + { + Machine item = new Machine(); + item.SetFieldValue(Models.Metadata.Machine.NameKey, "name"); + + Models.Metadata.Machine actual = item.GetInternalClone(); + Assert.Equal("name", actual[Models.Metadata.Machine.NameKey]); + } + + #endregion + + #region Equals + + [Fact] + public void Equals_Null_False() + { + Machine self = new Machine(); + Machine? other = null; + + bool actual = self.Equals(other); + Assert.False(actual); + } + + [Fact] + public void Equals_DefaultInternal_True() + { + Machine self = new Machine(); + Machine? other = new Machine(); + + bool actual = self.Equals(other); + Assert.True(actual); + } + + [Fact] + public void Equals_MismatchedInternal_False() + { + Machine self = new Machine(); + self.SetFieldValue(Models.Metadata.Machine.NameKey, "self"); + + Machine? other = new Machine(); + other.SetFieldValue(Models.Metadata.Machine.NameKey, "other"); + + bool actual = self.Equals(other); + Assert.False(actual); + } + + [Fact] + public void Equals_EqualInternal_True() + { + Machine self = new Machine(); + self.SetFieldValue(Models.Metadata.Machine.NameKey, "name"); + + Machine? other = new Machine(); + other.SetFieldValue(Models.Metadata.Machine.NameKey, "name"); + + bool actual = self.Equals(other); + Assert.True(actual); + } + + #endregion + } +} \ No newline at end of file diff --git a/SabreTools.DatItems/Machine.cs b/SabreTools.DatItems/Machine.cs index 1b4f8d74..0fbe9e25 100644 --- a/SabreTools.DatItems/Machine.cs +++ b/SabreTools.DatItems/Machine.cs @@ -11,7 +11,7 @@ namespace SabreTools.DatItems /// Represents the information specific to a set/game/machine /// [JsonObject("machine"), XmlRoot("machine")] - public sealed class Machine : ModelBackedItem, ICloneable + public sealed class Machine : ModelBackedItem, ICloneable, IEquatable { #region Constants @@ -57,15 +57,6 @@ namespace SabreTools.DatItems #endregion - #region Accessors - - /// - /// Get a clone of the current internal model - /// - public Models.Metadata.Machine GetInternalClone() => (_internal.Clone() as Models.Metadata.Machine)!; - - #endregion - #region Cloning methods /// @@ -76,10 +67,15 @@ namespace SabreTools.DatItems { return new Machine() { - _internal = this._internal.Clone() as Models.Metadata.Machine ?? [], + _internal = _internal.Clone() as Models.Metadata.Machine ?? [], }; } + /// + /// Get a clone of the current internal model + /// + public Models.Metadata.Machine GetInternalClone() => (_internal.Clone() as Models.Metadata.Machine)!; + #endregion #region Comparision Methods @@ -114,6 +110,17 @@ namespace SabreTools.DatItems return _internal.EqualTo(otherItem._internal); } + /// + public bool Equals(Machine? other) + { + // If other is null + if (other == null) + return false; + + // Compare internal models + return _internal.EqualTo(other._internal); + } + #endregion #region Manipulation