From e71f0ec8da4ad8d229bf3caf57a9ec0af6644522 Mon Sep 17 00:00:00 2001 From: LubieKakao1212 Date: Fri, 17 Feb 2023 13:09:17 +0100 Subject: [PATCH] Local backend tests + Some related fixes --- Achievments/Achievment.cs | 56 +++++++++++++---- ...BackendConfig.cs => LocalBackendConfig.cs} | 7 +-- ...fig.cs.meta => LocalBackendConfig.cs.meta} | 0 Achievments/PlaymodeTests.meta | 8 +++ Achievments/PlaymodeTests/BackendTests.cs | 62 +++++++++++++++++++ .../PlaymodeTests/BackendTests.cs.meta | 11 ++++ ...EG.Utils.Achievments.Tests.Playmode.asmdef | 22 +++++++ ...ils.Achievments.Tests.Playmode.asmdef.meta | 7 +++ Achievments/PlaymodeTests/TestAssets.meta | 8 +++ .../TestAssets/TestLocalBackend.asset | 15 +++++ .../TestAssets/TestLocalBackend.asset.meta | 8 +++ Achievments/TODO.txt | 6 +- 12 files changed, 191 insertions(+), 19 deletions(-) rename Achievments/Backend/LoacalBaackend/{LoaclBackendConfig.cs => LocalBackendConfig.cs} (96%) rename Achievments/Backend/LoacalBaackend/{LoaclBackendConfig.cs.meta => LocalBackendConfig.cs.meta} (100%) create mode 100644 Achievments/PlaymodeTests.meta create mode 100644 Achievments/PlaymodeTests/BackendTests.cs create mode 100644 Achievments/PlaymodeTests/BackendTests.cs.meta create mode 100644 Achievments/PlaymodeTests/NEG.Utils.Achievments.Tests.Playmode.asmdef create mode 100644 Achievments/PlaymodeTests/NEG.Utils.Achievments.Tests.Playmode.asmdef.meta create mode 100644 Achievments/PlaymodeTests/TestAssets.meta create mode 100644 Achievments/PlaymodeTests/TestAssets/TestLocalBackend.asset create mode 100644 Achievments/PlaymodeTests/TestAssets/TestLocalBackend.asset.meta diff --git a/Achievments/Achievment.cs b/Achievments/Achievment.cs index ef4803f..448fd58 100644 --- a/Achievments/Achievment.cs +++ b/Achievments/Achievment.cs @@ -17,7 +17,7 @@ namespace NEG.Utils.Achievments { if (instance == null) { - instance = AchievmentManager.Builder.FromDefaultConfig() + instance = AchievmentManager.Builder.FromLabeledConfig(ConfigLabel) .WithLabeledBackend(BackendLabel) .Build(); } @@ -32,17 +32,37 @@ namespace NEG.Utils.Achievments { if(instance != null) { - throw new ApplicationException("Achievments - Cannot set backend label, Managed already created"); + //Log + Quit helps debug builds + Debug.LogError("Achievments - Cannot set backend label, Managed already created"); + Application.Quit(1); } if (backendLabel != null) { - throw new ApplicationException("Multiple AchievmentBackends enabled, this is not allowed"); + //Log + Quit helps debug builds + Debug.LogError("Multiple AchievmentBackends enabled, this is not allowed"); + Application.Quit(1); } backendLabel = value; } } private static string backendLabel; + /// + /// You shouldn't have any reason to change this + /// Used for tests. + /// + public static string ConfigLabel + { + private get => configLabel; + set => configLabel = value; + } + + /// + /// You shouldn't have any reason to change this + /// Used for tests. + /// + private static string configLabel = "Achivments"; + private static AchievmentManager instance; #region Achievment Manipulation (Sets, Gets) @@ -56,7 +76,7 @@ namespace NEG.Utils.Achievments /// throws an if there is no achievment under id public static bool IsCompleted(string id) { - return instance.IsCompleted(id); + return Instance.IsCompleted(id); } #region Toggle @@ -68,7 +88,7 @@ namespace NEG.Utils.Achievments /// throws an if there is no achievment under id or an if achievment under id is of a different type public static bool SetToggleAchivment(string id) { - return instance.SetToggleAchivment(id); + return Instance.SetToggleAchivment(id); } /// @@ -79,7 +99,7 @@ namespace NEG.Utils.Achievments /// throws an if there is no achievment under id or an if achievment under id is of a different type public static bool GetToggleState(string id) { - return instance.GetToggleState(id); + return Instance.GetToggleState(id); } #endregion @@ -92,7 +112,7 @@ namespace NEG.Utils.Achievments /// throws an if there is no achievment under id or an if achievment under id is of a different type public static bool SetIntProgress(string id, int progress) { - return instance.SetIntProgress(id, progress); + return Instance.SetIntProgress(id, progress); } /// @@ -103,7 +123,7 @@ namespace NEG.Utils.Achievments /// throws an if there is no achievment under id or an if achievment under id is of a different type public static bool ChangeIntProgress(string id, int delta) { - return instance.ChangeIntProgress(id, delta); + return Instance.ChangeIntProgress(id, delta); } /// @@ -114,7 +134,7 @@ namespace NEG.Utils.Achievments /// throws an if there is no achievment under id or an if achievment under id is of a different type public static int GetIntProgress(string id) { - return instance.GetIntProgress(id); + return Instance.GetIntProgress(id); } #endregion @@ -127,7 +147,7 @@ namespace NEG.Utils.Achievments /// throws an if there is no achievment under id or an if achievment under id is of a different type public static bool SetFloatProgress(string id, float progress) { - return instance.SetFloatProgress(id, progress); + return Instance.SetFloatProgress(id, progress); } /// @@ -138,7 +158,7 @@ namespace NEG.Utils.Achievments /// throws an if there is no achievment under id or an if achievment under id is of a different type public static bool ChangeFloatProgress(string id, float delta) { - return instance.ChangeFloatProgress(id, delta); + return Instance.ChangeFloatProgress(id, delta); } /// @@ -149,10 +169,22 @@ namespace NEG.Utils.Achievments /// throws an if there is no achievment under id or an if achievment under id is of a different type public static float GetFloatProgress(string id) { - return instance.GetFloatProgress(id); + return Instance.GetFloatProgress(id); } #endregion #endregion + #region Test Api + /// + /// You shouldn't have any reason to use this
+ /// Use at your own risk, may cause unexpected behaviour
+ /// Used for tests + ///
+ public static void NullifyInstance() + { + instance = null; + } + #endregion + } } \ No newline at end of file diff --git a/Achievments/Backend/LoacalBaackend/LoaclBackendConfig.cs b/Achievments/Backend/LoacalBaackend/LocalBackendConfig.cs similarity index 96% rename from Achievments/Backend/LoacalBaackend/LoaclBackendConfig.cs rename to Achievments/Backend/LoacalBaackend/LocalBackendConfig.cs index 3431616..71b6e22 100644 --- a/Achievments/Backend/LoacalBaackend/LoaclBackendConfig.cs +++ b/Achievments/Backend/LoacalBaackend/LocalBackendConfig.cs @@ -9,7 +9,7 @@ using UnityEngine; namespace NEG.Utils.Achievments { [CreateAssetMenu(menuName = "Achievments/Config/Backend/Local")] - public class LoaclBackendConfig : ScriptableObject, IAchievmentBackendConfig + public class LocalBackendConfig : ScriptableObject, IAchievmentBackendConfig { [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)] private static void Init() @@ -51,7 +51,7 @@ namespace NEG.Utils.Achievments achievmentObj["completed"] = true; - token.Replace(achievmentObj); + jobj[id] = achievmentObj; SaveJson(jobj); } @@ -81,8 +81,7 @@ namespace NEG.Utils.Achievments break; } - - token.Replace(achievmentObj); + jobj[id] = achievmentObj; SaveJson(jobj); } diff --git a/Achievments/Backend/LoacalBaackend/LoaclBackendConfig.cs.meta b/Achievments/Backend/LoacalBaackend/LocalBackendConfig.cs.meta similarity index 100% rename from Achievments/Backend/LoacalBaackend/LoaclBackendConfig.cs.meta rename to Achievments/Backend/LoacalBaackend/LocalBackendConfig.cs.meta diff --git a/Achievments/PlaymodeTests.meta b/Achievments/PlaymodeTests.meta new file mode 100644 index 0000000..852d17f --- /dev/null +++ b/Achievments/PlaymodeTests.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d7c140577a904c8419a760a8ac6133c7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Achievments/PlaymodeTests/BackendTests.cs b/Achievments/PlaymodeTests/BackendTests.cs new file mode 100644 index 0000000..f5e9f8e --- /dev/null +++ b/Achievments/PlaymodeTests/BackendTests.cs @@ -0,0 +1,62 @@ +using System.Collections; +using System.Collections.Generic; +using System.IO; +using NUnit.Framework; +using UnityEngine; + +namespace NEG.Utils.Achievments.Tests +{ + public class BackendTests + { + //If stests start to fail first make sure these are correct in relation to test config asset + public const string configLabel = "TestAchievments"; + public const string backendLabel = "AchievmentsLocalTests"; + public const string saveLocation = "./LocalAchievments/Tests.json"; + + public const string AchievmentIdToggle = "TOGGLE"; + public const string AchievmentIdInt = "INT"; + public const string AchievmentIdFloat = "FLOAT"; + + [OneTimeSetUp] + public void OneTtimeSetup() + { + Achievment.BackendLabel = backendLabel; + Achievment.ConfigLabel = configLabel; + } + + [TearDown] + public void TearDown() + { + Achievment.NullifyInstance(); + if (File.Exists(saveLocation)) + { + File.Delete(saveLocation); + } + } + + [Test] + public void ReadWrite() + { + //We assume that the achievments are set correctly because otherwise other tests would fail + Achievment.SetToggleAchivment(AchievmentIdToggle); + Achievment.SetIntProgress(AchievmentIdInt, 20); + Achievment.SetFloatProgress(AchievmentIdFloat, 20); + + //We need to assume NullifyInstance works correctly because we dont have access to an AchievmentManager which has not syncked yet + Achievment.NullifyInstance(); + + Assert.IsTrue(Achievment.IsCompleted(AchievmentIdToggle)); + Assert.AreEqual(Achievment.GetIntProgress(AchievmentIdInt), 20); + Assert.AreEqual(Achievment.GetFloatProgress(AchievmentIdFloat), 20, 0f); + + Achievment.SetIntProgress(AchievmentIdInt, 30); + Achievment.SetFloatProgress(AchievmentIdFloat, 30); + + Achievment.NullifyInstance(); + + Assert.AreEqual(Achievment.GetIntProgress(AchievmentIdInt), 30); + Assert.AreEqual(Achievment.GetFloatProgress(AchievmentIdFloat), 30, 0f); + + } + } +} \ No newline at end of file diff --git a/Achievments/PlaymodeTests/BackendTests.cs.meta b/Achievments/PlaymodeTests/BackendTests.cs.meta new file mode 100644 index 0000000..a543b95 --- /dev/null +++ b/Achievments/PlaymodeTests/BackendTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c09ce9536c2c5f541bb7d07d5eca1d69 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Achievments/PlaymodeTests/NEG.Utils.Achievments.Tests.Playmode.asmdef b/Achievments/PlaymodeTests/NEG.Utils.Achievments.Tests.Playmode.asmdef new file mode 100644 index 0000000..f8c24bd --- /dev/null +++ b/Achievments/PlaymodeTests/NEG.Utils.Achievments.Tests.Playmode.asmdef @@ -0,0 +1,22 @@ +{ + "name": "NEG.Utils.Achievments.Tests.Playmode", + "rootNamespace": "", + "references": [ + "UnityEngine.TestRunner", + "UnityEditor.TestRunner", + "NEG.Utils.Achivments" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": true, + "precompiledReferences": [ + "nunit.framework.dll" + ], + "autoReferenced": false, + "defineConstraints": [ + "UNITY_INCLUDE_TESTS" + ], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Achievments/PlaymodeTests/NEG.Utils.Achievments.Tests.Playmode.asmdef.meta b/Achievments/PlaymodeTests/NEG.Utils.Achievments.Tests.Playmode.asmdef.meta new file mode 100644 index 0000000..1bb12eb --- /dev/null +++ b/Achievments/PlaymodeTests/NEG.Utils.Achievments.Tests.Playmode.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 38e8b1e483202e14182d34baaea3958e +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Achievments/PlaymodeTests/TestAssets.meta b/Achievments/PlaymodeTests/TestAssets.meta new file mode 100644 index 0000000..ebead8c --- /dev/null +++ b/Achievments/PlaymodeTests/TestAssets.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: fad16eb700fc70c408c359dca9a76fc9 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Achievments/PlaymodeTests/TestAssets/TestLocalBackend.asset b/Achievments/PlaymodeTests/TestAssets/TestLocalBackend.asset new file mode 100644 index 0000000..5cb8195 --- /dev/null +++ b/Achievments/PlaymodeTests/TestAssets/TestLocalBackend.asset @@ -0,0 +1,15 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 6a1257a87feec064697193df412554d4, type: 3} + m_Name: TestLocalBackend + m_EditorClassIdentifier: + saveLocation: ./LocalAchievments/Tests.json diff --git a/Achievments/PlaymodeTests/TestAssets/TestLocalBackend.asset.meta b/Achievments/PlaymodeTests/TestAssets/TestLocalBackend.asset.meta new file mode 100644 index 0000000..dd21e44 --- /dev/null +++ b/Achievments/PlaymodeTests/TestAssets/TestLocalBackend.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 47c9689c811dc9842a5a5e9ca19c6e3c +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Achievments/TODO.txt b/Achievments/TODO.txt index bce4482..ca82cc6 100644 --- a/Achievments/TODO.txt +++ b/Achievments/TODO.txt @@ -1,5 +1,5 @@ -Static Achievments class +Static Achievments class (done) Implement Storage again API (done) -Fix typos +Fix typos Merge AchievmentCollection with AchievmentManagerConfig (done) -Static backend constructors \ No newline at end of file +Static backend constructors (done) \ No newline at end of file