Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored HandManager and Added comprehensive test coverage for HandManager #135

Merged
merged 8 commits into from
Apr 24, 2024
Merged
72 changes: 30 additions & 42 deletions Crypto Wars/Assets/Scripts/GUI/HandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class HandManager : MonoBehaviour
private Card newSlot1Card, newSlot2Card, newSlot3Card, newSlot4Card, newSlot5Card;
private int cardsInHandLast, newSlot1Count, newSlot2Count, newSlot3Count, newSlot4Count, newSlot5Count;
private GameObject panel, handGrid, canvas;
private TextMeshProUGUI handSlot1Text, handSlot2Text, handSlot3Text, handSlot4Text, handSlot5Text;
public TextMeshProUGUI handSlot1Text, handSlot2Text, handSlot3Text, handSlot4Text, handSlot5Text;
private bool visible, cardsUpdated;
private Image image;

Expand Down Expand Up @@ -112,14 +112,12 @@ void Start()



for (int i = 0; i < 5; i++) {
// SetupSlot(i); // Create each slot in the inventory
Debug.Log("Hand Slot " + Slots.Count);
}
// for (int i = 0; i < 5; i++) {
// // SetupSlot(i); // Create each slot in the inventory
// Debug.Log("Hand Slot " + Slots.Count);
// }

ForceHideAll();

visible = true;
cardsUpdated = false;
cardsInHandLast = 0;
}
Expand All @@ -144,36 +142,35 @@ void Update()
ChangeDisplayedCards();
}

// DEBUG: Remove before shipping?
if (Input.GetKeyDown(KeyCode.H)) {
visible = !visible;
ToggleHideAll();
Debug.Log("Toggling Hand Visibility Status: " + visible);
}
// DEBUG: Toggles hand info on/off
// if (Input.GetKeyDown(KeyCode.H)) {
// ToggleHideAll();
// Debug.Log("Toggling Hand Visibility Status: " + visible);
// }

// DEBUG: Remove before shipping?
if (Input.GetKeyDown(KeyCode.P)) {
int count = 4;
PartialReveal(count);
Debug.Log("Partially Revealing: " + count);
}
// DEBUG: Reveals the first *count* cards in hand
// if (Input.GetKeyDown(KeyCode.P)) {
// int count = 4;
// PartialReveal(count);
// Debug.Log("Partially Revealing: " + count);
// }

// DEBUG: Remove before shipping?
if (Input.GetKeyDown(KeyCode.C)) {
visible = false;
ForceHideAll();
Debug.Log("Forcing Hand Visibility Status: " + visible);
}
// DEBUG: Forces all hand display text to hide
// if (Input.GetKeyDown(KeyCode.C)) {
// ForceHideAll();
// Debug.Log("Forcing Hand Visibility Status: " + visible);
// }

// DEBUG: Remove before shipping?
if (Input.GetKeyDown(KeyCode.Q)) {
Debug.Log("Testing Cards In Hand: " + currentPlayerHand.GetHandCards().Count);
}
// DEBUG: Tells the player how many cards are in their hand
// if (Input.GetKeyDown(KeyCode.Q)) {
// Debug.Log("Testing Cards In Hand: " + currentPlayerHand.GetHandCards().Count);
// }
}

// Reveals the requested number of card names
public void PartialReveal(int count)
{
visible = true;
image.enabled = true;

switch (count)
Expand Down Expand Up @@ -211,6 +208,7 @@ public void PartialReveal(int count)
// DEBUG: Hides/Reveals all info about the hand slots, regardless of status
public void ToggleHideAll()
{
visible = !visible;
image.enabled = visible;
handSlot1Text.enabled = visible;
handSlot2Text.enabled = visible;
Expand All @@ -222,6 +220,7 @@ public void ToggleHideAll()
// DEBUG: Forcibly hides all info about hand slots
public void ForceHideAll()
{
visible = false;
image.enabled = false;
handSlot1Text.enabled = false;
handSlot2Text.enabled = false;
Expand Down Expand Up @@ -309,18 +308,7 @@ public void EditCard(ref Card cardSlot, Card oldCard, ref int slotNum, int index
}
}

// Sets the text of a given TextMeshProUGUI object
public void SetText(string textObject, int index, string newText) {
if(Slots[index] != null)
Slots[index].transform.Find(textObject).GetComponent<TextMeshProUGUI>().text = newText;
}


// Sets up a hand slot and sets the text values to an empty string
public void SetupSlot(int index)
{
Slots.Add(gameObject.transform.Find("Hand Grid").Find("Hand_Slot_" + (index + 1)).gameObject);
SetText("HandCardName", index, "");
SetText("HandAmount", index, "");
public Hand GetCurrentPlayerHand() {
return currentPlayerHand;
}
}
141 changes: 141 additions & 0 deletions Crypto Wars/Assets/Scripts/Test_PlayMode/HandManagerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using NUnit.Framework;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using TMPro;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
using System;
using System.Collections;
using System.Collections.Generic;

public class HandManagerTest
{
public HandManager testManager;
public string slot1Text, slot2Text, slot3Text, slot4Text, slot5Text;
public TMPro.TextMeshProUGUI testText;
Vector3 startPos, previousPos;

[UnitySetUp]
public IEnumerator SetUp()
{
SceneManager.LoadScene("Project", LoadSceneMode.Single);
Vector3 startPos = new Vector3(0, 0, 0);
yield return null;
yield return new EnterPlayMode();
}

[UnityTearDown]
public IEnumerator TearDown()
{
yield return new ExitPlayMode();
}

[UnityTest]
public IEnumerator MouseMovementTest()
{
yield return new WaitForSeconds(0.5f);
Vector3 previousPos = startPos;
}

[UnityTest]
public IEnumerator PartialRevealTest()
{
yield return new WaitForSeconds(0.5f);
GameObject camera = GameObject.Find("Main Camera");
testManager = camera.GetComponent<HandManager>();
Image image = GameObject.Find("Handheld Cards").GetComponent<Image>();
// visible starts as false
testManager.PartialReveal(1);
Assert.IsTrue(image.enabled);
testManager.ForceHideAll();
testManager.PartialReveal(2);
Assert.IsTrue(image.enabled);
testManager.ForceHideAll();
testManager.PartialReveal(3);
Assert.IsTrue(image.enabled);
testManager.ForceHideAll();
testManager.PartialReveal(4);
Assert.IsTrue(image.enabled);
testManager.ForceHideAll();
testManager.PartialReveal(5);
Assert.IsTrue(image.enabled);
}

[UnityTest]
public IEnumerator ToggleHideAllTest()
{
yield return new WaitForSeconds(0.5f);
GameObject camera = GameObject.Find("Main Camera");
testManager = camera.GetComponent<HandManager>();
Image image = GameObject.Find("Handheld Cards").GetComponent<Image>();
// visible starts as false
testManager.ToggleHideAll();
Assert.IsTrue(image.enabled);
// test for visible is true
testManager.ToggleHideAll();
Assert.IsFalse(image.enabled);
}

[UnityTest]
public IEnumerator ForceHideAllTest()
{
yield return new WaitForSeconds(0.5f);
Image image = GameObject.Find("Handheld Cards").GetComponent<Image>();
// TextMeshProUGUI slot1Text = GameObject.Find("Hand_Slot_1").GetComponentInChildren<TextMeshProUGUI>();
Assert.IsFalse(image.enabled);
// Assert.IsFalse(slot1Text.enabled); // not working for whatever reason
}

[UnityTest]
public IEnumerator ChangeDisplayedCardsTest()
{
yield return new WaitForSeconds(0.5f);
GameObject camera = GameObject.Find("Main Camera");
testManager = camera.GetComponent<HandManager>();
Hand testHand = testManager.GetCurrentPlayerHand();
Image image = GameObject.Find("Handheld Cards").GetComponent<Image>();
Card testCard1 = new Card(null, "Test1");
Card testCard2 = new Card(null, "Test2");
Card testCard3 = new Card(null, "Test3");
Card testCard4 = new Card(null, "Test4");
Card testCard5 = new Card(null, "Test5");

testText = testManager.GetComponent<TMPro.TextMeshProUGUI>();

// visible starts as false
testManager.ChangeDisplayedCards();
// player should not have any cards, so should go to ForceHideAll, setting visibility to false
Assert.IsFalse(image.enabled);

testHand.AddCardtoHand(testCard1);
testManager.ChangeDisplayedCards();
slot1Text = testManager.GetComponent<HandManager>().handSlot1Text.text;
// player should have 1 unique card, so should only modify handSlot1Text
Assert.AreEqual("1x " + testCard1.GetName(), slot1Text);

testHand.AddCardtoHand(testCard2);
testManager.ChangeDisplayedCards();
slot2Text = testManager.GetComponent<HandManager>().handSlot2Text.text;
// player should have 2 unique cards
Assert.AreEqual("1x " + testCard2.GetName(), slot2Text);

testHand.AddCardtoHand(testCard3);
testManager.ChangeDisplayedCards();
slot3Text = testManager.GetComponent<HandManager>().handSlot3Text.text;
// player should have 3 unique cards
Assert.AreEqual("1x " + testCard3.GetName(), slot3Text);

testHand.AddCardtoHand(testCard4);
testManager.ChangeDisplayedCards();
slot4Text = testManager.GetComponent<HandManager>().handSlot4Text.text;
// player should have 4 unique cards
Assert.AreEqual("1x " + testCard4.GetName(), slot4Text);

testHand.AddCardtoHand(testCard5);
testManager.ChangeDisplayedCards();
slot5Text = testManager.GetComponent<HandManager>().handSlot5Text.text;
// player should have 5 unique cards
Assert.AreEqual("1x " + testCard5.GetName(), slot5Text);
}
}
11 changes: 11 additions & 0 deletions Crypto Wars/Assets/Scripts/Test_PlayMode/HandManagerTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading