211 lines
6.9 KiB
C#
211 lines
6.9 KiB
C#
using NUnit.Framework;
|
|
using UnityEngine;
|
|
using YachtDice.Dice;
|
|
using YachtDice.Economy;
|
|
using YachtDice.Inventory;
|
|
using YachtDice.Player;
|
|
using YachtDice.Shop;
|
|
using YachtDice.Modifiers.Definition;
|
|
using YachtDice.Modifiers.Runtime;
|
|
|
|
namespace YachtDice.Tests
|
|
{
|
|
public sealed class ShopModelTests
|
|
{
|
|
private CurrencyBank _bank;
|
|
private ModifierRegistry _registry;
|
|
private InventoryModel _inventory;
|
|
private DiceCollection _diceCollection;
|
|
private ShopModel _shop;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
var go = new GameObject("Bank");
|
|
_bank = go.AddComponent<CurrencyBank>();
|
|
_bank.SetBalance(500);
|
|
|
|
_registry = new ModifierRegistry(5);
|
|
_inventory = new InventoryModel(_registry);
|
|
_diceCollection = new DiceCollection();
|
|
_shop = new ShopModel(_bank, _inventory, _diceCollection);
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
foreach (var go in Object.FindObjectsByType<CurrencyBank>(FindObjectsSortMode.None))
|
|
Object.DestroyImmediate(go.gameObject);
|
|
}
|
|
|
|
private ModifierDefinition CreateDef(string id = "test",
|
|
bool hasLimitedUses = false, int maxUses = 0,
|
|
int shopPrice = 100, int sellPrice = 50)
|
|
{
|
|
return ModifierDefinition.CreateForTest(id, null,
|
|
hasLimitedUses: hasLimitedUses, maxUses: maxUses,
|
|
shopPrice: shopPrice, sellPrice: sellPrice);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Покупка проходит успешно при достаточном балансе.
|
|
/// </summary>
|
|
[Test]
|
|
public void TryPurchase_SucceedsWithSufficientCurrency()
|
|
{
|
|
var mod = CreateDef("test", shopPrice: 100);
|
|
|
|
bool result = _shop.TryPurchase(mod);
|
|
|
|
Assert.IsTrue(result);
|
|
Assert.AreEqual(400, _bank.Balance);
|
|
Assert.AreEqual(1, _inventory.OwnedModifiers.Count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Покупка отклоняется при нехватке валюты.
|
|
/// </summary>
|
|
[Test]
|
|
public void TryPurchase_FailsWhenBroke()
|
|
{
|
|
_bank.SetBalance(10);
|
|
var mod = CreateDef("test", shopPrice: 100);
|
|
|
|
bool result = _shop.TryPurchase(mod);
|
|
|
|
Assert.IsFalse(result);
|
|
Assert.AreEqual(10, _bank.Balance);
|
|
Assert.AreEqual(0, _inventory.OwnedModifiers.Count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Постоянный модификатор нельзя приобрести повторно.
|
|
/// </summary>
|
|
[Test]
|
|
public void TryPurchase_PermanentCannotBeBoughtTwice()
|
|
{
|
|
var mod = CreateDef("perm", shopPrice: 100);
|
|
|
|
_shop.TryPurchase(mod);
|
|
bool secondResult = _shop.TryPurchase(mod);
|
|
|
|
Assert.IsFalse(secondResult);
|
|
Assert.AreEqual(400, _bank.Balance);
|
|
Assert.AreEqual(1, _inventory.OwnedModifiers.Count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ограниченный модификатор можно купить несколько раз.
|
|
/// </summary>
|
|
[Test]
|
|
public void TryPurchase_LimitedCanBeReBought()
|
|
{
|
|
var mod = CreateDef("limited", hasLimitedUses: true, maxUses: 3, shopPrice: 100);
|
|
|
|
_shop.TryPurchase(mod);
|
|
bool secondResult = _shop.TryPurchase(mod);
|
|
|
|
Assert.IsTrue(secondResult);
|
|
Assert.AreEqual(300, _bank.Balance);
|
|
Assert.AreEqual(2, _inventory.OwnedModifiers.Count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Успешная покупка вызывает событие OnItemPurchased.
|
|
/// </summary>
|
|
[Test]
|
|
public void TryPurchase_FiresPurchaseEvent()
|
|
{
|
|
IShopItem purchased = null;
|
|
_shop.OnItemPurchased += item => purchased = item;
|
|
|
|
var mod = CreateDef("test", shopPrice: 100);
|
|
|
|
_shop.TryPurchase(mod);
|
|
|
|
Assert.IsNotNull(purchased);
|
|
Assert.AreEqual("test", purchased.Id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Товар имеет состояние Available, когда его можно позволить себе купить.
|
|
/// </summary>
|
|
[Test]
|
|
public void GetItemState_Available_WhenCanAfford()
|
|
{
|
|
var mod = CreateDef("test", shopPrice: 100);
|
|
|
|
Assert.AreEqual(ShopItemState.Available, _shop.GetItemState(mod));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Товар имеет состояние TooExpensive при недостатке валюты.
|
|
/// </summary>
|
|
[Test]
|
|
public void GetItemState_TooExpensive_WhenCannotAfford()
|
|
{
|
|
_bank.SetBalance(10);
|
|
var mod = CreateDef("test", shopPrice: 100);
|
|
|
|
Assert.AreEqual(ShopItemState.TooExpensive, _shop.GetItemState(mod));
|
|
}
|
|
|
|
/// <summary>
|
|
/// После покупки постоянного модификатора его состояние становится Owned.
|
|
/// </summary>
|
|
[Test]
|
|
public void GetItemState_Owned_WhenPermanentPurchased()
|
|
{
|
|
var mod = CreateDef("perm", shopPrice: 100);
|
|
|
|
_shop.TryPurchase(mod);
|
|
|
|
Assert.AreEqual(ShopItemState.Owned, _shop.GetItemState(mod));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Покупка кости добавляет ее в коллекцию игрока.
|
|
/// </summary>
|
|
[Test]
|
|
public void TryPurchase_DiceItem_AddsToDiceCollection()
|
|
{
|
|
var die = DiceDefinition.CreateForTest<StandardDice>("test_die", shopPrice: 100);
|
|
|
|
bool result = _shop.TryPurchase(die);
|
|
|
|
Assert.IsTrue(result);
|
|
Assert.AreEqual(400, _bank.Balance);
|
|
Assert.AreEqual(1, _diceCollection.OwnedDice.Count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Одну и ту же кость нельзя купить повторно.
|
|
/// </summary>
|
|
[Test]
|
|
public void TryPurchase_DiceItem_CannotBeBoughtTwice()
|
|
{
|
|
var die = DiceDefinition.CreateForTest<StandardDice>("unique_die", shopPrice: 100);
|
|
|
|
_shop.TryPurchase(die);
|
|
bool secondResult = _shop.TryPurchase(die);
|
|
|
|
Assert.IsFalse(secondResult);
|
|
Assert.AreEqual(400, _bank.Balance);
|
|
Assert.AreEqual(1, _diceCollection.OwnedDice.Count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// После покупки кость отображается в магазине как Owned.
|
|
/// </summary>
|
|
[Test]
|
|
public void GetItemState_Dice_Owned_AfterPurchase()
|
|
{
|
|
var die = DiceDefinition.CreateForTest<StandardDice>("die1", shopPrice: 50);
|
|
|
|
_shop.TryPurchase(die);
|
|
|
|
Assert.AreEqual(ShopItemState.Owned, _shop.GetItemState(die));
|
|
}
|
|
}
|
|
}
|