Skip to content

Commit 523f35d

Browse files
committed
Added AnimatorParametersCollection tests
1 parent 336042e commit 523f35d

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System;
2+
using Adnc.AnimatorHelpers.Editors.Testing.Utilities;
3+
using Adnc.AnimatorHelpers.HasParameters;
4+
using Adnc.Utility.Testing;
5+
using NUnit.Framework;
6+
using UnityEngine;
7+
using Object = UnityEngine.Object;
8+
9+
namespace Adnc.AnimatorHelpers.Editors.Testing.HasParameters {
10+
[TestFixture(Category = "HasParameter")]
11+
public class TestAnimatorParametersCollection : TestBase {
12+
private AnimatorParametersCollection _animParCol;
13+
private AnimatorStub _stub;
14+
15+
[SetUp]
16+
public void Setup () {
17+
_animParCol = new AnimatorParametersCollection();
18+
_stub = new AnimatorStub();
19+
20+
_stub.AnimatorCtrl.AddParameter(new AnimatorControllerParameter {
21+
defaultBool = true,
22+
name = "a",
23+
type = AnimatorControllerParameterType.Bool
24+
});
25+
}
26+
27+
[TearDown]
28+
public void Teardown () {
29+
Object.DestroyImmediate(_stub.Animator.gameObject);
30+
_stub = null;
31+
}
32+
33+
[Test]
34+
public void SetParameterReturnsAnimatorParameters () {
35+
_stub.InjectCtrl();
36+
var ps = _animParCol.SetParameters("a", _stub.Animator);
37+
38+
Assert.IsNotNull(ps);
39+
}
40+
41+
[Test]
42+
public void SetParameterStoresCachedAnimator () {
43+
_stub.InjectCtrl();
44+
var ps = _animParCol.SetParameters("a", _stub.Animator);
45+
var psCache = _animParCol.GetParameters("a");
46+
47+
Assert.AreSame(ps, psCache);
48+
}
49+
50+
[Test]
51+
public void SetAnimatorEmptyStringErrors () {
52+
Assert.Throws<ArgumentNullException>(() => {
53+
_animParCol.SetParameters("", _stub.Animator);
54+
});
55+
}
56+
57+
[Test]
58+
public void SetAnimatorNullStringErrors () {
59+
Assert.Throws<ArgumentNullException>(() => {
60+
_animParCol.SetParameters(null, _stub.Animator);
61+
});
62+
}
63+
64+
[Test]
65+
public void SetAnimatorNullAnimatorErrors () {
66+
Assert.Throws<ArgumentNullException>(() => {
67+
_animParCol.SetParameters("a", null);
68+
});
69+
}
70+
71+
[Test]
72+
public void GetMissingAnimatorParametersReturnsNull () {
73+
Assert.IsNull(_animParCol.GetParameters("a"));
74+
}
75+
76+
[Test]
77+
public void GetParametersNullReturnsError () {
78+
Assert.Throws<ArgumentNullException>(() => {
79+
_animParCol.GetParameters(null);
80+
});
81+
}
82+
83+
[Test]
84+
public void GetParametersEmptyStringReturnsError () {
85+
Assert.Throws<ArgumentNullException>(() => {
86+
_animParCol.GetParameters("");
87+
});
88+
}
89+
90+
[Test]
91+
public void GetParametersAutoGeneratesCacheIfItDoesNotExist () {
92+
_stub.InjectCtrl();
93+
var psCache = _animParCol.GetParameters("a", _stub.Animator);
94+
95+
Assert.IsNotNull(psCache);
96+
Assert.IsTrue(_animParCol.HasParameters("a"));
97+
}
98+
99+
[Test]
100+
public void GetParametersDoesNotAutoGenerateCacheIfItExists () {
101+
_stub.InjectCtrl();
102+
var ps = _animParCol.SetParameters("a", _stub.Animator);
103+
104+
Assert.AreSame(ps, _animParCol.GetParameters("a", _stub.Animator));
105+
}
106+
107+
[Test]
108+
public void HasParametersReturnsTrueIfParameters () {
109+
_stub.InjectCtrl();
110+
var ps = _animParCol.SetParameters("a", _stub.Animator);
111+
112+
Assert.IsTrue(_animParCol.HasParameters("a"));
113+
}
114+
115+
[Test]
116+
public void HasParametersReturnsFalseIfNoParameters () {
117+
Assert.IsFalse(_animParCol.HasParameters("a"));
118+
}
119+
}
120+
}

Assets/AdncAnimatorHelpers/Editor/Testing/HasParameter/TestAnimatorParametersCollection.cs.meta

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

Assets/AdncAnimatorHelpers/Scripts/HasParameter/AnimatorParametersCollection.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using UnityEngine;
34

45
namespace Adnc.AnimatorHelpers.HasParameters {
56
public class AnimatorParametersCollection {
67
private Dictionary<string, AnimatorParameters> _dic = new Dictionary<string, AnimatorParameters>();
78

89
public AnimatorParameters SetParameters (string id, Animator animator) {
10+
if (string.IsNullOrEmpty(id)) {
11+
throw new ArgumentNullException(id);
12+
}
13+
914
var ap = new AnimatorParameters(animator);
1015
_dic.Add(id, ap);
1116

1217
return ap;
1318
}
1419

1520
public AnimatorParameters GetParameters (string id) {
21+
if (string.IsNullOrEmpty(id)) {
22+
throw new ArgumentNullException("id");
23+
}
24+
1625
AnimatorParameters result;
1726
_dic.TryGetValue(id, out result);
1827

0 commit comments

Comments
 (0)