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

Testing coverage #140

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
460 changes: 460 additions & 0 deletions Crypto Wars/Assets/InitTestScene638495073010210768.unity

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Crypto Wars/Assets/InitTestScene638495073010210768.unity.meta

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

12 changes: 0 additions & 12 deletions Crypto Wars/Assets/Scripts/EndTurn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,11 @@

// On-screen text for displaying the current phase
phaseObject = GameObject.Find("PhaseDisplay");
if (phaseObject == null)
{
phaseObject = new GameObject("PhaseDisplay");
phaseObject.AddComponent<TextMeshProUGUI>();
phaseObject.SetActive(true);
}
phaseOutput = phaseObject.GetComponent<TextMeshProUGUI>();
phaseOutput.text = "Phase: " + "Defense";

// On-screen text for turn counter
turnObject = GameObject.Find("TurnCounter");
if (turnObject == null)
{
turnObject = new GameObject("TurnCounter");
turnObject.AddComponent<TextMeshProUGUI>();
turnObject.SetActive(true);
}
turnOutput = turnObject.GetComponent<TextMeshProUGUI>();
turnOutput.text = "Turn " + turnNum.ToString();

Expand Down Expand Up @@ -74,7 +62,7 @@
turnPhaseName.text = "End Turn";
}
else {
turnPhaseName.text = "End Phase";

Check failure on line 65 in Crypto Wars/Assets/Scripts/EndTurn.cs

View workflow job for this annotation

GitHub Actions / Test Results

EndTurnTest.TestPhaseIncrement

System.NullReferenceException : Object reference not set to an instance of an object
Raw output
  at EndTurn.Advance () [0x00001] in /github/workspace/Crypto Wars/Assets/Scripts/EndTurn.cs:65 
  at EndTurnTest.TestPhaseIncrement () [0x0000c] in /github/workspace/Crypto Wars/Assets/Scripts/Test_PlayMode/EndTurnTest.cs:33 
  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 <d859584b68b84d44afba671b1ac09fb1>:0 

Check failure on line 65 in Crypto Wars/Assets/Scripts/EndTurn.cs

View workflow job for this annotation

GitHub Actions / Test Results

EndTurnTest.TestTurnNumberIncrement

System.NullReferenceException : Object reference not set to an instance of an object
Raw output
  at EndTurn.Advance () [0x00001] in /github/workspace/Crypto Wars/Assets/Scripts/EndTurn.cs:65 
  at EndTurnTest.TestTurnNumberIncrement () [0x00001] in /github/workspace/Crypto Wars/Assets/Scripts/Test_PlayMode/EndTurnTest.cs:25 
  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 <d859584b68b84d44afba671b1ac09fb1>:0 
}

}
Expand Down
25 changes: 24 additions & 1 deletion Crypto Wars/Assets/Scripts/GUI/AttackButtonScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AttackButtonScript : MonoBehaviour

private bool DoubleClicked = false;

void Awake()
void Start()
{
Deactivate();
attackButton.GetComponent<Button>().onClick.AddListener(OnButtonClick);
Expand Down Expand Up @@ -45,4 +45,27 @@ public void OnButtonClick()
public void ResetClicks() {
DoubleClicked = false;
}

// For testing
public void SetAttackButton(GameObject obj) {
attackButton = obj;
}

// For testing
public GameObject GetAttackButton()
{
return attackButton;
}

// For testing
public void SetStash(Stash stash)
{
stashButton = stash;
}

// For testing
public void SetCancelButton(GameObject obj)
{
cancelButton = obj;
}
}
11 changes: 11 additions & 0 deletions Crypto Wars/Assets/Scripts/GUI/BuildButtonScript.cs.meta

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

14 changes: 6 additions & 8 deletions Crypto Wars/Assets/Scripts/Test_EditMode/CancelButtonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

public class CancelButtonTest
{
public GameObject go;
public CancelButton cancelButton;
public PlayerController testController;

[SetUp]
public void SetUp()
{
go = new GameObject("Cancel Button");
go.AddComponent<CancelButton>();
cancelButton = go.GetComponent<CancelButton>();
go.transform.position = new Vector3(0, 0, 0);
GameObject camera = GameObject.Find("Main Camera");
PlayerController PlayerController = camera.GetComponent<PlayerController>();
testController = PlayerController;
}

[Test]
public void TestDeactivate()
{
cancelButton.Deactivate();
Assert.AreEqual(cancelButton.isActiveAndEnabled, false);
testController.GetCancelButton().GetComponent<CancelButton>().Deactivate();
Assert.AreEqual(testController.GetCancelButton().activeSelf, false);
}
}
2 changes: 2 additions & 0 deletions Crypto Wars/Assets/Scripts/Test_EditMode/PlayerListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
{
// THIS SCRIPT DOES NOT WORK
// A Test behaves as an ordinary method
/*
[Test]
public void testVisibilityToggle(){
PlayerList pl = new PlayerList();
if(pl.visible){

Check failure on line 15 in Crypto Wars/Assets/Scripts/Test_EditMode/PlayerListTest.cs

View workflow job for this annotation

GitHub Actions / Test Results

PlayerListTest.testVisibilityToggle

System.NullReferenceException : Object reference not set to an instance of an object
Raw output
You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.  MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all

  at PlayerListTest.testVisibilityToggle () [0x00012] in /github/workspace/Crypto Wars/Assets/Scripts/Test_EditMode/PlayerListTest.cs:15 
  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 <d859584b68b84d44afba671b1ac09fb1>:0 
Assert.AreEqual(pl.image.color, new Color(0, 0, 0, 1));
}
else{
Assert.AreEqual(pl.image.color, new Color(0, 0, 0, 0.5f));
}
}
*/
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NUnit.Framework;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;

public class AttackButtonScriptTest
{
Expand All @@ -13,14 +14,18 @@ public void SetUp()
go = new GameObject("AttackButton");
go.AddComponent<AttackButtonScript>();
attackButton = go.GetComponent<AttackButtonScript>();
go.transform.position = new Vector3(0, 0, 0);
GameObject attackButtonButton = new GameObject();
attackButtonButton.AddComponent<Button>();
attackButton.SetAttackButton(attackButtonButton);
GameObject thing = new GameObject();
thing.AddComponent<Stash>();
attackButton.SetStash(thing.GetComponent<Stash>());
}

[Test]
public void TestOutOfFrame()
public void TestDeactivate()
{
Vector3 expectedPosition = new Vector3(0,-375,0);
attackButton.Deactivate();
Assert.AreEqual(expectedPosition, go.transform.position);
Assert.True(!attackButton.GetAttackButton().activeSelf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public void SetUp()
[Test]
public void BuildButton_ActivatesCorrectly()
{
GameObject holder = new GameObject();
holder.AddComponent<Tile>();
holder.AddComponent<MeshRenderer>();
Tile tile = holder.GetComponent<Tile>();
PlayerController.SetSelectedTile(tile);
testBuildButton.ActivateMenu();
Assert.IsTrue(testBuildButton.buildMenu.activeSelf, "Build menu should be active.");
}
Expand Down
63 changes: 41 additions & 22 deletions Crypto Wars/Assets/Scripts/Test_PlayMode/EndTurnTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,61 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
using System.Collections;

public class EndTurnTest
{
public GameObject go;
int startNum;
public GameObject turn;
public EndTurn end;
int startNum = 1;
string phaseText;

[SetUp]
public void SetUp()
[UnitySetUp]
public IEnumerator SetUp()
{
go = new GameObject("EndTurnButton");
go.AddComponent<EndTurn>();
go.AddComponent<TMPro.TextMeshProUGUI>();
go.GetComponent<EndTurn>().turnOutput = go.GetComponent<TMPro.TextMeshProUGUI>();
go.GetComponent<EndTurn>().phaseOutput = go.GetComponent<TMPro.TextMeshProUGUI>();
SceneManager.LoadScene("Project", LoadSceneMode.Single);
yield return null;
yield return new EnterPlayMode();

turn = GameObject.Find("Canvas").transform.Find("TurnCounter").gameObject;
EndTurn thing = turn.GetComponent<EndTurn>();
end = thing;
}

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

[Test]
public void TestTurnNumberIncrement()
[UnityTest]
public IEnumerator SceneTest()
{
go.GetComponent<EndTurn>().Advance();
Assert.AreNotEqual(startNum, go.GetComponent<EndTurn>().turnNum);
yield return new WaitForSeconds(0.5f);

Assert.IsNotNull(turn);
Assert.IsNotNull(end);

}

[Test]
public void TestPhaseIncrement()
[UnityTest]
public IEnumerator TestPhaseBuild()
{
phaseText = "Phase: " + "Defense";
go.GetComponent<EndTurn>().Advance();
Assert.AreNotEqual(phaseText, go.GetComponent<EndTurn>().phaseOutput.text);
yield return new WaitForSeconds(0.5f);
phaseText = "Phase: " + "Build";
end.Advance();
end.Advance();
Assert.AreEqual(phaseText, end.phaseOutput.text);
}

[TearDown]
public void TearDown()
[UnityTestAttribute]
public IEnumerator TestPhaseIncrement()
{
// Clean up after each test
Object.DestroyImmediate(go);
yield return new WaitForSeconds(0.5f);
phaseText = "Phase: " + "Attack";
end.Advance();
Assert.AreEqual(phaseText, end.phaseOutput.text);
}
}
10 changes: 5 additions & 5 deletions Crypto Wars/Assets/Scripts/Test_PlayMode/EscapeMenuTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public IEnumerator SetUp()
SceneManager.LoadScene("Project", LoadSceneMode.Single);
yield return null;
yield return new EnterPlayMode();
GameObject Camera = GameObject.Find("Main Camera");

Assert.IsNotNull(Camera);

testMenu = Camera.GetComponent<EscapeMenu>();
}

[UnityTearDown]
Expand All @@ -28,11 +33,6 @@ public IEnumerator TearDown()
public IEnumerator SceneTest()
{
yield return new WaitForSeconds(0.5f);
GameObject Camera = GameObject.Find("Main Camera");

Assert.IsNotNull(Camera);

testMenu = Camera.GetComponent<EscapeMenu>();
Assert.IsNotNull(testMenu);
}
[UnityTest]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Reflection;
using NUnit.Framework;
using TMPro;
using UnityEngine;
using UnityEngine.TestTools;

Expand All @@ -25,14 +26,18 @@ public class testWinConditions
public void SetUp()
{
winConditionsGameObject = new GameObject();
winConditions = winConditionsGameObject.AddComponent<WinConditions>();
winConditionsGameObject.AddComponent<WinConditions>();
winConditionsGameObject.AddComponent<TMPro.TextMeshProUGUI>();
winConditions = winConditionsGameObject.GetComponent<WinConditions>();

/************************************************************
* Set testWinConditions variables to match WinConditions.cs
***********************************************************/
timer = 0f;
//gameWinner = GetComponent<TextMeshProUGUI>();
//gameWinner.text = "Winner ";
winConditions.gameWinner = winConditionsGameObject.GetComponent<TMPro.TextMeshProUGUI>();
winConditions.gameWinner.text = "Winner ";
winConditions.gameOver = new GameObject();

playerCtrlGameObject = new GameObject();
playerCtrl = playerCtrlGameObject.AddComponent<PlayerController>();
}
Expand Down Expand Up @@ -64,6 +69,7 @@ public void testGameOver()
winner
};

Debug.Log(winConditions.gameWinner.text);
winConditions.gameIsOver(winner);
}

Expand All @@ -82,7 +88,8 @@ public void testFindWinner()

// timer < maxTime
winConditions.findWinner(playerCtrl);
Assert.AreEqual(null,winConditions.winningPlayer);
Assert.AreEqual("One", winConditions.winningPlayer.GetName());



// timer > maxTime
Expand Down
Loading
Loading