-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathBiffAttributeTest.cs
More file actions
173 lines (157 loc) · 6.16 KB
/
BiffAttributeTest.cs
File metadata and controls
173 lines (157 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Visual Pinball Engine
// Copyright (C) 2023 freezy and VPE Team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using VisualPinball.Engine.IO;
using VisualPinball.Engine.Math;
using VisualPinball.Engine.VPT;
namespace VisualPinball.Engine.Test.IO
{
public class BiffAttributeTest
{
[Test]
public void ShouldNotUseCountAndIndex()
{
GetAttributes<BiffAttribute>(typeof(BiffAttribute), (memberType, member, biffDataType, attr) =>
{
if (attr.Count > -1 && attr.Index > -1) {
throw new Exception($"Must use either Count or Index but not both at {biffDataType.FullName}.{member.Name} ({attr.Name}).");
}
});
}
[Test]
public void ShouldBeAppliedToStrings()
{
GetAttributes<BiffStringAttribute>(typeof(BiffStringAttribute), (memberType, member, biffDataType, attr) =>
{
if (memberType != typeof(string) && memberType != typeof(string[])) {
throw new Exception($"BiffString of {biffDataType.FullName}.{member.Name} ({attr.Name}) must be either string or string[], but is {memberType.Name}.");
}
});
}
[Test]
public void ShouldBeAppliedToIntegers()
{
GetAttributes<BiffIntAttribute>(typeof(BiffIntAttribute), (memberType, member, biffDataType, attr) => {
if (memberType != typeof(int) && memberType != typeof(int[]) && memberType != typeof(uint) && memberType != typeof(uint[])) {
throw new Exception($"BiffInt of {biffDataType.FullName}.{member.Name} ({attr.Name}) must be either (u)int or (u)int[], but is {memberType.Name}.");
}
});
}
[Test]
public void ShouldBeAppliedToFloats()
{
GetAttributes<BiffFloatAttribute>(typeof(BiffFloatAttribute), (memberType, member, biffDataType, attr) =>
{
if (!attr.AsInt) {
if (memberType != typeof(float) && memberType != typeof(float[])) {
throw new Exception($"BiffFloat of {biffDataType.FullName}.{member.Name} ({attr.Name}) must be either float or float[], but is {memberType.Name}.");
}
} else {
if (memberType != typeof(int) && memberType != typeof(int[])) {
throw new Exception($"BiffFloat of {biffDataType.FullName}.{member.Name} ({attr.Name}) is marked to be int or int[], but is {memberType.Name}.");
}
}
});
}
[Test]
public void ShouldBeAppliedToBooleans()
{
GetAttributes<BiffBoolAttribute>(typeof(BiffBoolAttribute), (memberType, member, biffDataType, attr) =>
{
if (memberType != typeof(bool) && memberType != typeof(bool[])) {
throw new Exception($"BiffBool of {biffDataType.FullName}.{member.Name} ({attr.Name}) must be either bool or bool[], but is {memberType.Name}.");
}
});
}
[Test]
public void ShouldBeAppliedToColors()
{
GetAttributes<BiffColorAttribute>(typeof(BiffColorAttribute), (memberType, member, biffDataType, attr) =>
{
if (memberType != typeof(Color) && memberType != typeof(Color[])) {
throw new Exception($"BiffColor of {biffDataType.FullName}.{member.Name} ({attr.Name}) must be either Color or Color[], but is {memberType.Name}.");
}
});
}
[Test]
public void ShouldBeAppliedToVertices()
{
GetAttributes<BiffVertexAttribute>(typeof(BiffVertexAttribute), (memberType, member, biffDataType, attr) =>
{
if (memberType != typeof(Vertex2D) && memberType != typeof(Vertex3D)) {
throw new Exception($"BiffVertex of {biffDataType.FullName}.{member.Name} ({attr.Name}) must be either Vertex2D or Vertex3D, but is {memberType.Name}.");
}
});
}
[Test]
public void ShouldBeAppliedToTextureBinary()
{
GetAttributes<BiffBinaryAttribute>(typeof(BiffBinaryAttribute), (memberType, member, biffDataType, attr) =>
{
if (memberType != typeof(BinaryData)) {
throw new Exception($"BiffBinary of {biffDataType.FullName}.{member.Name} ({attr.Name}) must be of type BinaryData, but is {memberType.Name}.");
}
});
}
[Test]
public void ShouldBeAppliedToTextureBits()
{
GetAttributes<BiffBitsAttribute>(typeof(BiffBitsAttribute), (memberType, member, biffDataType, attr) =>
{
if (memberType != typeof(Bitmap)) {
throw new Exception($"BiffBits of {biffDataType.FullName}.{member.Name} ({attr.Name}) must be of type Bitmap, but is {memberType.Name}.");
}
});
}
private static void GetAttributes<T>(Type attributeType, Action<Type, MemberInfo, Type, T> assert) where T: BiffAttribute
{
var biffDataTypes = AppDomain.CurrentDomain
.GetAssemblies()
.First(a => a.GetName().Name == "VisualPinball.Engine")
.GetTypes()
.Where(t => t.IsSubclassOf(typeof(BiffData)))
.ToArray();
foreach (var biffDataType in biffDataTypes) {
var members = biffDataType.GetMembers()
.Where(member => member.MemberType == MemberTypes.Field || member.MemberType == MemberTypes.Property);
foreach (var member in members) {
var attrs = Attribute
.GetCustomAttributes(member, attributeType)
.Select(a => a as BiffAttribute)
.Where(a => a != null);
foreach (var attr in attrs) {
Type memberType = null;
switch (member) {
case FieldInfo field:
memberType = field.FieldType;
break;
case PropertyInfo property:
memberType = property.PropertyType;
break;
}
if (memberType == null) {
throw new Exception("Member type is null, that shouldn't happen because we filter by fields and properties.");
}
assert(memberType, member, biffDataType, attr as T);
}
}
}
}
}
}