Skip to content

Instantly share code, notes, and snippets.

@charlieamat
Last active April 29, 2022 14:22
Show Gist options
  • Save charlieamat/fd947d70a0f5c4b136b88e75fd58248a to your computer and use it in GitHub Desktop.
Save charlieamat/fd947d70a0f5c4b136b88e75fd58248a to your computer and use it in GitHub Desktop.
[ta-edu-course-survival-game] Chapter 4 — EditMode Tests
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);
}
}
}
}
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);
}
}
}
}
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