From 62b264144d589c5a3bab2706cf9783d3be5a198d Mon Sep 17 00:00:00 2001 From: Hertzole Date: Sun, 28 Aug 2022 17:02:26 +0200 Subject: [PATCH] add: tests for toggleable values --- CHANGELOG.md | 20 ++- .../Runtime/Data/ToggleableFloat.cs | 2 +- .../Runtime/Data/ToggleableInt.cs | 2 +- .../Tests/Runtime/ToggleableValuesTest.cs | 169 ++++++++++++++++++ .../Runtime/ToggleableValuesTest.cs.meta | 3 + 5 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 Packages/se.hertzole.runtime-options-manager/Tests/Runtime/ToggleableValuesTest.cs create mode 100644 Packages/se.hertzole.runtime-options-manager/Tests/Runtime/ToggleableValuesTest.cs.meta diff --git a/CHANGELOG.md b/CHANGELOG.md index 886bfe1..bc554a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,14 +1,32 @@ -## [0.3.0] - Unreleased +## [0.3.0] - 2022-08-28 ### Added +- Added ButtonSetting for creating buttons in your settings +- Added option to decide how language names are displayed - Added `ISettingWriter` to overwrite how files are written +- Added language sample +- Added full support for refresh rate setting +- Added methods to add and remove categories and settings inside categories ### Changed +- All saveable settings now inherit from Setting and Setting inherits from BaseSetting - **[BREAKING]** Int/Float/AudioSetting now uses `ToggleableInt`/`ToggleableFloat` instead for min/max values +### Removed +- Removed settings creating UI toolkit elements from scratch + ### Fixed - Fixed settings being marked as dirty on boot/load and thus saving on boot/load - Fixed resolution dropdown value returning the wrong value +- Fixed fullscreen mode dropdown value returning the wrong value - Fixed input setting not loading keybinds correctly in some cases +- Fixed the game starting in the wrong resolution +- Fixed the game not booting in some situations +- Fixed language settings sometimes returning null +- Fixed v-sync setting not applying v-sync on load +- Fixed audio setting not loading properly in some cases +- Fixed settings going back to default values in the editor +- Fixed null reference exception when opening settings manager in the inspector +- Fixed tests being included in package ## [0.2.0] - 2022-06-18 ### Added diff --git a/Packages/se.hertzole.runtime-options-manager/Runtime/Data/ToggleableFloat.cs b/Packages/se.hertzole.runtime-options-manager/Runtime/Data/ToggleableFloat.cs index 7b38668..621c05d 100644 --- a/Packages/se.hertzole.runtime-options-manager/Runtime/Data/ToggleableFloat.cs +++ b/Packages/se.hertzole.runtime-options-manager/Runtime/Data/ToggleableFloat.cs @@ -22,7 +22,7 @@ public ToggleableFloat(bool enabled, float value) public bool Equals(ToggleableFloat other) { - return enabled == other.enabled && value.Equals(other.value); + return enabled == other.enabled && Math.Abs(value - other.value) < 0.0001f; } public override bool Equals(object obj) diff --git a/Packages/se.hertzole.runtime-options-manager/Runtime/Data/ToggleableInt.cs b/Packages/se.hertzole.runtime-options-manager/Runtime/Data/ToggleableInt.cs index 52a9aec..0583044 100644 --- a/Packages/se.hertzole.runtime-options-manager/Runtime/Data/ToggleableInt.cs +++ b/Packages/se.hertzole.runtime-options-manager/Runtime/Data/ToggleableInt.cs @@ -22,7 +22,7 @@ public ToggleableInt(bool enabled, int value) public bool Equals(ToggleableInt other) { - return enabled == other.enabled && value.Equals(other.value); + return enabled == other.enabled && value == other.value; } public override bool Equals(object obj) diff --git a/Packages/se.hertzole.runtime-options-manager/Tests/Runtime/ToggleableValuesTest.cs b/Packages/se.hertzole.runtime-options-manager/Tests/Runtime/ToggleableValuesTest.cs new file mode 100644 index 0000000..4ea692e --- /dev/null +++ b/Packages/se.hertzole.runtime-options-manager/Tests/Runtime/ToggleableValuesTest.cs @@ -0,0 +1,169 @@ +using NUnit.Framework; +using UnityEngine; +using Assert = UnityEngine.Assertions.Assert; + +namespace Hertzole.OptionsManager.Tests +{ + public class ToggleableValuesTest : BaseTest + { + [Test] + public void ToggleableFloat_Equals() + { + ToggleableFloat a = new ToggleableFloat(true, 1f); + ToggleableFloat b = new ToggleableFloat(true, 1f); + + Assert.IsTrue(a.Equals(b)); + Assert.IsTrue(a == b); + + a = new ToggleableFloat(false, 1f); + + Assert.IsFalse(a.Equals(b)); + Assert.IsTrue(a != b); + + b = new ToggleableFloat(false, 1f); + + Assert.IsTrue(a.Equals(b)); + Assert.IsTrue(a == b); + + a = new ToggleableFloat(true, 2f); + + Assert.IsFalse(a.Equals(b)); + Assert.IsTrue(a != b); + + b = new ToggleableFloat(true, 2f); + + Assert.IsTrue(a.Equals(b)); + Assert.IsTrue(a == b); + } + + [Test] + public void ToggleableInt_Equals() + { + ToggleableInt a = new ToggleableInt(true, 1); + ToggleableInt b = new ToggleableInt(true, 1); + + Assert.IsTrue(a.Equals(b)); + Assert.IsTrue(a == b); + + a = new ToggleableInt(false, 1); + + Assert.IsFalse(a.Equals(b)); + Assert.IsTrue(a != b); + + b = new ToggleableInt(false, 1); + + Assert.IsTrue(a.Equals(b)); + Assert.IsTrue(a == b); + + a = new ToggleableInt(true, 2); + + Assert.IsFalse(a.Equals(b)); + Assert.IsTrue(a != b); + + b = new ToggleableInt(true, 2); + + Assert.IsTrue(a.Equals(b)); + Assert.IsTrue(a == b); + } + + [Test] + public void ToggleableFloat_EqualsObject() + { + ToggleableFloat a = new ToggleableFloat(true, 1); + ToggleableFloat b = new ToggleableFloat(true, 1); + + Assert.IsTrue(a.Equals((object) b)); + + Assert.IsFalse(a.Equals(new Vector3(0, 0, 0))); + } + + [Test] + public void ToggleableInt_EqualsObject() + { + ToggleableInt a = new ToggleableInt(true, 1); + ToggleableInt b = new ToggleableInt(true, 1); + + Assert.IsTrue(a.Equals((object) b)); + + Assert.IsFalse(a.Equals(new Vector3(0, 0, 0))); + } + + [Test] + public void ToggleableFloat_GetHashCode() + { + ToggleableFloat a = new ToggleableFloat(true, 1f); + ToggleableFloat b = new ToggleableFloat(true, 1f); + + Assert.AreEqual(a.GetHashCode(), b.GetHashCode()); + + a = new ToggleableFloat(false, 1f); + + Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode()); + + b = new ToggleableFloat(false, 1f); + + Assert.AreEqual(a.GetHashCode(), b.GetHashCode()); + + a = new ToggleableFloat(true, 2f); + + Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode()); + + b = new ToggleableFloat(true, 2f); + + Assert.AreEqual(a.GetHashCode(), b.GetHashCode()); + } + + [Test] + public void ToggleableInt_GetHashCode() + { + ToggleableInt a = new ToggleableInt(true, 1); + ToggleableInt b = new ToggleableInt(true, 1); + + Assert.AreEqual(a.GetHashCode(), b.GetHashCode()); + + a = new ToggleableInt(false, 1); + + Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode()); + + b = new ToggleableInt(false, 1); + + Assert.AreEqual(a.GetHashCode(), b.GetHashCode()); + + a = new ToggleableInt(true, 2); + + Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode()); + + b = new ToggleableInt(true, 2); + + Assert.AreEqual(a.GetHashCode(), b.GetHashCode()); + } + + [Test] + public void ToggleableFloat_ToFloatOperator() + { + ToggleableFloat a = 42f; + Assert.AreEqual(a.Value, 42f); + } + + [Test] + public void ToggleableFloat_ToBoolOperator([ValueSource(nameof(boolValues))] bool enabled) + { + ToggleableFloat a = enabled; + Assert.AreEqual(enabled, a); + } + + [Test] + public void ToggleableInt_ToIntOperator() + { + ToggleableInt a = 42; + Assert.AreEqual(a, 42); + } + + [Test] + public void ToggleableInt_ToBoolOperator([ValueSource(nameof(boolValues))] bool enabled) + { + ToggleableInt a = enabled; + Assert.AreEqual(enabled, a); + } + } +} \ No newline at end of file diff --git a/Packages/se.hertzole.runtime-options-manager/Tests/Runtime/ToggleableValuesTest.cs.meta b/Packages/se.hertzole.runtime-options-manager/Tests/Runtime/ToggleableValuesTest.cs.meta new file mode 100644 index 0000000..707c517 --- /dev/null +++ b/Packages/se.hertzole.runtime-options-manager/Tests/Runtime/ToggleableValuesTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6ca86cad041c4d6d9457199ed627a47c +timeCreated: 1661697922 \ No newline at end of file