A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
13KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using DataLoader;
  6. using Svelto.Tasks;
  7. using Svelto.Tasks.Enumerators;
  8. using Unity.Mathematics;
  9. using UnityEngine;
  10. using TechbloxModdingAPI.App;
  11. using TechbloxModdingAPI.Tests;
  12. using TechbloxModdingAPI.Utility;
  13. namespace TechbloxModdingAPI.Blocks
  14. {
  15. #if TEST
  16. /// <summary>
  17. /// Block test cases. Not accessible in release versions.
  18. /// </summary>
  19. [APITestClass]
  20. public static class BlockTests
  21. {
  22. [APITestCase(TestType.Game)] //At least one block must be placed for simulation to work
  23. public static void TestPlaceNew()
  24. {
  25. Block newBlock = Block.PlaceNew(BlockIDs.Cube, float3.zero);
  26. Assert.NotNull(newBlock.Id, "Newly placed block is missing Id. This should be populated when the block is placed.", "Newly placed block Id is not null, block successfully placed.");
  27. }
  28. [APITestCase(TestType.EditMode)]
  29. public static void TestInitProperty()
  30. {
  31. Block newBlock = Block.PlaceNew(BlockIDs.Cube, float3.zero + 2);
  32. if (!Assert.CloseTo(newBlock.Position, (float3.zero + 2), $"Newly placed block at {newBlock.Position} is expected at {Unity.Mathematics.float3.zero + 2}.", "Newly placed block position matches.")) return;
  33. //Assert.Equal(newBlock.Exists, true, "Newly placed block does not exist, possibly because Sync() skipped/missed/failed.", "Newly placed block exists, Sync() successful.");
  34. }
  35. [APITestCase(TestType.EditMode)]
  36. public static void TestBlockIDCoverage()
  37. {
  38. Assert.Equal(
  39. FullGameFields._dataDb.GetValues<CubeListData>().Keys.Select(ushort.Parse).OrderBy(id => id)
  40. .SequenceEqual(Enum.GetValues(typeof(BlockIDs)).Cast<ushort>().OrderBy(id => id)
  41. .Except(new[] {(ushort) BlockIDs.Invalid})), true,
  42. "Block ID enum is different than the known block types, update needed.",
  43. "Block ID enum matches the known block types.");
  44. }
  45. private static Block[] blocks; // Store placed blocks as some blocks are already present as the workshop and the game save
  46. [APITestCase(TestType.EditMode)]
  47. public static void TestBlockIDs()
  48. {
  49. float3 pos = new float3();
  50. var values = Enum.GetValues(typeof(BlockIDs));
  51. blocks = new Block[values.Length - 1]; // Minus the invalid ID
  52. int i = 0;
  53. foreach (BlockIDs id in values)
  54. {
  55. if (id == BlockIDs.Invalid) continue;
  56. try
  57. {
  58. blocks[i++] = Block.PlaceNew(id, pos);
  59. pos += 0.2f;
  60. }
  61. catch (Exception e)
  62. { //Only print failed case
  63. Assert.Fail($"Failed to place block type {id}: {e}");
  64. return;
  65. }
  66. }
  67. Assert.Pass("Placing all possible block types succeeded.");
  68. }
  69. [APITestCase(TestType.EditMode)]
  70. public static IEnumerator<TaskContract> TestBlockProperties()
  71. { //Uses the result of the previous test case
  72. yield return Yield.It;
  73. if (blocks is null)
  74. yield break;
  75. for (var index = 0; index < blocks.Length; index++)
  76. {
  77. if (index % 50 == 0) yield return Yield.It; //The material or flipped status can only be changed 130 times per submission
  78. var block = blocks[index];
  79. if (!block.Exists) continue;
  80. foreach (var property in block.GetType().GetProperties())
  81. {
  82. if (property.Name == "Material" || property.Name == "Flipped") continue; // TODO: Crashes in game
  83. if (property.Name == "Material" || property.Name == "Flipped")
  84. {
  85. Console.WriteLine("Block type: "+block.Type);
  86. Console.WriteLine("Will set " + property.Name);
  87. yield return new WaitForSecondsEnumerator(1).Continue();
  88. }
  89. //Includes specialised block properties
  90. if (property.SetMethod == null) continue;
  91. var testValues = new (Type, object, Predicate<object>)[]
  92. {
  93. //(type, default value, predicate or null for equality)
  94. (typeof(long), 3, null),
  95. (typeof(int), 4, null),
  96. (typeof(double), 5.2f, obj => Math.Abs((double) obj - 5.2f) < float.Epsilon),
  97. (typeof(float), 5.2f, obj => Math.Abs((float) obj - 5.2f) < float.Epsilon),
  98. (typeof(bool), true, obj => (bool) obj),
  99. (typeof(string), "Test", obj => (string) obj == "Test"), //String equality check
  100. (typeof(float3), (float3) 2, obj => math.all((float3) obj - 2 < (float3) float.Epsilon)),
  101. (typeof(BlockColor), new BlockColor(BlockColors.Aqua, 2), null),
  102. (typeof(float4), (float4) 5, obj => math.all((float4) obj - 5 < (float4) float.Epsilon))
  103. };
  104. var propType = property.PropertyType;
  105. if (!propType.IsValueType) continue;
  106. (object valueToUse, Predicate<object> predicateToUse) = (null, null);
  107. foreach (var (type, value, predicate) in testValues)
  108. {
  109. if (type.IsAssignableFrom(propType))
  110. {
  111. valueToUse = value;
  112. predicateToUse = predicate ?? (obj => Equals(obj, value));
  113. break;
  114. }
  115. }
  116. if (propType.IsEnum)
  117. {
  118. var values = propType.GetEnumValues();
  119. valueToUse = values.GetValue(values.Length / 2);
  120. predicateToUse = val => Equals(val, valueToUse);
  121. }
  122. if (valueToUse == null)
  123. {
  124. Assert.Fail($"Property {block.GetType().Name}.{property.Name} has an unknown type {propType}, test needs fixing.");
  125. yield break;
  126. }
  127. try
  128. {
  129. property.SetValue(block, valueToUse);
  130. }
  131. catch (Exception e)
  132. {
  133. Assert.Fail($"Failed to set property {block.GetType().Name}.{property.Name} to {valueToUse}\n{e}");
  134. }
  135. object got;
  136. try
  137. {
  138. got = property.GetValue(block);
  139. }
  140. catch (Exception e)
  141. {
  142. Assert.Fail($"Failed to get property {block.GetType().Name}.{property.Name}\n{e}");
  143. continue;
  144. }
  145. var attr = property.GetCustomAttribute<TestValueAttribute>();
  146. if (!predicateToUse(got) && (attr == null || !Equals(attr.PossibleValue, got)))
  147. {
  148. Assert.Fail($"Property {block.GetType().Name}.{property.Name} value {got} does not equal {valueToUse} for block {block}.");
  149. yield break;
  150. }
  151. }
  152. }
  153. Assert.Pass("Setting all possible properties of all registered API block types succeeded.");
  154. }
  155. [APITestCase(TestType.EditMode)]
  156. public static IEnumerator<TaskContract> TestDefaultValue()
  157. {
  158. for (int i = 0; i < 2; i++)
  159. { //Tests shared defaults
  160. var block = Block.PlaceNew(BlockIDs.Cube, 1);
  161. while (!block.Exists)
  162. yield return Yield.It;
  163. block.Remove();
  164. while (block.Exists)
  165. yield return Yield.It;
  166. if(!Assert.Equal(block.Position, default,
  167. $"Block position default value {block.Position} is incorrect, should be 0.",
  168. $"Block position default value {block.Position} matches default."))
  169. yield break;
  170. block.Position = 4;
  171. }
  172. }
  173. [APITestCase(TestType.EditMode)]
  174. public static void TestDampedSpring()
  175. {
  176. Block newBlock = Block.PlaceNew(BlockIDs.DampedSpring, Unity.Mathematics.float3.zero + 1);
  177. DampedSpring b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler
  178. Assert.Errorless(() => { b = (DampedSpring) newBlock; }, "Casting block to DampedSpring raised an exception: ", "Casting block to DampedSpring completed without issue.");
  179. if (!Assert.CloseTo(b.Stiffness, 1f, $"DampedSpring.Stiffness {b.Stiffness} does not equal default value, possibly because it failed silently.", "DampedSpring.Stiffness is close enough to default.")) return;
  180. if (!Assert.CloseTo(b.Damping, 0.1f, $"DampedSpring.Damping {b.Damping} does not equal default value, possibly because it failed silently.", "DampedSpring.Damping is close enough to default.")) return;
  181. if (!Assert.CloseTo(b.MaxExtension, 0.3f, $"DampedSpring.MaxExtension {b.MaxExtension} does not equal default value, possibly because it failed silently.", "DampedSpring.MaxExtension is close enough to default.")) return;
  182. }
  183. /*[APITestCase(TestType.Game)]
  184. public static void TestMusicBlock1()
  185. {
  186. Block newBlock = Block.PlaceNew(BlockIDs.MusicBlock, Unity.Mathematics.float3.zero + 2);
  187. MusicBlock b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler
  188. Assert.Errorless(() => { b = newBlock.Specialise<MusicBlock>(); }, "Block.Specialize<MusicBlock>() raised an exception: ", "Block.Specialize<MusicBlock>() completed without issue.");
  189. if (!Assert.NotNull(b, "Block.Specialize<MusicBlock>() returned null, possibly because it failed silently.", "Specialized MusicBlock is not null.")) return;
  190. if (!Assert.CloseTo(b.Volume, 100f, $"MusicBlock.Volume {b.Volume} does not equal default value, possibly because it failed silently.", "MusicBlock.Volume is close enough to default.")) return;
  191. if (!Assert.Equal(b.TrackIndex, 0, $"MusicBlock.TrackIndex {b.TrackIndex} does not equal default value, possibly because it failed silently.", "MusicBlock.TrackIndex is equal to default.")) return;
  192. _musicBlock = b;
  193. }
  194. private static MusicBlock _musicBlock;
  195. [APITestCase(TestType.EditMode)]
  196. public static void TestMusicBlock2()
  197. {
  198. //Block newBlock = Block.GetLastPlacedBlock();
  199. var b = _musicBlock;
  200. if (!Assert.NotNull(b, "Block.Specialize<MusicBlock>() returned null, possibly because it failed silently.", "Specialized MusicBlock is not null.")) return;
  201. b.IsPlaying = true; // play sfx
  202. if (!Assert.Equal(b.IsPlaying, true, $"MusicBlock.IsPlaying {b.IsPlaying} does not equal true, possibly because it failed silently.", "MusicBlock.IsPlaying is set properly.")) return;
  203. if (!Assert.Equal(b.ChannelType, ChannelType.None, $"MusicBlock.ChannelType {b.ChannelType} does not equal default value, possibly because it failed silently.", "MusicBlock.ChannelType is equal to default.")) return;
  204. //Assert.Log(b.Track.ToString());
  205. if (!Assert.Equal(b.Track.ToString(), new Guid("3237ff8f-f5f2-4f84-8144-496ca280f8c0").ToString(), $"MusicBlock.Track {b.Track} does not equal default value, possibly because it failed silently.", "MusicBlock.Track is equal to default.")) return;
  206. }
  207. [APITestCase(TestType.EditMode)]
  208. public static void TestLogicGate()
  209. {
  210. Block newBlock = Block.PlaceNew(BlockIDs.NOTLogicBlock, Unity.Mathematics.float3.zero + 1);
  211. LogicGate b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler
  212. Assert.Errorless(() => { b = newBlock.Specialise<LogicGate>(); }, "Block.Specialize<LogicGate>() raised an exception: ", "Block.Specialize<LogicGate>() completed without issue.");
  213. if (!Assert.NotNull(b, "Block.Specialize<LogicGate>() returned null, possibly because it failed silently.", "Specialized LogicGate is not null.")) return;
  214. if (!Assert.Equal(b.InputCount, 1u, $"LogicGate.InputCount {b.InputCount} does not equal default value, possibly because it failed silently.", "LogicGate.InputCount is default.")) return;
  215. if (!Assert.Equal(b.OutputCount, 1u, $"LogicGate.OutputCount {b.OutputCount} does not equal default value, possibly because it failed silently.", "LogicGate.OutputCount is default.")) return;
  216. if (!Assert.NotNull(b, "Block.Specialize<LogicGate>() returned null, possibly because it failed silently.", "Specialized LogicGate is not null.")) return;
  217. //if (!Assert.Equal(b.PortName(0, true), "Input", $"LogicGate.PortName(0, input:true) {b.PortName(0, true)} does not equal default value, possibly because it failed silently.", "LogicGate.PortName(0, input:true) is close enough to default.")) return;
  218. LogicGate target = null;
  219. if (!Assert.Errorless(() => { target = Block.PlaceNew<LogicGate>(BlockIDs.ANDLogicBlock, Unity.Mathematics.float3.zero + 2); })) return;
  220. Wire newWire = null;
  221. if (!Assert.Errorless(() => { newWire = b.Connect(0, target, 0);})) return;
  222. if (!Assert.NotNull(newWire, "SignalingBlock.Connect(...) returned null, possible because it failed silently.", "SignalingBlock.Connect(...) returned a non-null value.")) return;
  223. }*/
  224. /*[APITestCase(TestType.EditMode)]
  225. public static void TestSpecialiseError()
  226. {
  227. Block newBlock = Block.PlaceNew(BlockIDs.Bench, new float3(1, 1, 1));
  228. if (Assert.Errorful<BlockTypeException>(() => newBlock.Specialise<MusicBlock>(), "Block.Specialise<MusicBlock>() was expected to error on a bench block.", "Block.Specialise<MusicBlock>() errored as expected for a bench block.")) return;
  229. }*/
  230. }
  231. #endif
  232. }