public void Parse_ComplexSegment_OptionalParameter_NotTheLastPart(
     string template,
     string parameter,
     string invalid)
 {
     // Act and Assert
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse(template),
         "An optional parameter must be at the end of the segment. In the segment '" + template +
         "', optional parameter '" + parameter + "' is followed by '" + invalid + "'.");
 }
Exemple #2
0
 public static RoutePattern Parse(string pattern)
 {
     try
     {
         return(RoutePatternParser.Parse(pattern));
     }
     catch (RoutePatternException ex)
     {
         throw new ArgumentException(ex.Message, nameof(pattern), ex);
     }
 }
        public void ParseRouteParameter_ThrowsIf_ParameterContainsSpecialCharacters(
            string template,
            string parameterName)
        {
            // Arrange
            var expectedMessage = "The route parameter name '" + parameterName + "' is invalid. Route parameter " +
                                  "names must be non-empty and cannot contain these characters: '{', '}', '/'. The '?' character " +
                                  "marks a parameter as optional, and can occur only at the end of the parameter. The '*' character " +
                                  "marks a parameter as catch-all, and can occur only at the start of the parameter.";

            // Act & Assert
            ExceptionAssert.Throws <RoutePatternException>(() => RoutePatternParser.Parse(template), expectedMessage);
        }
        public void Parse_OptionalParameter()
        {
            // Arrange
            var template = "{p?}";

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText("{p?}", RoutePatternPart.CreateParameterFromText("{p?}", "p", null, RoutePatternParameterKind.Optional));

            var expected = builder.Build();

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
        public void Parse_SingleLiteral()
        {
            // Arrange
            var template = "cool";

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText("cool", RoutePatternPart.CreateLiteralFromText("cool", "cool"));

            var expected = builder.Build();

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
        public void Parse_MultipleParameters()
        {
            // Arrange
            var template = "{p1}/{p2}/{*p3}";

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText("{p1}", RoutePatternPart.CreateParameterFromText("{p1}", "p1"));
            builder.AddPathSegmentFromText("{p2}", RoutePatternPart.CreateParameterFromText("{p2}", "p2"));
            builder.AddPathSegmentFromText("{*p3}", RoutePatternPart.CreateParameterFromText("{*p3}", "p3", null, RoutePatternParameterKind.CatchAll));

            var expected = builder.Build();

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
        public void Parse_ComplexSegment_OptionalParameterFollowingPeriod_PeriodAfterSlash()
        {
            // Arrange
            var template = "{p2}/.{p3?}";

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText("{p2}", RoutePatternPart.CreateParameterFromText("{p2}", "p2"));
            builder.AddPathSegmentFromText(".{p3?}",
                                           RoutePatternPart.CreateSeparatorFromText(".", "."),
                                           RoutePatternPart.CreateParameterFromText("{p3?}", "p3", null, RoutePatternParameterKind.Optional));

            var expected = builder.Build();

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
        public void Parse_ComplexSegment_LPL()
        {
            // Arrange
            var template = "cool-{p1}-awesome";

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText(
                template,
                RoutePatternPart.CreateLiteralFromText("cool-", "cool-"),
                RoutePatternPart.CreateParameterFromText("{p1}", "p1"),
                RoutePatternPart.CreateLiteralFromText("-awesome", "-awesome"));

            var expected = builder.Build();

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
        public void Parse_ComplexSegment_ParametersFollowingPeriod()
        {
            // Arrange
            var template = "{p1}.{p2}";

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText(
                "{p1}.{p2}",
                RoutePatternPart.CreateParameterFromText("{p1}", "p1"),
                RoutePatternPart.CreateLiteralFromText(".", "."),
                RoutePatternPart.CreateParameterFromText("{p2}", "p2"));

            var expected = builder.Build();

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
        [InlineData(@"{p1:regex(([{{(])\w+)}", @"regex(([{(])\w+)")]                                      // Not balanced {
        public void Parse_RegularExpressions(string template, string constraint)
        {
            // Arrange
            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText(
                template,
                RoutePatternPart.CreateParameterFromText(
                    template,
                    "p1",
                    null,
                    RoutePatternParameterKind.Standard,
                    ConstraintReference.CreateFromText(constraint, constraint)));

            var expected = builder.Build();

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
 public void InvalidTemplate_CatchAllMarkedOptional()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{a}/{*b?}"),
         "A catch-all parameter cannot be marked optional.");
 }
 public void InvalidTemplate_CannotContainQuestionMark()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("foor?bar"),
         "The literal section 'foor?bar' is invalid. Literal sections cannot contain the '?' character.");
 }
 public void InvalidTemplate_CannotStartWithTilde()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("~foo"),
         "The route template cannot start with a '~' character unless followed by a '/'.");
 }
 public void InvalidTemplate_WithCatchAllNotAtTheEndThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("foo/{p1}/{*p2}/{p3}"),
         "A catch-all parameter can only appear as the last segment of the route template.");
 }
 public void InvalidTemplate_InvalidParameterNameWithOpenBracketThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{a}/{a{aa}/{z}"),
         "In a route parameter, '{' and '}' must be escaped with '{{' and '}}'.");
 }
 public void InvalidTemplate_SameParameterTwiceAndOneCatchAllThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{aaa}/{*AAA}"),
         "The route parameter name 'AAA' appears more than one time in the route template.");
 }
 public void InvalidTemplate_CannotHaveMoreThanOneCatchAll()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{*p1}/{*p2}"),
         "A catch-all parameter can only appear as the last segment of the route template.");
 }
 public void InvalidTemplate_WithRepeatedParameter()
 {
     var ex = ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{Controller}.mvc/{id}/{controller}"),
         "The route parameter name 'controller' appears more than one time in the route template.");
 }