/src/Manos.Tests/Manos.Routing/RegexMatchOperationTest.cs

http://github.com/jacksonh/manos · C# · 131 lines · 84 code · 24 blank · 23 comment · 0 complexity · 89402a78c0099fe9898ac3f5dc91e70d MD5 · raw file

  1. //
  2. // Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. using System;
  25. using System.Text.RegularExpressions;
  26. using NUnit.Framework;
  27. using Manos.Routing;
  28. using System.Collections.Specialized;
  29. using Manos.ShouldExt;
  30. using Manos.Collections;
  31. namespace Manos.Routing.Tests
  32. {
  33. [TestFixture()]
  34. public class RegexMatchOperationTest
  35. {
  36. [Test]
  37. public void CtorTest ()
  38. {
  39. Should.Throw<ArgumentNullException> (() => new RegexMatchOperation (null), "a1");
  40. var r = new Regex (".*");
  41. var op = new RegexMatchOperation (r);
  42. Assert.AreEqual (r, op.Regex, "a2");
  43. }
  44. [Test]
  45. public void SetRegexTest ()
  46. {
  47. var r = new Regex (".*");
  48. var op = new RegexMatchOperation (r);
  49. Assert.AreEqual (r, op.Regex, "a1");
  50. Should.Throw<ArgumentNullException> (() => op.Regex = null, "a2");
  51. r = new Regex ("foo");
  52. op.Regex = r;
  53. Assert.AreEqual (r, op.Regex, "a3");
  54. }
  55. [Test()]
  56. public void EmptyRegexTest ()
  57. {
  58. var r = new Regex ("");
  59. var op = new RegexMatchOperation (r);
  60. var col = new DataDictionary ();
  61. int end;
  62. bool m = op.IsMatch ("", 0, out col, out end);
  63. Assert.IsTrue (m, "a1");
  64. Assert.AreEqual (0, end, "a2");
  65. }
  66. [Test]
  67. public void SimpleRegexTest ()
  68. {
  69. var r = new Regex (".og");
  70. var op = new RegexMatchOperation (r);
  71. var col = new DataDictionary ();
  72. int end;
  73. bool m;
  74. m = op.IsMatch ("dog", 0, out col, out end);
  75. Assert.IsTrue (m, "a1");
  76. Assert.AreEqual (0, col.Count, "a2");
  77. Assert.AreEqual (3, end, "a3");
  78. m = op.IsMatch ("log", 0, out col, out end);
  79. Assert.IsTrue (m, "a4");
  80. Assert.AreEqual (0, col.Count, "a5");
  81. Assert.AreEqual (3, end, "a6");
  82. m = op.IsMatch ("fox", 0, out col, out end);
  83. Assert.IsFalse (m, "a7");
  84. Assert.IsNull (col, "a8");
  85. Assert.AreEqual (0, end, "a9");
  86. }
  87. [Test]
  88. public void SimpleGroupTest ()
  89. {
  90. var r = new Regex ("-(?<foo>.*?)-");
  91. var op = new RegexMatchOperation (r);
  92. var col = new DataDictionary ();
  93. int end;
  94. bool m;
  95. m = op.IsMatch ("-manos-", 0, out col, out end);
  96. Assert.IsTrue (m, "a1");
  97. Assert.AreEqual (1, col.Count, "a2");
  98. Assert.AreEqual ("manos", col ["foo"], "a3");
  99. Assert.AreEqual (7, end, "a4");
  100. col = new DataDictionary ();
  101. m = op.IsMatch ("manos-", 0, out col, out end);
  102. Assert.IsFalse (m, "a5");
  103. Assert.IsNull (col, "a6");
  104. Assert.AreEqual (0, end, "a7");
  105. }
  106. }
  107. }