From e94f8ec78ffc7e7f197c155eb047a4a6dc26843d Mon Sep 17 00:00:00 2001 From: tudor <7089284+tudddorrr@users.noreply.github.com> Date: Fri, 3 Mar 2023 22:39:34 +0000 Subject: [PATCH] handle empty liveconfig array, add tests --- .../Runtime/Entities/LiveConfig.cs | 11 +- .../Tests/Runtime/LiveConfig.meta | 8 + .../Tests/Runtime/LiveConfig/GetPropTest.cs | 59 +++++ .../Runtime/LiveConfig/GetPropTest.cs.meta | 11 + UserSettings/Layouts/default-2021.dwlt | 220 +++++------------- 5 files changed, 146 insertions(+), 163 deletions(-) create mode 100644 Packages/com.trytalo.talo/Tests/Runtime/LiveConfig.meta create mode 100644 Packages/com.trytalo.talo/Tests/Runtime/LiveConfig/GetPropTest.cs create mode 100644 Packages/com.trytalo.talo/Tests/Runtime/LiveConfig/GetPropTest.cs.meta diff --git a/Packages/com.trytalo.talo/Runtime/Entities/LiveConfig.cs b/Packages/com.trytalo.talo/Runtime/Entities/LiveConfig.cs index b7d004e..b79e57a 100644 --- a/Packages/com.trytalo.talo/Runtime/Entities/LiveConfig.cs +++ b/Packages/com.trytalo.talo/Runtime/Entities/LiveConfig.cs @@ -14,8 +14,15 @@ public LiveConfig(Prop[] props) public T GetProp(string key, T fallback = default(T)) { - Prop prop = props.First((prop) => prop.key == key); - return ((T)Convert.ChangeType(prop.key, typeof(T))) ?? fallback; + try + { + Prop prop = props.First((prop) => prop.key == key); + return ((T)Convert.ChangeType(prop.value, typeof(T))); + } + catch (Exception e) + { + return fallback; + } } } } diff --git a/Packages/com.trytalo.talo/Tests/Runtime/LiveConfig.meta b/Packages/com.trytalo.talo/Tests/Runtime/LiveConfig.meta new file mode 100644 index 0000000..c6bb2ac --- /dev/null +++ b/Packages/com.trytalo.talo/Tests/Runtime/LiveConfig.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4f5a7c38f45d544a1b24c65d86186d16 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.trytalo.talo/Tests/Runtime/LiveConfig/GetPropTest.cs b/Packages/com.trytalo.talo/Tests/Runtime/LiveConfig/GetPropTest.cs new file mode 100644 index 0000000..c6e753a --- /dev/null +++ b/Packages/com.trytalo.talo/Tests/Runtime/LiveConfig/GetPropTest.cs @@ -0,0 +1,59 @@ +using System.Collections; +using NUnit.Framework; +using UnityEngine.TestTools; +using TaloGameServices; +using UnityEngine; +using System; + +public class GetPropTest +{ + [UnityTest] + public IEnumerator GetProp_WithALiveConfigThatHasValues_ReturnsCorrectValue() + { + var config = new LiveConfig(new[] { new Prop(("gameName", "Crawle")), new Prop(("halloweenEventEnabled", "True")) }); + + Assert.AreEqual("Crawle", config.GetProp("gameName", "No name")); + + yield return null; + } + + [UnityTest] + public IEnumerator GetProp_WithAnEmptyList_ReturnsFallback() + { + var config = new LiveConfig(Array.Empty()); + + Assert.AreEqual("No name", config.GetProp("gameName", "No name")); + + yield return null; + } + + [UnityTest] + public IEnumerator GetProp_WithNoMatchingValuesInList_ReturnsFallback() + { + var config = new LiveConfig(new[] { new Prop(("gameName", "Crawle")), new Prop(("halloweenEventEnabled", "True")) }); + + Assert.AreEqual("1.0", config.GetProp("latestGameVersion", "1.0")); + + yield return null; + } + + [UnityTest] + public IEnumerator GetProp_WhenConvertingTypeToBoolean_ReturnsCorrectValue() + { + var config = new LiveConfig(new[] { new Prop(("halloweenEventEnabled", "True")) }); + + Assert.AreEqual(true, config.GetProp("halloweenEventEnabled", false)); + + yield return null; + } + + [UnityTest] + public IEnumerator GetProp_WhenConvertingTypeToNumber_ReturnsCorrectValue() + { + var config = new LiveConfig(new[] { new Prop(("maxLevel", "60")) }); + + Assert.AreEqual(60, config.GetProp("maxLevel", 0)); + + yield return null; + } +} diff --git a/Packages/com.trytalo.talo/Tests/Runtime/LiveConfig/GetPropTest.cs.meta b/Packages/com.trytalo.talo/Tests/Runtime/LiveConfig/GetPropTest.cs.meta new file mode 100644 index 0000000..d1dafdd --- /dev/null +++ b/Packages/com.trytalo.talo/Tests/Runtime/LiveConfig/GetPropTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 13a34a9c69a52404fb00c94b5bc5988e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UserSettings/Layouts/default-2021.dwlt b/UserSettings/Layouts/default-2021.dwlt index f338350..9a8a069 100644 --- a/UserSettings/Layouts/default-2021.dwlt +++ b/UserSettings/Layouts/default-2021.dwlt @@ -1,30 +1,6 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: --- !u!114 &1 -MonoBehaviour: - m_ObjectHideFlags: 52 - 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: 12004, guid: 0000000000000000e000000000000000, type: 0} - m_Name: - m_EditorClassIdentifier: - m_PixelRect: - serializedVersion: 2 - x: 20 - y: 72 - width: 1407 - height: 837 - m_ShowMode: 0 - m_Title: UI Builder - m_RootView: {fileID: 4} - m_MinSize: {x: 200, y: 221} - m_MaxSize: {x: 4000, y: 4021} - m_Maximized: 0 ---- !u!114 &2 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -44,61 +20,11 @@ MonoBehaviour: height: 1097 m_ShowMode: 4 m_Title: Project - m_RootView: {fileID: 9} + m_RootView: {fileID: 6} m_MinSize: {x: 875, y: 300} m_MaxSize: {x: 10000, y: 10000} m_Maximized: 1 ---- !u!114 &3 -MonoBehaviour: - m_ObjectHideFlags: 52 - 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: 12006, guid: 0000000000000000e000000000000000, type: 0} - m_Name: Builder - m_EditorClassIdentifier: - m_Children: [] - m_Position: - serializedVersion: 2 - x: 0 - y: 0 - width: 1407 - height: 837 - m_MinSize: {x: 200, y: 221} - m_MaxSize: {x: 4000, y: 4021} - m_ActualView: {fileID: 15} - m_Panes: - - {fileID: 15} - m_Selected: 0 - m_LastSelected: 0 ---- !u!114 &4 -MonoBehaviour: - m_ObjectHideFlags: 52 - 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: 12010, guid: 0000000000000000e000000000000000, type: 0} - m_Name: - m_EditorClassIdentifier: - m_Children: - - {fileID: 3} - m_Position: - serializedVersion: 2 - x: 0 - y: 0 - width: 1407 - height: 837 - m_MinSize: {x: 200, y: 221} - m_MaxSize: {x: 4000, y: 4021} - vertical: 0 - controlID: 1894 ---- !u!114 &5 +--- !u!114 &2 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -111,8 +37,8 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_Children: - - {fileID: 12} - - {fileID: 6} + - {fileID: 9} + - {fileID: 3} m_Position: serializedVersion: 2 x: 0 @@ -122,8 +48,8 @@ MonoBehaviour: m_MinSize: {x: 300, y: 200} m_MaxSize: {x: 24288, y: 16192} vertical: 0 - controlID: 39 ---- !u!114 &6 + controlID: 41 +--- !u!114 &3 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -142,14 +68,14 @@ MonoBehaviour: y: 0 width: 425 height: 1047 - m_MinSize: {x: 275, y: 50} - m_MaxSize: {x: 4000, y: 4000} - m_ActualView: {fileID: 17} + m_MinSize: {x: 276, y: 71} + m_MaxSize: {x: 4001, y: 4021} + m_ActualView: {fileID: 13} m_Panes: - - {fileID: 17} + - {fileID: 13} m_Selected: 0 m_LastSelected: 0 ---- !u!114 &7 +--- !u!114 &4 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -168,14 +94,14 @@ MonoBehaviour: y: 0 width: 338.5 height: 621.5 - m_MinSize: {x: 200, y: 200} - m_MaxSize: {x: 4000, y: 4000} - m_ActualView: {fileID: 18} + m_MinSize: {x: 201, y: 221} + m_MaxSize: {x: 4001, y: 4021} + m_ActualView: {fileID: 14} m_Panes: - - {fileID: 18} + - {fileID: 14} m_Selected: 0 m_LastSelected: 0 ---- !u!114 &8 +--- !u!114 &5 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -196,13 +122,13 @@ MonoBehaviour: height: 425.5 m_MinSize: {x: 231, y: 271} m_MaxSize: {x: 10001, y: 10021} - m_ActualView: {fileID: 16} + m_ActualView: {fileID: 12} m_Panes: - - {fileID: 16} - - {fileID: 21} + - {fileID: 12} + - {fileID: 17} m_Selected: 0 m_LastSelected: 1 ---- !u!114 &9 +--- !u!114 &6 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -215,9 +141,9 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_Children: - - {fileID: 10} - - {fileID: 5} - - {fileID: 11} + - {fileID: 7} + - {fileID: 2} + - {fileID: 8} m_Position: serializedVersion: 2 x: 0 @@ -230,7 +156,7 @@ MonoBehaviour: m_TopViewHeight: 30 m_UseBottomView: 1 m_BottomViewHeight: 20 ---- !u!114 &10 +--- !u!114 &7 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -252,7 +178,7 @@ MonoBehaviour: m_MinSize: {x: 0, y: 0} m_MaxSize: {x: 0, y: 0} m_LastLoadedLayoutName: ---- !u!114 &11 +--- !u!114 &8 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -273,7 +199,7 @@ MonoBehaviour: height: 20 m_MinSize: {x: 0, y: 0} m_MaxSize: {x: 0, y: 0} ---- !u!114 &12 +--- !u!114 &9 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -286,8 +212,8 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_Children: - - {fileID: 13} - - {fileID: 8} + - {fileID: 10} + - {fileID: 5} m_Position: serializedVersion: 2 x: 0 @@ -297,8 +223,8 @@ MonoBehaviour: m_MinSize: {x: 200, y: 200} m_MaxSize: {x: 16192, y: 16192} vertical: 1 - controlID: 40 ---- !u!114 &13 + controlID: 42 +--- !u!114 &10 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -311,8 +237,8 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_Children: - - {fileID: 7} - - {fileID: 14} + - {fileID: 4} + - {fileID: 11} m_Position: serializedVersion: 2 x: 0 @@ -322,8 +248,8 @@ MonoBehaviour: m_MinSize: {x: 200, y: 100} m_MaxSize: {x: 16192, y: 8096} vertical: 0 - controlID: 41 ---- !u!114 &14 + controlID: 91 +--- !u!114 &11 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -342,43 +268,15 @@ MonoBehaviour: y: 0 width: 1036.5 height: 621.5 - m_MinSize: {x: 200, y: 200} - m_MaxSize: {x: 4000, y: 4000} - m_ActualView: {fileID: 19} + m_MinSize: {x: 202, y: 221} + m_MaxSize: {x: 4002, y: 4021} + m_ActualView: {fileID: 15} m_Panes: - - {fileID: 19} - - {fileID: 20} + - {fileID: 15} + - {fileID: 16} m_Selected: 0 m_LastSelected: 1 ---- !u!114 &15 -MonoBehaviour: - m_ObjectHideFlags: 52 - 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: 13999, guid: 0000000000000000e000000000000000, type: 0} - m_Name: - m_EditorClassIdentifier: - m_MinSize: {x: 200, y: 200} - m_MaxSize: {x: 4000, y: 4000} - m_TitleContent: - m_Text: UI Builder - m_Image: {fileID: -3932862461143797134, guid: 0000000000000000d000000000000000, type: 0} - m_Tooltip: - m_Pos: - serializedVersion: 2 - x: 20 - y: 72 - width: 1407 - height: 816 - m_ViewDataDictionary: {fileID: 0} - m_OverlayCanvas: - m_LastAppliedPresetName: Default - m_SaveData: [] ---- !u!114 &16 +--- !u!114 &12 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -394,7 +292,7 @@ MonoBehaviour: m_MaxSize: {x: 10000, y: 10000} m_TitleContent: m_Text: Project - m_Image: {fileID: -5179483145760003458, guid: 0000000000000000d000000000000000, type: 0} + m_Image: {fileID: -5467254957812901981, guid: 0000000000000000d000000000000000, type: 0} m_Tooltip: m_Pos: serializedVersion: 2 @@ -419,22 +317,22 @@ MonoBehaviour: m_SkipHidden: 0 m_SearchArea: 1 m_Folders: - - Assets/Samples/LeaderboardsDemo/UI + - Packages/com.unity.test-framework m_Globs: [] m_OriginalText: m_ViewMode: 1 m_StartGridSize: 64 m_LastFolders: - - Assets/Samples/LeaderboardsDemo/UI + - Packages/com.unity.test-framework m_LastFoldersGridSize: -1 m_LastProjectPath: /Users/tudor/Documents/unity m_LockTracker: m_IsLocked: 0 m_FolderTreeState: - scrollPos: {x: 0, y: 0} - m_SelectedIDs: 9a450000 - m_LastClickedID: 17818 - m_ExpandedIDs: 00000000984400009a4400009c4400009e440000a0440000a2440000a4440000a6440000aa440000ca440000ce44000000ca9a3b + scrollPos: {x: 0, y: 163.5} + m_SelectedIDs: 34440000 + m_LastClickedID: 17460 + m_ExpandedIDs: 000000002c4400002e44000030440000324400003444000036440000384400003a4400003c4400003e440000404400006044000000ca9a3bffffff7f m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -462,7 +360,7 @@ MonoBehaviour: scrollPos: {x: 0, y: 0} m_SelectedIDs: m_LastClickedID: 0 - m_ExpandedIDs: 00000000984400009a4400009c4400009e440000a0440000a2440000a4440000a6440000a8440000aa440000 + m_ExpandedIDs: 000000002c4400002e44000030440000324400003444000036440000384400003a4400003c4400003e44000040440000 m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -518,7 +416,7 @@ MonoBehaviour: m_GridSize: 64 m_SkipHiddenPackages: 0 m_DirectoriesAreaWidth: 207 ---- !u!114 &17 +--- !u!114 &13 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -559,7 +457,7 @@ MonoBehaviour: m_LockTracker: m_IsLocked: 0 m_PreviewWindow: {fileID: 0} ---- !u!114 &18 +--- !u!114 &14 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -575,7 +473,7 @@ MonoBehaviour: m_MaxSize: {x: 4000, y: 4000} m_TitleContent: m_Text: Hierarchy - m_Image: {fileID: -3734745235275155857, guid: 0000000000000000d000000000000000, type: 0} + m_Image: {fileID: 7966133145522015247, guid: 0000000000000000d000000000000000, type: 0} m_Tooltip: m_Pos: serializedVersion: 2 @@ -590,9 +488,9 @@ MonoBehaviour: m_SceneHierarchy: m_TreeViewState: scrollPos: {x: 0, y: 0} - m_SelectedIDs: f2450000 + m_SelectedIDs: 9a440000 m_LastClickedID: 0 - m_ExpandedIDs: ccf6ffffccf8ffff56f9ffff38fbffff8a450000 + m_ExpandedIDs: 38fbffff m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -616,7 +514,7 @@ MonoBehaviour: m_IsLocked: 0 m_CurrentSortingName: TransformSorting m_WindowGUID: 4c969a2b90040154d917609493e03593 ---- !u!114 &19 +--- !u!114 &15 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -632,7 +530,7 @@ MonoBehaviour: m_MaxSize: {x: 4000, y: 4000} m_TitleContent: m_Text: Scene - m_Image: {fileID: 8634526014445323508, guid: 0000000000000000d000000000000000, type: 0} + m_Image: {fileID: 2593428753322112591, guid: 0000000000000000d000000000000000, type: 0} m_Tooltip: m_Pos: serializedVersion: 2 @@ -921,7 +819,7 @@ MonoBehaviour: m_SceneVisActive: 1 m_LastLockedObject: {fileID: 0} m_ViewIsLockedToObject: 0 ---- !u!114 &20 +--- !u!114 &16 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -937,7 +835,7 @@ MonoBehaviour: m_MaxSize: {x: 4000, y: 4000} m_TitleContent: m_Text: Game - m_Image: {fileID: 4621777727084837110, guid: 0000000000000000d000000000000000, type: 0} + m_Image: {fileID: -6423792434712278376, guid: 0000000000000000d000000000000000, type: 0} m_Tooltip: m_Pos: serializedVersion: 2 @@ -1013,7 +911,7 @@ MonoBehaviour: m_LowResolutionForAspectRatios: 01000000000000000000 m_XRRenderMode: 0 m_RenderTexture: {fileID: 0} ---- !u!114 &21 +--- !u!114 &17 MonoBehaviour: m_ObjectHideFlags: 52 m_CorrespondingSourceObject: {fileID: 0} @@ -1029,7 +927,7 @@ MonoBehaviour: m_MaxSize: {x: 4000, y: 4000} m_TitleContent: m_Text: Console - m_Image: {fileID: -4950941429401207979, guid: 0000000000000000d000000000000000, type: 0} + m_Image: {fileID: -4327648978806127646, guid: 0000000000000000d000000000000000, type: 0} m_Tooltip: m_Pos: serializedVersion: 2