sashomaga

UnitTests -> Simulate Console I/O

May 16th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. namespace GameTest
  2. {
  3.     using System;
  4.     using System.IO;
  5.     using System.Text;
  6.     using Game15;
  7.     using Microsoft.VisualStudio.TestTools.UnitTesting;
  8.  
  9.     [TestClass]
  10.     public class ComunicatorTests
  11.     {
  12.         [TestMethod]
  13.         public void TestGetNumber()
  14.         {
  15.             using (StringWriter sw = new StringWriter())
  16.             {
  17.                 Console.SetOut(sw);
  18.  
  19.                 using (StringReader sr = new StringReader(string.Format("5{0}", Environment.NewLine)))
  20.                 {
  21.                     Console.SetIn(sr);
  22.  
  23.                     string result = Communicator.GetNumber();
  24.  
  25.                     string expected = "5";
  26.  
  27.                     Assert.AreEqual<string>(expected, result);
  28.                 }
  29.             }
  30.         }
  31.  
  32.         [TestMethod]
  33.         public void TestDisplayMessage()
  34.         {
  35.             using (StringWriter sw = new StringWriter())
  36.             {
  37.                 Console.SetOut(sw);
  38.  
  39.                 string message = "Test message";
  40.                 Communicator.DisplayMessage(message);
  41.  
  42.                 string expected = string.Format("Test message{0}", Environment.NewLine);
  43.  
  44.                 Assert.AreEqual<string>(expected, sw.ToString());
  45.             }
  46.         }
  47.  
  48.         [TestMethod]
  49.         public void TestGetName()
  50.         {
  51.             using (StringWriter sw = new StringWriter())
  52.             {
  53.                 Console.SetOut(sw);
  54.  
  55.                 using (StringReader sr = new StringReader(string.Format("Pesho{0}", Environment.NewLine)))
  56.                 {
  57.                     Console.SetIn(sr);
  58.  
  59.                     string result = Communicator.GetName();
  60.                     string expected = "Pesho";
  61.  
  62.                     Assert.AreEqual<string>(expected, result);
  63.                 }
  64.             }
  65.         }
  66.  
  67.         [TestMethod]
  68.         public void TestDisplayIntroMessage()
  69.         {
  70.             using (StringWriter sw = new StringWriter())
  71.             {
  72.                 Console.SetOut(sw);
  73.  
  74.                 Communicator.DisplayIntroMessage();
  75.  
  76.                 StringBuilder sb = new StringBuilder();
  77.                 sb.AppendLine("Welcome to the game “15”.");
  78.                 sb.AppendLine("Please try to arrange the numbers sequentially.");
  79.                 sb.AppendLine("Commands:");                
  80.                 sb.AppendLine(" - 'top' - view the top scoreboard");              
  81.                 sb.AppendLine(" - 'restart' - start a new game");          
  82.                 sb.AppendLine(" - 'exit' - quit the game");
  83.  
  84.                 string expected = sb.ToString();
  85.  
  86.                 Assert.AreEqual<string>(expected, sw.ToString());
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment