Skip to content

Commit

Permalink
Implemenet for unit tests for the PropertyUtility class
Browse files Browse the repository at this point in the history
  • Loading branch information
arimger committed Aug 26, 2024
1 parent 690d3fe commit db56817
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
44 changes: 34 additions & 10 deletions Assets/Editor Toolbox/Tests/Editor/PropertyUtilitesTest.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
using NUnit.Framework;

using System;

using UnityEditor;
using UnityEngine;

namespace Toolbox.Editor.Tests
{
public class PropertyUtilitesTest
{
private SerializedObject scriptableObject;

private SerializedObject serializedObject;

[OneTimeSetUp]
public void SetUp()
{
var target = ScriptableObject.CreateInstance<TestObject2>();
scriptableObject = new SerializedObject(target);
var array = scriptableObject.FindProperty("var2.var3");
array.InsertArrayElementAtIndex(0);
serializedObject = new SerializedObject(target);

var var2var3Property = serializedObject.FindProperty("var2.var3");
var2var3Property.InsertArrayElementAtIndex(0);

#if UNITY_2019_3_OR_NEWER
var var3Property = serializedObject.FindProperty("var3");
var3Property.managedReferenceValue = new TestObject2.TestNestedObject3()
{
var1 = 3.0f
};
#endif
}

[TestCase("var1", null)]
[TestCase("var2.var1", "var2")]
[TestCase("var2.var2.var1", "var2")]
public void TestGetParentPass(string propertyPath, string parentName)
{
var property = scriptableObject.FindProperty(propertyPath);
var property = serializedObject.FindProperty(propertyPath);
var parent = property?.GetParent();
Assert.AreEqual(parent?.name, parentName);
}
Expand All @@ -35,7 +45,7 @@ public void TestGetParentPass(string propertyPath, string parentName)
[TestCase("var2.var3.Array.data[0]", 1)]
public void TestGetValuePass(string propertyPath, object value)
{
var property = scriptableObject.FindProperty(propertyPath);
var property = serializedObject.FindProperty(propertyPath);
var fieldInfo = property.GetFieldInfo(out _);
property.SetProperValue(fieldInfo, value);
var newValue = property.GetProperValue(fieldInfo);
Expand All @@ -47,7 +57,7 @@ public void TestGetValuePass(string propertyPath, object value)
[TestCase("var2.var3.Array.data[0]", true)]
public void TestIsArrayElementPass(string propertyPath, bool expected)
{
var property = scriptableObject.FindProperty(propertyPath);
var property = serializedObject.FindProperty(propertyPath);
var actual = PropertyUtility.IsSerializableArrayElement(property);
Assert.AreEqual(expected, actual);
}
Expand All @@ -59,7 +69,7 @@ public void TestIsArrayElementPass(string propertyPath, bool expected)
[TestCase("var2.var3.Array.data[0]", "var2.var3.[0]")]
public void TestGetPathTreePass(string propertyPath, string path)
{
var property = scriptableObject.FindProperty(propertyPath);
var property = serializedObject.FindProperty(propertyPath);
var pathTree = PropertyUtility.GetPropertyFieldTree(property);
var actual = string.Join(".", pathTree);
Assert.AreEqual(path, actual);
Expand All @@ -69,9 +79,23 @@ public void TestGetPathTreePass(string propertyPath, string path)
[TestCase("var2.var1", false)]
public void TestIsMonoScriptPass(string propertyPath, bool expected)
{
var property = scriptableObject.FindProperty(propertyPath);
var property = serializedObject.FindProperty(propertyPath);
var actual = PropertyUtility.IsDefaultScriptProperty(property);
Assert.AreEqual(expected, actual);
}

[TestCase("var1", typeof(int))]
[TestCase("var2.var1", typeof(string))]
#if UNITY_2019_3_OR_NEWER
[TestCase("var3", typeof(TestObject2.TestNestedObject3))]
[TestCase("var3.var1", typeof(float))]
#endif
public void TestGetFieldInfoPass(string propertyPath, Type expected)
{
var property = serializedObject.FindProperty(propertyPath);
var fieldInfo = PropertyUtility.GetFieldInfo(property, out var propertyType);
Assert.IsNotNull(fieldInfo);
Assert.AreEqual(expected, propertyType);
}
}
}
1 change: 0 additions & 1 deletion Assets/Editor Toolbox/Tests/Editor/ReorderableListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class ReorderableListTest

private ToolboxEditorList list;


[OneTimeSetUp]
public void SetUp()
{
Expand Down
13 changes: 13 additions & 0 deletions Assets/Editor Toolbox/Tests/Editor/TestObject2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ internal class TestObject2 : ScriptableObject
{
public int var1;
public TestNestedObject1 var2;
#if UNITY_2019_3_OR_NEWER
[SerializeReference]
public ITestInterface var3;
#endif

public bool BoolValue
{
Expand All @@ -23,6 +27,9 @@ public double GetDoubleValue(int arg)
return arg + 2.0005;
}

public interface ITestInterface
{ }

[Serializable]
public class TestNestedObject1
{
Expand Down Expand Up @@ -50,5 +57,11 @@ public int IntValue
public void DoSomething()
{ }
}

[Serializable]
public class TestNestedObject3 : ITestInterface
{
public float var1;
}
}
}

0 comments on commit db56817

Please sign in to comment.