Skip to content

Commit

Permalink
Added extra key checkers and proper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
richardelms committed Nov 20, 2017
1 parent a12a7cc commit 0e34bbb
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 15 deletions.
58 changes: 54 additions & 4 deletions Assets/Plugins/FileBasedPrefs/FileBasedPrefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ public static class FileBasedPrefs
private const string SaveFileName = "saveData.txt";
private const bool ScrambleSaveData = true;

const string String_Empty = "";

#region Public Get, Set and Util methods

public static void SetString(string key, string value = default(string))
public static void SetString(string key, string value = String_Empty)
{
AddDataToSaveFile(key, value);
}

public static string GetString(string key, string defaultValue = default(string))
public static string GetString(string key, string defaultValue = String_Empty)
{
return (string) GetDataFromSaveFile(key, defaultValue);
}
Expand Down Expand Up @@ -54,23 +56,71 @@ public static bool HasKey(string key)
return GetSaveFile().HasKey(key);
}

public static bool HasKeyForString(string key)
{
return GetSaveFile().HasKeyFromObject(key, string.Empty);
}

public static bool HasKeyForInt(string key)
{
return GetSaveFile().HasKeyFromObject(key, default(int));
}

public static bool HasKeyForFloat(string key)
{
return GetSaveFile().HasKeyFromObject(key, default(float));
}

public static bool HasKeyForBool(string key)
{
return GetSaveFile().HasKeyFromObject(key, default(bool));
}

public static void DeleteKey(string key)
{
var saveFile = GetSaveFile();
saveFile.DeleteKey(key);
SaveSaveFile(saveFile);
}

public static void OverwriteLocalSaveFile(string data)
public static void DeleteString(string key)
{
WriteToSaveFile(data);
var saveFile = GetSaveFile();
saveFile.DeleteString(key);
SaveSaveFile(saveFile);
}

public static void DeleteInt(string key)
{
var saveFile = GetSaveFile();
saveFile.DeleteInt(key);
SaveSaveFile(saveFile);
}

public static void DeleteFloat(string key)
{
var saveFile = GetSaveFile();
saveFile.DeleteFloat(key);
SaveSaveFile(saveFile);
}

public static void DeleteBool(string key)
{
var saveFile = GetSaveFile();
saveFile.DeleteBool(key);
SaveSaveFile(saveFile);
}

public static void DeleteAll()
{
WriteToSaveFile(JsonUtility.ToJson(new SaveFile()));
}

public static void OverwriteLocalSaveFile(string data)
{
WriteToSaveFile(data);
}

public static string GetSaveFilePath()
{
return Path.Combine(Application.persistentDataPath, SaveFileName);
Expand Down
52 changes: 52 additions & 0 deletions Assets/Plugins/FileBasedPrefs/SaveFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,58 @@ public void DeleteKey(string key)
}
}

public void DeleteString(string key)
{
for (int i = 0; i < StringData.Length; i++)
{
if (StringData[i].Key.Equals(key))
{
var dataAsList = StringData.ToList();
dataAsList.RemoveAt(i);
StringData = dataAsList.ToArray();
}
}
}

public void DeleteInt(string key)
{
for (int i = 0; i < IntData.Length; i++)
{
if (IntData[i].Key.Equals(key))
{
var dataAsList = IntData.ToList();
dataAsList.RemoveAt(i);
IntData = dataAsList.ToArray();
}
}
}

public void DeleteFloat(string key)
{
for (int i = 0; i < FloatData.Length; i++)
{
if (FloatData[i].Key.Equals(key))
{
var dataAsList = FloatData.ToList();
dataAsList.RemoveAt(i);
FloatData = dataAsList.ToArray();
}
}
}

public void DeleteBool(string key)
{
for (int i = 0; i < BoolData.Length; i++)
{
if (BoolData[i].Key.Equals(key))
{
var dataAsList = BoolData.ToList();
dataAsList.RemoveAt(i);
BoolData = dataAsList.ToArray();
}
}
}

public bool HasKey(string key)
{
for (int i = 0; i < StringData.Length; i++)
Expand Down
183 changes: 172 additions & 11 deletions Assets/Plugins/FileBasedPrefs/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,183 @@

public class Test : MonoBehaviour {

// Use this for initialization
void Start ()
{
FileBasedPrefs.SetString("Test","ddd");
FileBasedPrefs.SetInt("Test", 333);
FileBasedPrefs.SetFloat("Test", 333.1f);
FileBasedPrefs.SetBool("Test", true);
Invoke("Result", 0.5f);
FileBasedPrefs.DeleteAll();
Invoke("StringTests", 0.25f);
Invoke("IntTests", 0.5f);
Invoke("FloatTests", 0.75f);
Invoke("BoolTests", 1f);
Invoke("GeneralTests", 1.25f);
}

void Result()



void StringTests()
{
FileBasedPrefs.SetString("test", "test");

if(!FileBasedPrefs.GetString("test").Equals("test"))
{
Debug.LogException(new System.Exception("SetStringFailed"));
return;
}

FileBasedPrefs.SetString("test", "test2");

if (!FileBasedPrefs.GetString("test").Equals("test2"))
{
Debug.LogException(new System.Exception("ReplaceStringFailed"));
return;
}

if (!FileBasedPrefs.HasKeyForString("test"))
{
Debug.LogException(new System.Exception("HasKeyForStringFailed"));
return;
}

FileBasedPrefs.DeleteString("test");

if (!FileBasedPrefs.GetString("test").Equals(""))
{
Debug.LogException(new System.Exception("DeleteStringFailed"));
return;
}

Debug.Log("String Tests Passed");

}

void IntTests()
{
Debug.Log(FileBasedPrefs.GetString("Test","Failed"));
Debug.Log(FileBasedPrefs.GetInt("Test", 0));
Debug.Log(FileBasedPrefs.GetFloat("Test", 0));
Debug.Log(FileBasedPrefs.GetBool("Test", false));
FileBasedPrefs.SetInt("test", 1);

if (!FileBasedPrefs.GetInt("test").Equals(1))
{
Debug.LogException(new System.Exception("SetIntFailed"));
return;
}

FileBasedPrefs.SetInt("test", 2);

if (!FileBasedPrefs.GetInt("test").Equals(2))
{
Debug.LogException(new System.Exception("ReplaceIntFailed"));
return;
}

if (!FileBasedPrefs.HasKeyForInt("test"))
{
Debug.LogException(new System.Exception("HasKeyForIntFailed"));
return;
}

FileBasedPrefs.DeleteInt("test");

if (!FileBasedPrefs.GetInt("test").Equals(0))
{
Debug.LogException(new System.Exception("DeleteIntFailed"));
return;
}

Debug.Log("Int Tests Passed");

}

void FloatTests()
{
FileBasedPrefs.SetFloat("test", 1);

if (!FileBasedPrefs.GetFloat("test").Equals(1))
{
Debug.LogException(new System.Exception("SetFloatFailed"));
return;
}

FileBasedPrefs.SetFloat("test", 2);

if (!FileBasedPrefs.GetFloat("test").Equals(2))
{
Debug.LogException(new System.Exception("ReplaceFloatFailed"));
return;
}

if (!FileBasedPrefs.HasKeyForFloat("test"))
{
Debug.LogException(new System.Exception("HasKeyForFloatFailed"));
return;
}

FileBasedPrefs.DeleteFloat("test");

if (!FileBasedPrefs.GetFloat("test").Equals(0))
{
Debug.LogException(new System.Exception("DeleteFloatFailed"));
return;
}

Debug.Log("Float Tests Passed");

}

void BoolTests()
{
FileBasedPrefs.SetBool("test", true);

if (!FileBasedPrefs.GetBool("test").Equals(true))
{
Debug.LogException(new System.Exception("SetBoolFailed"));
return;
}

FileBasedPrefs.SetBool("test", true);

if (!FileBasedPrefs.GetBool("test").Equals(true))
{
Debug.LogException(new System.Exception("ReplaceBoolFailed"));
return;
}

if (!FileBasedPrefs.HasKeyForBool("test"))
{
Debug.LogException(new System.Exception("HasKeyForBoolFailed"));
return;
}

FileBasedPrefs.DeleteBool("test");

if (!FileBasedPrefs.GetBool("test").Equals(false))
{
Debug.LogException(new System.Exception("DeleteBoolFailed"));
return;
}

Debug.Log("Bool Tests Passed");

}

void GeneralTests()
{
FileBasedPrefs.SetString("test", "test");

if (!FileBasedPrefs.HasKey("test"))
{
Debug.LogException(new System.Exception("HasKeyFailed"));
return;
}

FileBasedPrefs.DeleteKey("test");

if (FileBasedPrefs.HasKey("test"))
{
Debug.LogException(new System.Exception("DeleteKeyFailed"));
return;
}

Debug.Log("General Tests Passed");

}


Expand Down

0 comments on commit 0e34bbb

Please sign in to comment.