diff --git a/src/Microsoft.AspNetCore.Routing/Template/TemplateSegment.cs b/src/Microsoft.AspNetCore.Routing/Template/TemplateSegment.cs index a3aec42686..304472653e 100644 --- a/src/Microsoft.AspNetCore.Routing/Template/TemplateSegment.cs +++ b/src/Microsoft.AspNetCore.Routing/Template/TemplateSegment.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -18,7 +19,17 @@ namespace Microsoft.AspNetCore.Routing.Template public TemplateSegment(RoutePatternPathSegment other) { - Parts = new List(other.Parts.Select(s => new TemplatePart(s))); + if (other == null) + { + throw new ArgumentNullException(nameof(other)); + } + + var partCount = other.Parts.Count; + Parts = new List(partCount); + for (var i = 0; i < partCount; i++) + { + Parts.Add(new TemplatePart(other.Parts[i])); + } } public bool IsSimple => Parts.Count == 1; diff --git a/test/Microsoft.AspNetCore.Routing.Tests/Template/TemplateSegmentTest.cs b/test/Microsoft.AspNetCore.Routing.Tests/Template/TemplateSegmentTest.cs new file mode 100644 index 0000000000..62f56f20b7 --- /dev/null +++ b/test/Microsoft.AspNetCore.Routing.Tests/Template/TemplateSegmentTest.cs @@ -0,0 +1,49 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNetCore.Routing.Patterns; +using Xunit; + +namespace Microsoft.AspNetCore.Routing.Template +{ + public class TemplateSegmentTest + { + [Fact] + public void Ctor_RoutePatternPathSegment_ShouldThrowArgumentNullExceptionWhenOtherIsNull() + { + const RoutePatternPathSegment other = null; + + var actual = Assert.ThrowsAny(() => new TemplateSegment(other)); + Assert.Equal(nameof(other), actual.ParamName); + } + + [Fact] + public void ToRoutePatternPathSegment() + { + // Arrange + var literalPartA = RoutePatternFactory.LiteralPart("A"); + var paramPartB = RoutePatternFactory.ParameterPart("B"); + var paramPartC = RoutePatternFactory.ParameterPart("C"); + var paramPartD = RoutePatternFactory.ParameterPart("D"); + var separatorPartE = RoutePatternFactory.SeparatorPart("E"); + var templateSegment = new TemplateSegment(RoutePatternFactory.Segment(paramPartC, literalPartA, separatorPartE, paramPartB)); + + // Act + var routePatternPathSegment = templateSegment.ToRoutePatternPathSegment(); + templateSegment.Parts[1] = new TemplatePart(RoutePatternFactory.ParameterPart("D")); + templateSegment.Parts.RemoveAt(0); + + // Assert + Assert.Equal(4, routePatternPathSegment.Parts.Count); + Assert.IsType(routePatternPathSegment.Parts[0]); + Assert.Equal(paramPartC.Name, ((RoutePatternParameterPart) routePatternPathSegment.Parts[0]).Name); + Assert.IsType(routePatternPathSegment.Parts[1]); + Assert.Equal(literalPartA.Content, ((RoutePatternLiteralPart) routePatternPathSegment.Parts[1]).Content); + Assert.IsType(routePatternPathSegment.Parts[2]); + Assert.Equal(separatorPartE.Content, ((RoutePatternSeparatorPart) routePatternPathSegment.Parts[2]).Content); + Assert.IsType(routePatternPathSegment.Parts[3]); + Assert.Equal(paramPartB.Name, ((RoutePatternParameterPart) routePatternPathSegment.Parts[3]).Name); + } + } +}