From 5d73756cb4da82102515647cae6481bc5bfdd8d5 Mon Sep 17 00:00:00 2001 From: Ivan Murashka Date: Sat, 22 Jun 2024 13:39:57 +0200 Subject: [PATCH 1/6] binary prefs class --- Runtime/BinaryPrefs.cs | 151 ++++++++++++++++++++++++++++++++++++ Runtime/BinaryPrefs.cs.meta | 3 + 2 files changed, 154 insertions(+) create mode 100644 Runtime/BinaryPrefs.cs create mode 100644 Runtime/BinaryPrefs.cs.meta diff --git a/Runtime/BinaryPrefs.cs b/Runtime/BinaryPrefs.cs new file mode 100644 index 0000000..264b19d --- /dev/null +++ b/Runtime/BinaryPrefs.cs @@ -0,0 +1,151 @@ +using System.IO; +using UnityEngine; + +namespace Appegy.Storage +{ + public static class BinaryPrefs + { + private static readonly BinaryStorage _storage = BinaryStorage + .Construct(Path.Combine(Application.persistentDataPath, PackageInfo.Name, "player_prefs.bin")) + .AddPrimitiveTypes() + .EnableAutoSaveOnChange() + .Build(); + + /// + /// Sets the value of the preference identified by the given key. + /// + /// The key to set the value for. + /// The value to set. + public static void SetInt(string key, int value) + { + _storage.Set(key, value); + } + + /// + /// Returns the value corresponding to key in the preference file if it exists. + /// If the key is not found in the current storage, it checks PlayerPrefs. + /// + /// The key to retrieve the value for. + /// The default value to return if the key does not exist. + /// The value corresponding to key. + public static int GetInt(string key, int defaultValue = 0) + { + if (_storage.Has(key)) + { + return _storage.Get(key, defaultValue); + } + + if (PlayerPrefs.HasKey(key)) + { + int value = PlayerPrefs.GetInt(key, defaultValue); + _storage.Set(key, value); + return value; + } + + return defaultValue; + } + + /// + /// Sets the value of the preference identified by the given key. + /// + /// The key to set the value for. + /// The value to set. + public static void SetFloat(string key, float value) + { + _storage.Set(key, value); + } + + /// + /// Returns the value corresponding to key in the preference file if it exists. + /// If the key is not found in the current storage, it checks PlayerPrefs. + /// + /// The key to retrieve the value for. + /// The default value to return if the key does not exist. + /// The value corresponding to key. + public static float GetFloat(string key, float defaultValue = 0f) + { + if (_storage.Has(key)) + { + return _storage.Get(key, defaultValue); + } + + if (PlayerPrefs.HasKey(key)) + { + float value = PlayerPrefs.GetFloat(key, defaultValue); + _storage.Set(key, value); + return value; + } + + return defaultValue; + } + + /// + /// Sets the value of the preference identified by the given key. + /// + /// The key to set the value for. + /// The value to set. + public static void SetString(string key, string value) + { + _storage.Set(key, value); + } + + /// + /// Returns the value corresponding to key in the preference file if it exists. + /// If the key is not found in the current storage, it checks PlayerPrefs. + /// + /// The key to retrieve the value for. + /// The default value to return if the key does not exist. + /// The value corresponding to key. + public static string GetString(string key, string defaultValue = "") + { + if (_storage.Has(key)) + { + return _storage.Get(key, defaultValue); + } + + if (PlayerPrefs.HasKey(key)) + { + string value = PlayerPrefs.GetString(key, defaultValue); + _storage.Set(key, value); + return value; + } + + return defaultValue; + } + + /// + /// Returns true if the key exists in the preference file. + /// + /// The key to check for existence. + /// True if the key exists; otherwise, false. + public static bool HasKey(string key) + { + return _storage.Has(key) || PlayerPrefs.HasKey(key); + } + + /// + /// Removes the given key from the preference file. + /// + /// The key to remove. + public static void DeleteKey(string key) + { + _storage.Remove(key); + } + + /// + /// Removes all keys and values from the preference file. + /// + public static void DeleteAll() + { + _storage.RemoveAll(); + } + + /// + /// Writes all modified preferences to disk. + /// + public static void Save() + { + _storage.Save(); + } + } +} diff --git a/Runtime/BinaryPrefs.cs.meta b/Runtime/BinaryPrefs.cs.meta new file mode 100644 index 0000000..3588755 --- /dev/null +++ b/Runtime/BinaryPrefs.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6aa0713cf5824cd5acef4288973e8500 +timeCreated: 1718807205 \ No newline at end of file From 34fdc06eae56740d153ac318cfd9d695db77474c Mon Sep 17 00:00:00 2001 From: Ivan Murashka Date: Sat, 22 Jun 2024 15:08:19 +0200 Subject: [PATCH 2/6] type mismatch behaviour --- Runtime/BinaryPrefs.cs | 4 +- Runtime/BinaryStorage.Builder.cs | 24 +++ Runtime/BinaryStorage.cs | 41 ++++-- Runtime/Settings/TypeMismatchBehaviour.cs | 23 +++ .../Settings/TypeMismatchBehaviour.cs.meta | 3 + Tests/BinaryStorageTests.cs | 138 ++++++++++++++++++ 6 files changed, 217 insertions(+), 16 deletions(-) create mode 100644 Runtime/Settings/TypeMismatchBehaviour.cs create mode 100644 Runtime/Settings/TypeMismatchBehaviour.cs.meta diff --git a/Runtime/BinaryPrefs.cs b/Runtime/BinaryPrefs.cs index 264b19d..3222e20 100644 --- a/Runtime/BinaryPrefs.cs +++ b/Runtime/BinaryPrefs.cs @@ -9,6 +9,8 @@ public static class BinaryPrefs .Construct(Path.Combine(Application.persistentDataPath, PackageInfo.Name, "player_prefs.bin")) .AddPrimitiveTypes() .EnableAutoSaveOnChange() + .SetMissingKeyBehaviour(MissingKeyBehavior.ReturnDefaultValueOnly) + .SetTypeMismatchBehaviour(TypeMismatchBehaviour.OverrideValueAndType) .Build(); /// @@ -148,4 +150,4 @@ public static void Save() _storage.Save(); } } -} +} \ No newline at end of file diff --git a/Runtime/BinaryStorage.Builder.cs b/Runtime/BinaryStorage.Builder.cs index 6557777..a361241 100644 --- a/Runtime/BinaryStorage.Builder.cs +++ b/Runtime/BinaryStorage.Builder.cs @@ -56,6 +56,8 @@ public class Builder private readonly string _filePath; private readonly List _serializers = new(); private bool _autoSave; + private MissingKeyBehavior _missingKeyBehavior = MissingKeyBehavior.InitializeWithDefaultValue; + private TypeMismatchBehaviour _typeMismatchBehaviour = TypeMismatchBehaviour.ThrowException; internal Builder(string filePath) { @@ -72,6 +74,26 @@ public Builder EnableAutoSaveOnChange() return this; } + /// + /// Specifies the behavior when a requested key is not found in the storage. + /// + /// The current instance for method chaining. + public Builder SetMissingKeyBehaviour(MissingKeyBehavior behavior) + { + _missingKeyBehavior = behavior; + return this; + } + /// + /// Specifies the behavior when the type of value associated with a key does not match the expected type. + /// + /// The type mismatch behavior. + /// The current instance for method chaining. + public Builder SetTypeMismatchBehaviour(TypeMismatchBehaviour behavior) + { + _typeMismatchBehaviour = behavior; + return this; + } + /// /// Adds serializers for primitive types to the storage configuration. /// @@ -225,6 +247,8 @@ public BinaryStorage Build() { var storage = new BinaryStorage(_filePath, _serializers); storage.AutoSave = _autoSave; + storage.MissingKeyBehavior = _missingKeyBehavior; + storage.TypeMismatchBehaviour = _typeMismatchBehaviour; storage.LoadDataFromDisk(); return storage; } diff --git a/Runtime/BinaryStorage.cs b/Runtime/BinaryStorage.cs index a721de9..7be8053 100644 --- a/Runtime/BinaryStorage.cs +++ b/Runtime/BinaryStorage.cs @@ -27,6 +27,11 @@ public partial class BinaryStorage : IDisposable /// public MissingKeyBehavior MissingKeyBehavior { get; set; } = MissingKeyBehavior.ReturnDefaultValueOnly; + /// + /// Gets or sets the behavior when the type of a value associated with a key does not match the expected type. + /// + public TypeMismatchBehaviour TypeMismatchBehaviour { get; set; } = TypeMismatchBehaviour.OverrideValueAndType; + /// /// Gets a value indicating whether there are unsaved changes. /// @@ -92,11 +97,12 @@ public virtual bool Supports() /// The key to get the value for. /// The default value to use if the key does not exist. /// The value associated with the key. - public virtual T Get(string key, T defaultValue = default) + public virtual T Get(string key, T defaultValue = default, MissingKeyBehavior? overrideMissingKeyBehavior = null) { ThrowIfDisposed(); ThrowIfCollection(); var record = GetRecord(key); + var missingKeyBehavior = overrideMissingKeyBehavior ?? MissingKeyBehavior; switch (record) { case Record typedRecord: @@ -104,11 +110,11 @@ public virtual T Get(string key, T defaultValue = default) case not null: throw new UnexpectedTypeException(key, nameof(Get), record.Type, typeof(T)); case null: - return MissingKeyBehavior switch + return missingKeyBehavior switch { MissingKeyBehavior.InitializeWithDefaultValue => AddRecord(key, defaultValue).Value, MissingKeyBehavior.ReturnDefaultValueOnly => defaultValue, - _ => throw new UnexpectedEnumException(typeof(MissingKeyBehavior), MissingKeyBehavior) + _ => throw new UnexpectedEnumException(typeof(MissingKeyBehavior), missingKeyBehavior) }; } } @@ -119,9 +125,9 @@ public virtual T Get(string key, T defaultValue = default) /// The type of the value. /// The key to set the value for. /// The value to set. - /// Whether to override the value if the key already exists but with another type. + /// Whether to override the value if the key already exists but with another type. /// True if the value was set; otherwise, false. - public virtual bool Set(string key, T value, bool overrideTypeMismatch = false) + public virtual bool Set(string key, T value, TypeMismatchBehaviour? overrideTypeMismatchBehaviour = null) { ThrowIfDisposed(); ThrowIfCollection(); @@ -138,18 +144,23 @@ public virtual bool Set(string key, T value, bool overrideTypeMismatch = fals return ChangeRecord(typedRecord, value); } - if (!overrideTypeMismatch) + var mismatchBehaviour = overrideTypeMismatchBehaviour ?? TypeMismatchBehaviour; + switch (mismatchBehaviour) { - throw new UnexpectedTypeException(key, nameof(Set), record.Type, typeof(T)); - } - - using (MultipleChangeScope()) - { - RemoveRecord(key); - AddRecord(key, value); + case TypeMismatchBehaviour.OverrideValueAndType: + using (MultipleChangeScope()) + { + RemoveRecord(key); + AddRecord(key, value); + } + return true; + case TypeMismatchBehaviour.ThrowException: + throw new UnexpectedTypeException(key, nameof(Set), record.Type, typeof(T)); + case TypeMismatchBehaviour.Ignore: + return false; + default: + throw new UnexpectedEnumException(typeof(TypeMismatchBehaviour), mismatchBehaviour); } - - return true; } /// diff --git a/Runtime/Settings/TypeMismatchBehaviour.cs b/Runtime/Settings/TypeMismatchBehaviour.cs new file mode 100644 index 0000000..bfef555 --- /dev/null +++ b/Runtime/Settings/TypeMismatchBehaviour.cs @@ -0,0 +1,23 @@ +namespace Appegy.Storage +{ + /// + /// Specifies the behavior when the type of a value associated with a key does not match the expected type. + /// + public enum TypeMismatchBehaviour + { + /// + /// Throws an exception if there is a type mismatch. + /// + ThrowException, + + /// + /// Overrides the existing value and type with the new value and type. + /// + OverrideValueAndType, + + /// + /// Ignores the new value and type if there is a type mismatch. + /// + Ignore + } +} \ No newline at end of file diff --git a/Runtime/Settings/TypeMismatchBehaviour.cs.meta b/Runtime/Settings/TypeMismatchBehaviour.cs.meta new file mode 100644 index 0000000..39021ca --- /dev/null +++ b/Runtime/Settings/TypeMismatchBehaviour.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a2462c117fb34b0185b5336fe57b69bd +timeCreated: 1719058726 \ No newline at end of file diff --git a/Tests/BinaryStorageTests.cs b/Tests/BinaryStorageTests.cs index 9374be4..c224990 100644 --- a/Tests/BinaryStorageTests.cs +++ b/Tests/BinaryStorageTests.cs @@ -388,5 +388,143 @@ public void WhenReactiveDictionaryChanged_AndStorageReloaded_ThenValuesInStorage } #endregion + + #region TypeMismatchBehaviour Tests + + [Test] + public void WhenTypeMismatchBehaviorIsThrowException_ThenExceptionIsThrown() + { + // Arrange + using var storage = BinaryStorage.Construct(StoragePath) + .AddPrimitiveTypes() + .SetTypeMismatchBehaviour(TypeMismatchBehaviour.ThrowException) + .Build(); + + storage.Set("key", 123); + + // Act + // ReSharper disable once AccessToDisposedClosure + Action action = () => storage.Set("key", "value"); + + // Assert + action.Should().Throw(); + } + + [Test] + public void WhenTypeMismatchBehaviorIsOverrideValueAndType_ThenValueAndTypeAreOverridden() + { + // Arrange + using var storage = BinaryStorage.Construct(StoragePath) + .AddPrimitiveTypes() + .SetTypeMismatchBehaviour(TypeMismatchBehaviour.OverrideValueAndType) + .Build(); + + storage.Set("key", 123); + + // Act + var result = storage.Set("key", "value"); + + // Assert + result.Should().BeTrue(); + storage.Get("key").Should().Be("value"); + } + + [Test] + public void WhenTypeMismatchBehaviorIsIgnore_ThenValueAndTypeAreIgnored() + { + // Arrange + using var storage = BinaryStorage.Construct(StoragePath) + .AddPrimitiveTypes() + .SetTypeMismatchBehaviour(TypeMismatchBehaviour.Ignore) + .Build(); + + storage.Set("key", 123); + + // Act + var result = storage.Set("key", "value"); + + // Assert + result.Should().BeFalse(); + storage.Get("key").Should().Be(123); + } + + [Test] + public void WhenTypeMismatchBehaviorOverrideIsSetInGetMethod_ThenBehaviorIsOverridden() + { + // Arrange + using var storage = BinaryStorage.Construct(StoragePath) + .AddPrimitiveTypes() + .SetTypeMismatchBehaviour(TypeMismatchBehaviour.ThrowException) + .Build(); + + storage.Set("key", 123); + + // Act + var value = storage.Get("key", "defaultValue", MissingKeyBehavior.InitializeWithDefaultValue); + + // Assert + value.Should().Be("defaultValue"); + storage.Has("key").Should().BeTrue(); + storage.Get("key").Should().Be("defaultValue"); + } + + #endregion + + #region MissingKeyBehavior Tests + + [Test] + public void WhenMissingKeyBehaviorIsInitializeWithDefaultValue_ThenKeyIsInitialized() + { + // Arrange + using var storage = BinaryStorage.Construct(StoragePath) + .AddPrimitiveTypes() + .SetMissingKeyBehaviour(MissingKeyBehavior.InitializeWithDefaultValue) + .Build(); + + // Act + var value = storage.Get("key", 10); + + // Assert + value.Should().Be(10); + storage.Has("key").Should().BeTrue(); + storage.Get("key").Should().Be(10); + } + + [Test] + public void WhenMissingKeyBehaviorIsReturnDefaultValueOnly_ThenDefaultValueIsReturned() + { + // Arrange + using var storage = BinaryStorage.Construct(StoragePath) + .AddPrimitiveTypes() + .SetMissingKeyBehaviour(MissingKeyBehavior.ReturnDefaultValueOnly) + .Build(); + + // Act + var value = storage.Get("key", 10); + + // Assert + value.Should().Be(10); + storage.Has("key").Should().BeFalse(); + } + + [Test] + public void WhenMissingKeyBehaviorOverrideIsSetInGetMethod_ThenBehaviorIsOverridden() + { + // Arrange + using var storage = BinaryStorage.Construct(StoragePath) + .AddPrimitiveTypes() + .SetMissingKeyBehaviour(MissingKeyBehavior.ReturnDefaultValueOnly) + .Build(); + + // Act + var value = storage.Get("key", 10, MissingKeyBehavior.InitializeWithDefaultValue); + + // Assert + value.Should().Be(10); + storage.Has("key").Should().BeTrue(); + storage.Get("key").Should().Be(10); + } + + #endregion } } \ No newline at end of file From 6e6d70153ab9e3b42ac59b6caa56e2d01ce3e02d Mon Sep 17 00:00:00 2001 From: Ivan Murashka Date: Sat, 22 Jun 2024 15:08:41 +0200 Subject: [PATCH 3/6] setup application identifiers --- .BinaryPrefs/ProjectSettings/ProjectSettings.asset | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.BinaryPrefs/ProjectSettings/ProjectSettings.asset b/.BinaryPrefs/ProjectSettings/ProjectSettings.asset index 388a9fa..0730eae 100644 --- a/.BinaryPrefs/ProjectSettings/ProjectSettings.asset +++ b/.BinaryPrefs/ProjectSettings/ProjectSettings.asset @@ -159,7 +159,8 @@ PlayerSettings: androidMaxAspectRatio: 2.1 applicationIdentifier: Android: org.appegy.binaryprefs - iPhone: org.appegy.tools.ulog + Standalone: org.appegy.binaryprefs + iPhone: org.appegy.binaryprefs buildNumber: Standalone: 0 iPhone: 0 From 5aa72ca9591d31cd669f8f94a679fed721cbad86 Mon Sep 17 00:00:00 2001 From: Ivan Murashka Date: Fri, 16 Aug 2024 19:51:00 +0200 Subject: [PATCH 4/6] few tests for prefs --- .../ProjectSettings/ProjectSettings.asset | 2 +- Runtime/BinaryPrefs.cs | 6 +- Tests/BinaryPrefsTests.cs | 99 +++++++++++++++++++ Tests/BinaryPrefsTests.cs.meta | 3 + 4 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 Tests/BinaryPrefsTests.cs create mode 100644 Tests/BinaryPrefsTests.cs.meta diff --git a/.BinaryPrefs/ProjectSettings/ProjectSettings.asset b/.BinaryPrefs/ProjectSettings/ProjectSettings.asset index 0730eae..930900f 100644 --- a/.BinaryPrefs/ProjectSettings/ProjectSettings.asset +++ b/.BinaryPrefs/ProjectSettings/ProjectSettings.asset @@ -52,7 +52,7 @@ PlayerSettings: m_MTRendering: 1 mipStripping: 0 numberOfMipsStripped: 0 - m_StackTraceTypes: 000000000000000000000000000000000000000001000000 + m_StackTraceTypes: 010000000100000001000000010000000100000001000000 iosShowActivityIndicatorOnLoading: -1 androidShowActivityIndicatorOnLoading: -1 iosUseCustomAppBackgroundBehavior: 0 diff --git a/Runtime/BinaryPrefs.cs b/Runtime/BinaryPrefs.cs index 3222e20..fa8a298 100644 --- a/Runtime/BinaryPrefs.cs +++ b/Runtime/BinaryPrefs.cs @@ -39,7 +39,7 @@ public static int GetInt(string key, int defaultValue = 0) if (PlayerPrefs.HasKey(key)) { - int value = PlayerPrefs.GetInt(key, defaultValue); + var value = PlayerPrefs.GetInt(key, defaultValue); _storage.Set(key, value); return value; } @@ -73,7 +73,7 @@ public static float GetFloat(string key, float defaultValue = 0f) if (PlayerPrefs.HasKey(key)) { - float value = PlayerPrefs.GetFloat(key, defaultValue); + var value = PlayerPrefs.GetFloat(key, defaultValue); _storage.Set(key, value); return value; } @@ -107,7 +107,7 @@ public static string GetString(string key, string defaultValue = "") if (PlayerPrefs.HasKey(key)) { - string value = PlayerPrefs.GetString(key, defaultValue); + var value = PlayerPrefs.GetString(key, defaultValue); _storage.Set(key, value); return value; } diff --git a/Tests/BinaryPrefsTests.cs b/Tests/BinaryPrefsTests.cs new file mode 100644 index 0000000..2bba57e --- /dev/null +++ b/Tests/BinaryPrefsTests.cs @@ -0,0 +1,99 @@ +using NUnit.Framework; +using FluentAssertions; +using UnityEngine; +using Appegy.Storage; + +namespace Appegy.Tests.Storage +{ + [TestFixture] + public class BinaryPrefsIntTests + { + [SetUp] + public void SetUp() + { + // Clear PlayerPrefs and BinaryPrefs before each test + PlayerPrefs.DeleteAll(); + BinaryPrefs.DeleteAll(); + } + + [Test] + public void SetInt_ShouldStoreValueInBinaryStorage() + { + // Arrange + var key = "testInt"; + var value = 42; + + // Act + BinaryPrefs.SetInt(key, value); + + // Assert + BinaryPrefs.GetInt(key).Should().Be(value); + } + + [Test] + public void GetInt_ShouldReturnDefaultValueIfKeyNotFound() + { + // Arrange + var key = "unknownInt"; + var defaultValue = 10; + + // Act + var result = BinaryPrefs.GetInt(key, defaultValue); + + // Assert + result.Should().Be(defaultValue); + } + + [Test] + public void GetInt_ShouldReturnValueFromPlayerPrefsIfNotInBinaryStorage() + { + // Arrange + var key = "testInt"; + var playerPrefsValue = 100; + + // Store value in PlayerPrefs only + PlayerPrefs.SetInt(key, playerPrefsValue); + + // Act + var result = BinaryPrefs.GetInt(key); + + // Assert + result.Should().Be(playerPrefsValue); + + // Verify that the value is now stored in BinaryStorage + BinaryPrefs.GetInt(key).Should().Be(playerPrefsValue); + } + + [Test] + public void GetInt_ShouldReturnDefaultValueIfKeyNotFoundInBothStorages() + { + // Arrange + var key = "nonExistentKey"; + var defaultValue = 20; + + // Act + var result = BinaryPrefs.GetInt(key, defaultValue); + + // Assert + result.Should().Be(defaultValue); + } + + [Test] + public void SetInt_ShouldOverrideExistingValueInBinaryStorage() + { + // Arrange + var key = "testInt"; + var initialValue = 42; + var newValue = 84; + + // Store initial value + BinaryPrefs.SetInt(key, initialValue); + + // Act + BinaryPrefs.SetInt(key, newValue); + + // Assert + BinaryPrefs.GetInt(key).Should().Be(newValue); + } + } +} diff --git a/Tests/BinaryPrefsTests.cs.meta b/Tests/BinaryPrefsTests.cs.meta new file mode 100644 index 0000000..0f6a260 --- /dev/null +++ b/Tests/BinaryPrefsTests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e14b5abec5094cdc82b51f6861f43f94 +timeCreated: 1723741900 \ No newline at end of file From d02e2e214526f6eb6abb4f88819716e0dc3d67fb Mon Sep 17 00:00:00 2001 From: Ivan Murashka Date: Thu, 29 Aug 2024 21:59:46 +0200 Subject: [PATCH 5/6] float and string tests --- Tests/BinaryPrefsTests.cs | 184 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 178 insertions(+), 6 deletions(-) diff --git a/Tests/BinaryPrefsTests.cs b/Tests/BinaryPrefsTests.cs index 2bba57e..62a0635 100644 --- a/Tests/BinaryPrefsTests.cs +++ b/Tests/BinaryPrefsTests.cs @@ -8,7 +8,7 @@ namespace Appegy.Tests.Storage [TestFixture] public class BinaryPrefsIntTests { - [SetUp] + [SetUp, TearDown] public void SetUp() { // Clear PlayerPrefs and BinaryPrefs before each test @@ -16,11 +16,13 @@ public void SetUp() BinaryPrefs.DeleteAll(); } + #region Integer + [Test] public void SetInt_ShouldStoreValueInBinaryStorage() { // Arrange - var key = "testInt"; + var key = "key"; var value = 42; // Act @@ -34,7 +36,7 @@ public void SetInt_ShouldStoreValueInBinaryStorage() public void GetInt_ShouldReturnDefaultValueIfKeyNotFound() { // Arrange - var key = "unknownInt"; + var key = "unknownKey"; var defaultValue = 10; // Act @@ -48,7 +50,7 @@ public void GetInt_ShouldReturnDefaultValueIfKeyNotFound() public void GetInt_ShouldReturnValueFromPlayerPrefsIfNotInBinaryStorage() { // Arrange - var key = "testInt"; + var key = "key"; var playerPrefsValue = 100; // Store value in PlayerPrefs only @@ -82,7 +84,7 @@ public void GetInt_ShouldReturnDefaultValueIfKeyNotFoundInBothStorages() public void SetInt_ShouldOverrideExistingValueInBinaryStorage() { // Arrange - var key = "testInt"; + var key = "key"; var initialValue = 42; var newValue = 84; @@ -95,5 +97,175 @@ public void SetInt_ShouldOverrideExistingValueInBinaryStorage() // Assert BinaryPrefs.GetInt(key).Should().Be(newValue); } + + #endregion + + #region Float + + [Test] + public void SetFloat_ShouldStoreValueInBinaryStorage() + { + // Arrange + var key = "key"; + var value = 42f; + + // Act + BinaryPrefs.SetFloat(key, value); + + // Assert + BinaryPrefs.GetFloat(key).Should().Be(value); + } + + [Test] + public void GetFloat_ShouldReturnDefaultValueIfKeyNotFound() + { + // Arrange + var key = "unknownKey"; + var defaultValue = 10f; + + // Act + var result = BinaryPrefs.GetFloat(key, defaultValue); + + // Assert + result.Should().Be(defaultValue); + } + + [Test] + public void GetFloat_ShouldReturnValueFromPlayerPrefsIfNotInBinaryStorage() + { + // Arrange + var key = "key"; + var playerPrefsValue = 100f; + + // Store value in PlayerPrefs only + PlayerPrefs.SetFloat(key, playerPrefsValue); + + // Act + var result = BinaryPrefs.GetFloat(key); + + // Assert + result.Should().Be(playerPrefsValue); + + // Verify that the value is now stored in BinaryStorage + BinaryPrefs.GetFloat(key).Should().Be(playerPrefsValue); + } + + [Test] + public void GetFloat_ShouldReturnDefaultValueIfKeyNotFoundInBothStorages() + { + // Arrange + var key = "nonExistentKey"; + var defaultValue = 20f; + + // Act + var result = BinaryPrefs.GetFloat(key, defaultValue); + + // Assert + result.Should().Be(defaultValue); + } + + [Test] + public void SetFloat_ShouldOverrideExistingValueInBinaryStorage() + { + // Arrange + var key = "key"; + var initialValue = 42f; + var newValue = 84f; + + // Store initial value + BinaryPrefs.SetFloat(key, initialValue); + + // Act + BinaryPrefs.SetFloat(key, newValue); + + // Assert + BinaryPrefs.GetFloat(key).Should().Be(newValue); + } + + #endregion + + #region String + + [Test] + public void SetString_ShouldStoreValueInBinaryStorage() + { + // Arrange + var key = "key"; + var value = "42"; + + // Act + BinaryPrefs.SetString(key, value); + + // Assert + BinaryPrefs.GetString(key).Should().Be(value); + } + + [Test] + public void GetString_ShouldReturnDefaultValueIfKeyNotFound() + { + // Arrange + var key = "unknownKey"; + var defaultValue = "10"; + + // Act + var result = BinaryPrefs.GetString(key, defaultValue); + + // Assert + result.Should().Be(defaultValue); + } + + [Test] + public void GetString_ShouldReturnValueFromPlayerPrefsIfNotInBinaryStorage() + { + // Arrange + var key = "key"; + var playerPrefsValue = "100"; + + // Store value in PlayerPrefs only + PlayerPrefs.SetString(key, playerPrefsValue); + + // Act + var result = BinaryPrefs.GetString(key); + + // Assert + result.Should().Be(playerPrefsValue); + + // Verify that the value is now stored in BinaryStorage + BinaryPrefs.GetString(key).Should().Be(playerPrefsValue); + } + + [Test] + public void GetString_ShouldReturnDefaultValueIfKeyNotFoundInBothStorages() + { + // Arrange + var key = "nonExistentKey"; + var defaultValue = "20"; + + // Act + var result = BinaryPrefs.GetString(key, defaultValue); + + // Assert + result.Should().Be(defaultValue); + } + + [Test] + public void SetString_ShouldOverrideExistingValueInBinaryStorage() + { + // Arrange + var key = "key"; + var initialValue = "42"; + var newValue = "84"; + + // Store initial value + BinaryPrefs.SetString(key, initialValue); + + // Act + BinaryPrefs.SetString(key, newValue); + + // Assert + BinaryPrefs.GetString(key).Should().Be(newValue); + } + + #endregion } -} +} \ No newline at end of file From 6216d9a76f4faa78ff89c3227523a1534c877e1f Mon Sep 17 00:00:00 2001 From: Ivan Murashka Date: Thu, 29 Aug 2024 22:17:00 +0200 Subject: [PATCH 6/6] small test for edge case --- Tests/BinaryPrefsTests.cs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Tests/BinaryPrefsTests.cs b/Tests/BinaryPrefsTests.cs index 62a0635..1f58c39 100644 --- a/Tests/BinaryPrefsTests.cs +++ b/Tests/BinaryPrefsTests.cs @@ -1,9 +1,8 @@ -using NUnit.Framework; using FluentAssertions; +using NUnit.Framework; using UnityEngine; -using Appegy.Storage; -namespace Appegy.Tests.Storage +namespace Appegy.Storage { [TestFixture] public class BinaryPrefsIntTests @@ -267,5 +266,24 @@ public void SetString_ShouldOverrideExistingValueInBinaryStorage() } #endregion + + #region Edges + + [Test] + public void SetInt_GetFloat_ShouldReturnCorrectInt() + { + // Arrange + var key = "key"; + var value = 42; + + // Act + PlayerPrefs.SetInt(key, value); + PlayerPrefs.SetFloat(key, value); + + // Assert + BinaryPrefs.GetFloat(key).Should().Be(value); + } + + #endregion } } \ No newline at end of file