Skip to content

Commit

Permalink
add: tests for toggleable values
Browse files Browse the repository at this point in the history
  • Loading branch information
Hertzole committed Aug 28, 2022
1 parent 5243a6f commit 62b2641
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 3 deletions.
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 62b2641

Please sign in to comment.