sashomaga

UnitTests -> Reflection

May 16th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.26 KB | None | 0 0
  1. namespace GameTest
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Reflection;
  6.     using System.Text;
  7.     using Game15;
  8.     using Microsoft.VisualStudio.TestTools.UnitTesting;
  9.    
  10.     [TestClass]
  11.     public class GameFieldTests
  12.     {
  13.         [TestMethod]
  14.         public void TestCanMoveNumberTrue()
  15.         {
  16.             int fieldLength = 4;
  17.             int fillCounter = 0;
  18.             int[,] testMatrix = new int[fieldLength, fieldLength];
  19.             for (int x = 0; x < fieldLength; x++)
  20.             {
  21.                 for (int y = 0; y < fieldLength; y++)
  22.                 {
  23.                     testMatrix[x, y] = fillCounter;
  24.  
  25.                     fillCounter++;
  26.                 }
  27.             }
  28.  
  29.             FieldInfo changedField = typeof(GameField).GetField("field", BindingFlags.Static | BindingFlags.NonPublic);
  30.             changedField.SetValue(null, testMatrix);            
  31.             FieldInfo changedDict = typeof(GameField).GetField("NumbersAndPositions", BindingFlags.Static | BindingFlags.NonPublic);
  32.  
  33.             Coordinates testCooridates = new Coordinates(0, 1);
  34.             Dictionary<int, Coordinates> testDict = new Dictionary<int, Coordinates>();
  35.             testDict.Add(1, testCooridates);
  36.             testCooridates = new Coordinates(0, 0);
  37.             testDict.Add(0, testCooridates);
  38.  
  39.             changedDict.SetValue(null, testDict);            
  40.  
  41.             bool result = GameField.CanMoveNumber(1);        
  42.             Assert.IsTrue(result);
  43.         }
  44.  
  45.         [TestMethod]
  46.         public void TestCanMoveNumberFalse()
  47.         {            
  48.             FieldInfo changedDict = typeof(GameField).GetField("NumbersAndPositions", BindingFlags.Static | BindingFlags.NonPublic);
  49.  
  50.             Coordinates testCooridates = new Coordinates(0, 1);
  51.             Dictionary<int, Coordinates> testDict = new Dictionary<int, Coordinates>();
  52.             testDict.Add(1, testCooridates);
  53.             testCooridates = new Coordinates(1, 0);
  54.             testDict.Add(0, testCooridates);
  55.  
  56.             changedDict.SetValue(null, testDict);
  57.  
  58.             bool result = GameField.CanMoveNumber(1);
  59.             Assert.IsFalse(result);
  60.         }
  61.  
  62.         [TestMethod]
  63.         public void TestMoveNumber()
  64.         {
  65.             int fieldLength = 4;
  66.             int fillCounter = 0;
  67.             int[,] testMatrix = new int[fieldLength, fieldLength];
  68.             for (int x = 0; x < fieldLength; x++)
  69.             {
  70.                 for (int y = 0; y < fieldLength; y++)
  71.                 {
  72.                     testMatrix[x, y] = fillCounter;
  73.  
  74.                     fillCounter++;
  75.                 }
  76.             }
  77.  
  78.             FieldInfo changedField = typeof(GameField).GetField("field", BindingFlags.Static | BindingFlags.NonPublic);
  79.             changedField.SetValue(null, testMatrix);
  80.  
  81.             FieldInfo changedDict = typeof(GameField).GetField("NumbersAndPositions", BindingFlags.Static | BindingFlags.NonPublic);
  82.             Coordinates testCooridates = new Coordinates(0, 1);
  83.             Dictionary<int, Coordinates> testDict = new Dictionary<int, Coordinates>();
  84.             testDict.Add(1, testCooridates);
  85.             testCooridates = new Coordinates(0, 0);
  86.             testDict.Add(0, testCooridates);
  87.             changedDict.SetValue(null, testDict);
  88.  
  89.             GameField.MoveNumber(1);
  90.  
  91.             int[,] check = (int[,])changedField.GetValue(null);
  92.             bool result = check[0, 0] == 1;
  93.  
  94.             Assert.IsTrue(result);
  95.         }
  96.  
  97.         [TestMethod]
  98.         public void TestIsSolved()
  99.         {
  100.             int fieldLength = 4;
  101.             int fillCounter = 1;
  102.             int[,] testMatrix = new int[fieldLength, fieldLength];
  103.             for (int x = 0; x < fieldLength; x++)
  104.             {
  105.                 for (int y = 0; y < fieldLength; y++)
  106.                 {
  107.                     if (fillCounter != 16)
  108.                     {
  109.                         testMatrix[x, y] = fillCounter;
  110.                     }
  111.                     else
  112.                     {
  113.                         testMatrix[x, y] = 0;
  114.                     }
  115.  
  116.                     fillCounter++;
  117.                 }
  118.             }
  119.  
  120.             FieldInfo changedField = typeof(GameField).GetField("field", BindingFlags.Static | BindingFlags.NonPublic);
  121.             changedField.SetValue(null, testMatrix);
  122.  
  123.             bool result = GameField.IsSolved();
  124.             Assert.IsTrue(result);
  125.         }
  126.  
  127.         [TestMethod]
  128.         public void TestToString()
  129.         {
  130.             GameField.RandomField();
  131.             int[,] copyGameField = (int[,])GameField.Field.Clone();
  132.  
  133.             StringBuilder sb = new StringBuilder();
  134.             sb.AppendLine(" -------------------");
  135.             for (int row = 0; row < copyGameField.GetLength(0); row++)
  136.             {
  137.                 sb.Append("|");
  138.                 for (int col = 0; col < copyGameField.GetLength(1); col++)
  139.                 {
  140.                     if (copyGameField[row, col] == 0)
  141.                     {
  142.                         sb.Append("    |");
  143.                         continue;
  144.                     }
  145.  
  146.                     sb.Append(string.Format(" {0,2} |", copyGameField[row, col]));
  147.                 }
  148.                 sb.AppendLine();
  149.             }
  150.             sb.Append(" -------------------");
  151.  
  152.             Assert.AreEqual(GameField.ToString(), sb.ToString());
  153.         }
  154.  
  155.         [TestMethod]
  156.         public void TestRandomField()
  157.         {
  158.             GameField.RandomField();
  159.             int[,] firstGameField = (int[,])GameField.Field.Clone();
  160.             GameField.RandomField();
  161.             int[,] secondGameField = (int[,])GameField.Field.Clone();
  162.  
  163.             bool areTheSame = this.AreMatricesTheSame(firstGameField, secondGameField);
  164.             Assert.IsFalse(areTheSame);
  165.         }
  166.  
  167.         private bool AreMatricesTheSame(int[,] first, int[,] second)
  168.         {
  169.             for (int row = 0; row < first.GetLength(0); row++)
  170.             {
  171.                 for (int col = 0; col < first.GetLength(1); col++)
  172.                 {
  173.                     if (first[row, col] != second[row, col])
  174.                     {
  175.                         return false;
  176.                     }
  177.                 }
  178.             }
  179.  
  180.             return true;
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment