Skip to content

Instantly share code, notes, and snippets.

@NullEntity
Created April 2, 2018 00:34
Show Gist options
  • Save NullEntity/e48d9cce6df12732c614c72d46b571ea to your computer and use it in GitHub Desktop.
Save NullEntity/e48d9cce6df12732c614c72d46b571ea to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
namespace Domm.Engine
{
public class Recipe
{
private readonly IReadOnlyDictionary<IIngredient, int> _ingredients;
public Recipe([NotNull] Dictionary<IIngredient, int> ingredients)
{
if (ingredients == null) throw new ArgumentNullException(nameof(ingredients));
_ingredients = ingredients;
}
public bool IsSatisfiedBy(IEnumerable<IIngredient> ingredients)
{
return ingredients
.GroupBy(x => x)
.All(x => _ingredients.ContainsKey(x.Key) && _ingredients[x.Key] == x.Count());
}
}
}
using System;
using System.Collections.Generic;
using System.Threading;
using Domm.Engine;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEditor;
using UnityEngine;
namespace Domm.UI
{
[CreateAssetMenu(fileName = "New Recipe", menuName = "DOMM/Recipe")]
public class RecipeScriptableObject : SerializedScriptableObject
{
[OdinSerialize]
private readonly Dictionary<IIngredient, int> _requiredIngredients
= new Dictionary<IIngredient, int>();
public Recipe Recipe { get; private set; }
public void Awake()
{
Recipe = new Recipe(_requiredIngredients);
}
}
}
using System.Collections.Generic;
using Domm.Engine;
using Moq;
using NUnit.Framework;
namespace Domm.Tests.Editor
{
public class RecipeTests
{
[Test]
public void HappyPath()
{
// a recipe is just a list of ingredients
var ingredientA = new Mock<IIngredient>().Object;
var ingredientB = new Mock<IIngredient>().Object;
var recipe = new Recipe(new Dictionary<IIngredient, int>
{
{ingredientA, 1},
{ingredientB, 1}
});
Assert.IsTrue(recipe.IsSatisfiedBy(new[] {ingredientA, ingredientB}));
}
[Test]
public void TooManyIngredientsFails()
{
// a recipe is just a list of ingredients
var ingredientA = new Mock<IIngredient>().Object;
var ingredientB = new Mock<IIngredient>().Object;
var recipe = new Recipe(new Dictionary<IIngredient, int>
{
{ingredientA, 10},
{ingredientB, 1}
});
Assert.IsFalse(recipe.IsSatisfiedBy(new[] {ingredientA, ingredientB}));
}
[Test]
public void MissingIngredientsFails()
{
// a recipe is just a list of ingredients
var ingredientA = new Mock<IIngredient>().Object;
var ingredientB = new Mock<IIngredient>().Object;
var recipe = new Recipe(new Dictionary<IIngredient, int>
{
{ingredientB, 1}
});
Assert.IsFalse(recipe.IsSatisfiedBy(new[] {ingredientA, ingredientB}));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment