From 5aa72ca9591d31cd669f8f94a679fed721cbad86 Mon Sep 17 00:00:00 2001 From: Ivan Murashka Date: Fri, 16 Aug 2024 19:51:00 +0200 Subject: [PATCH] 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