Skip to content

Commit 00a26d2

Browse files
committed
Add tests related to types filtering & caching
1 parent c494fcd commit 00a26d2

10 files changed

Lines changed: 230 additions & 17 deletions

File tree

Assets/Editor Toolbox/Editor/Internal/TypesCachedCollection.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public virtual int IndexOf(Type type)
2323
return values.IndexOf(type);
2424
}
2525

26+
public virtual bool Contains(Type type)
27+
{
28+
return values.Contains(type);
29+
}
30+
2631
public IEnumerator<Type> GetEnumerator()
2732
{
2833
return values.GetEnumerator();

Assets/Editor Toolbox/Editor/Utilities/TypeUtilities.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public static class TypeUtilities
1515
internal static readonly Dictionary<string, Type> referenceTypesByNames = new Dictionary<string, Type>();
1616

1717

18+
internal static void ClearCache()
19+
{
20+
cachedCollections.Clear();
21+
editorCollections.Clear();
22+
referenceTypesByNames.Clear();
23+
}
24+
25+
1826
public static TypesCachedCollection GetCollection(Type parentType)
1927
{
2028
return GetCollection(new TypeConstraintContext(parentType));

Assets/Editor Toolbox/Tests/Editor/ExtractionTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using NUnit.Framework;
2+
23
using UnityEditor;
34
using UnityEngine;
45

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using NUnit.Framework;
2+
23
using UnityEditor;
34
using UnityEngine;
45

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using NUnit.Framework;
2+
23
using UnityEditor;
34
using UnityEngine;
45

Assets/Editor Toolbox/Tests/Editor/SerializationTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using NUnit.Framework;
2+
23
using UnityEditor;
34
using UnityEngine;
45

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
using System;
2+
3+
using NUnit.Framework;
4+
5+
using UnityEngine;
6+
7+
namespace Toolbox.Editor.Tests
8+
{
9+
using Toolbox.Editor.Internal;
10+
11+
public class TypesFilteringTest
12+
{
13+
public interface Interface1 { }
14+
public interface Interface2 : Interface1 { }
15+
public interface Interface3 : Interface1 { }
16+
public interface Interface4 : Interface2 { }
17+
public interface Interface4<T> : Interface3 { }
18+
19+
public abstract class ClassBase : Interface1 { }
20+
public class ClassWithInterface1 : ClassBase { }
21+
[Obsolete]
22+
public class ClassWithInterface2 : ClassBase { }
23+
public class ClassWithInterface3 : ClassBase { }
24+
25+
26+
[TestCase(typeof(ClassBase), 3)]
27+
[TestCase(typeof(Interface1), 6)]
28+
[TestCase(typeof(MonoBehaviour), 2)]
29+
public void TestTypesCachingPass(Type parentType, int count)
30+
{
31+
TypeUtilities.ClearCache();
32+
for (var i = 0; i < count; i++)
33+
{
34+
TypeUtilities.GetCollection(parentType);
35+
}
36+
37+
Assert.AreEqual(1, TypeUtilities.cachedCollections.Count);
38+
}
39+
40+
[Test]
41+
public void TestStandardConstraintPass1()
42+
{
43+
var constraint = new TypeConstraintStandard(typeof(Interface1), TypeSettings.Class, false, false);
44+
var collection = TypeUtilities.GetCollection(constraint);
45+
Assert.IsFalse(collection.Contains(typeof(ClassBase)));
46+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface1)));
47+
#pragma warning disable CS0612
48+
Assert.IsFalse(collection.Contains(typeof(ClassWithInterface2)));
49+
#pragma warning restore CS0612
50+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface3)));
51+
Assert.IsFalse(collection.Contains(typeof(Interface2)));
52+
Assert.IsFalse(collection.Contains(typeof(Interface3)));
53+
}
54+
55+
[Test]
56+
public void TestStandardConstraintPass2()
57+
{
58+
var constraint = new TypeConstraintStandard(typeof(Interface1), TypeSettings.Class, true, false);
59+
var collection = TypeUtilities.GetCollection(constraint);
60+
Assert.IsTrue(collection.Contains(typeof(ClassBase)));
61+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface1)));
62+
#pragma warning disable CS0612
63+
Assert.IsFalse(collection.Contains(typeof(ClassWithInterface2)));
64+
#pragma warning restore CS0612
65+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface3)));
66+
Assert.IsFalse(collection.Contains(typeof(Interface2)));
67+
Assert.IsFalse(collection.Contains(typeof(Interface3)));
68+
}
69+
70+
[Test]
71+
public void TestStandardConstraintPass3()
72+
{
73+
var constraint = new TypeConstraintStandard(typeof(Interface1), TypeSettings.Class, true, true);
74+
var collection = TypeUtilities.GetCollection(constraint);
75+
Assert.IsTrue(collection.Contains(typeof(ClassBase)));
76+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface1)));
77+
#pragma warning disable CS0612
78+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface2)));
79+
#pragma warning restore CS0612
80+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface3)));
81+
Assert.IsFalse(collection.Contains(typeof(Interface2)));
82+
Assert.IsFalse(collection.Contains(typeof(Interface3)));
83+
}
84+
85+
[Test]
86+
public void TestStandardConstraintPass4()
87+
{
88+
var constraint = new TypeConstraintStandard(typeof(Interface1), TypeSettings.Interface, true, true);
89+
var collection = TypeUtilities.GetCollection(constraint);
90+
Assert.IsFalse(collection.Contains(typeof(ClassBase)));
91+
Assert.IsFalse(collection.Contains(typeof(ClassWithInterface1)));
92+
#pragma warning disable CS0612
93+
Assert.IsFalse(collection.Contains(typeof(ClassWithInterface2)));
94+
#pragma warning restore CS0612
95+
Assert.IsFalse(collection.Contains(typeof(ClassWithInterface3)));
96+
Assert.IsTrue(collection.Contains(typeof(Interface2)));
97+
Assert.IsTrue(collection.Contains(typeof(Interface3)));
98+
}
99+
100+
[Test]
101+
public void TestStandardConstraintPass5()
102+
{
103+
var constraint = new TypeConstraintStandard(typeof(Interface1), TypeSettings.Class | TypeSettings.Interface, true, false);
104+
var collection = TypeUtilities.GetCollection(constraint);
105+
Assert.IsTrue(collection.Contains(typeof(ClassBase)));
106+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface1)));
107+
#pragma warning disable CS0612
108+
Assert.IsFalse(collection.Contains(typeof(ClassWithInterface2)));
109+
#pragma warning restore CS0612
110+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface3)));
111+
Assert.IsTrue(collection.Contains(typeof(Interface2)));
112+
Assert.IsTrue(collection.Contains(typeof(Interface3)));
113+
}
114+
115+
[Test]
116+
public void TestStandardConstraintPass6()
117+
{
118+
var constraint = new TypeConstraintStandard(typeof(ClassBase), TypeSettings.Class | TypeSettings.Interface, true, false);
119+
var collection = TypeUtilities.GetCollection(constraint);
120+
Assert.IsFalse(collection.Contains(typeof(ClassBase)));
121+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface1)));
122+
#pragma warning disable CS0612
123+
Assert.IsFalse(collection.Contains(typeof(ClassWithInterface2)));
124+
#pragma warning restore CS0612
125+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface3)));
126+
Assert.IsFalse(collection.Contains(typeof(Interface2)));
127+
Assert.IsFalse(collection.Contains(typeof(Interface3)));
128+
}
129+
130+
[Test]
131+
public void TestStandardConstraintPass7()
132+
{
133+
var constraint = new TypeConstraintStandard(typeof(Component), TypeSettings.Class | TypeSettings.Interface, true, false);
134+
var collection = TypeUtilities.GetCollection(constraint);
135+
Assert.IsFalse(collection.Contains(typeof(ClassBase)));
136+
Assert.IsTrue(collection.Contains(typeof(Collider)));
137+
}
138+
139+
[Test]
140+
public void TestReferenceConstraintPass1()
141+
{
142+
var constraint = new TypeConstraintReference(typeof(Component));
143+
var collection = TypeUtilities.GetCollection(constraint);
144+
Assert.AreEqual(0, collection.Values.Count);
145+
}
146+
147+
[Test]
148+
public void TestReferenceConstraintPass2()
149+
{
150+
var constraint = new TypeConstraintReference(typeof(ClassBase));
151+
var collection = TypeUtilities.GetCollection(constraint);
152+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface1)));
153+
#pragma warning disable CS0612
154+
Assert.IsFalse(collection.Contains(typeof(ClassWithInterface2)));
155+
#pragma warning restore CS0612
156+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface3)));
157+
Assert.IsFalse(collection.Contains(typeof(Interface2)));
158+
Assert.IsFalse(collection.Contains(typeof(Interface3)));
159+
}
160+
161+
[Test]
162+
public void TestReferenceConstraintPass3()
163+
{
164+
var constraint = new TypeConstraintReference(typeof(Interface1));
165+
var collection = TypeUtilities.GetCollection(constraint);
166+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface1)));
167+
#pragma warning disable CS0612
168+
Assert.IsFalse(collection.Contains(typeof(ClassWithInterface2)));
169+
#pragma warning restore CS0612
170+
Assert.IsTrue(collection.Contains(typeof(ClassWithInterface3)));
171+
Assert.IsFalse(collection.Contains(typeof(Interface2)));
172+
Assert.IsFalse(collection.Contains(typeof(Interface3)));
173+
}
174+
}
175+
}

Assets/Editor Toolbox/Tests/Editor/TypesFilteringTest.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Examples/Scenes/SampleScene.unity

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,20 +617,27 @@ MonoBehaviour:
617617
m_Name:
618618
m_EditorClassIdentifier:
619619
type1:
620-
typeReference: Interface4`1, Assembly-CSharp
620+
typeReference: SampleReferenceTest+Interface2, Assembly-CSharp
621621
type2:
622-
typeReference: Interface3, Assembly-CSharp
622+
typeReference: SampleReferenceTest+Interface3, Assembly-CSharp
623623
type3:
624-
typeReference: ClassWithInterface, Assembly-CSharp
624+
typeReference: SampleReferenceTest+ClassWithInterface, Assembly-CSharp
625625
reference:
626626
id: 0
627+
reference2:
628+
id: 1
627629
references:
628630
version: 1
629631
00000000:
630632
type: {class: SampleReference1, ns: , asm: Assembly-CSharp}
631633
data:
632-
var1: 0.68
633-
var2: 1
634+
var1: 0
635+
var2: 0
636+
00000001:
637+
type: {class: SampleReference2, ns: , asm: Assembly-CSharp}
638+
data:
639+
var1: 0
640+
var2: 0
634641
--- !u!4 &752799893
635642
Transform:
636643
m_ObjectHideFlags: 0

Assets/Examples/Scripts/SampleReferenceTest.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ public class SampleReferenceTest : MonoBehaviour
44
{
55
[TypeConstraint(typeof(Interface1), AddTextSearchField = true)]
66
public SerializedType type1;
7-
[TypeConstraint(typeof(Interface1), TypeSettings = TypeSettings.Interface)]
7+
[TypeConstraint(typeof(Interface1), TypeSettings = TypeSettings.Interface, AddTextSearchField = true)]
88
public SerializedType type2;
99
[ClassExtends(typeof(Interface1))]
1010
public SerializedType type3;
11+
1112
[SerializeReference, ReferencePicker]
1213
public SampleReferenceBase reference;
13-
}
14+
[SerializeReference, ReferencePicker]
15+
public SampleReferenceBase reference2;
1416

15-
public interface Interface1 { }
16-
public interface Interface2 : Interface1 { }
17-
public interface Interface3 : Interface1 { }
18-
public interface Interface4 : Interface2 { }
19-
public interface Interface4<T> : Interface3 { }
17+
public interface Interface1 { }
18+
public interface Interface2 : Interface1 { }
19+
public interface Interface3 : Interface1 { }
20+
public interface Interface4 : Interface2 { }
21+
public interface Interface4<T> : Interface3 { }
2022

21-
public class ClassWithInterface : Interface1 { }
23+
public class ClassWithInterface : Interface1 { }
24+
}

0 commit comments

Comments
 (0)