PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Irony/Languages/Refal/UnitTests/RefalRegressionTests.cs

http://riftaddonstudio.codeplex.com
C# | 182 lines | 134 code | 27 blank | 21 comment | 0 complexity | 5e52e6a7711e80e941f4fc9a9a3e77f0 MD5 | raw file
  1. using System;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4. using Irony;
  5. using Irony.Parsing;
  6. namespace Refal.UnitTests
  7. {
  8. #region Unit testing platform abstraction layer
  9. #if NUNIT
  10. using NUnit.Framework;
  11. using TestClass = NUnit.Framework.TestFixtureAttribute;
  12. using TestMethod = NUnit.Framework.TestAttribute;
  13. using TestContext = System.Object;
  14. #else
  15. using Microsoft.VisualStudio.TestTools.UnitTesting;
  16. #endif
  17. #endregion
  18. /// <summary>
  19. /// Refal regression tests.
  20. /// Written by Alexey Yakovlev, yallie@yandex.ru.
  21. /// http://refal.codeplex.com
  22. /// </summary>
  23. [TestClass]
  24. public class RefalRegressionTests
  25. {
  26. /// <summary>
  27. /// Resource name = DefaultNamespace + "." + FolderName + "." + FileName
  28. /// Example: Irony.Samples.Refal.Samples.hello.ref
  29. /// </summary>
  30. const string BaseResourceName = "Refal.UnitTests.Sources.";
  31. /// <summary>
  32. /// Initialized by MSTest Framework
  33. /// </summary>
  34. public TestContext TestContext { get; set; }
  35. [TestMethod]
  36. public void RefalTest_Hello()
  37. {
  38. RunSampleAndCompareResults("hello.ref", "hello.txt");
  39. }
  40. [TestMethod]
  41. public void RefalTest_Binary()
  42. {
  43. RunSampleAndCompareResults("binary.ref", "binary.txt");
  44. }
  45. [TestMethod]
  46. public void RefalTest_Factorial()
  47. {
  48. RunSampleAndCompareResults("factorial.ref", "factorial.txt");
  49. }
  50. [TestMethod]
  51. public void RefalTest_ChangeV1()
  52. {
  53. RunSampleAndCompareResults("change-v1.ref", "change.txt");
  54. }
  55. [TestMethod]
  56. public void RefalTest_ChangeV2()
  57. {
  58. RunSampleAndCompareResults("change-v2.ref", "change.txt");
  59. }
  60. [TestMethod]
  61. public void RefalTest_Italian()
  62. {
  63. RunSampleAndCompareResults("italian.ref", "italian.txt");
  64. }
  65. [TestMethod]
  66. public void RefalTest_Palyndrome()
  67. {
  68. RunSampleAndCompareResults("palyndrome.ref", "palyndrome.txt");
  69. }
  70. [TestMethod]
  71. public void RefalTest_ArithmeticTranslator()
  72. {
  73. RunSampleAndCompareResults("arith.ref", "arith.txt");
  74. }
  75. [TestMethod]
  76. public void RefalTest_QuinePlain()
  77. {
  78. RunSampleAndCompareResults("quine-plain.ref");
  79. }
  80. [TestMethod]
  81. public void RefalTest_QuineSimple()
  82. {
  83. RunSampleAndCompareResults("quine-simple.ref");
  84. }
  85. [TestMethod]
  86. public void RefalTest_QuineXplained()
  87. {
  88. RunSampleAndCompareResults("quine-xplained.ref");
  89. }
  90. [TestMethod]
  91. public void RefalTest_XtrasBigint()
  92. {
  93. RunSampleAndCompareResults("xtras-bigint.ref", "xtras-bigint.txt");
  94. }
  95. [TestMethod]
  96. public void RefalTest_XtrasFactorial()
  97. {
  98. RunSampleAndCompareResults("xtras-factorial.ref", "xtras-factorial.txt");
  99. }
  100. [TestMethod]
  101. public void RefalTest_99BottlesV1()
  102. {
  103. RunSampleAndCompareResults("99-bottles-v1.ref", "99-bottles-v1.txt");
  104. }
  105. [TestMethod]
  106. public void RefalTest_99BottlesV2()
  107. {
  108. RunSampleAndCompareResults("99-bottles-v2.ref", "99-bottles-v2.txt");
  109. }
  110. [TestMethod]
  111. public void RefalTest_BrainfuckInterpreter()
  112. {
  113. RunSampleAndCompareResults("brainfuck.ref", "brainfuck.txt");
  114. }
  115. /// <summary>
  116. /// Load sample program from resources, run it and check its output
  117. /// </summary>
  118. void RunSampleAndCompareResults(string programResourceName, string outputResourceName)
  119. {
  120. var grammar = new RefalGrammar();
  121. var parser = new Parser(grammar);
  122. var parseTree = parser.Parse(LoadResourceText(programResourceName));
  123. Assert.IsNotNull(parseTree);
  124. Assert.IsFalse(parseTree.HasErrors());
  125. string result = grammar.RunSample(new RunSampleArgs(parser.Language, null, parseTree));
  126. Assert.IsNotNull(result);
  127. Assert.AreEqual(LoadResourceText(outputResourceName), result);
  128. }
  129. /// <summary>
  130. /// Load quine program from resources, run it and compare its output with itself
  131. /// </summary>
  132. void RunSampleAndCompareResults(string programResourceName)
  133. {
  134. RunSampleAndCompareResults(programResourceName, programResourceName);
  135. }
  136. /// <summary>
  137. /// Load sample Refal program or output from assembly resources
  138. /// </summary>
  139. string LoadResourceText(string resourceName)
  140. {
  141. var asm = this.GetType().Assembly;
  142. using (var stream = asm.GetManifestResourceStream(BaseResourceName + resourceName))
  143. {
  144. Assert.IsNotNull(stream);
  145. using (var sr = new StreamReader(stream))
  146. {
  147. var s = sr.ReadToEnd();
  148. Assert.IsFalse(string.IsNullOrEmpty(s));
  149. s = Regex.Replace(s, @"\r\n?", Environment.NewLine);
  150. return s;
  151. }
  152. }
  153. }
  154. }
  155. }