#TODO
Last active
April 29, 2022 14:22
-
-
Save charlieamat/fd947d70a0f5c4b136b88e75fd58248a to your computer and use it in GitHub Desktop.
[ta-edu-course-survival-game] Chapter 4 — EditMode Tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
namespace Items | |
{ | |
public class Inventory : MonoBehaviour | |
{ | |
public const int MaxCapacity = 99; | |
public Dictionary<ItemType, int> Items; | |
public void Add(ItemType item) | |
{ | |
if (Items.ContainsKey(item)) | |
{ | |
Items[item] += 1; | |
} | |
else | |
{ | |
Items.Add(item, 1); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
namespace Items | |
{ | |
public class Inventory : MonoBehaviour | |
{ | |
public const int MaxCapacity = 99; | |
public Dictionary<ItemType, int> Items; | |
public void Add(ItemType item) | |
{ | |
if (Items.ContainsKey(item)) | |
{ | |
Items[item] = Mathf.Min(Items[item] + 1, MaxCapacity); | |
} | |
else | |
{ | |
Items.Add(item, 1); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using NUnit.Framework; | |
using System.Collections.Generic; | |
using Items; | |
public class InventoryTests | |
{ | |
[Test] | |
public void Can_Add_New_Item() | |
{ | |
var itemType = ScriptableObject.CreateInstance<ItemType>(); | |
var gameObject = new GameObject(); | |
var inventory = gameObject.AddComponent<Inventory>(); | |
inventory.Items = new Dictionary<ItemType, int>(); | |
inventory.Add(itemType); | |
Assert.AreEqual(inventory.Items[itemType], 1); | |
} | |
[Test] | |
public void Can_Add_To_Existing_Item() | |
{ | |
var itemType = ScriptableObject.CreateInstance<ItemType>(); | |
var gameObject = new GameObject(); | |
var inventory = gameObject.AddComponent<Inventory>(); | |
inventory.Items = new Dictionary<ItemType, int> { { itemType, 1 } }; | |
inventory.Add(itemType); | |
Assert.AreEqual(inventory.Items[itemType], 2); | |
} | |
[Test] | |
public void Cannot_Exceed_Max_Capacity() | |
{ | |
var itemType = ScriptableObject.CreateInstance<ItemType>(); | |
var gameObject = new GameObject(); | |
var inventory = gameObject.AddComponent<Inventory>(); | |
inventory.Items = new Dictionary<ItemType, int> { { itemType, Inventory.MaxCapacity } }; | |
inventory.Add(itemType); | |
Assert.AreEqual(inventory.Items[itemType], Inventory.MaxCapacity); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment