public void ParameterPart_ParameterNameAndDefaultAndParameterKindAndEnumerableOfParameterPolicies_ShouldMakeCopyOfParameterPolicies()
        {
            // Arrange (going through hoops to get an enumerable of RoutePatternParameterPolicyReference)
            const string name         = "Id";
            var          defaults     = new { a = "13", };
            var          x            = new InlineConstraint("x");
            var          y            = new InlineConstraint("y");
            var          z            = new InlineConstraint("z");
            var          constraints  = new[] { x, y, z };
            var          templatePart = TemplatePart.CreateParameter("t", false, false, null, constraints);
            var          routePatternParameterPart = (RoutePatternParameterPart)templatePart.ToRoutePatternPart();
            var          policies = routePatternParameterPart.ParameterPolicies.ToList();

            // Act
            var parameterPart = RoutePatternFactory.ParameterPart(name, defaults, RoutePatternParameterKind.Standard, policies);

            policies[0] = null;
            policies.RemoveAt(1);

            // Assert
            Assert.NotNull(parameterPart.ParameterPolicies);
            Assert.Equal(3, parameterPart.ParameterPolicies.Count);
            Assert.NotNull(parameterPart.ParameterPolicies[0]);
            Assert.NotNull(parameterPart.ParameterPolicies[1]);
            Assert.NotNull(parameterPart.ParameterPolicies[2]);
        }
        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]);
        }
        private static void RemoveParameterDefault(List <RoutePatternPathSegment> segments, List <RoutePatternParameterPart> parameters, RoutePatternParameterPart parameter)
        {
            // We know that a parameter can only appear once, so we only need to rewrite one segment and one parameter.
            for (var i = 0; i < segments.Count; i++)
            {
                var segment = segments[i];
                for (var j = 0; j < segment.Parts.Count; j++)
                {
                    if (object.ReferenceEquals(parameter, segment.Parts[j]))
                    {
                        // Found it!
                        var updatedParameter = RoutePatternFactory.ParameterPart(parameter.Name, @default: null, parameter.ParameterKind, parameter.ParameterPolicies);

                        var updatedParts = new List <RoutePatternPart>(segment.Parts);
                        updatedParts[j] = updatedParameter;
                        segments[i]     = RoutePatternFactory.Segment(updatedParts);

                        for (var k = 0; k < parameters.Count; k++)
                        {
                            if (ReferenceEquals(parameter, parameters[k]))
                            {
                                parameters[k] = updatedParameter;
                                break;
                            }
                        }

                        return;
                    }
                }
            }
        }
        public void Segment_ArrayOfParts()
        {
            // Arrange
            var paramPartB = RoutePatternFactory.ParameterPart("B");
            var paramPartC = RoutePatternFactory.ParameterPart("C");
            var paramPartD = RoutePatternFactory.ParameterPart("D");
            var parts      = new[] { paramPartB, paramPartC, paramPartD };

            // Act
            var actual = RoutePatternFactory.Segment(parts);

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

            // Assert
            Assert.Equal(3, actual.Parts.Count);
            Assert.Same(paramPartB, actual.Parts[0]);
            Assert.Same(paramPartC, actual.Parts[1]);
            Assert.Same(paramPartD, actual.Parts[2]);
        }
        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);
        }