Skip to content

Commit

Permalink
few tests for prefs
Browse files Browse the repository at this point in the history
  • Loading branch information
imurashka committed Aug 16, 2024
1 parent 34fa951 commit 5aa72ca
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .BinaryPrefs/ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ PlayerSettings:
m_MTRendering: 1
mipStripping: 0
numberOfMipsStripped: 0
m_StackTraceTypes: 000000000000000000000000000000000000000001000000
m_StackTraceTypes: 010000000100000001000000010000000100000001000000
iosShowActivityIndicatorOnLoading: -1
androidShowActivityIndicatorOnLoading: -1
iosUseCustomAppBackgroundBehavior: 0
Expand Down
6 changes: 3 additions & 3 deletions Runtime/BinaryPrefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
99 changes: 99 additions & 0 deletions Tests/BinaryPrefsTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
3 changes: 3 additions & 0 deletions Tests/BinaryPrefsTests.cs.meta

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

0 comments on commit 5aa72ca

Please sign in to comment.