/LINQToTTree/LINQToTTreeLib.Tests/Statements/StatementSimpleStatementTest.cs

# · C# · 118 lines · 95 code · 15 blank · 8 comment · 7 complexity · 0093dff619c2faa149043ca0899d3356 MD5 · raw file

  1. // <copyright file="StatementSimpleStatementTest.cs" company="Microsoft">Copyright Š Microsoft 2010</copyright>
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using LinqToTTreeInterfacesLib;
  7. using LINQToTTreeLib.Utils;
  8. using Microsoft.Pex.Framework;
  9. using Microsoft.Pex.Framework.Validation;
  10. using Microsoft.VisualStudio.TestTools.UnitTesting;
  11. using LINQToTTreeLib.Tests;
  12. namespace LINQToTTreeLib.Statements
  13. {
  14. /// <summary>This class contains parameterized unit tests for StatementSimpleStatement</summary>
  15. [PexClass(typeof(StatementSimpleStatement))]
  16. [PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))]
  17. [PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)]
  18. [TestClass]
  19. public partial class StatementSimpleStatementTest
  20. {
  21. [TestInitialize]
  22. public void TestSTart()
  23. {
  24. TestUtils.ResetLINQLibrary();
  25. }
  26. /// <summary>Test stub for CodeItUp()</summary>
  27. [PexMethod]
  28. public IEnumerable<string> CodeItUp([PexAssumeUnderTest]StatementSimpleStatement target)
  29. {
  30. IEnumerable<string> result = target.CodeItUp();
  31. var lines = result.ToArray();
  32. Assert.AreEqual(1, lines.Length, "bad # of lines");
  33. return result;
  34. // TODO: add assertions to method StatementSimpleStatementTest.CodeItUp(StatementSimpleStatement)
  35. }
  36. /// <summary>Test stub for .ctor(String)</summary>
  37. [PexMethod]
  38. public StatementSimpleStatement Constructor(string line)
  39. {
  40. StatementSimpleStatement target = new StatementSimpleStatement(line);
  41. Assert.IsFalse(target.Line.EndsWith(";"), "semicolon should have been stripped off ('" + target.Line + "')");
  42. Assert.AreNotEqual(0, target.Line, "empty line is not allowed");
  43. line = line.Trim();
  44. while (line.EndsWith(";"))
  45. {
  46. line = line.Substring(0, line.Length - 1);
  47. line = line.Trim();
  48. }
  49. Assert.AreEqual(line, target.Line, "bad line set");
  50. return target;
  51. }
  52. [TestMethod]
  53. public void CTorTest()
  54. {
  55. Constructor("int j");
  56. }
  57. [TestMethod]
  58. public void CTorTestSmi()
  59. {
  60. Constructor("int j;");
  61. }
  62. [TestMethod]
  63. public void TestLine()
  64. {
  65. CodeItUp(new StatementSimpleStatement("int j"));
  66. }
  67. [TestMethod]
  68. public void TestTwoSemicolons()
  69. {
  70. CodeItUp(new StatementSimpleStatement("int j;"));
  71. }
  72. [TestMethod]
  73. public void TestCombineSame()
  74. {
  75. var st1 = new StatementSimpleStatement("int");
  76. var st2 = new StatementSimpleStatement("int");
  77. Assert.IsTrue(st1.TryCombineStatement(st2, null), "same statements should combine");
  78. var st3 = new StatementSimpleStatement("float");
  79. Assert.IsFalse(st1.TryCombineStatement(st3, null), "diff statements should not combine");
  80. }
  81. [PexMethod]
  82. public bool TestTryCombine([PexAssumeUnderTest] StatementSimpleStatement target, IStatement st)
  83. {
  84. return target.TryCombineStatement(st, null);
  85. }
  86. [PexMethod, PexAllowedException(typeof(ArgumentNullException)), PexAllowedException(typeof(ArgumentException)), PexAllowedException(typeof(AssertFailedException))]
  87. public StatementSimpleStatement TestRename([PexAssumeUnderTest] StatementSimpleStatement target, string oldvar, string newvar)
  88. {
  89. //
  90. // Make sure that Pex is using a legal variable name
  91. //
  92. var goodVar = new Regex(string.Format(@"w+"));
  93. if (!goodVar.Match(oldvar).Success)
  94. throw new ArgumentException("The old var is not a proper variable name");
  95. if (!goodVar.Match(newvar).Success)
  96. throw new ArgumentException("THe new var is not a proper variable name");
  97. target.RenameVariable(oldvar, newvar);
  98. if (oldvar != null && oldvar != newvar)
  99. Assert.IsFalse(Regex.Match(target.Line, string.Format(@"\b{0}\b", oldvar)).Success, "old guy should not be in there!");
  100. return target;
  101. }
  102. }
  103. }