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+ }
0 commit comments