using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DataLoader;
using Svelto.Tasks;
using Unity.Mathematics;
using TechbloxModdingAPI.App;
using TechbloxModdingAPI.Tests;
using TechbloxModdingAPI.Utility;
namespace TechbloxModdingAPI.Blocks
{
#if TEST
///
/// Block test cases. Not accessible in release versions.
///
[APITestClass]
public static class BlockTests
{
[APITestCase(TestType.Game)] //At least one block must be placed for simulation to work
public static void TestPlaceNew()
{
Block newBlock = Block.PlaceNew(BlockIDs.Cube, float3.zero);
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.");
}
[APITestCase(TestType.EditMode)]
public static void TestInitProperty()
{
Block newBlock = Block.PlaceNew(BlockIDs.Cube, float3.zero + 2);
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;
//Assert.Equal(newBlock.Exists, true, "Newly placed block does not exist, possibly because Sync() skipped/missed/failed.", "Newly placed block exists, Sync() successful.");
}
[APITestCase(TestType.EditMode)]
public static void TestBlockIDCoverage()
{
Assert.Equal(
FullGameFields._dataDb.GetValues().Keys.Select(ushort.Parse).OrderBy(id => id)
.SequenceEqual(Enum.GetValues(typeof(BlockIDs)).Cast().OrderBy(id => id)
.Except(new[] {(ushort) BlockIDs.Invalid})), true,
"Block ID enum is different than the known block types, update needed.",
"Block ID enum matches the known block types.");
}
private static Block[] blocks; // Store placed blocks as some blocks are already present as the workshop and the game save
[APITestCase(TestType.EditMode)]
public static void TestBlockIDs()
{
float3 pos = new float3();
var values = Enum.GetValues(typeof(BlockIDs));
blocks = new Block[values.Length - 1]; // Minus the invalid ID
int i = 0;
foreach (BlockIDs id in values)
{
if (id == BlockIDs.Invalid) continue;
try
{
blocks[i++] = Block.PlaceNew(id, pos);
pos += 0.2f;
}
catch (Exception e)
{ //Only print failed case
Assert.Fail($"Failed to place block type {id}: {e}");
return;
}
}
Assert.Pass("Placing all possible block types succeeded.");
}
[APITestCase(TestType.EditMode)]
public static IEnumerator TestBlockProperties()
{ //Uses the result of the previous test case
yield return Yield.It;
if (blocks is null)
yield break;
for (var index = 0; index < blocks.Length; index++)
{
if (index % 50 == 0) yield return Yield.It; //The material or flipped status can only be changed 130 times per submission
var block = blocks[index];
if (!block.Exists) continue;
foreach (var property in block.GetType().GetProperties())
{
if (property.Name == "Material" || property.Name == "Flipped") continue; // TODO: Crashes in game
//Includes specialised block properties
if (property.SetMethod == null) continue;
var testValues = new (Type, object, Predicate