diff --git a/CHANGELOG.md b/CHANGELOG.md index c345b8a..3ce7ba0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All package updates & migration steps will be listed in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [4.5.1] - 2025-04-29 +### Fixes +- Fix support for multiple parameter managers initalized at once. + ## [4.5.0] - 2025-03-18 ### Added - `InfoExists` and `HasAssignedValue` getters added to the `ParameterReference` diff --git a/Editor/CodeGeneration/Util/CodeGenerator.cs b/Editor/CodeGeneration/Util/CodeGenerator.cs index 59d4510..23d49a9 100644 --- a/Editor/CodeGeneration/Util/CodeGenerator.cs +++ b/Editor/CodeGeneration/Util/CodeGenerator.cs @@ -251,6 +251,7 @@ public static string GenerateFlatBufferClassFile(IParameterInterface parameterIn { "interfaceName", parameterInterface.InterfaceName }, { "flatBufferStructName", parameterInterface.FlatBufferStructName(false) }, { "propertyTypeDicts", propertyTypeDicts }, + { "parameterManagerType", nameof(IParameterManager) }, }; var fileName = parameterInterface.FlatBufferClassName(true); var filePath = Path.Combine(outputDirectory, fileName); diff --git a/Editor/Common/PropertyTypes/ParameterReferenceListPropertyType.cs b/Editor/Common/PropertyTypes/ParameterReferenceListPropertyType.cs index cb4a5db..4cecbea 100644 --- a/Editor/Common/PropertyTypes/ParameterReferenceListPropertyType.cs +++ b/Editor/Common/PropertyTypes/ParameterReferenceListPropertyType.cs @@ -49,10 +49,10 @@ public override string FlatBufferPropertyImplementationCode() $" if ({OverrideFieldName} != null)\n" + $" return new ReadOnlyListContainer>(\n" + $" () => {OverrideFieldName}.Length,\n" + - $" i => new ParameterReference<{_genericType.Name}>({OverrideFieldName}[i], true));\n" + + $" i => new ParameterReference<{_genericType.Name}>(_parameterManager, {OverrideFieldName}[i], true));\n" + $" return new ReadOnlyListContainer>(\n" + $" () => _fb.{FlatBufferStructPropertyName}Length,\n" + - $" i => new ParameterReference<{_genericType.Name}>(_fb.{FlatBufferStructPropertyName}(i)));\n" + + $" i => new ParameterReference<{_genericType.Name}>(_parameterManager, _fb.{FlatBufferStructPropertyName}(i)));\n" + $" }}\n" + $"}}"; } @@ -60,7 +60,7 @@ public override string FlatBufferPropertyImplementationCode() public override string CSVBridgeColumnTypeText => $"{_genericType.Name}[]"; public override string CSVBridgeReadFromCSVCode(string variableName) => - $"data.{FieldName} = {nameof(CSVValueConverter)}.ParameterReferenceArray.FromString<{_genericType.Name}>({variableName});"; + $"data.{FieldName} = {nameof(CSVValueConverter)}.ParameterReferenceArray.FromString<{_genericType.Name}>(parameterManager, {variableName});"; public override string CSVBridgeUpdateCSVRowCode(string variableName) => $"{variableName} = {nameof(CSVValueConverter)}.ParameterReferenceArray.ToString<{_genericType.Name}>(data.{FieldName});"; diff --git a/Editor/Common/PropertyTypes/ParameterReferencePropertyType.cs b/Editor/Common/PropertyTypes/ParameterReferencePropertyType.cs index 6269655..6fc4163 100644 --- a/Editor/Common/PropertyTypes/ParameterReferencePropertyType.cs +++ b/Editor/Common/PropertyTypes/ParameterReferencePropertyType.cs @@ -41,7 +41,7 @@ public override string FlatBufferPropertyImplementationCode() { var referenceClassName = SanitizedPropertyTypeName(); return - $"public {referenceClassName}<{_genericType.Name}> {PropertyName} => {OverrideFieldName} ?? new {referenceClassName}<{_genericType.Name}>(_fb.{FlatBufferStructPropertyName});"; + $"public {referenceClassName}<{_genericType.Name}> {PropertyName} => {OverrideFieldName} ?? new {referenceClassName}<{_genericType.Name}>(_parameterManager, _fb.{FlatBufferStructPropertyName});"; } public override string FlatBufferEditPropertyCode(string variableName) => @@ -74,6 +74,6 @@ void IPropertyType.DefineFlatBufferSchema(SchemaBuilder schemaBuilder, string ta } private string FromStringCode(string variableName) => - $"{nameof(CSVValueConverter)}.{TypeString()}.FromString<{_genericType.Name}>({variableName})"; + $"{nameof(CSVValueConverter)}.{TypeString()}.FromString<{_genericType.Name}>(parameterManager, {variableName})"; } } diff --git a/Editor/Common/PropertyTypes/ParameterStructReferenceListPropertyType.cs b/Editor/Common/PropertyTypes/ParameterStructReferenceListPropertyType.cs index 9cb424e..8b9e8ff 100644 --- a/Editor/Common/PropertyTypes/ParameterStructReferenceListPropertyType.cs +++ b/Editor/Common/PropertyTypes/ParameterStructReferenceListPropertyType.cs @@ -62,10 +62,10 @@ public override string FlatBufferPropertyImplementationCode() $" if ({OverrideFieldName} != null)\n" + $" return new ReadOnlyListContainer>(\n" + $" () => {OverrideFieldName}.Length,\n" + - $" i => new ParameterStructReferenceRuntime<{_genericType.Name}>({OverrideFieldName}[i]));\n" + + $" i => new ParameterStructReferenceRuntime<{_genericType.Name}>(_parameterManager, {OverrideFieldName}[i]));\n" + $" return new ReadOnlyListContainer>(\n" + $" () => _fb.{FlatBufferStructPropertyName}Length,\n" + - $" i => new ParameterStructReferenceRuntime<{_genericType.Name}>(_fb.{FlatBufferStructPropertyName}(i)));\n" + + $" i => new ParameterStructReferenceRuntime<{_genericType.Name}>(_parameterManager, _fb.{FlatBufferStructPropertyName}(i)));\n" + $" }}\n" + $"}}"; } diff --git a/Editor/Common/PropertyTypes/ParameterStructReferencePropertyType.cs b/Editor/Common/PropertyTypes/ParameterStructReferencePropertyType.cs index ab8ce19..da252d6 100644 --- a/Editor/Common/PropertyTypes/ParameterStructReferencePropertyType.cs +++ b/Editor/Common/PropertyTypes/ParameterStructReferencePropertyType.cs @@ -41,7 +41,7 @@ public override string FlatBufferPropertyImplementationCode() { var referenceClassName = SanitizedPropertyTypeName(); return - $"public {referenceClassName}<{_genericType.Name}> {PropertyName} => new ParameterStructReferenceRuntime<{_genericType.Name}>(_fb.{FlatBufferStructPropertyName});"; + $"public {referenceClassName}<{_genericType.Name}> {PropertyName} => new ParameterStructReferenceRuntime<{_genericType.Name}>(_parameterManager, _fb.{FlatBufferStructPropertyName});"; } public override string FlatBufferEditPropertyCode(string variableName) => diff --git a/Editor/Common/Templates/CSVBridge_Info.template b/Editor/Common/Templates/CSVBridge_Info.template index 9cc5023..3aebed3 100644 --- a/Editor/Common/Templates/CSVBridge_Info.template +++ b/Editor/Common/Templates/CSVBridge_Info.template @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.IO; +using PocketGems.Parameters; using PocketGems.Parameters.Common.DataTypes.Editor; using PocketGems.Parameters.DataGeneration.LocalCSV.Editor; using PocketGems.Parameters.DataGeneration.Util.Editor; @@ -57,6 +58,9 @@ namespace {{namespace}} { if (_scriptableObjectMetadatas == null) return; + // parameter manager is used for the Info/Struct references but doesn't need to be + // assigned since we're only reading for serialization, not runtime + IParameterManager parameterManager = null; var keyPathBuilder = new StructKeyPathBuilder(); var csvFile = _infoCSVFileCache.Load<{{interface.InterfaceName}}>(); _currentCSVFile = csvFile; diff --git a/Editor/Common/Templates/CSVBridge_Struct.template b/Editor/Common/Templates/CSVBridge_Struct.template index 7ce70d9..55d3c42 100644 --- a/Editor/Common/Templates/CSVBridge_Struct.template +++ b/Editor/Common/Templates/CSVBridge_Struct.template @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.IO; +using PocketGems.Parameters; using PocketGems.Parameters.Common.DataTypes.Editor; using PocketGems.Parameters.DataGeneration.LocalCSV.Editor; using PocketGems.Parameters.DataGeneration.Util.Editor; @@ -58,6 +59,9 @@ namespace {{namespace}} { try { + // parameter manager is used for the Info/Struct references but doesn't need to be + // assigned since we're only reading for serialization, not runtime + IParameterManager parameterManager = null; var keyPath = keyPathBuilder.KeyPath(); _currentStructPath = keyPath; var csvRow = _structCSVFileCache.LoadRow<{{interface.InterfaceName}}>(keyPath); diff --git a/Editor/Common/Templates/DataLoader.template b/Editor/Common/Templates/DataLoader.template index 3bbeded..e138eae 100644 --- a/Editor/Common/Templates/DataLoader.template +++ b/Editor/Common/Templates/DataLoader.template @@ -35,7 +35,7 @@ namespace {{namespace}} for (int i = 0; i < infoLength; i++) { var parameter = collection.{{interface.FlatBufferStructName}}(i).Value; - var wrapper = new {{interface.FlatBufferClassName}}(parameter); + var wrapper = new {{interface.FlatBufferClassName}}(parameterManager, parameter); var identifier = wrapper.Identifier; // use the Identifier from the wrapper to pre-cache the field var guid = parameter.GUID; {{~for baseInterface in interface.BaseInterfaceNames~}} @@ -55,7 +55,7 @@ namespace {{namespace}} for (int i = 0; i < structLength; i++) { var parameter = collection.{{interface.FlatBufferStructName}}(i).Value; - var wrapper = new {{interface.FlatBufferClassName}}(parameter); + var wrapper = new {{interface.FlatBufferClassName}}(parameterManager, parameter); parameterManager.Load<{{interface.InterfaceName}}, {{interface.FlatBufferClassName}}>(wrapper, parameter.GUID); } {{~end~}} diff --git a/Editor/Common/Templates/FlatBufferClass.template b/Editor/Common/Templates/FlatBufferClass.template index 0fdf3df..b6ab323 100644 --- a/Editor/Common/Templates/FlatBufferClass.template +++ b/Editor/Common/Templates/FlatBufferClass.template @@ -21,6 +21,7 @@ namespace {{namespace}} { #region private fields + private {{parameterManagerType}} _parameterManager; private {{flatBufferStructName}} _fb; {{~ for propertyTypeDict in propertyTypeDicts @@ -38,9 +39,10 @@ end #endregion - public {{className}}({{flatBufferStructName}} fb) + public {{className}}({{parameterManagerType}} parameterManager, {{flatBufferStructName}} fb) { _fb = fb; + _parameterManager = parameterManager; } {{~if isInfo~}} @@ -60,7 +62,7 @@ end #region mutation - public bool EditProperty(string propertyName, string value, out string error) + public bool EditProperty(IParameterManager parameterManager, string propertyName, string value, out string error) { error = null; try diff --git a/Runtime/Interface/IMutableParameter.cs b/Runtime/Interface/IMutableParameter.cs index 2a2c76b..d844798 100644 --- a/Runtime/Interface/IMutableParameter.cs +++ b/Runtime/Interface/IMutableParameter.cs @@ -2,7 +2,7 @@ namespace PocketGems.Parameters.Interface { public interface IMutableParameter { - bool EditProperty(string propertyName, string value, out string error); + bool EditProperty(IParameterManager parameterManager, string propertyName, string value, out string error); void RemoveAllEdits(); } } diff --git a/Runtime/LocalCSV/CSVValueConverter.cs b/Runtime/LocalCSV/CSVValueConverter.cs index dcbef36..95f21e4 100644 --- a/Runtime/LocalCSV/CSVValueConverter.cs +++ b/Runtime/LocalCSV/CSVValueConverter.cs @@ -239,11 +239,11 @@ public static string ToString(ParameterReference value) } #endif - public static ParameterReference FromString(string value) + public static ParameterReference FromString(IParameterManager parameterManager, string value) where T : class, IBaseInfo { if (string.IsNullOrWhiteSpace(value)) - return new ParameterReference(); + return new ParameterReference(parameterManager); #if UNITY_EDITOR if (ScriptableObjectLookupCache.Enabled) { @@ -254,7 +254,7 @@ public static ParameterReference FromString(string value) var asset = AssetDatabase.LoadAssetAtPath(path); if (asset != null && asset.name == value && asset is T) { - return new ParameterReference(data[i].Item1); + return new ParameterReference(parameterManager, data[i].Item1); } } } @@ -268,7 +268,7 @@ public static ParameterReference FromString(string value) var asset = AssetDatabase.LoadAssetAtPath(path); if (asset != null && asset.name == value && asset is T) { - return new ParameterReference(guids[i]); + return new ParameterReference(parameterManager, guids[i]); } } } @@ -352,7 +352,7 @@ public static string ToString(ParameterReference[] value) } #endif - public static ParameterReference[] FromString(string value) + public static ParameterReference[] FromString(IParameterManager parameterManager, string value) where T : class, IBaseInfo { // must return a non null value so we can detect overriding of properties by checking non null @@ -361,7 +361,7 @@ public static ParameterReference[] FromString(string value) var strings = value.Split(ListDelimiter); var assetRefs = new ParameterReference[strings.Length]; for (int i = 0; i < strings.Length; i++) - assetRefs[i] = ParameterReference.FromString(strings[i]); + assetRefs[i] = ParameterReference.FromString(parameterManager, strings[i]); return assetRefs; } } diff --git a/Runtime/ParameterManager.cs b/Runtime/ParameterManager.cs index aa4205c..7d86f56 100644 --- a/Runtime/ParameterManager.cs +++ b/Runtime/ParameterManager.cs @@ -106,7 +106,7 @@ void Error(string error) } _overriddenParameters.Add(mutableParameter); - if (!mutableParameter.EditProperty(propertyName, value, out string error)) + if (!mutableParameter.EditProperty(this, propertyName, value, out string error)) Error( $"Error editing ({interfaceType})[{identifierOrGuid}] property [{propertyName}] with value [{value}]: {error}"); } diff --git a/Runtime/ParameterReference.cs b/Runtime/ParameterReference.cs index 41db739..81e23bd 100644 --- a/Runtime/ParameterReference.cs +++ b/Runtime/ParameterReference.cs @@ -41,29 +41,28 @@ protected ParameterReference(string value, bool isIdentifier) [Serializable] public class ParameterReference : ParameterReference, IComparable> where T : class, IBaseInfo { - public ParameterReference(string value = null, bool isIdentifier = false) : base(value, isIdentifier) + private readonly IParameterManager _parameterManager; + private readonly bool _createdWithConstructor; + + public ParameterReference(IParameterManager parameterManager, string value = null, bool isIdentifier = false) : base(value, isIdentifier) { + _parameterManager = parameterManager; + _createdWithConstructor = true; } + [Obsolete("Use other ParameterReference() constructor.")] + public ParameterReference(string value = null, bool isIdentifier = false) : this(Params.ParameterManager, value, isIdentifier) { } + public override string ToString() { - string description; - if (string.IsNullOrWhiteSpace(guid) && string.IsNullOrWhiteSpace(_identifier)) - { - description = "not assigned"; - } - else if (Info == null) - { - if (!string.IsNullOrWhiteSpace(_identifier)) - description = $"missing asset for identifier {_identifier}"; - else - description = $"missing asset for GUID {guid}"; - } + string description = null; + if (!string.IsNullOrWhiteSpace(guid) && string.IsNullOrWhiteSpace(_identifier)) + description = $"guid: {guid}"; + else if (!string.IsNullOrWhiteSpace(_identifier)) + description = $"identifier: {_identifier}"; else - { - description = $"{Info.Identifier}({guid})"; - } - return $"{GetType().Name}<{typeof(T).Name}>: {description}"; + description = "not assigned"; + return $"{GetType().Name}<{typeof(T).Name}> {description}"; } public int CompareTo(ParameterReference other) @@ -100,13 +99,24 @@ public T Info return GetInfo(EditorParams.ParameterManager); } #endif - if (Params.ParameterManager == null) + + var parameterManager = _parameterManager; + if (parameterManager == null) { - Debug.LogError("Fetching Info before ParamsSetup.Setup() has been called."); - return null; + if (_createdWithConstructor) + { + Debug.LogError($"No {nameof(_parameterManager)} set in {nameof(ParameterReference)}<{typeof(T).Name}>"); + return null; + } + if (Params.ParameterManager == null) + { + Debug.LogError("Fetching Info before ParamsSetup.Setup() has been called."); + return null; + } + parameterManager = Params.ParameterManager; } - return GetInfo(Params.ParameterManager); + return GetInfo(parameterManager); } } diff --git a/Runtime/ParameterStructReference.cs b/Runtime/ParameterStructReference.cs index 9804ddf..c0d8d29 100644 --- a/Runtime/ParameterStructReference.cs +++ b/Runtime/ParameterStructReference.cs @@ -1,3 +1,4 @@ +using System; using PocketGems.Parameters.Interface; using UnityEngine; #if UNITY_EDITOR @@ -19,8 +20,11 @@ protected ParameterStructReference(string guid) public abstract class ParameterStructReference : ParameterStructReference where T : class, IBaseStruct { - protected ParameterStructReference(string guid) : base(guid) + private readonly IParameterManager _parameterManager; + + protected ParameterStructReference(IParameterManager parameterManager, string guid) : base(guid) { + _parameterManager = parameterManager; } public T Struct @@ -35,13 +39,7 @@ public T Struct return GetStruct(EditorParams.ParameterManager); } #endif - if (Params.ParameterManager == null) - { - Debug.LogError("Fetching Struct before ParamsSetup.Setup() has been called."); - return null; - } - - return GetStruct(Params.ParameterManager); + return GetStruct(_parameterManager); } } @@ -51,9 +49,9 @@ public T Struct #if UNITY_EDITOR public class ParameterStructReferenceEditor : ParameterStructReference where T : class, IBaseStruct { - private T _struct; + private readonly T _struct; - public ParameterStructReferenceEditor(T @struct) : base(null) + public ParameterStructReferenceEditor(T @struct) : base(null, null) { _struct = @struct; } @@ -64,10 +62,17 @@ public ParameterStructReferenceEditor(T @struct) : base(null) public class ParameterStructReferenceRuntime : ParameterStructReference where T : class, IBaseStruct { - public ParameterStructReferenceRuntime(string guid) : base(guid) + public ParameterStructReferenceRuntime(IParameterManager parameterManager, string guid) : base(parameterManager, guid) { + if (parameterManager == null) + { + Debug.LogError($"Must provide {nameof(IParameterManager)} when constructing {nameof(ParameterStructReference)}."); + } } + [Obsolete("Use other ParameterStructReferenceRuntime() constructor.")] + public ParameterStructReferenceRuntime(string guid) : this(Params.ParameterManager, guid) { } + public override string ToString() { string description = _guid; @@ -84,6 +89,6 @@ public override string ToString() } - internal override T GetStruct(IParameterManager parameterManager) => parameterManager.GetStructWithGuid(_guid); + internal override T GetStruct(IParameterManager parameterManager) => parameterManager?.GetStructWithGuid(_guid); } } diff --git a/Tests/Runtime/ParameterManagerTest.cs b/Tests/Runtime/ParameterManagerTest.cs index 28950f1..6826f1c 100644 --- a/Tests/Runtime/ParameterManagerTest.cs +++ b/Tests/Runtime/ParameterManagerTest.cs @@ -27,53 +27,53 @@ public class ParameterManagerTest private const string StructGuid = "guid3"; - private ParameterManager _pm; + private ParameterManager _parameterManager; [SetUp] public void SetUp() { + _parameterManager = new ParameterManager(); _mockMySpecialInfo = new MockMySpecialInfo(); _mockMyVerySpecialInfo = new MockMyVerySpecialInfo(); - _mockKeyValueStruct = new MockKeyValueStruct("desc", 10, "guid", new string[0]); - _pm = new ParameterManager(); + _mockKeyValueStruct = new MockKeyValueStruct(_parameterManager, "desc", 10, "guid", new string[0]); } private void LoadInfos() { - _pm.Load(_mockKeyValueStruct, StructGuid); + _parameterManager.Load(_mockKeyValueStruct, StructGuid); // load under one interface - _pm.Load(_mockMySpecialInfo, SpecialId, SpecialGuid); + _parameterManager.Load(_mockMySpecialInfo, SpecialId, SpecialGuid); // load under two interfaces - _pm.Load(_mockMyVerySpecialInfo, VerySpecialId, VerySpecialGuid); - _pm.Load(_mockMyVerySpecialInfo, VerySpecialId, VerySpecialGuid); + _parameterManager.Load(_mockMyVerySpecialInfo, VerySpecialId, VerySpecialGuid); + _parameterManager.Load(_mockMyVerySpecialInfo, VerySpecialId, VerySpecialGuid); } private void AssertEmptyManager() { - Assert.IsNull(_pm.Get(SpecialId)); - Assert.IsNull(_pm.Get(SpecialId)); - Assert.IsNull(_pm.Get(SpecialId, typeof(IMySpecialInfo))); - Assert.IsNull(_pm.Get(SpecialId, typeof(IMyVerySpecialInfo))); + Assert.IsNull(_parameterManager.Get(SpecialId)); + Assert.IsNull(_parameterManager.Get(SpecialId)); + Assert.IsNull(_parameterManager.Get(SpecialId, typeof(IMySpecialInfo))); + Assert.IsNull(_parameterManager.Get(SpecialId, typeof(IMyVerySpecialInfo))); - Assert.IsNull(_pm.Get(VerySpecialId)); - Assert.IsNull(_pm.Get(VerySpecialId)); - Assert.IsNull(_pm.Get(VerySpecialId, typeof(IMySpecialInfo))); - Assert.IsNull(_pm.Get(VerySpecialId, typeof(IMyVerySpecialInfo))); + Assert.IsNull(_parameterManager.Get(VerySpecialId)); + Assert.IsNull(_parameterManager.Get(VerySpecialId)); + Assert.IsNull(_parameterManager.Get(VerySpecialId, typeof(IMySpecialInfo))); + Assert.IsNull(_parameterManager.Get(VerySpecialId, typeof(IMyVerySpecialInfo))); LogAssert.Expect(LogType.Error, new Regex(".*")); - Assert.IsNull(_pm.GetWithGUID(SpecialGuid)); + Assert.IsNull(_parameterManager.GetWithGUID(SpecialGuid)); LogAssert.Expect(LogType.Error, new Regex(".*")); - Assert.IsNull(_pm.GetWithGUID(SpecialGuid)); + Assert.IsNull(_parameterManager.GetWithGUID(SpecialGuid)); LogAssert.Expect(LogType.Error, new Regex(".*")); - Assert.IsNull(_pm.GetWithGUID(VerySpecialGuid)); + Assert.IsNull(_parameterManager.GetWithGUID(VerySpecialGuid)); LogAssert.Expect(LogType.Error, new Regex(".*")); - Assert.IsNull(_pm.GetWithGUID(VerySpecialGuid)); + Assert.IsNull(_parameterManager.GetWithGUID(VerySpecialGuid)); LogAssert.Expect(LogType.Error, new Regex(".*")); - Assert.IsNull(_pm.GetStructWithGuid(StructGuid)); + Assert.IsNull(_parameterManager.GetStructWithGuid(StructGuid)); } [Test] @@ -82,22 +82,22 @@ public void LoadAndGet() AssertEmptyManager(); LoadInfos(); - Assert.AreEqual(_mockMySpecialInfo, _pm.Get(SpecialId)); - Assert.AreEqual(_mockMySpecialInfo, _pm.Get(SpecialId, typeof(IMySpecialInfo))); - Assert.IsNull(_pm.Get(SpecialId)); - Assert.IsNull(_pm.Get(SpecialId, typeof(IMyVerySpecialInfo))); + Assert.AreEqual(_mockMySpecialInfo, _parameterManager.Get(SpecialId)); + Assert.AreEqual(_mockMySpecialInfo, _parameterManager.Get(SpecialId, typeof(IMySpecialInfo))); + Assert.IsNull(_parameterManager.Get(SpecialId)); + Assert.IsNull(_parameterManager.Get(SpecialId, typeof(IMyVerySpecialInfo))); - Assert.AreEqual(_mockMyVerySpecialInfo, _pm.Get(VerySpecialId, typeof(IMySpecialInfo))); - Assert.AreEqual(_mockMyVerySpecialInfo, _pm.Get(VerySpecialId, typeof(IMyVerySpecialInfo))); - Assert.AreEqual(_mockMyVerySpecialInfo, _pm.Get(VerySpecialId)); - Assert.AreEqual(_mockMyVerySpecialInfo, _pm.Get(VerySpecialId)); + Assert.AreEqual(_mockMyVerySpecialInfo, _parameterManager.Get(VerySpecialId, typeof(IMySpecialInfo))); + Assert.AreEqual(_mockMyVerySpecialInfo, _parameterManager.Get(VerySpecialId, typeof(IMyVerySpecialInfo))); + Assert.AreEqual(_mockMyVerySpecialInfo, _parameterManager.Get(VerySpecialId)); + Assert.AreEqual(_mockMyVerySpecialInfo, _parameterManager.Get(VerySpecialId)); - Assert.AreEqual(_mockMySpecialInfo, _pm.GetWithGUID(SpecialGuid)); + Assert.AreEqual(_mockMySpecialInfo, _parameterManager.GetWithGUID(SpecialGuid)); LogAssert.Expect(LogType.Error, new Regex(".*")); - Assert.IsNull(_pm.GetWithGUID(SpecialGuid)); + Assert.IsNull(_parameterManager.GetWithGUID(SpecialGuid)); - Assert.AreEqual(_mockMyVerySpecialInfo, _pm.GetWithGUID(VerySpecialGuid)); - Assert.AreEqual(_mockMyVerySpecialInfo, _pm.GetWithGUID(VerySpecialGuid)); + Assert.AreEqual(_mockMyVerySpecialInfo, _parameterManager.GetWithGUID(VerySpecialGuid)); + Assert.AreEqual(_mockMyVerySpecialInfo, _parameterManager.GetWithGUID(VerySpecialGuid)); } [Test] @@ -106,10 +106,10 @@ public void LoadAndGetWithStruct() AssertEmptyManager(); LoadInfos(); - Assert.AreEqual(_mockKeyValueStruct, _pm.GetStructWithGuid(StructGuid)); + Assert.AreEqual(_mockKeyValueStruct, _parameterManager.GetStructWithGuid(StructGuid)); LogAssert.Expect(LogType.Error, new Regex(".*")); - Assert.IsNull(_pm.GetStructWithGuid("non existing guid")); + Assert.IsNull(_parameterManager.GetStructWithGuid("non existing guid")); } [Test] @@ -117,19 +117,19 @@ public void InvalidUsesWithIBaseInfo() { LoadInfos(); LogAssert.Expect(LogType.Error, BaseInfoErrorMsg); - Assert.IsNull(_pm.Get("some_identifier")); + Assert.IsNull(_parameterManager.Get("some_identifier")); LogAssert.Expect(LogType.Error, BaseInterfaceErrorMsg); - Assert.IsNull(_pm.GetWithGUID("some_guid")); + Assert.IsNull(_parameterManager.GetWithGUID("some_guid")); LogAssert.Expect(LogType.Error, BaseInfoErrorMsg); - Assert.AreEqual(0, _pm.Get().ToArray().Length); + Assert.AreEqual(0, _parameterManager.Get().ToArray().Length); LogAssert.Expect(LogType.Error, BaseInfoErrorMsg); - Assert.AreEqual(0, _pm.GetSorted().ToArray().Length); + Assert.AreEqual(0, _parameterManager.GetSorted().ToArray().Length); LogAssert.Expect(LogType.Error, BaseInfoErrorMsg); - _pm.Load(null, "some_id", "some guid"); + _parameterManager.Load(null, "some_id", "some guid"); } [Test] @@ -138,10 +138,10 @@ public void InvalidUsesWithIBaseStruct() LoadInfos(); LogAssert.Expect(LogType.Error, BaseInterfaceErrorMsg); - Assert.IsNull(_pm.GetStructWithGuid("some_guid")); + Assert.IsNull(_parameterManager.GetStructWithGuid("some_guid")); LogAssert.Expect(LogType.Error, BaseStructErrorMsg); - _pm.Load(null, "some guid"); + _parameterManager.Load(null, "some guid"); } [Test] @@ -150,12 +150,12 @@ public void GetIEnumerable() AssertEmptyManager(); LoadInfos(); - var specialInfos = _pm.Get().ToList(); + var specialInfos = _parameterManager.Get().ToList(); Assert.AreEqual(2, specialInfos.Count); Assert.IsTrue(specialInfos.Contains(_mockMySpecialInfo)); Assert.IsTrue(specialInfos.Contains(_mockMyVerySpecialInfo)); - var verySpecialInfos = _pm.Get().ToList(); + var verySpecialInfos = _parameterManager.Get().ToList(); Assert.AreEqual(1, verySpecialInfos.Count); Assert.AreEqual(_mockMyVerySpecialInfo, verySpecialInfos[0]); } @@ -168,12 +168,12 @@ public void GetSortedIEnumerable() var info3 = new MockMySpecialInfo { Identifier = "a" }; var info4 = new MockMySpecialInfo { Identifier = "1" }; - _pm.Load(info1, info1.Identifier, "guid1"); - _pm.Load(info2, info2.Identifier, "guid2"); - _pm.Load(info3, info3.Identifier, "guid3"); - _pm.Load(info4, info4.Identifier, "guid4"); + _parameterManager.Load(info1, info1.Identifier, "guid1"); + _parameterManager.Load(info2, info2.Identifier, "guid2"); + _parameterManager.Load(info3, info3.Identifier, "guid3"); + _parameterManager.Load(info4, info4.Identifier, "guid4"); - var infos = _pm.GetSorted().ToArray(); + var infos = _parameterManager.GetSorted().ToArray(); Assert.AreEqual(4, infos.Length); Assert.AreEqual(info2, infos[0]); Assert.AreEqual(info4, infos[1]); @@ -186,31 +186,31 @@ public void LoadUpdatedObject() { LoadInfos(); - Assert.AreEqual(_mockMyVerySpecialInfo, _pm.Get(VerySpecialId)); - Assert.AreEqual(_mockMyVerySpecialInfo, _pm.Get(VerySpecialId, typeof(IMySpecialInfo))); - Assert.AreEqual(_mockMyVerySpecialInfo, _pm.GetWithGUID(VerySpecialGuid)); - Assert.AreEqual(_mockKeyValueStruct, _pm.GetStructWithGuid(StructGuid)); + Assert.AreEqual(_mockMyVerySpecialInfo, _parameterManager.Get(VerySpecialId)); + Assert.AreEqual(_mockMyVerySpecialInfo, _parameterManager.Get(VerySpecialId, typeof(IMySpecialInfo))); + Assert.AreEqual(_mockMyVerySpecialInfo, _parameterManager.GetWithGUID(VerySpecialGuid)); + Assert.AreEqual(_mockKeyValueStruct, _parameterManager.GetStructWithGuid(StructGuid)); // override an existing loaded object // load new object with the same identifier & guid var newInfo = new MockMyVerySpecialInfo(); - _pm.Load(newInfo, VerySpecialId, VerySpecialGuid); - _pm.Load(newInfo, VerySpecialId, VerySpecialGuid); + _parameterManager.Load(newInfo, VerySpecialId, VerySpecialGuid); + _parameterManager.Load(newInfo, VerySpecialId, VerySpecialGuid); - var newStruct = new MockKeyValueStruct("new struct", 100, "guid", new string[0]); - _pm.Load(newStruct, StructGuid); + var newStruct = new MockKeyValueStruct(_parameterManager, "new struct", 100, "guid", new string[0]); + _parameterManager.Load(newStruct, StructGuid); - Assert.AreEqual(newInfo, _pm.Get(VerySpecialId)); - Assert.AreEqual(newInfo, _pm.Get(VerySpecialId, typeof(IMySpecialInfo))); - Assert.AreEqual(newInfo, _pm.GetWithGUID(VerySpecialGuid)); - Assert.AreEqual(newStruct, _pm.GetStructWithGuid(StructGuid)); + Assert.AreEqual(newInfo, _parameterManager.Get(VerySpecialId)); + Assert.AreEqual(newInfo, _parameterManager.Get(VerySpecialId, typeof(IMySpecialInfo))); + Assert.AreEqual(newInfo, _parameterManager.GetWithGUID(VerySpecialGuid)); + Assert.AreEqual(newStruct, _parameterManager.GetStructWithGuid(StructGuid)); - var specialInfos = _pm.Get().ToList(); + var specialInfos = _parameterManager.Get().ToList(); Assert.AreEqual(2, specialInfos.Count); Assert.IsTrue(specialInfos.Contains(_mockMySpecialInfo)); Assert.IsTrue(specialInfos.Contains(newInfo)); - var verySpecialInfos = _pm.Get().ToList(); + var verySpecialInfos = _parameterManager.Get().ToList(); Assert.AreEqual(1, verySpecialInfos.Count); Assert.AreEqual(newInfo, verySpecialInfos[0]); } @@ -226,10 +226,10 @@ public void LoadUpdatedObject_GuidChange() var newGuid = "myNewGuid"; LogAssert.Expect(LogType.Error, new Regex(".*")); - _pm.Load(newInfo, VerySpecialId, newGuid); + _parameterManager.Load(newInfo, VerySpecialId, newGuid); LogAssert.Expect(LogType.Error, new Regex(".*")); - _pm.Load(newInfo, VerySpecialId, newGuid); + _parameterManager.Load(newInfo, VerySpecialId, newGuid); } [Test] @@ -243,10 +243,10 @@ public void LoadUpdatedObject_IdentifierChange() var newIdentifier = "myNewIdentifier"; LogAssert.Expect(LogType.Error, new Regex(".*")); - _pm.Load(newInfo, newIdentifier, VerySpecialGuid); + _parameterManager.Load(newInfo, newIdentifier, VerySpecialGuid); LogAssert.Expect(LogType.Error, new Regex(".*")); - _pm.Load(newInfo, newIdentifier, VerySpecialGuid); + _parameterManager.Load(newInfo, newIdentifier, VerySpecialGuid); } [Test] @@ -254,7 +254,7 @@ public void RemoveAll() { LoadInfos(); - _pm.RemoveAll(); + _parameterManager.RemoveAll(); AssertEmptyManager(); } @@ -264,11 +264,11 @@ public void ApplyOverrides_BaseCases() { LoadInfos(); IReadOnlyList errors; - var success = _pm.ApplyOverrides(null, out errors); + var success = _parameterManager.ApplyOverrides(null, out errors); Assert.IsTrue(success); Assert.IsNull(errors); - success = _pm.ApplyOverrides(JObject.Parse("{}"), out errors); + success = _parameterManager.ApplyOverrides(JObject.Parse("{}"), out errors); Assert.IsTrue(success); Assert.IsNull(errors); @@ -284,29 +284,29 @@ public void ApplyOverrides_ErrorChecking() bool success; IReadOnlyList errors; - success = _pm.ApplyOverrides(JObject.Parse("{\"delete\":[]}"), out errors); + success = _parameterManager.ApplyOverrides(JObject.Parse("{\"delete\":[]}"), out errors); Assert.IsFalse(success); Assert.AreEqual(1, errors.Count); Assert.AreEqual("Override action delete isn't supported yet.", errors[0]); - success = _pm.ApplyOverrides(JObject.Parse("{\"add\":[]}"), out errors); + success = _parameterManager.ApplyOverrides(JObject.Parse("{\"add\":[]}"), out errors); Assert.IsFalse(success); Assert.AreEqual(1, errors.Count); Assert.AreEqual("Override action add isn't supported yet.", errors[0]); - success = _pm.ApplyOverrides(JObject.Parse("{\"jump\":[]}"), out errors); + success = _parameterManager.ApplyOverrides(JObject.Parse("{\"jump\":[]}"), out errors); Assert.IsFalse(success); Assert.AreEqual(1, errors.Count); Assert.AreEqual("Override action jump isn't valid.", errors[0]); - success = _pm.ApplyOverrides(JObject.Parse("{\"edit\":[[\"hello\"]]}"), out errors); + success = _parameterManager.ApplyOverrides(JObject.Parse("{\"edit\":[[\"hello\"]]}"), out errors); Assert.IsFalse(success); Assert.AreEqual(1, errors.Count); Assert.AreEqual("ParameterManager expects 4 elements in the array of data.", errors[0]); LogAssert.Expect(LogType.Error, $"Missing: Cannot find parameter by GUID {SpecialId} for type IMySpecialInfo"); - success = _pm.ApplyOverrides(JObject.Parse("{\"edit\":" + + success = _parameterManager.ApplyOverrides(JObject.Parse("{\"edit\":" + "[" + " [\"MySpecialInfo.csv\"," + $" \"{SpecialId}\"," + @@ -326,7 +326,7 @@ public void ApplyOverrides_ErrorChecking() _mockMySpecialInfo.ReturnEditPropertyError = "some error"; - success = _pm.ApplyOverrides(JObject.Parse("{\"edit\":" + + success = _parameterManager.ApplyOverrides(JObject.Parse("{\"edit\":" + "[" + " [\"MySpecialInfo.csv\"," + $" \"{SpecialId}\"," + @@ -349,14 +349,14 @@ public void ApplyOverrides_ErrorChecking() public void ApplyAndClearOverrides() { LoadInfos(); - _pm.ClearAllOverrides(); + _parameterManager.ClearAllOverrides(); Assert.AreEqual(0, _mockMySpecialInfo.EditPropertyCalls); Assert.AreEqual(0, _mockMySpecialInfo.RemoveAllEditCalls); Assert.AreEqual(0, _mockMyVerySpecialInfo.EditPropertyCalls); Assert.AreEqual(0, _mockMyVerySpecialInfo.RemoveAllEditCalls); // apply to one row - var success = _pm.ApplyOverrides(JObject.Parse("{\"edit\":" + + var success = _parameterManager.ApplyOverrides(JObject.Parse("{\"edit\":" + "[" + " [\"MySpecialInfo.csv\"," + $" \"{SpecialId}\"," + @@ -385,7 +385,7 @@ public void ApplyAndClearOverrides() Assert.AreEqual("SomeColumnName2", _mockKeyValueStruct.EditPropertyPropertyName); Assert.AreEqual("SomeValue2", _mockKeyValueStruct.EditPropertyValue); - _pm.ClearAllOverrides(); + _parameterManager.ClearAllOverrides(); Assert.AreEqual(1, _mockMySpecialInfo.EditPropertyCalls); Assert.AreEqual(1, _mockMySpecialInfo.RemoveAllEditCalls); @@ -395,7 +395,7 @@ public void ApplyAndClearOverrides() Assert.AreEqual(0, _mockMyVerySpecialInfo.RemoveAllEditCalls); // apply to both rows - success = _pm.ApplyOverrides(JObject.Parse("{\"edit\":" + + success = _parameterManager.ApplyOverrides(JObject.Parse("{\"edit\":" + "[" + " [\"MySpecialInfo.csv\"," + $" \"{SpecialId}\"," + @@ -424,7 +424,7 @@ public void ApplyAndClearOverrides() Assert.AreEqual(1, _mockKeyValueStruct.EditPropertyCalls); Assert.AreEqual(1, _mockKeyValueStruct.RemoveAllEditCalls); - _pm.ClearAllOverrides(); + _parameterManager.ClearAllOverrides(); Assert.AreEqual(2, _mockMySpecialInfo.EditPropertyCalls); Assert.AreEqual(2, _mockMySpecialInfo.RemoveAllEditCalls); @@ -437,34 +437,34 @@ public void ApplyAndClearOverrides() [Test] public void UnsafeGetError() { - _pm.IsGettingSafe = false; + _parameterManager.IsGettingSafe = false; var regex = new Regex($".*{nameof(IParameterManager.IsGettingSafe)}.*"); LogAssert.Expect(LogType.Error, regex); - Assert.IsNull(_pm.Get(SpecialId)); - Assert.IsTrue(_pm.HasGetBeenCalled); + Assert.IsNull(_parameterManager.Get(SpecialId)); + Assert.IsTrue(_parameterManager.HasGetBeenCalled); LogAssert.Expect(LogType.Error, regex); - Assert.IsNull(_pm.Get(SpecialId, typeof(IMySpecialInfo))); + Assert.IsNull(_parameterManager.Get(SpecialId, typeof(IMySpecialInfo))); LogAssert.Expect(LogType.Error, regex); - Assert.IsEmpty(_pm.Get()); + Assert.IsEmpty(_parameterManager.Get()); LogAssert.Expect(LogType.Error, regex); LogAssert.Expect(LogType.Error, new Regex(".*")); - Assert.IsNull(_pm.GetWithGUID(SpecialGuid)); + Assert.IsNull(_parameterManager.GetWithGUID(SpecialGuid)); LogAssert.Expect(LogType.Error, regex); LogAssert.Expect(LogType.Error, new Regex(".*")); - Assert.IsNull(_pm.GetStructWithGuid(StructGuid)); + Assert.IsNull(_parameterManager.GetStructWithGuid(StructGuid)); LogAssert.Expect(LogType.Error, regex); - Assert.IsEmpty(_pm.GetSorted()); + Assert.IsEmpty(_parameterManager.GetSorted()); // no more errors - _pm.IsGettingSafe = true; - Assert.IsNull(_pm.Get(SpecialId)); + _parameterManager.IsGettingSafe = true; + Assert.IsNull(_parameterManager.Get(SpecialId)); } } } diff --git a/Tests/Runtime/ParameterReferenceTest.cs b/Tests/Runtime/ParameterReferenceTest.cs index 1e3eacd..73ca9d7 100644 --- a/Tests/Runtime/ParameterReferenceTest.cs +++ b/Tests/Runtime/ParameterReferenceTest.cs @@ -1,9 +1,11 @@ using System.Collections.Generic; +using System.IO; using System.Text.RegularExpressions; using NSubstitute; using NSubstitute.ReturnsExtensions; using NUnit.Framework; using PocketGems.Parameters.Interface; +using UnityEditor; using UnityEngine; using UnityEngine.TestTools; @@ -27,7 +29,6 @@ public void SetUp() _parameterManagerMock.GetWithGUID(kTestGuid).Returns(_mockInfo); _parameterManagerMock.Get(default).ReturnsNullForAnyArgs(); _parameterManagerMock.Get(kTestIdentifier).Returns(_mockInfo); - Params.SetInstance(_parameterManagerMock); } [TearDown] @@ -40,17 +41,17 @@ public void TearDown() [Test] public void Init() { - var parameterReference = new ParameterReference(); + var parameterReference = new ParameterReference(_parameterManagerMock); Assert.That(parameterReference.AssignedGUID, Is.Null); Assert.That(parameterReference.AssignedIdentifier, Is.Null); Assert.That(parameterReference.ToString(), Is.Not.Empty); - parameterReference = new ParameterReference(kTestGuid); + parameterReference = new ParameterReference(_parameterManagerMock, kTestGuid); Assert.That(parameterReference.AssignedGUID, Is.EqualTo(kTestGuid)); Assert.That(parameterReference.AssignedIdentifier, Is.Null); Assert.That(parameterReference.ToString(), Is.Not.Empty); - parameterReference = new ParameterReference(kTestIdentifier, true); + parameterReference = new ParameterReference(_parameterManagerMock, kTestIdentifier, true); Assert.That(parameterReference.AssignedGUID, Is.Null); Assert.That(parameterReference.AssignedIdentifier, Is.EqualTo(kTestIdentifier)); Assert.That(parameterReference.ToString(), Is.Not.Empty); @@ -59,35 +60,35 @@ public void Init() [Test] public void GetByGuid() { - var parameterReference = new ParameterReference(kTestGuid); + var parameterReference = new ParameterReference(_parameterManagerMock, kTestGuid); Assert.That(parameterReference.Info, Is.EqualTo(_mockInfo)); } [Test] public void GetByIdentifier() { - var parameterReference = new ParameterReference(kTestIdentifier, true); + var parameterReference = new ParameterReference(_parameterManagerMock, kTestIdentifier, true); Assert.That(parameterReference.Info, Is.EqualTo(_mockInfo)); } [Test] public void GetNothing() { - var parameterReference = new ParameterReference(); + var parameterReference = new ParameterReference(_parameterManagerMock); Assert.That(parameterReference.Info, Is.Null); } [Test] public void MissingGuidInParameterManager() { - var parameterReference = new ParameterReference("bad guid"); + var parameterReference = new ParameterReference(_parameterManagerMock, "bad guid"); Assert.That(parameterReference.Info, Is.Null); } [Test] public void MissingIdentifierInParameterManager() { - var parameterReference = new ParameterReference("bad id", true); + var parameterReference = new ParameterReference(_parameterManagerMock, "bad id", true); LogAssert.Expect(LogType.Error, new Regex("Cannot find.*")); Assert.That(parameterReference.Info, Is.Null); } @@ -95,20 +96,16 @@ public void MissingIdentifierInParameterManager() [Test] public void NonExistentParameterManager() { - Params.SetInstance(null); + const string errorString = "No _parameterManager set in ParameterReference"; - const string errorString = "Fetching Info before ParamsSetup.Setup() has been called."; - - var parameterReference = new ParameterReference(kTestGuid); + var parameterReference = new ParameterReference(null, kTestGuid); LogAssert.Expect(LogType.Error, errorString); Assert.That(parameterReference.Info, Is.Null); - LogAssert.Expect(LogType.Error, errorString); Assert.That(parameterReference.ToString(), Is.Not.Empty); - parameterReference = new ParameterReference(kTestIdentifier, true); + parameterReference = new ParameterReference(null, kTestIdentifier, true); LogAssert.Expect(LogType.Error, errorString); Assert.That(parameterReference.Info, Is.Null); - LogAssert.Expect(LogType.Error, errorString); Assert.That(parameterReference.ToString(), Is.Not.Empty); } @@ -121,7 +118,7 @@ ParameterReference CreateReference(string guid, string identifier, bo mockInfo.Identifier.Returns(identifier); _parameterManagerMock.GetWithGUID(guid).Returns(mockInfo); _parameterManagerMock.Get(identifier).Returns(mockInfo); - var reference = new ParameterReference(refIsIdentifier ? identifier : guid, refIsIdentifier); + var reference = new ParameterReference(_parameterManagerMock, refIsIdentifier ? identifier : guid, refIsIdentifier); Assert.AreEqual(identifier, reference.Info.Identifier); if (refIsIdentifier) { @@ -136,7 +133,7 @@ ParameterReference CreateReference(string guid, string identifier, bo return reference; } - var refNull = new ParameterReference(); + var refNull = new ParameterReference(_parameterManagerMock); var refA = CreateReference("def", "A", true); var refB = CreateReference("abc", "B", false); var refC = CreateReference("aaa", "C", true); @@ -166,7 +163,7 @@ ParameterReference CreateReference(string guid, string identifier, bo } // two nulls are equal - Assert.That(refNull.CompareTo(new ParameterReference()), Is.Zero); + Assert.That(refNull.CompareTo(new ParameterReference(_parameterManagerMock)), Is.Zero); } [Test] @@ -180,11 +177,29 @@ ParameterReference CreateReference(string guid, string identifier, bo [TestCase("blah", true, true, false)] public void InfoExists_HasAssignedValue(string value, bool isIdentifier, bool expectedHasAssignedValue, bool expectedInfoExists) { - var parameterReference = new ParameterReference(value, isIdentifier); + var parameterReference = new ParameterReference(_parameterManagerMock, value, isIdentifier); Assert.That(parameterReference.HasAssignedValue, Is.EqualTo(expectedHasAssignedValue)); if (isIdentifier && expectedHasAssignedValue && !expectedInfoExists) LogAssert.Expect(LogType.Error, new Regex("Cannot find info of type .*")); Assert.That(parameterReference.InfoExists, Is.EqualTo(expectedInfoExists)); } + + [Test] + public void Deserialization() + { + var parameterReference = new ParameterReference(_parameterManagerMock, kTestGuid); + string json = JsonUtility.ToJson(parameterReference); + var deserialized = JsonUtility.FromJson>(json); + + // unable to get info + LogAssert.Expect(LogType.Error, "Fetching Info before ParamsSetup.Setup() has been called."); + Assert.That(deserialized.Info, Is.Null); + + // deserialized references uses the global Params since this only happens when the Unity game is running. + Params.SetInstance(_parameterManagerMock); + + // able to get info + Assert.That(deserialized.Info, Is.EqualTo(_mockInfo)); + } } } diff --git a/Tests/Runtime/ParameterStructReferenceTest.cs b/Tests/Runtime/ParameterStructReferenceTest.cs index 34bee1d..6c0b5d6 100644 --- a/Tests/Runtime/ParameterStructReferenceTest.cs +++ b/Tests/Runtime/ParameterStructReferenceTest.cs @@ -32,7 +32,6 @@ public void SetUp() _parameterManagerMock = Substitute.For(); _parameterManagerMock.GetStructWithGuid(default).ReturnsNullForAnyArgs(); _parameterManagerMock.GetStructWithGuid(kTestGuid).Returns(_mockStruct); - Params.SetInstance(_parameterManagerMock); } [TearDown] @@ -45,7 +44,7 @@ public void TearDown() [Test] public void GetStruct() { - var reference = new ParameterStructReferenceRuntime(kTestGuid); + var reference = new ParameterStructReferenceRuntime(_parameterManagerMock, kTestGuid); Assert.AreEqual(_mockStruct, reference.Struct); } @@ -54,28 +53,18 @@ public void GetStruct() [TestCase(null)] public void MissingGuid(string guid) { - var reference = new ParameterStructReferenceRuntime(null); + var reference = new ParameterStructReferenceRuntime(_parameterManagerMock, null); Assert.IsNull(reference.Struct); } [Test] public void NonExistentParameterManager() { - Params.SetInstance(null); - - const string errorString = "Fetching Struct before ParamsSetup.Setup() has been called."; + const string errorString = "Must provide IParameterManager when constructing ParameterStructReference."; - var reference = new ParameterStructReferenceRuntime(kTestGuid); LogAssert.Expect(LogType.Error, errorString); + var reference = new ParameterStructReferenceRuntime(null, kTestGuid); Assert.IsNull(reference.Struct); - LogAssert.Expect(LogType.Error, errorString); - Assert.IsNotEmpty(reference.ToString()); - - reference = new ParameterStructReferenceRuntime(null); - Assert.IsNotEmpty(reference.ToString()); - - reference = new ParameterStructReferenceRuntime("asdf"); - LogAssert.Expect(LogType.Error, errorString); Assert.IsNotEmpty(reference.ToString()); } diff --git a/Tests/Runtime/TestCode/TestClasses.cs b/Tests/Runtime/TestCode/TestClasses.cs index 910114f..bb56d5e 100644 --- a/Tests/Runtime/TestCode/TestClasses.cs +++ b/Tests/Runtime/TestCode/TestClasses.cs @@ -11,7 +11,7 @@ public class MockMutableParameter : IMutableParameter public string EditPropertyValue; public string ReturnEditPropertyError; - public bool EditProperty(string propertyName, string value, out string error) + public bool EditProperty(IParameterManager parameterManager, string propertyName, string value, out string error) { EditPropertyCalls++; EditPropertyPropertyName = propertyName; @@ -47,14 +47,14 @@ public class MockKeyValueStruct : MockMutableBaseStruct, IKeyValueStruct public IReadOnlyList> InnerStructs => _innerStructs; public ParameterStructReferenceRuntime[] _innerStructs; - public MockKeyValueStruct(string description, int value, string innerStruct, string[] innerStructs) + public MockKeyValueStruct(IParameterManager parameterManager, string description, int value, string innerStruct, string[] innerStructs) { Description = description; Value = value; - InnerStruct = new ParameterStructReferenceRuntime(innerStruct); + InnerStruct = new ParameterStructReferenceRuntime(parameterManager, innerStruct); var innerStructsArray = new ParameterStructReferenceRuntime[innerStructs.Length]; for (int i = 0; i < innerStructs.Length; i++) - innerStructsArray[i] = new ParameterStructReferenceRuntime(innerStructs[i]); + innerStructsArray[i] = new ParameterStructReferenceRuntime(parameterManager, innerStructs[i]); _innerStructs = innerStructsArray; } } @@ -81,7 +81,8 @@ public class MockTestValidationInfo : IMutableParameter, ITestValidationInfo public IReadOnlyList> StructRefs => _structRefs; public ParameterStructReference[] _structRefs; - public bool EditProperty(string propertyName, string value, out string error) => throw new System.NotImplementedException(); + public bool EditProperty(IParameterManager parameterManager, string propertyName, string value, out string error) => + throw new System.NotImplementedException(); public void RemoveAllEdits() => throw new System.NotImplementedException(); } @@ -91,7 +92,7 @@ public class MockSubBadValidationInfo : IMutableParameter, ITestSubInterfaceInfo public int Value { get; set; } public int SubValue { get; set; } - public bool EditProperty(string propertyName, string value, out string error) => + public bool EditProperty(IParameterManager parameterManager, string propertyName, string value, out string error) => throw new System.NotImplementedException(); public void RemoveAllEdits() => throw new System.NotImplementedException(); diff --git a/Tests/Runtime/Validation/Attributes/AttributesTest.cs b/Tests/Runtime/Validation/Attributes/AttributesTest.cs index e7a3fd9..0c11620 100644 --- a/Tests/Runtime/Validation/Attributes/AttributesTest.cs +++ b/Tests/Runtime/Validation/Attributes/AttributesTest.cs @@ -278,7 +278,7 @@ public void AssertAssignedReference() const string testGuid = "abcd"; // true - references - AssertAttribute(attribute, nameof(ParameterReference), new ParameterReference(testGuid), true); + AssertAttribute(attribute, nameof(ParameterReference), new ParameterReference(_parameterManager, testGuid), true); #if ADDRESSABLE_PARAMS AssertAttribute(attribute, nameof(AssetReference), new AssetReference(testGuid), true); AssertAttribute(attribute, nameof(AssetReferenceSprite), new AssetReferenceSprite(testGuid), true); @@ -286,8 +286,8 @@ public void AssertAssignedReference() // true - list of references ParameterReferenceArray = new[] { - new ParameterReference(testGuid), - new ParameterReference(testGuid) }; + new ParameterReference(_parameterManager, testGuid), + new ParameterReference(_parameterManager, testGuid) }; AssertAttribute(attribute, nameof(ParameterReferenceArray), null, true); AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, true); @@ -308,9 +308,9 @@ public void AssertAssignedReference() // false references AssertAttribute(attribute, nameof(ParameterReference), null, false); - AssertAttribute(attribute, nameof(ParameterReference), new ParameterReference(), false); - AssertAttribute(attribute, nameof(ParameterReference), new ParameterReference(""), false); - AssertAttribute(attribute, nameof(ParameterReference), new ParameterReference("identifier", true), false); + AssertAttribute(attribute, nameof(ParameterReference), new ParameterReference(_parameterManager), false); + AssertAttribute(attribute, nameof(ParameterReference), new ParameterReference(_parameterManager, ""), false); + AssertAttribute(attribute, nameof(ParameterReference), new ParameterReference(_parameterManager, "identifier", true), false); #if ADDRESSABLE_PARAMS AssertAttribute(attribute, nameof(AssetReference), new AssetReference(), false); AssertAttribute(attribute, nameof(AssetReference), new AssetReference(""), false); @@ -319,20 +319,20 @@ public void AssertAssignedReference() // false - list of references ParameterReferenceArray = new[] { - new ParameterReference(testGuid), + new ParameterReference(_parameterManager, testGuid), null }; AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, false); ParameterReferenceArray = new[] { - new ParameterReference(testGuid), - new ParameterReference() }; + new ParameterReference(_parameterManager, testGuid), + new ParameterReference(_parameterManager) }; AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, false); ParameterReferenceArray = new[] { - new ParameterReference(testGuid), - new ParameterReference("") }; + new ParameterReference(_parameterManager, testGuid), + new ParameterReference(_parameterManager, "") }; AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, false); ParameterReferenceArray = new[] { - new ParameterReference(testGuid), - new ParameterReference("identifier", true) }; + new ParameterReference(_parameterManager, testGuid), + new ParameterReference(_parameterManager, "identifier", true) }; AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, false); #if ADDRESSABLE_PARAMS @@ -436,8 +436,8 @@ public void AssertAssignedReferenceExists() #endif // valid - unassigned - ParameterReference = new ParameterReference(); - ParameterReferenceArray = new[] { new ParameterReference() }; + ParameterReference = new ParameterReference(_parameterManager); + ParameterReferenceArray = new[] { new ParameterReference(_parameterManager) }; AssertAttribute(attribute, nameof(ParameterReference), ParameterReference, true); AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, true); @@ -461,13 +461,13 @@ public void AssertAssignedReferenceExists() const string validIdentifier = "someIdentifier"; var info = new MockMySpecialInfo(); _parameterManager.Load(info, validIdentifier, validGuid); - ParameterReference = new ParameterReference(validGuid); + ParameterReference = new ParameterReference(_parameterManager, validGuid); AssertAttribute(attribute, nameof(ParameterReference), ParameterReference, true); - ParameterReference = new ParameterReference(validIdentifier, true); + ParameterReference = new ParameterReference(_parameterManager, validIdentifier, true); AssertAttribute(attribute, nameof(ParameterReference), ParameterReference, true); - ParameterReferenceArray = new[] { new ParameterReference(validGuid) }; + ParameterReferenceArray = new[] { new ParameterReference(_parameterManager, validGuid) }; AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, true); - ParameterReferenceArray = new[] { new ParameterReference(validIdentifier, true) }; + ParameterReferenceArray = new[] { new ParameterReference(_parameterManager, validIdentifier, true) }; AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, true); // valid addressables - difficult to test without messing with existing groups @@ -478,16 +478,16 @@ public void AssertAssignedReferenceExists() // false const string falseGuid = "abcd"; - ParameterReference = new ParameterReference(falseGuid); + ParameterReference = new ParameterReference(_parameterManager, falseGuid); LogAssert.Expect(LogType.Error, new Regex(".*")); AssertAttribute(attribute, nameof(ParameterReference), ParameterReference, false); - ParameterReference = new ParameterReference(falseGuid, true); + ParameterReference = new ParameterReference(_parameterManager, falseGuid, true); LogAssert.Expect(LogType.Error, new Regex(".*")); AssertAttribute(attribute, nameof(ParameterReference), ParameterReference, false); - ParameterReferenceArray = new[] { new ParameterReference(falseGuid) }; + ParameterReferenceArray = new[] { new ParameterReference(_parameterManager, falseGuid) }; LogAssert.Expect(LogType.Error, new Regex(".*")); AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, false); - ParameterReferenceArray = new[] { new ParameterReference(falseGuid, true) }; + ParameterReferenceArray = new[] { new ParameterReference(_parameterManager, falseGuid, true) }; LogAssert.Expect(LogType.Error, new Regex(".*")); AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, false); @@ -522,7 +522,7 @@ public void AssertListNotEmpty() // true IntArray = new[] { 10, 11 }; AssertAttribute(attribute, nameof(IntArray), IntArray, true); - ParameterReferenceArray = new[] { new ParameterReference() }; + ParameterReferenceArray = new[] { new ParameterReference(_parameterManager) }; AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, true); ParameterReferenceArray = new ParameterReference[] { null }; AssertAttribute(attribute, nameof(ParameterReferenceArray), ParameterReferenceArray, true); diff --git a/Tests/Runtime/Validation/ParameterManagerValidatorTest.cs b/Tests/Runtime/Validation/ParameterManagerValidatorTest.cs index 472e3ca..a6debe3 100644 --- a/Tests/Runtime/Validation/ParameterManagerValidatorTest.cs +++ b/Tests/Runtime/Validation/ParameterManagerValidatorTest.cs @@ -64,13 +64,6 @@ public void SetUp() InnerKeyValueStructDataValidator.NextInstanceErrors = null; _parameterManager = Substitute.For(); - Params.SetInstance(_parameterManager); - } - - [TearDown] - public void TearDown() - { - Params.SetInstance(null); } private void SetupGeneralValidationParameters() @@ -95,25 +88,25 @@ private void SetupGeneralValidationParameters() _info1.Identifier = "Info1"; _info1.DisplayName = "Some Display Name"; _info1.Description = "Some Description"; - _info1.StructRef = new ParameterStructReferenceRuntime(keyValueGuid1); + _info1.StructRef = new ParameterStructReferenceRuntime(_parameterManager, keyValueGuid1); _info1._structRefs = new[] { - new ParameterStructReferenceRuntime(keyValueGuid2), - new ParameterStructReferenceRuntime(keyValueGuid3) + new ParameterStructReferenceRuntime(_parameterManager, keyValueGuid2), + new ParameterStructReferenceRuntime(_parameterManager, keyValueGuid3) }; _info2 = new MockTestValidationInfo(); _info2.Identifier = "Info2"; _info2.DisplayName = "Some Display Name"; _info2.Description = "Some Description"; - _info2.StructRef = new ParameterStructReferenceRuntime(keyValueGuid4); + _info2.StructRef = new ParameterStructReferenceRuntime(_parameterManager, keyValueGuid4); _info2._structRefs = Array.Empty>(); _info3 = new MockTestValidationInfo(); _info3.Identifier = "Info3"; _info3.DisplayName = "Some Display Name"; _info3.Description = "Some Description"; - _info3.StructRef = new ParameterStructReferenceRuntime(keyValueGuid5); + _info3.StructRef = new ParameterStructReferenceRuntime(_parameterManager, keyValueGuid5); _info3._structRefs = Array.Empty>(); var mocks = new[] { _info1, _info2, _info3 }; @@ -123,15 +116,15 @@ private void SetupGeneralValidationParameters() // create key value structs _info1KeyValueStruct1 = new MockKeyValueStruct( - "Some Description", 100, innerKeyValueGuid1, new[] { innerKeyValueGuid2, innerKeyValueGuid3 }); + _parameterManager, "Some Description", 100, innerKeyValueGuid1, new[] { innerKeyValueGuid2, innerKeyValueGuid3 }); _info1KeyValueStruct2 = new MockKeyValueStruct( - "Some Description", 100, innerKeyValueGuid4, Array.Empty()); + _parameterManager, "Some Description", 100, innerKeyValueGuid4, Array.Empty()); _info1KeyValueStruct3 = new MockKeyValueStruct( - "Some Description", 100, innerKeyValueGuid5, Array.Empty()); + _parameterManager, "Some Description", 100, innerKeyValueGuid5, Array.Empty()); _info2KeyValueStruct = new MockKeyValueStruct( - "Some Description", 100, innerKeyValueGuid6, Array.Empty()); + _parameterManager, "Some Description", 100, innerKeyValueGuid6, Array.Empty()); _info3KeyValueStruct = new MockKeyValueStruct( - "Some Description", 100, innerKeyValueGuid7, Array.Empty()); + _parameterManager, "Some Description", 100, innerKeyValueGuid7, Array.Empty()); _parameterManager.GetStructWithGuid(keyValueGuid1).Returns(_info1KeyValueStruct1); _parameterManager.GetStructWithGuid(keyValueGuid2).Returns(_info1KeyValueStruct2); _parameterManager.GetStructWithGuid(keyValueGuid3).Returns(_info1KeyValueStruct3); @@ -275,7 +268,7 @@ public void Failed_ExceptionHandling() { // setup var structGuid = "someStructGuid"; - var structReference = new ParameterStructReferenceRuntime(structGuid); + var structReference = new ParameterStructReferenceRuntime(_parameterManager, structGuid); var testExceptionInfo1 = Substitute.For(); testExceptionInfo1.ExceptionStruct.Returns(structReference); var testExceptionInfo2 = Substitute.For(); @@ -376,8 +369,8 @@ public void Fail_MissingValidatorWithStructs() var mocks = new[] { new MockMyVerySpecialInfo() { - Struct = new ParameterStructReferenceRuntime(structGuid1), - Structs = new []{ new ParameterStructReferenceRuntime(structGuid2) } + Struct = new ParameterStructReferenceRuntime(_parameterManager, structGuid1), + Structs = new []{ new ParameterStructReferenceRuntime(_parameterManager, structGuid2) } }, }; _parameterManager.Get().Returns(mocks); @@ -432,11 +425,11 @@ public void Fail_BuiltInAttribute() string badInnerStructGuid = "bad_inner_struct_guid"; _parameterManager.GetStructWithGuid(badInnerStructGuid).ReturnsNull(); - _info1.Ref = new ParameterReference(badInfoGuid); - _info1._structRefs[1] = new ParameterStructReferenceRuntime(badKeyValueStructGuid); - _info1KeyValueStruct1._innerStructs[0] = new ParameterStructReferenceRuntime(badInnerStructGuid); - _info1KeyValueStruct2.InnerStruct = new ParameterStructReferenceRuntime(badInnerStructGuid); - _info2.StructRef = new ParameterStructReferenceRuntime(badKeyValueStructGuid); + _info1.Ref = new ParameterReference(_parameterManager, badInfoGuid); + _info1._structRefs[1] = new ParameterStructReferenceRuntime(_parameterManager, badKeyValueStructGuid); + _info1KeyValueStruct1._innerStructs[0] = new ParameterStructReferenceRuntime(_parameterManager, badInnerStructGuid); + _info1KeyValueStruct2.InnerStruct = new ParameterStructReferenceRuntime(_parameterManager, badInnerStructGuid); + _info2.StructRef = new ParameterStructReferenceRuntime(_parameterManager, badKeyValueStructGuid); ValidateGeneralValidationParameters(out var validator); diff --git a/package.json b/package.json index 1695aa2..47c476a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.pocketgems.scriptableobject.flatbuffer", - "version": "4.5.0", + "version": "4.5.1", "displayName": "Scriptable Object - FlatBuffer", "description": "Seamless syncing between Scriptable Objects and CSVs. Scriptable Object data built to Google FlatBuffers for optimal runtime loading & access.", "unity": "2021.3",