Skip to content

Commit db56817

Browse files
committed
Implemenet for unit tests for the PropertyUtility class
1 parent 690d3fe commit db56817

3 files changed

Lines changed: 47 additions & 11 deletions

File tree

Assets/Editor Toolbox/Tests/Editor/PropertyUtilitesTest.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
using NUnit.Framework;
22

3+
using System;
4+
35
using UnityEditor;
46
using UnityEngine;
57

68
namespace Toolbox.Editor.Tests
79
{
810
public class PropertyUtilitesTest
911
{
10-
private SerializedObject scriptableObject;
11-
12+
private SerializedObject serializedObject;
1213

1314
[OneTimeSetUp]
1415
public void SetUp()
1516
{
1617
var target = ScriptableObject.CreateInstance<TestObject2>();
17-
scriptableObject = new SerializedObject(target);
18-
var array = scriptableObject.FindProperty("var2.var3");
19-
array.InsertArrayElementAtIndex(0);
18+
serializedObject = new SerializedObject(target);
19+
20+
var var2var3Property = serializedObject.FindProperty("var2.var3");
21+
var2var3Property.InsertArrayElementAtIndex(0);
22+
23+
#if UNITY_2019_3_OR_NEWER
24+
var var3Property = serializedObject.FindProperty("var3");
25+
var3Property.managedReferenceValue = new TestObject2.TestNestedObject3()
26+
{
27+
var1 = 3.0f
28+
};
29+
#endif
2030
}
2131

2232
[TestCase("var1", null)]
2333
[TestCase("var2.var1", "var2")]
2434
[TestCase("var2.var2.var1", "var2")]
2535
public void TestGetParentPass(string propertyPath, string parentName)
2636
{
27-
var property = scriptableObject.FindProperty(propertyPath);
37+
var property = serializedObject.FindProperty(propertyPath);
2838
var parent = property?.GetParent();
2939
Assert.AreEqual(parent?.name, parentName);
3040
}
@@ -35,7 +45,7 @@ public void TestGetParentPass(string propertyPath, string parentName)
3545
[TestCase("var2.var3.Array.data[0]", 1)]
3646
public void TestGetValuePass(string propertyPath, object value)
3747
{
38-
var property = scriptableObject.FindProperty(propertyPath);
48+
var property = serializedObject.FindProperty(propertyPath);
3949
var fieldInfo = property.GetFieldInfo(out _);
4050
property.SetProperValue(fieldInfo, value);
4151
var newValue = property.GetProperValue(fieldInfo);
@@ -47,7 +57,7 @@ public void TestGetValuePass(string propertyPath, object value)
4757
[TestCase("var2.var3.Array.data[0]", true)]
4858
public void TestIsArrayElementPass(string propertyPath, bool expected)
4959
{
50-
var property = scriptableObject.FindProperty(propertyPath);
60+
var property = serializedObject.FindProperty(propertyPath);
5161
var actual = PropertyUtility.IsSerializableArrayElement(property);
5262
Assert.AreEqual(expected, actual);
5363
}
@@ -59,7 +69,7 @@ public void TestIsArrayElementPass(string propertyPath, bool expected)
5969
[TestCase("var2.var3.Array.data[0]", "var2.var3.[0]")]
6070
public void TestGetPathTreePass(string propertyPath, string path)
6171
{
62-
var property = scriptableObject.FindProperty(propertyPath);
72+
var property = serializedObject.FindProperty(propertyPath);
6373
var pathTree = PropertyUtility.GetPropertyFieldTree(property);
6474
var actual = string.Join(".", pathTree);
6575
Assert.AreEqual(path, actual);
@@ -69,9 +79,23 @@ public void TestGetPathTreePass(string propertyPath, string path)
6979
[TestCase("var2.var1", false)]
7080
public void TestIsMonoScriptPass(string propertyPath, bool expected)
7181
{
72-
var property = scriptableObject.FindProperty(propertyPath);
82+
var property = serializedObject.FindProperty(propertyPath);
7383
var actual = PropertyUtility.IsDefaultScriptProperty(property);
7484
Assert.AreEqual(expected, actual);
7585
}
86+
87+
[TestCase("var1", typeof(int))]
88+
[TestCase("var2.var1", typeof(string))]
89+
#if UNITY_2019_3_OR_NEWER
90+
[TestCase("var3", typeof(TestObject2.TestNestedObject3))]
91+
[TestCase("var3.var1", typeof(float))]
92+
#endif
93+
public void TestGetFieldInfoPass(string propertyPath, Type expected)
94+
{
95+
var property = serializedObject.FindProperty(propertyPath);
96+
var fieldInfo = PropertyUtility.GetFieldInfo(property, out var propertyType);
97+
Assert.IsNotNull(fieldInfo);
98+
Assert.AreEqual(expected, propertyType);
99+
}
76100
}
77101
}

Assets/Editor Toolbox/Tests/Editor/ReorderableListTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class ReorderableListTest
1313

1414
private ToolboxEditorList list;
1515

16-
1716
[OneTimeSetUp]
1817
public void SetUp()
1918
{

Assets/Editor Toolbox/Tests/Editor/TestObject2.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ internal class TestObject2 : ScriptableObject
77
{
88
public int var1;
99
public TestNestedObject1 var2;
10+
#if UNITY_2019_3_OR_NEWER
11+
[SerializeReference]
12+
public ITestInterface var3;
13+
#endif
1014

1115
public bool BoolValue
1216
{
@@ -23,6 +27,9 @@ public double GetDoubleValue(int arg)
2327
return arg + 2.0005;
2428
}
2529

30+
public interface ITestInterface
31+
{ }
32+
2633
[Serializable]
2734
public class TestNestedObject1
2835
{
@@ -50,5 +57,11 @@ public int IntValue
5057
public void DoSomething()
5158
{ }
5259
}
60+
61+
[Serializable]
62+
public class TestNestedObject3 : ITestInterface
63+
{
64+
public float var1;
65+
}
5366
}
5467
}

0 commit comments

Comments
 (0)