Skip to content

Commit

Permalink
Merge pull request #1 from philipcass/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
philipcass committed Oct 24, 2020
2 parents 17618ac + 8929bc0 commit 055b816
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 12 deletions.
60 changes: 53 additions & 7 deletions Runtime/ClickerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ namespace uClicker
public class ClickerManager : ClickerComponent
{
public ManagerSaveSettings SaveSettings = new ManagerSaveSettings();
public ManagerConfig Config;
public ManagerState State;
public ManagerConfig Config = new ManagerConfig();
public ManagerState State = new ManagerState();

public UnityEvent OnTick;
public UnityEvent OnBuyUpgrade;
public UnityEvent OnBuyBuilding;
public UnityEvent OnTick = new UnityEvent();
public UnityEvent OnBuyUpgrade = new UnityEvent();
public UnityEvent OnBuyBuilding = new UnityEvent();

#region Unity Events

#if UNITY_EDITOR
/// <summary>
/// Validates platform settings and switches to platform compatible settings automatically
/// </summary>
private void OnValidate()
{
if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL &&
Expand All @@ -36,9 +39,22 @@ private void OnValidate()
}
#endif

/// <summary>
/// Inits Currency totals for manager on startup
/// </summary>
public void OnEnable()
{
foreach (var configClickable in Config.Currencies)
{
UpdateTotal(configClickable, 0);
}
}

/// <summary>
/// Clear save on unload so we don't try deserializing the save between play/stop
/// </summary>
private void OnDisable()
{
// Clear save on unload so we don't try deserializing the save between play/stop
State = new ManagerState();
}

Expand Down Expand Up @@ -174,6 +190,9 @@ public CurrencyTuple BuildingCost(Building building)
return currencyTuple;
}

/// <summary>
/// Serializes state and saves to target <see cref="ManagerSaveSettings.SaveTypeEnum"/> in <see cref="SaveSettings"/>
/// </summary>
public void SaveProgress()
{
string value = JsonUtility.ToJson(State, true);
Expand All @@ -190,18 +209,27 @@ public void SaveProgress()
}
}

/// <summary>
/// Loads from target <see cref="ManagerSaveSettings.SaveTypeEnum"/> in <see cref="SaveSettings"/>
/// </summary>
/// <exception cref="ArgumentException">When save does not exist</exception>
public void LoadProgress()
{
string json;
switch (SaveSettings.SaveType)
{
case ManagerSaveSettings.SaveTypeEnum.SaveToPlayerPrefs:
if (!PlayerPrefs.HasKey(SaveSettings.SaveName))
{
throw new ArgumentException($"Save '{SaveSettings.SaveName}' is null");
}

json = PlayerPrefs.GetString(SaveSettings.SaveName);
break;
case ManagerSaveSettings.SaveTypeEnum.SaveToFile:
if (!File.Exists(SaveSettings.FullSavePath))
{
return;
throw new ArgumentException($"Save '{SaveSettings.FullSavePath}' is null");
}

#if DEBUG
Expand All @@ -221,6 +249,24 @@ public void LoadProgress()
OnBuyUpgrade.Invoke();
}

/// <summary>
/// Clear progress from target <see cref="ManagerSaveSettings.SaveTypeEnum"/> in <see cref="SaveSettings"/>
/// </summary>
public void ClearProgress()
{
switch (SaveSettings.SaveType)
{
case ManagerSaveSettings.SaveTypeEnum.SaveToPlayerPrefs:
PlayerPrefs.DeleteKey(SaveSettings.SaveName);
break;
case ManagerSaveSettings.SaveTypeEnum.SaveToFile:
File.Delete(SaveSettings.FullSavePath);
break;
default:
throw new ArgumentOutOfRangeException();
}
}

#endregion

#region Internal Logic
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Components/ClickerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ public struct RequirementGroup
public abstract class UnlockableComponent : ClickerComponent
{
public bool Unlocked;
public RequirementGroup[] RequirementGroups;
public RequirementGroup[] RequirementGroups = new RequirementGroup[0];
}
}
8 changes: 4 additions & 4 deletions Runtime/ManagerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace uClicker
[Serializable]
public class ManagerConfig
{
public Currency[] Currencies;
public Clickable[] Clickables;
public Building[] AvailableBuildings;
public Upgrade[] AvailableUpgrades;
public Currency[] Currencies = new Currency[0];
public Clickable[] Clickables = new Clickable[0];
public Building[] AvailableBuildings = new Building[0];
public Upgrade[] AvailableUpgrades = new Upgrade[0];
public float BuildingCostIncrease = 0.15f;
}
}
8 changes: 8 additions & 0 deletions Tests.meta

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

81 changes: 81 additions & 0 deletions Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using NUnit.Framework;
using uClicker;
using UnityEditor;
using UnityEngine;

namespace Tests
{
public class Tests
{
private ClickerManager _testManager;
private Currency _testCurrency;
private Clickable _testClickable;

[SetUp]
public void CreateObjects()
{
_testManager = ScriptableObject.CreateInstance<ClickerManager>();
_testCurrency = ScriptableObject.CreateInstance<Currency>();
_testClickable = ScriptableObject.CreateInstance<Clickable>();
_testManager.SaveSettings.SaveName = "TEST_SAVE";
_testManager.SaveSettings.SavePath = Application.dataPath;
}

[TearDown]
public void TearDown()
{
_testManager.ClearProgress();
}

[TestCase(3, ExpectedResult = 3)]
public int TestClicking(int clickCount)
{
_testClickable.Currency = _testCurrency;
_testClickable.Amount = 1;
ArrayUtility.Add(ref _testManager.Config.Currencies, _testCurrency);
_testManager.OnEnable();

Assert.AreEqual(0, _testManager.State.CurrencyCurrentTotals[_testCurrency]);
for (int i = 0; i < clickCount; i++)
{
_testManager.Click(_testClickable);
}

return (int) _testManager.State.CurrencyCurrentTotals[_testCurrency];
}

[Test]
public void TestGenerators()
{
_testClickable.Currency = _testCurrency;
_testClickable.Amount = 1;
ArrayUtility.Add(ref _testManager.Config.Currencies, _testCurrency);

var building = ScriptableObject.CreateInstance<Building>();
building.YieldAmount = new CurrencyTuple()
{
Amount = 5,
Currency = _testCurrency
};
_testManager.State.EarnedBuildings[building] = 1;
_testManager.OnEnable();
_testManager.Tick();
Assert.AreEqual(5, _testManager.State.CurrencyCurrentTotals[_testCurrency]);
}

[TestCase(ManagerSaveSettings.SaveTypeEnum.SaveToPlayerPrefs)]
[TestCase(ManagerSaveSettings.SaveTypeEnum.SaveToFile)]
public void TestSave(ManagerSaveSettings.SaveTypeEnum saveType)
{
_testClickable.Currency = _testCurrency;
_testClickable.Amount = 1;
ArrayUtility.Add(ref _testManager.Config.Currencies, _testCurrency);

_testManager.SaveSettings.SaveType = saveType;
Assert.Throws<ArgumentException>(() => { _testManager.LoadProgress(); });
_testManager.SaveProgress();
_testManager.LoadProgress();
}
}
}
11 changes: 11 additions & 0 deletions Tests/Tests.cs.meta

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

23 changes: 23 additions & 0 deletions Tests/uClicker.Tests.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "uClicker.Tests",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"uClicker.Runtime"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Tests/uClicker.Tests.asmdef.meta

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

0 comments on commit 055b816

Please sign in to comment.