Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blksmithchain committed Jun 22, 2023
1 parent 8c888c3 commit 6304d1e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
26 changes: 17 additions & 9 deletions Assets/Scripts/UI/CraftingUI.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void DisplayNextRollSprite()
currentRollSpriteIndex = (currentRollSpriteIndex + 1) % rollResults.rollSprites.Length;
Transform nftImage = rollSlots[0].transform.Find("NFT_Image");
nftImage.GetComponent<Image>().sprite = rollResults.rollSprites[currentRollSpriteIndex];
rollNameText.text = rollResults.rollNames[currentRollSpriteIndex];
SetRollNameText(rollResults.rollNames[currentRollSpriteIndex]);
float rollPercentage = (float)rollResults.rollPercentageRolls[currentRollSpriteIndex] / rollResults.totalOdds * 100;
rollPercentage = (float)Math.Round(rollPercentage, 1);
SetRollPercentageText(rollPercentage.ToString());
Expand All @@ -98,7 +99,7 @@ public void DisplayPreviousRollSprite()
}
Transform nftImage = rollSlots[0].transform.Find("NFT_Image");
nftImage.GetComponent<Image>().sprite = rollResults.rollSprites[currentRollSpriteIndex];
rollNameText.text = rollResults.rollNames[currentRollSpriteIndex];
SetRollNameText(rollResults.rollNames[currentRollSpriteIndex]);
float rollPercentage = ((float)rollResults.rollPercentageRolls[currentRollSpriteIndex] / rollResults.totalOdds * 100);
rollPercentage = (float)Math.Round(rollPercentage, 1);
SetRollPercentageText(rollPercentage.ToString());
Expand All @@ -117,7 +118,7 @@ public void DisplayRollData(RollResult rollResult)

if (rollResult.rollSprites != null && rollResult.rollSprites.Length > 0)
{
rollNameText.text = rollResults.rollNames[0];
SetRollNameText(rollResults.rollNames[0]);
float rollPercentage = ((float)rollResults.rollPercentageRolls[0] / rollResults.totalOdds * 100);
rollPercentage = (float)Math.Round(rollPercentage, 1);
SetRollPercentageText(rollPercentage.ToString());
Expand All @@ -135,7 +136,7 @@ public async void DisplayRollImage(RollResult rollResult)

}
}
public void DisplayRequirementsData(RequiredAssetsResult requiredAssetResult, IndexIngredientAssetsResult indexIngredientAssetsResult)
public async Task DisplayRequirementsData(RequiredAssetsResult requiredAssetResult, IndexIngredientAssetsResult indexIngredientAssetsResult)
{
int totalRequiredAssets = requiredAssetResult.requiredAssetAmount.Sum();
InstantiateRequirementSlots(totalRequiredAssets);
Expand All @@ -159,17 +160,23 @@ public void DisplayRequirementsData(RequiredAssetsResult requiredAssetResult, In

}
}
SortAndSelectAssetsInRequirementSlots(requirementSlots,indexIngredientAssetsResult);
var sortedRequirementSlots = SortAssets(requirementSlots);
await SetupRequirements(sortedRequirementSlots, indexIngredientAssetsResult);
uIController.ChangePrefabColor();

}

// Sort and select the assets in the requirement slots based on priority.
public async void SortAndSelectAssetsInRequirementSlots(GameObject[] requirementSlots, IndexIngredientAssetsResult indexIngredientAssetsResult)
// Method for sorting the assets based on priority.
public List<GameObject> SortAssets(GameObject[] requirementSlots)
{
string[] priorityOrder = { "TEMPLATE_INGREDIENT", "ATTRIBUTE_INGREDIENT", "SCHEMA_INGREDIENT", "COLLECTION_INGREDIENT" };
var sortedRequirementSlots = requirementSlots.OrderBy(slot => Array.IndexOf(priorityOrder, slot.GetComponent<TemplateNFT>().GetRequirementType())).ToList();
return sortedRequirementSlots;
}

// Method for setting up the requirements based on the sorted assets.
public async Task SetupRequirements(List<GameObject> sortedRequirementSlots, IndexIngredientAssetsResult indexIngredientAssetsResult)
{
List<string> selectedAssetIds = new List<string>();
foreach (var requirementSlot in sortedRequirementSlots)
{
Expand All @@ -193,6 +200,7 @@ public async void SortAndSelectAssetsInRequirementSlots(GameObject[] requirement
}
}


private void DisplayFTIngredientRequirement(RequiredAssetsResult requiredAssetResult, int currentRequirementSlotIndex, int i)
{
for (int j = 0; j < requiredAssetResult.requiredAssetAmount[i]; j++)
Expand Down Expand Up @@ -226,11 +234,11 @@ private void DisplayNFTIngredientRequirement(RequiredAssetsResult requiredAssetR
}
}

public void DisplayAssetImages(RequiredAssetsResult requiredAssetResult,IndexIngredientAssetsResult indexIngredientAssetsResult,RollResult rollResult,int securityId)
public async Task DisplayAssetImages(RequiredAssetsResult requiredAssetResult,IndexIngredientAssetsResult indexIngredientAssetsResult,RollResult rollResult,int securityId)
{
LoadDefaultRollImages(rollResult);
DisplayRollData(rollResult);
DisplayRequirementsData(requiredAssetResult, indexIngredientAssetsResult);
await DisplayRequirementsData(requiredAssetResult, indexIngredientAssetsResult);
DisplayRollPaginationArrows(rollResult.rollSprites);
DisplayBlendProtection(securityId);
LoadRollImages(rollResult);
Expand Down
36 changes: 23 additions & 13 deletions Assets/Tests/EditModeTests/CraftingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,39 @@ public void TearDown()
Object.DestroyImmediate(craftingUI.gameObject);
}

/* [Test]
[Test]
public void TestSortAndSelectAssetsInRequirementSlots_WithUnique_AssetIds()
{
SetUpRequirements(new int[] { 1, 2, 3, 4, 5 });
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, CreateIndexIngredientAssetsResult("Asset", 5));
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, CreateIndexIngredientAssetsResult("Asset", 5)).Wait();

Check failure on line 35 in Assets/Tests/EditModeTests/CraftingTest.cs

View workflow job for this annotation

GitHub Actions / EditMode test results

CraftingTest.TestSortAndSelectAssetsInRequirementSlots_WithUnique_AssetIds

System.AggregateException : One or more errors occurred. (Object reference not set to an instance of an object) ----> System.NullReferenceException : Object reference not set to an instance of an object
Raw output
  at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in <12d27ebfb46146b18b40876234894c77>:0 
  at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in <12d27ebfb46146b18b40876234894c77>:0 
  at System.Threading.Tasks.Task.Wait () [0x00000] in <12d27ebfb46146b18b40876234894c77>:0 
  at CraftingTest.TestSortAndSelectAssetsInRequirementSlots_WithUnique_AssetIds () [0x0002c] in /github/workspace/Assets/Tests/EditModeTests/CraftingTest.cs:35 
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <12d27ebfb46146b18b40876234894c77>:0 
--NullReferenceException
  at CraftingUI.SetupRequirements (System.Collections.Generic.List`1[T] sortedRequirementSlots, IndexIngredientAssetsResult indexIngredientAssetsResult) [0x00121] in /github/workspace/Assets/Scripts/UI/CraftingUI.cs:193 
AssertRequirements(new string[] { "Asset1", "Asset2", "Asset3", "Asset4", "Asset5" });
}

[Test]
public void TestSortAndSelectAssetsInRequirementSlots_WithNoMatchingIngredients_ShouldNotSelectAnyAssets()
{
SetUpRequirements(new int[] { 6, 7, 8, 9, 10 });
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, CreateIndexIngredientAssetsResult("Asset", 5));
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, CreateIndexIngredientAssetsResult("Asset", 5)).Wait();
AssertRequirements(new string[] { null, null, null, null, null });
}

[Test]
public void TestSortAndSelectAssetsInRequirementSlots_WithMultipleMatchingIngredients_ShouldPrioritizeTemplateIngredient()
{
SetUpRequirements(new int[] { 1, 3, 4, 2, 0 }, new string[] { "ATTRIBUTE_INGREDIENT", "SCHEMA_INGREDIENT", "FT_INGREDIENT", "SCHEMA_INGREDIENT", "TEMPLATE_INGREDIENT" });
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, new IndexIngredientAssetsResult()
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, new IndexIngredientAssetsResult()

Check failure on line 53 in Assets/Tests/EditModeTests/CraftingTest.cs

View workflow job for this annotation

GitHub Actions / EditMode test results

CraftingTest.TestSortAndSelectAssetsInRequirementSlots_WithMultipleMatchingIngredients_ShouldPrioritizeTemplateIngredient

System.AggregateException : One or more errors occurred. (Object reference not set to an instance of an object) ----> System.NullReferenceException : Object reference not set to an instance of an object
Raw output
  at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in <12d27ebfb46146b18b40876234894c77>:0 
  at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in <12d27ebfb46146b18b40876234894c77>:0 
  at System.Threading.Tasks.Task.Wait () [0x00000] in <12d27ebfb46146b18b40876234894c77>:0 
  at CraftingTest.TestSortAndSelectAssetsInRequirementSlots_WithMultipleMatchingIngredients_ShouldPrioritizeTemplateIngredient () [0x00059] in /github/workspace/Assets/Tests/EditModeTests/CraftingTest.cs:53 
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <12d27ebfb46146b18b40876234894c77>:0 
--NullReferenceException
  at CraftingUI.SetupRequirements (System.Collections.Generic.List`1[T] sortedRequirementSlots, IndexIngredientAssetsResult indexIngredientAssetsResult) [0x00121] in /github/workspace/Assets/Scripts/UI/CraftingUI.cs:193 
{
assetIds = new List<string>() { "Asset1", "Asset2", "Asset3", "Asset4", "Asset5", "Asset1" },
indexId = new List<int>() { 0, 2, 3, 4, 0, 1 },
mintNumbers = new List<int>() { 0,0,0,0,0,0 }
mintNumbers = new List<int>() { 0, 0, 0, 0, 0, 0 }

});
}).Wait();
AssertRequirements(new string[] { null, "Asset3", null, "Asset2", "Asset1" });
}

[Test]
public void TestSortAndSelectAssetsInRequirementSlots_FT_INGREDIENT_ShouldBeEmpty()
{
Expand All @@ -71,7 +75,8 @@ public void TestSortAndSelectAssetsInRequirementSlots_FT_INGREDIENT_ShouldBeEmpt
requirementSlots[0].GetComponent<TemplateNFT>().SetBlendIngredientIndex(0);

// Act
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, indexIngredientAssetsResult);
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, indexIngredientAssetsResult).Wait();

Check failure on line 79 in Assets/Tests/EditModeTests/CraftingTest.cs

View workflow job for this annotation

GitHub Actions / EditMode test results

CraftingTest.TestSortAndSelectAssetsInRequirementSlots_FT_INGREDIENT_ShouldBeEmpty

System.AggregateException : One or more errors occurred. (Object reference not set to an instance of an object) ----> System.NullReferenceException : Object reference not set to an instance of an object
Raw output
  at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in <12d27ebfb46146b18b40876234894c77>:0 
  at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in <12d27ebfb46146b18b40876234894c77>:0 
  at System.Threading.Tasks.Task.Wait () [0x00000] in <12d27ebfb46146b18b40876234894c77>:0 
  at CraftingTest.TestSortAndSelectAssetsInRequirementSlots_FT_INGREDIENT_ShouldBeEmpty () [0x00085] in /github/workspace/Assets/Tests/EditModeTests/CraftingTest.cs:79 
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <12d27ebfb46146b18b40876234894c77>:0 
--NullReferenceException
  at CraftingUI.SetupRequirements (System.Collections.Generic.List`1[T] sortedRequirementSlots, IndexIngredientAssetsResult indexIngredientAssetsResult) [0x00121] in /github/workspace/Assets/Scripts/UI/CraftingUI.cs:193 

// Assert
Assert.IsNull(requirementSlots[0].transform.Find("Selected_Ingredient_Background/SelectedIngredient").GetComponent<TextMeshProUGUI>().text);
Expand All @@ -81,23 +86,25 @@ public void TestSortAndSelectAssetsInRequirementSlots_FT_INGREDIENT_ShouldBeEmpt
public void TestSortAndSelectAssetsInRequirementSlots_WithEmptyIngredient()
{
SetUpRequirements(new int[] { 1, 2, 3, 4, 5 });
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, new IndexIngredientAssetsResult()
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, new IndexIngredientAssetsResult()
{
assetIds = new List<string>() { },
indexId = new List<int>() { 1, 2, 3, 4, 5 },
mintNumbers = new List<int>() { 0, 0, 0, 0, 0, 0 }

});
}).Wait();
AssertRequirements(new string[] { null, null, null, null, null });
}

[Test]
public void TestSortAndSelectAssetsInRequirementSlots_WithLessIngredients_ShouldSelectAssetsForMatchingIndexes()
{
SetUpRequirements(new int[] { 1, 2, 3, 4, 5 });
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, CreateIndexIngredientAssetsResult("Asset", 3));
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, CreateIndexIngredientAssetsResult("Asset", 3)).Wait();

Check failure on line 105 in Assets/Tests/EditModeTests/CraftingTest.cs

View workflow job for this annotation

GitHub Actions / EditMode test results

CraftingTest.TestSortAndSelectAssetsInRequirementSlots_WithLessIngredients_ShouldSelectAssetsForMatchingIndexes

System.AggregateException : One or more errors occurred. (Object reference not set to an instance of an object) ----> System.NullReferenceException : Object reference not set to an instance of an object
Raw output
  at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in <12d27ebfb46146b18b40876234894c77>:0 
  at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in <12d27ebfb46146b18b40876234894c77>:0 
  at System.Threading.Tasks.Task.Wait () [0x00000] in <12d27ebfb46146b18b40876234894c77>:0 
  at CraftingTest.TestSortAndSelectAssetsInRequirementSlots_WithLessIngredients_ShouldSelectAssetsForMatchingIndexes () [0x0002c] in /github/workspace/Assets/Tests/EditModeTests/CraftingTest.cs:105 
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <12d27ebfb46146b18b40876234894c77>:0 
--NullReferenceException
  at CraftingUI.SetupRequirements (System.Collections.Generic.List`1[T] sortedRequirementSlots, IndexIngredientAssetsResult indexIngredientAssetsResult) [0x00121] in /github/workspace/Assets/Scripts/UI/CraftingUI.cs:193 
AssertRequirements(new string[] { "Asset1", "Asset2", "Asset3", null, null });
}*/
}

private GameObject[] CreateSlots(int number)
{
Expand Down Expand Up @@ -144,18 +151,21 @@ private void AssertRequirements(string[] expectedTexts)

private IndexIngredientAssetsResult CreateIndexIngredientAssetsResult(string baseAssetName, int number)
{
IndexIngredientAssetsResult result = new IndexIngredientAssetsResult()
var result = new IndexIngredientAssetsResult()
{
assetIds = new List<string>(),
indexId = new List<int>(),
mintNumbers = new List<int>()
mintNumbers = new List<int>(),
ingredientSpriteHashes = new List<string>()

};

for (int i = 0; i < number; i++)
{
result.assetIds.Add($"{baseAssetName}{i + 1}");
result.indexId.Add(i + 1);
result.mintNumbers.Add(i);
result.ingredientSpriteHashes.Add("fakeimghash");
}

return result;
Expand Down

0 comments on commit 6304d1e

Please sign in to comment.