/src/Manos.Tests/Manos.Routing/MatchOperationFactoryTest.cs

http://github.com/jacksonh/manos · C# · 157 lines · 97 code · 37 blank · 23 comment · 0 complexity · d392c89403fbe2a7403a8bd2f080252b 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 NUnit.Framework;
  26. using Manos.Routing;
  27. using Manos.ShouldExt;
  28. namespace Manos.Routing.Tests
  29. {
  30. [TestFixture()]
  31. public class MatchOperationFactoryTest
  32. {
  33. [Test ()]
  34. public void TestCreateNull ()
  35. {
  36. Should.Throw<ArgumentNullException> (() => MatchOperationFactory.Create (null));
  37. }
  38. [Test ()]
  39. public void TestIsNop ()
  40. {
  41. IMatchOperation op;
  42. op = MatchOperationFactory.Create (String.Empty);
  43. Should.BeInstanceOf<NopMatchOperation> (op, "a1");
  44. }
  45. [Test()]
  46. public void TestIsRegex ()
  47. {
  48. IMatchOperation op;
  49. op = MatchOperationFactory.Create ("dog.");
  50. Should.BeInstanceOf<RegexMatchOperation> (op, "a1");
  51. op = MatchOperationFactory.Create (".dog");
  52. Should.BeInstanceOf<RegexMatchOperation> (op, "a2");
  53. op = MatchOperationFactory.Create ("d.og");
  54. Should.BeInstanceOf<RegexMatchOperation> (op, "a3");
  55. op = MatchOperationFactory.Create (".");
  56. Should.BeInstanceOf<RegexMatchOperation> (op, "a4");
  57. op = MatchOperationFactory.Create ("[dog]");
  58. Should.BeInstanceOf<RegexMatchOperation> (op, "a6");
  59. op = MatchOperationFactory.Create ("(dog)");
  60. Should.BeInstanceOf<RegexMatchOperation> (op, "a7");
  61. op = MatchOperationFactory.Create ("^dog");
  62. Should.BeInstanceOf<RegexMatchOperation> (op, "a8");
  63. op = MatchOperationFactory.Create ("dog*");
  64. Should.BeInstanceOf<RegexMatchOperation> (op, "a9");
  65. op = MatchOperationFactory.Create (".*dog");
  66. Should.BeInstanceOf<RegexMatchOperation> (op, "a10");
  67. op = MatchOperationFactory.Create ("$dog");
  68. Should.BeInstanceOf<RegexMatchOperation> (op, "a11");
  69. op = MatchOperationFactory.Create ("dog$");
  70. Should.BeInstanceOf<RegexMatchOperation> (op, "a12");
  71. }
  72. [Test]
  73. public void Create_SimpleMatchInMiddle_CreatesSimpleMatch ()
  74. {
  75. IMatchOperation op = MatchOperationFactory.Create ("/Foo/{bar}/");
  76. Should.BeInstanceOf<SimpleMatchOperation> (op);
  77. }
  78. [Test]
  79. public void Create_SimpleMatchAtBeginning_CreatesSimpleMatch ()
  80. {
  81. IMatchOperation op = MatchOperationFactory.Create ("{bar}/Foo");
  82. Should.BeInstanceOf<SimpleMatchOperation> (op);
  83. }
  84. [Test]
  85. public void Create_SimpleMatchAtEnd_CreatesSimpleMatch ()
  86. {
  87. IMatchOperation op = MatchOperationFactory.Create ("/Foo/{bar}");
  88. Should.BeInstanceOf<SimpleMatchOperation> (op);
  89. }
  90. [Test]
  91. public void Create_SimpleMatchIsWholePattern_CreatesSimpleMatch ()
  92. {
  93. IMatchOperation op = MatchOperationFactory.Create ("{bar}");
  94. Should.BeInstanceOf<SimpleMatchOperation> (op);
  95. }
  96. [Test]
  97. public void Create_EscapedOpenSimpleMatch_CreatesStringMatch ()
  98. {
  99. IMatchOperation op = MatchOperationFactory.Create ("{{bar}");
  100. Should.BeInstanceOf<StringMatchOperation> (op);
  101. }
  102. [Test]
  103. public void Create_EscapedCloseSimpleMatch_CreatesStringMatch ()
  104. {
  105. IMatchOperation op = MatchOperationFactory.Create ("{bar}}");
  106. Should.BeInstanceOf<StringMatchOperation> (op);
  107. }
  108. [Test]
  109. public void TestIsString ()
  110. {
  111. IMatchOperation op;
  112. op = MatchOperationFactory.Create ("foobar");
  113. Should.BeInstanceOf<StringMatchOperation> (op, "a1");
  114. op = MatchOperationFactory.Create ("1");
  115. Should.BeInstanceOf<StringMatchOperation> (op, "a2");
  116. op = MatchOperationFactory.Create ("i am the walrus");
  117. Should.BeInstanceOf<StringMatchOperation> (op, "a3");
  118. }
  119. }
  120. }