Skip to content

Commit ae8f1f1

Browse files
committed
fix: test assumptions
1 parent 4600ffd commit ae8f1f1

4 files changed

Lines changed: 137 additions & 65 deletions

File tree

Tests/EditMode/FixedSerializationPersistenceTests.cs

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace FixedMathSharp.Unity.Tests
88
{
99
public sealed class FixedSerializationPersistenceTests
1010
{
11-
private const string TestAssetFolder = "Assets/Packages/Tests/EditMode/GeneratedSerializationPersistenceTests";
11+
private const string TestAssetFolder = "Assets/GeneratedSerializationPersistenceTests";
1212

1313
private static readonly FixedSerializationProbeSnapshot ExpectedSnapshot = new()
1414
{
@@ -20,25 +20,20 @@ public sealed class FixedSerializationPersistenceTests
2020
Fixed64.FromRaw(0x33333333),
2121
Fixed64.FromRaw(0x44444444),
2222
Fixed64.FromRaw(0x55555555)),
23-
NestedValue = new FixedSerializationNestedProbe
24-
{
25-
Value = Fixed64.FromRaw(0x66666666)
26-
},
27-
ListValue = new FixedSerializationListProbe
28-
{
29-
Value = Fixed64.FromRaw(0x77777777),
30-
Position = new Vector3d(
31-
Fixed64.FromRaw(0x11112222),
32-
Fixed64.FromRaw(0x33334444),
33-
Fixed64.FromRaw(0x55556666))
34-
}
23+
NestedValue = Fixed64.FromRaw(0x66666666),
24+
ListCount = 1,
25+
ListValue = Fixed64.FromRaw(0x77777777),
26+
ListPosition = new Vector3d(
27+
Fixed64.FromRaw(0x11112222),
28+
Fixed64.FromRaw(0x33334444),
29+
Fixed64.FromRaw(0x55556666))
3530
};
3631

3732
[SetUp]
3833
public void SetUp()
3934
{
4035
DeleteTestAssets();
41-
AssetDatabase.CreateFolder("Assets/Packages/Tests/EditMode", "GeneratedSerializationPersistenceTests");
36+
AssetDatabase.CreateFolder("Assets", "GeneratedSerializationPersistenceTests");
4237
}
4338

4439
[TearDown]
@@ -52,7 +47,7 @@ public void ScriptableObjectSaveReload_PreservesFixedMathSharpValues()
5247
{
5348
string assetPath = $"{TestAssetFolder}/FixedSerializationProbe.asset";
5449
FixedSerializationScriptableProbe asset = ScriptableObject.CreateInstance<FixedSerializationScriptableProbe>();
55-
asset.SetSnapshot(ExpectedSnapshot);
50+
SetSnapshot(asset, ExpectedSnapshot);
5651

5752
AssetDatabase.CreateAsset(asset, assetPath);
5853
AssetDatabase.SaveAssets();
@@ -64,7 +59,7 @@ public void ScriptableObjectSaveReload_PreservesFixedMathSharpValues()
6459
FixedSerializationScriptableProbe reloaded = AssetDatabase.LoadAssetAtPath<FixedSerializationScriptableProbe>(assetPath);
6560

6661
Assert.That(reloaded, Is.Not.Null);
67-
AssertSnapshotEqual(reloaded.GetSnapshot(), ExpectedSnapshot);
62+
AssertSnapshotEqual(GetSnapshot(reloaded), ExpectedSnapshot);
6863
}
6964

7065
[Test]
@@ -75,7 +70,7 @@ public void ScriptableObjectEditSaveReload_PreservesFixedMathSharpValues()
7570
AssetDatabase.CreateAsset(asset, assetPath);
7671
AssetDatabase.SaveAssets();
7772

78-
asset.SetSnapshot(ExpectedSnapshot);
73+
SetSnapshot(asset, ExpectedSnapshot);
7974
EditorUtility.SetDirty(asset);
8075
AssetDatabase.SaveAssets();
8176
Resources.UnloadAsset(asset);
@@ -86,15 +81,15 @@ public void ScriptableObjectEditSaveReload_PreservesFixedMathSharpValues()
8681
FixedSerializationScriptableProbe reloaded = AssetDatabase.LoadAssetAtPath<FixedSerializationScriptableProbe>(assetPath);
8782

8883
Assert.That(reloaded, Is.Not.Null);
89-
AssertSnapshotEqual(reloaded.GetSnapshot(), ExpectedSnapshot);
84+
AssertSnapshotEqual(GetSnapshot(reloaded), ExpectedSnapshot);
9085
}
9186

9287
[Test]
9388
public void PrefabSaveReload_PreservesFixedMathSharpValues()
9489
{
9590
string prefabPath = $"{TestAssetFolder}/FixedSerializationProbe.prefab";
9691
GameObject source = new("Fixed Serialization Probe");
97-
source.AddComponent<FixedSerializationPrefabProbe>().SetSnapshot(ExpectedSnapshot);
92+
SetSnapshot(source.AddComponent<FixedSerializationPrefabProbe>(), ExpectedSnapshot);
9893

9994
try
10095
{
@@ -113,7 +108,7 @@ public void PrefabSaveReload_PreservesFixedMathSharpValues()
113108
FixedSerializationPrefabProbe reloaded = reloadedPrefab.GetComponent<FixedSerializationPrefabProbe>();
114109

115110
Assert.That(reloaded, Is.Not.Null);
116-
AssertSnapshotEqual(reloaded.GetSnapshot(), ExpectedSnapshot);
111+
AssertSnapshotEqual(GetSnapshot(reloaded), ExpectedSnapshot);
117112
}
118113

119114
[Test]
@@ -136,7 +131,7 @@ public void PrefabEditSaveReload_PreservesFixedMathSharpValues()
136131
GameObject contents = PrefabUtility.LoadPrefabContents(prefabPath);
137132
try
138133
{
139-
contents.GetComponent<FixedSerializationPrefabProbe>().SetSnapshot(ExpectedSnapshot);
134+
SetSnapshot(contents.GetComponent<FixedSerializationPrefabProbe>(), ExpectedSnapshot);
140135
PrefabUtility.SaveAsPrefabAsset(contents, prefabPath);
141136
}
142137
finally
@@ -151,7 +146,72 @@ public void PrefabEditSaveReload_PreservesFixedMathSharpValues()
151146
FixedSerializationPrefabProbe reloaded = reloadedPrefab.GetComponent<FixedSerializationPrefabProbe>();
152147

153148
Assert.That(reloaded, Is.Not.Null);
154-
AssertSnapshotEqual(reloaded.GetSnapshot(), ExpectedSnapshot);
149+
AssertSnapshotEqual(GetSnapshot(reloaded), ExpectedSnapshot);
150+
}
151+
152+
private struct FixedSerializationProbeSnapshot
153+
{
154+
public Fixed64 FixedValue;
155+
public Vector2d Vector2Value;
156+
public Vector3d Vector3Value;
157+
public Fixed64 NestedValue;
158+
public int ListCount;
159+
public Fixed64 ListValue;
160+
public Vector3d ListPosition;
161+
}
162+
163+
private static void SetSnapshot(
164+
FixedSerializationScriptableProbe probe,
165+
FixedSerializationProbeSnapshot snapshot)
166+
{
167+
probe.SetValues(
168+
snapshot.FixedValue,
169+
snapshot.Vector2Value,
170+
snapshot.Vector3Value,
171+
snapshot.NestedValue,
172+
snapshot.ListValue,
173+
snapshot.ListPosition);
174+
}
175+
176+
private static void SetSnapshot(
177+
FixedSerializationPrefabProbe probe,
178+
FixedSerializationProbeSnapshot snapshot)
179+
{
180+
probe.SetValues(
181+
snapshot.FixedValue,
182+
snapshot.Vector2Value,
183+
snapshot.Vector3Value,
184+
snapshot.NestedValue,
185+
snapshot.ListValue,
186+
snapshot.ListPosition);
187+
}
188+
189+
private static FixedSerializationProbeSnapshot GetSnapshot(FixedSerializationScriptableProbe probe)
190+
{
191+
return new FixedSerializationProbeSnapshot
192+
{
193+
FixedValue = probe.FixedValue,
194+
Vector2Value = probe.Vector2Value,
195+
Vector3Value = probe.Vector3Value,
196+
NestedValue = probe.NestedValue,
197+
ListCount = probe.ListCount,
198+
ListValue = probe.ListValue,
199+
ListPosition = probe.ListPosition
200+
};
201+
}
202+
203+
private static FixedSerializationProbeSnapshot GetSnapshot(FixedSerializationPrefabProbe probe)
204+
{
205+
return new FixedSerializationProbeSnapshot
206+
{
207+
FixedValue = probe.FixedValue,
208+
Vector2Value = probe.Vector2Value,
209+
Vector3Value = probe.Vector3Value,
210+
NestedValue = probe.NestedValue,
211+
ListCount = probe.ListCount,
212+
ListValue = probe.ListValue,
213+
ListPosition = probe.ListPosition
214+
};
155215
}
156216

157217
private static void AssertSnapshotEqual(
@@ -163,9 +223,11 @@ private static void AssertSnapshotEqual(
163223
CollectFixedFailure(actual.FixedValue, expected.FixedValue, "Fixed64 field", failures);
164224
CollectVector2Failure(actual.Vector2Value, expected.Vector2Value, "Vector2d field", failures);
165225
CollectVector3Failure(actual.Vector3Value, expected.Vector3Value, "Vector3d field", failures);
166-
CollectFixedFailure(actual.NestedValue.Value, expected.NestedValue.Value, "nested Fixed64 field", failures);
167-
CollectFixedFailure(actual.ListValue.Value, expected.ListValue.Value, "list Fixed64 field", failures);
168-
CollectVector3Failure(actual.ListValue.Position, expected.ListValue.Position, "list Vector3d field", failures);
226+
CollectFixedFailure(actual.NestedValue, expected.NestedValue, "nested Fixed64 field", failures);
227+
if (actual.ListCount != expected.ListCount)
228+
failures.Add($"list count: expected {expected.ListCount}, got {actual.ListCount}");
229+
CollectFixedFailure(actual.ListValue, expected.ListValue, "list Fixed64 field", failures);
230+
CollectVector3Failure(actual.ListPosition, expected.ListPosition, "list Vector3d field", failures);
169231

170232
if (failures.Count > 0)
171233
Assert.Fail(string.Join(Environment.NewLine, failures));

Tests/Runtime/FixedSerializationPrefabProbe.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,34 @@ public sealed class FixedSerializationPrefabProbe : MonoBehaviour
1111
[SerializeField] private FixedSerializationNestedProbe nestedValue;
1212
[SerializeField] private List<FixedSerializationListProbe> listValues = new();
1313

14-
public void SetSnapshot(FixedSerializationProbeSnapshot snapshot)
14+
public void SetValues(
15+
Fixed64 fixedValue,
16+
Vector2d vector2Value,
17+
Vector3d vector3Value,
18+
Fixed64 nestedValue,
19+
Fixed64 listValue,
20+
Vector3d listPosition)
1521
{
16-
fixedValue = snapshot.FixedValue;
17-
vector2Value = snapshot.Vector2Value;
18-
vector3Value = snapshot.Vector3Value;
19-
nestedValue = snapshot.NestedValue;
20-
listValues = new List<FixedSerializationListProbe> { snapshot.ListValue };
21-
}
22-
23-
public FixedSerializationProbeSnapshot GetSnapshot()
24-
{
25-
return new FixedSerializationProbeSnapshot
22+
this.fixedValue = fixedValue;
23+
this.vector2Value = vector2Value;
24+
this.vector3Value = vector3Value;
25+
this.nestedValue = new FixedSerializationNestedProbe { Value = nestedValue };
26+
listValues = new List<FixedSerializationListProbe>
2627
{
27-
FixedValue = fixedValue,
28-
Vector2Value = vector2Value,
29-
Vector3Value = vector3Value,
30-
NestedValue = nestedValue,
31-
ListValue = listValues[0]
28+
new FixedSerializationListProbe
29+
{
30+
Value = listValue,
31+
Position = listPosition
32+
}
3233
};
3334
}
35+
36+
public Fixed64 FixedValue => fixedValue;
37+
public Vector2d Vector2Value => vector2Value;
38+
public Vector3d Vector3Value => vector3Value;
39+
public Fixed64 NestedValue => nestedValue.Value;
40+
public int ListCount => listValues.Count;
41+
public Fixed64 ListValue => listValues.Count > 0 ? listValues[0].Value : default;
42+
public Vector3d ListPosition => listValues.Count > 0 ? listValues[0].Position : default;
3443
}
3544
}

Tests/Runtime/FixedSerializationProbeData.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,4 @@ public struct FixedSerializationListProbe
1515
public Vector3d Position;
1616
}
1717

18-
public struct FixedSerializationProbeSnapshot
19-
{
20-
public Fixed64 FixedValue;
21-
public Vector2d Vector2Value;
22-
public Vector3d Vector3Value;
23-
public FixedSerializationNestedProbe NestedValue;
24-
public FixedSerializationListProbe ListValue;
25-
}
2618
}

Tests/Runtime/FixedSerializationScriptableProbe.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,34 @@ public sealed class FixedSerializationScriptableProbe : ScriptableObject
1111
[SerializeField] private FixedSerializationNestedProbe nestedValue;
1212
[SerializeField] private List<FixedSerializationListProbe> listValues = new();
1313

14-
public void SetSnapshot(FixedSerializationProbeSnapshot snapshot)
14+
public void SetValues(
15+
Fixed64 fixedValue,
16+
Vector2d vector2Value,
17+
Vector3d vector3Value,
18+
Fixed64 nestedValue,
19+
Fixed64 listValue,
20+
Vector3d listPosition)
1521
{
16-
fixedValue = snapshot.FixedValue;
17-
vector2Value = snapshot.Vector2Value;
18-
vector3Value = snapshot.Vector3Value;
19-
nestedValue = snapshot.NestedValue;
20-
listValues = new List<FixedSerializationListProbe> { snapshot.ListValue };
21-
}
22-
23-
public FixedSerializationProbeSnapshot GetSnapshot()
24-
{
25-
return new FixedSerializationProbeSnapshot
22+
this.fixedValue = fixedValue;
23+
this.vector2Value = vector2Value;
24+
this.vector3Value = vector3Value;
25+
this.nestedValue = new FixedSerializationNestedProbe { Value = nestedValue };
26+
listValues = new List<FixedSerializationListProbe>
2627
{
27-
FixedValue = fixedValue,
28-
Vector2Value = vector2Value,
29-
Vector3Value = vector3Value,
30-
NestedValue = nestedValue,
31-
ListValue = listValues[0]
28+
new FixedSerializationListProbe
29+
{
30+
Value = listValue,
31+
Position = listPosition
32+
}
3233
};
3334
}
35+
36+
public Fixed64 FixedValue => fixedValue;
37+
public Vector2d Vector2Value => vector2Value;
38+
public Vector3d Vector3Value => vector3Value;
39+
public Fixed64 NestedValue => nestedValue.Value;
40+
public int ListCount => listValues.Count;
41+
public Fixed64 ListValue => listValues.Count > 0 ? listValues[0].Value : default;
42+
public Vector3d ListPosition => listValues.Count > 0 ? listValues[0].Position : default;
3443
}
3544
}

0 commit comments

Comments
 (0)