// Copyright © 2007 John M Rusk (http://dotnet.agilekiwi.com) // // You may use this source code ("The Software") in any manner you wish, // subject to the following conditions: // // (a) The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // (b) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. using System; using NUnit.Framework; namespace AgileKiwi.RegularExpressions.Tests { // In this version, we only support the features required to implement this example: http://dotnet.agilekiwi.com/blog/2006/10/shorthand-interfaces.html // Full regex support is not present in this version /// /// Derived from so that we don't have to qualify the methods we are testing /// [TestFixture] public class SmartRegexTests: SmartRegex { /// /// Assert that a set of regex elements matches the given string /// private static void AssertEx(string expected, params Element[] elements) { SmartRegex regexWrapper = new SmartRegex(elements); string regexString = regexWrapper.ToString(); Assert.AreEqual(expected, regexString); } [Test] public void CanCreateLiteral() { AssertEx("foo", Literal("foo")); // the verbose way AssertEx("foo", "foo"); // the concise way, recommended to use this when possible } [Test] public void CanCreateCharacterClasses() { AssertEx( ".", Anything); AssertEx(@"\d", Digit); AssertEx(@"\s", Space); } [Test] public void CanCreateQuantifiers() { // only support one kind of quantifier in this example AssertEx(@"\s*", Space >= 0); AssertEx(@"\s+", Space >= 1); AssertEx(@"\s{2,}", Space >= 2); AssertEx(@"\s?", Space <= 1); AssertEx(@"\s{0,2}", Space <= 2); AssertEx( "a+", Literal("a") >= 1); // if you are using the operator syntax against a literal, you have to use Literal() around the literal - since we need one of "our" classes on one side of the operator, in order to make the operator overloading work } [Test] public void CanCreateLazyQuantifiers() { AssertEx(@"\d{2,}?", Digit >= Lazy(2)); } [Test] public void CanCreateGroups() { AssertEx("(foo)", Group("foo")); AssertEx(@"(\d+)", Group(Digit >= 1)); // make a "composite group" i.e. a group made up of several expressions AssertEx(@"(\d+\s*)", Group(Digit >= 1, Space >= 0)); } [Test] public void CanNameGroups() { AssertEx("(?foo)", Group("foo").Named("TestGroup")); } // this test case is from: http://dotnet.agilekiwi.com/blog/2006/10/shorthand-interfaces.html [Test] public void CanMakeRealisticRegex() { string expectedRegex = @"\d+)-game""(?.*?)"; Assert.AreEqual(expectedRegex, _findGamesPattern.ToString()); } class GameFinder : SmartRegex { public GameFinder() : base( "= 0, "class=\"game\"", Space >= 0, "id=\"", Group(Digit >= 1).Named("gameID"), "-game\"", Group(Anything >= Lazy(0)).Named("content"), "" ) { } } SmartRegex _findGamesPattern = new GameFinder(); } }