Ejemplo n.º 1
0
        public void Pattern_RawTextAndArrayOfSegments_ShouldMakeCopyOfArrayOfSegments()
        {
            // Arrange
            var rawText      = "raw";
            var literalPartA = RoutePatternFactory.LiteralPart("A");
            var paramPartB   = RoutePatternFactory.ParameterPart("B");
            var paramPartC   = RoutePatternFactory.ParameterPart("C");
            var paramPartD   = RoutePatternFactory.ParameterPart("D");
            var segments     = new[]
            {
                RoutePatternFactory.Segment(literalPartA, paramPartB),
                RoutePatternFactory.Segment(paramPartC, literalPartA),
                RoutePatternFactory.Segment(paramPartD),
                RoutePatternFactory.Segment(literalPartA)
            };

            // Act
            var actual = RoutePatternFactory.Pattern(rawText, segments);

            segments[1] = RoutePatternFactory.Segment(RoutePatternFactory.ParameterPart("E"));
            Array.Resize(ref segments, 2);

            // Assert
            Assert.Equal(3, actual.Parameters.Count);
            Assert.Same(paramPartB, actual.Parameters[0]);
            Assert.Same(paramPartC, actual.Parameters[1]);
            Assert.Same(paramPartD, actual.Parameters[2]);
        }
Ejemplo n.º 2
0
        public void Pattern_RawTextAndDefaultsAndParameterPoliciesAndArrayOfSegments_ShouldMakeCopyOfArrayOfSegments()
        {
            // Arrange
            var    rawText           = "raw";
            object defaults          = new { B = 12, C = 4 };
            object parameterPolicies = null;
            var    literalPartA      = RoutePatternFactory.LiteralPart("A");
            var    paramPartB        = RoutePatternFactory.ParameterPart("B");
            var    paramPartC        = RoutePatternFactory.ParameterPart("C");
            var    paramPartD        = RoutePatternFactory.ParameterPart("D");
            var    segments          = new[]
            {
                RoutePatternFactory.Segment(literalPartA, paramPartB),
                RoutePatternFactory.Segment(paramPartC, literalPartA),
                RoutePatternFactory.Segment(paramPartD),
                RoutePatternFactory.Segment(literalPartA)
            };

            // Act
            var actual = RoutePatternFactory.Pattern(rawText, defaults, parameterPolicies, segments);

            segments[1] = RoutePatternFactory.Segment(RoutePatternFactory.ParameterPart("E"));
            Array.Resize(ref segments, 2);

            // Assert
            Assert.Equal(3, actual.Parameters.Count);
            Assert.Equal(paramPartB.Name, actual.Parameters[0].Name);
            Assert.Equal(12, actual.Parameters[0].Default);
            Assert.Null(paramPartB.Default);
            Assert.NotSame(paramPartB, actual.Parameters[0]);
            Assert.Equal(paramPartC.Name, actual.Parameters[1].Name);
            Assert.Equal(4, actual.Parameters[1].Default);
            Assert.NotSame(paramPartC, actual.Parameters[1]);
            Assert.Null(paramPartC.Default);
            Assert.Equal(paramPartD.Name, actual.Parameters[2].Name);
            Assert.Null(actual.Parameters[2].Default);
            Assert.Same(paramPartD, actual.Parameters[2]);
            Assert.Null(paramPartD.Default);
        }
Ejemplo n.º 3
0
        private static bool ParseLiteral(Context context, List <RoutePatternPart> parts)
        {
            context.Mark();

            while (true)
            {
                if (context.Current == Separator)
                {
                    // End of the segment
                    break;
                }
                else if (context.Current == OpenBrace)
                {
                    if (!context.MoveNext())
                    {
                        // This is a dangling open-brace, which is not allowed
                        context.Error = Resources.TemplateRoute_MismatchedParameter;
                        return(false);
                    }

                    if (context.Current == OpenBrace)
                    {
                        // This is an 'escaped' brace in a literal, like "{{foo" - keep going.
                    }
                    else
                    {
                        // We've just seen the start of a parameter, so back up.
                        context.Back();
                        break;
                    }
                }
                else if (context.Current == CloseBrace)
                {
                    if (!context.MoveNext())
                    {
                        // This is a dangling close-brace, which is not allowed
                        context.Error = Resources.TemplateRoute_MismatchedParameter;
                        return(false);
                    }

                    if (context.Current == CloseBrace)
                    {
                        // This is an 'escaped' brace in a literal, like "{{foo" - keep going.
                    }
                    else
                    {
                        // This is an unbalanced close-brace, which is not allowed
                        context.Error = Resources.TemplateRoute_MismatchedParameter;
                        return(false);
                    }
                }

                if (!context.MoveNext())
                {
                    break;
                }
            }

            var encoded = context.Capture();
            var decoded = encoded.Replace("}}", "}").Replace("{{", "{");

            if (IsValidLiteral(context, decoded))
            {
                parts.Add(RoutePatternFactory.LiteralPart(decoded));
                return(true);
            }
            else
            {
                return(false);
            }
        }