Advertisement
Guest User

Laka laka e o

a guest
Aug 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using NUnit.Framework;
  6.  
  7. namespace Inventory.Tests
  8. {
  9.     [TestFixture]
  10.     public class TestClass
  11.     {
  12.         private HeroInventory target;
  13.  
  14.         [SetUp]
  15.         public void TestInitialization()
  16.         {
  17.             target = new HeroInventory();
  18.         }
  19.  
  20.         [Test]
  21.         public void CommonItemAdded()
  22.         {
  23.             IItem item = new CommonItem("Test sword", 10, 20, 30, 40, 50);
  24.  
  25.             this.target.AddCommonItem(item);
  26.  
  27.             Type clazz = typeof(HeroInventory);
  28.             var field =
  29.                 clazz.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
  30.                     .FirstOrDefault(x => x.GetCustomAttributes(typeof(ItemAttribute)) != null);
  31.             var reflectionItems = (Dictionary<string, IItem>)field.GetValue(this.target);
  32.  
  33.             Assert.AreEqual(1, reflectionItems.Count);
  34.         }
  35.  
  36.         [Test]
  37.         public void CommonItemsAdded()
  38.         {
  39.             IItem item = new CommonItem("Test sword", 10, 20, 30, 40, 50);
  40.             IItem item2 = new CommonItem("Test axe", 10, 20, 30, 40, 50);
  41.  
  42.             this.target.AddCommonItem(item);
  43.             this.target.AddCommonItem(item2);
  44.  
  45.             Type clazz = typeof(HeroInventory);
  46.             var field =
  47.                 clazz.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
  48.                     .FirstOrDefault(x => x.GetCustomAttributes(typeof(ItemAttribute)) != null);
  49.             var reflectionItems = (Dictionary<string, IItem>)field.GetValue(this.target);
  50.  
  51.             Assert.AreEqual(2, reflectionItems.Count);
  52.         }
  53.  
  54.         [Test]
  55.         public void RecipeItemAdded()
  56.         {
  57.             IRecipe recipe = new RecipeItem("Recipe", 10, 20, 30, 40, 50, new List<string>() { "Sword", "Axe" });
  58.  
  59.             this.target.AddRecipeItem(recipe);
  60.  
  61.             Type clazz = typeof(HeroInventory);
  62.             var field =
  63.                 clazz.GetField("recipeItems", BindingFlags.NonPublic | BindingFlags.Instance);
  64.             var reflectionItems = (Dictionary<string, IRecipe>)field.GetValue(this.target);
  65.  
  66.             Assert.AreEqual(1, reflectionItems.Count);
  67.         }
  68.  
  69.         [Test]
  70.         public void RecipeItemsAdded()
  71.         {
  72.             IRecipe recipe = new RecipeItem("Recipe", 10, 20, 30, 40, 50, new List<string>() { "Sword", "Axe" });
  73.             IRecipe recipe2 = new RecipeItem("Recipe2", 10, 20, 30, 40, 50, new List<string>() { "Sword", "Axe" });
  74.  
  75.             this.target.AddRecipeItem(recipe);
  76.             this.target.AddRecipeItem(recipe2);
  77.             Type clazz = typeof(HeroInventory);
  78.             var field =
  79.                 clazz.GetField("recipeItems", BindingFlags.NonPublic | BindingFlags.Instance);
  80.             var reflectionItems = (Dictionary<string, IRecipe>)field.GetValue(this.target);
  81.  
  82.             Assert.AreEqual(2, reflectionItems.Count);
  83.         }
  84.  
  85.         [Test]
  86.         public void RecipeWithItemsAndRecipeRemains()
  87.         {
  88.             IItem item = new CommonItem("Sword", 10, 20, 30, 40, 50);
  89.             IItem item2 = new CommonItem("Axe", 10, 20, 30, 40, 50);
  90.             IRecipe recipe = new RecipeItem("Recipe", 10, 20, 30, 40, 50, new List<string>() { "Sword", "Axe" });
  91.  
  92.             this.target.AddRecipeItem(recipe);
  93.             this.target.AddCommonItem(item);
  94.             this.target.AddCommonItem(item2);
  95.             Type clazz = typeof(HeroInventory);
  96.             var recipeItems = clazz.GetField("recipeItems", BindingFlags.NonPublic | BindingFlags.Instance);
  97.             var recipeItemsReflection = (Dictionary<string, IRecipe>)recipeItems.GetValue(this.target);
  98.  
  99.             var commonItems = clazz.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
  100. .FirstOrDefault(x => x.GetCustomAttributes(typeof(ItemAttribute)) != null);
  101.             var commonItemsReflection = (Dictionary<string, IItem>)commonItems.GetValue(this.target);
  102.             Assert.AreEqual(1, commonItemsReflection.Count);
  103.             Assert.AreEqual(1, recipeItemsReflection.Count);
  104.         }
  105.  
  106.         [Test]
  107.         public void RecipeWithAllNeededItems()
  108.         {
  109.             IItem item = new CommonItem("Sword", 10, 20, 30, 40, 50);
  110.             IItem item2 = new CommonItem("Axe", 10, 20, 30, 40, 50);
  111.             IRecipe recipe = new RecipeItem("Recipe", 10, 20, 30, 40, 50, new List<string>() { "Sword", "Axe" });
  112.             IItem item3 = new CommonItem("Axe", 10, 20, 30, 40, 50);
  113.  
  114.             this.target.AddCommonItem(item);
  115.             this.target.AddCommonItem(item2);
  116.             this.target.AddRecipeItem(recipe);
  117.             this.target.AddCommonItem(item3);
  118.             Type clazz = typeof(HeroInventory);
  119.             var recipeItems = clazz.GetField("recipeItems", BindingFlags.NonPublic | BindingFlags.Instance);
  120.             var recipeItemsReflection = (Dictionary<string, IRecipe>)recipeItems.GetValue(this.target);
  121.  
  122.             var commonItems = clazz.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
  123. .FirstOrDefault(x => x.GetCustomAttributes(typeof(ItemAttribute)) != null);
  124.             var commonItemsReflection = (Dictionary<string, IItem>)commonItems.GetValue(this.target);
  125.             Assert.AreEqual(2, commonItemsReflection.Count);
  126.             Assert.AreEqual(1, recipeItemsReflection.Count);
  127.         }
  128.  
  129.         [Test]
  130.         public void StrengthBonusGatheretFromItems()
  131.         {
  132.             IItem item = new CommonItem("Test sword", 10, 20, 30, 40, 50);
  133.  
  134.             this.target.AddCommonItem(item);
  135.  
  136.             Assert.AreEqual(10, item.StrengthBonus);
  137.         }
  138.  
  139.         [Test]
  140.         public void AgilityBonusGatheretFromItems()
  141.         {
  142.             IItem item = new CommonItem("Test sword", 10, 20, 30, 40, 50);
  143.  
  144.             this.target.AddCommonItem(item);
  145.  
  146.             Assert.AreEqual(20, item.AgilityBonus);
  147.         }
  148.  
  149.         [Test]
  150.         public void IntelligenceBonusGatheretFromItems()
  151.         {
  152.             IItem item = new CommonItem("Test sword", 10, 20, 30, 40, 50);
  153.  
  154.             this.target.AddCommonItem(item);
  155.  
  156.             Assert.AreEqual(30, item.IntelligenceBonus);
  157.         }
  158.  
  159.         [Test]
  160.         public void HitPointsBonusGatheretFromItems()
  161.         {
  162.             IItem item = new CommonItem("Test sword", 10, 20, 30, 40, 50);
  163.  
  164.             this.target.AddCommonItem(item);
  165.  
  166.             Assert.AreEqual(40, item.HitPointsBonus);
  167.         }
  168.  
  169.         [Test]
  170.         public void DamageBonusPointsBonusGatheretFromItems()
  171.         {
  172.             IItem item = new CommonItem("Test sword", 10, 20, 30, 40, 50);
  173.  
  174.             this.target.AddCommonItem(item);
  175.  
  176.             Assert.AreEqual(50, item.DamageBonus);
  177.         }
  178.  
  179.         [Test]
  180.         public void NameGatheretFromItems()
  181.         {
  182.             IItem item = new CommonItem("Test sword", 10, 20, 30, 40, 50);
  183.  
  184.             this.target.AddCommonItem(item);
  185.  
  186.             Assert.AreEqual("Test sword", item.Name);
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement