Skip to content

Commit

Permalink
Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanStinson committed Dec 23, 2021
1 parent abbdbff commit 6b0e18c
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 37 deletions.
58 changes: 28 additions & 30 deletions Mobile Game Store/Assets/Scenes/GameStoreScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ GameObject:
- component: {fileID: 183866095}
- component: {fileID: 183866097}
- component: {fileID: 183866096}
- component: {fileID: 183866098}
m_Layer: 5
m_Name: AmountText
m_TagString: Untagged
Expand Down Expand Up @@ -817,6 +818,18 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 183866094}
m_CullTransparentMesh: 1
--- !u!114 &183866098
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 183866094}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: eeed92b94cb4a7940ab713cbaee1fa57, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &187123046
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -7058,6 +7071,7 @@ GameObject:
- component: {fileID: 684481826}
- component: {fileID: 684481828}
- component: {fileID: 684481827}
- component: {fileID: 684481829}
m_Layer: 5
m_Name: AmountText
m_TagString: Untagged
Expand Down Expand Up @@ -7181,6 +7195,18 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 684481825}
m_CullTransparentMesh: 1
--- !u!114 &684481829
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 684481825}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: eeed92b94cb4a7940ab713cbaee1fa57, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &792762082
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -9810,7 +9836,6 @@ GameObject:
m_Component:
- component: {fileID: 1870445914}
- component: {fileID: 1870445915}
- component: {fileID: 1870445916}
- component: {fileID: 1870445917}
m_Layer: 5
m_Name: CoinsHUD
Expand Down Expand Up @@ -9866,19 +9891,6 @@ MonoBehaviour:
m_ChildScaleWidth: 0
m_ChildScaleHeight: 1
m_ReverseArrangement: 0
--- !u!114 &1870445916
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1870445913}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: eeed92b94cb4a7940ab713cbaee1fa57, type: 3}
m_Name:
m_EditorClassIdentifier:
_amountText: {fileID: 183866096}
--- !u!114 &1870445917
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -9895,7 +9907,7 @@ MonoBehaviour:
_onTriggerEvent:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1870445916}
- m_Target: {fileID: 183866098}
m_TargetAssemblyTypeName: JGM.GameStore.HUD.CurrencyHUD, JGM.GameStore
m_MethodName: RefreshCurrencyAmount
m_Mode: 0
Expand Down Expand Up @@ -10494,7 +10506,6 @@ GameObject:
m_Component:
- component: {fileID: 2039971824}
- component: {fileID: 2039971825}
- component: {fileID: 2039971827}
- component: {fileID: 2039971826}
m_Layer: 5
m_Name: GemsHUD
Expand Down Expand Up @@ -10566,7 +10577,7 @@ MonoBehaviour:
_onTriggerEvent:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 2039971827}
- m_Target: {fileID: 684481829}
m_TargetAssemblyTypeName: JGM.GameStore.HUD.CurrencyHUD, JGM.GameStore
m_MethodName: RefreshCurrencyAmount
m_Mode: 0
Expand All @@ -10578,19 +10589,6 @@ MonoBehaviour:
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!114 &2039971827
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2039971823}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: eeed92b94cb4a7940ab713cbaee1fa57, type: 3}
m_Name:
m_EditorClassIdentifier:
_amountText: {fileID: 684481827}
--- !u!1 &2055040874
GameObject:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
namespace JGM.GameStore.Events.Data
{
public class RefreshCurrencyAmountEventData : IGameEventData
public interface ICurrencyEventData : IGameEventData
{
public readonly float Amount;
float Amount { get; }
}

public class RefreshCurrencyAmountEventData : ICurrencyEventData
{
public float Amount { get; private set; }

public RefreshCurrencyAmountEventData(float amount)
{
Expand Down
11 changes: 8 additions & 3 deletions Mobile Game Store/Assets/Scripts/HUD/CurrencyHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@

namespace JGM.GameStore.HUD
{
[RequireComponent(typeof(TextMeshProUGUI))]
public class CurrencyHUD : MonoBehaviour, IHUDElement
{
[SerializeField]
private TextMeshProUGUI _amountText;
private float _amount = 0;

private void Awake()
{
_amountText = GetComponent<TextMeshProUGUI>();
_amountText.text = string.Format("{0:n0}", _amount);
}

public void RefreshCurrencyAmount(IGameEventData gameEventData)
{
var data = gameEventData as RefreshCurrencyAmountEventData;
_amount += data.Amount;
RefreshCurrencyAmount(gameEventData as ICurrencyEventData);
}

public void RefreshCurrencyAmount(ICurrencyEventData currencyEventData)
{
_amount += currencyEventData.Amount;
_amountText.text = string.Format("{0:n0}", _amount);
}
}
Expand Down
8 changes: 8 additions & 0 deletions Mobile Game Store/Assets/Tests.meta

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

8 changes: 8 additions & 0 deletions Mobile Game Store/Assets/Tests/HUD.meta

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

59 changes: 59 additions & 0 deletions Mobile Game Store/Assets/Tests/HUD/CurrencyHUDTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using JGM.GameStore.Events.Data;
using JGM.GameStore.HUD;
using Moq;
using NUnit.Framework;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.TestTools;

namespace JGM.GameStoreTests.HUD
{
public class CurrencyHUDTest
{
private CurrencyHUD _currencyHUD;
private Mock<ICurrencyEventData> _gameEventDataMock;
private const float _testCurrencyAmount = 200f;

[UnitySetUp]
public IEnumerator SetUp()
{
var dummyGO = new GameObject("Dummy");
_currencyHUD = dummyGO.AddComponent<CurrencyHUD>();
_gameEventDataMock = new Mock<ICurrencyEventData>();
_gameEventDataMock.SetupGet(x => x.Amount).Returns(_testCurrencyAmount);
yield return new WaitForEndOfFrame();
}

[UnityTest]
public IEnumerator OnComponentAwake_NoTMPIsAttached_LogsError()
{
GameObject.Destroy(_currencyHUD.GetComponent<TextMeshProUGUI>());
yield return new WaitForEndOfFrame();
LogAssert.Expect(LogType.Error, "Can't remove TextMeshProUGUI (Script) because CurrencyHUD (Script) depends on it");
}

[Test]
public void OnComponentAwake_TMPIsAttached_AmountIsZero()
{
float currencyAmount = float.Parse(_currencyHUD.GetComponent<TextMeshProUGUI>().text);
Assert.AreEqual(0f, currencyAmount);
}

[Test]
public void OnRefreshCurrencyAmount_DataPassedIsNull_AmountIsZero()
{
_currencyHUD.RefreshCurrencyAmount(new Mock<ICurrencyEventData>().Object);
float currencyAmount = float.Parse(_currencyHUD.GetComponent<TextMeshProUGUI>().text);
Assert.AreEqual(0f, currencyAmount);
}

[Test]
public void OnRefreshCurrencyAmount_DataPassedIsValid_ReturnsExpectedAmount()
{
_currencyHUD.RefreshCurrencyAmount(_gameEventDataMock.Object);
float currencyAmount = float.Parse(_currencyHUD.GetComponent<TextMeshProUGUI>().text);
Assert.AreEqual(_testCurrencyAmount, currencyAmount);
}
}
}
11 changes: 11 additions & 0 deletions Mobile Game Store/Assets/Tests/HUD/CurrencyHUDTest.cs.meta

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

24 changes: 24 additions & 0 deletions Mobile Game Store/Assets/Tests/JGM.GameStoreTests.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "JGM.GameStoreTests",
"rootNamespace": "",
"references": [
"GUID:27619889b8ba8c24980f49ee34dbb44a",
"GUID:0acc523941302664db1f4e527237feb3",
"GUID:6374b22523226c546bd57944e41676e8",
"GUID:6055be8ebefd69e48b49212b09b47b2f"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll",
"Moq.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Mobile Game Store/Assets/Tests/JGM.GameStoreTests.asmdef.meta

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

3 changes: 2 additions & 1 deletion Mobile Game Store/Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"com.unity.modules.video": "1.0.0",
"com.unity.modules.vr": "1.0.0",
"com.unity.modules.wind": "1.0.0",
"com.unity.modules.xr": "1.0.0"
"com.unity.modules.xr": "1.0.0",
"nuget.moq": "1.0.0"
}
}
16 changes: 16 additions & 0 deletions Mobile Game Store/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@
"com.unity.modules.imgui": "1.0.0"
}
},
"nuget.castle-core": {
"version": "1.0.1",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"nuget.moq": {
"version": "1.0.0",
"depth": 0,
"source": "registry",
"dependencies": {
"nuget.castle-core": "1.0.1"
},
"url": "https://packages.unity.com"
},
"com.unity.modules.ai": {
"version": "1.0.0",
"depth": 0,
Expand Down
3 changes: 3 additions & 0 deletions Mobile Game Store/ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ EditorBuildSettings:
- enabled: 1
path: Assets/Scenes/GameStoreScene.unity
guid: 63364e9dd62c7634084346193c923b6a
- enabled: 1
path: Assets/Plugins/Zenject/OptionalExtras/IntegrationTests/SceneTests/TestSceneContextEvents/TestSceneContextEvents.unity
guid: 6a6e139b8a4d4d544a3318c53b9ae3d6
m_configObjects: {}
2 changes: 1 addition & 1 deletion Mobile Game Store/ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ PlayerSettings:
androidFullscreenMode: 1
defaultIsNativeResolution: 1
macRetinaSupport: 1
runInBackground: 0
runInBackground: 1
captureSingleScreen: 0
muteOtherAudioSources: 0
Prepare IOS For Recording: 0
Expand Down

0 comments on commit 6b0e18c

Please sign in to comment.