Changed expected exception message to the one defined in Resources.resx.

Removed the Debug.Assert as that caused the test runner to crash as in
this case the preceding part is not a literal.
Fixes the test runner crash.
This commit is contained in:
Joonas Westlin 2017-05-22 22:16:52 +03:00 committed by Ryan Nowak
parent f457c7b9d8
commit a18f59ab88
3 changed files with 24 additions and 17 deletions

View File

@ -187,7 +187,7 @@
<value>In a route parameter, '{' and '}' must be escaped with '{{' and '}}'.</value> <value>In a route parameter, '{' and '}' must be escaped with '{{' and '}}'.</value>
</data> </data>
<data name="TemplateRoute_OptionalParameterCanbBePrecededByPeriod" xml:space="preserve"> <data name="TemplateRoute_OptionalParameterCanbBePrecededByPeriod" xml:space="preserve">
<value>In the segment '{0}', the optional parameter '{1}' is preceded by an invalid segment '{2}'. Only a period (.) can precede an optional parameter.</value> <value>In the segment '{0}', the optional parameter '{1}' is preceded by an invalid segment '{2}'. Only a period (.) can precede an optional parameter.</value>
</data> </data>
<data name="TemplateRoute_OptionalParameterHasTobeTheLast" xml:space="preserve"> <data name="TemplateRoute_OptionalParameterHasTobeTheLast" xml:space="preserve">
<value>An optional parameter must be at the end of the segment. In the segment '{0}', optional parameter '{1}' is followed by '{2}'.</value> <value>An optional parameter must be at the end of the segment. In the segment '{0}', optional parameter '{1}' is followed by '{2}'.</value>

View File

@ -342,20 +342,26 @@ namespace Microsoft.AspNetCore.Routing.Template
// This optional parameter is the last part in the segment // This optional parameter is the last part in the segment
if (i == segment.Parts.Count - 1) if (i == segment.Parts.Count - 1)
{ {
Debug.Assert(segment.Parts[i - 1].IsLiteral); if(!segment.Parts[i - 1].IsLiteral)
// the optional parameter is preceded by a period
if (segment.Parts[i - 1].Text == PeriodString)
{ {
segment.Parts[i - 1].IsOptionalSeperator = true; // The optional parameter is preceded by something that is not a literal.
}
else
{
// The optional parameter is preceded by a literal other than period
// Example of error message: // Example of error message:
// "In the complex segment {RouteValue}-{param?}, the optional parameter 'param'is preceded // "In the segment '{RouteValue}-{param?}', the optional parameter 'param' is preceded
// by an invalid segment "-". Only valid literal to precede an optional parameter is a // by an invalid segment '-'. Only a period (.) can precede an optional parameter.
// period (.). context.Error = string.Format(
Resources.TemplateRoute_OptionalParameterCanbBePrecededByPeriod,
segment.DebuggerToString(),
part.Name,
segment.Parts[i - 1].DebuggerToString());
return false;
}
if(segment.Parts[i - 1].Text != PeriodString)
{
// The optional parameter is preceded by a literal other than period.
// Example of error message:
// "In the segment '{RouteValue}-{param?}', the optional parameter 'param' is preceded
// by an invalid segment '-'. Only a period (.) can precede an optional parameter.
context.Error = string.Format( context.Error = string.Format(
Resources.TemplateRoute_OptionalParameterCanbBePrecededByPeriod, Resources.TemplateRoute_OptionalParameterCanbBePrecededByPeriod,
segment.DebuggerToString(), segment.DebuggerToString(),
@ -364,6 +370,7 @@ namespace Microsoft.AspNetCore.Routing.Template
return false; return false;
} }
segment.Parts[i - 1].IsOptionalSeperator = true;
} }
else else
{ {

View File

@ -545,19 +545,19 @@ namespace Microsoft.AspNetCore.Routing.Template.Tests
+ Environment.NewLine + "Parameter name: routeTemplate"); + Environment.NewLine + "Parameter name: routeTemplate");
} }
[Theory(Skip = "Skipped because it causes the test framework to crash")] [Theory]
[InlineData("{p1}-{p2?}", "-")] [InlineData("{p1}-{p2?}", "-")]
[InlineData("{p1}..{p2?}", "..")] [InlineData("{p1}..{p2?}", "..")]
[InlineData("..{p2?}", "..")] [InlineData("..{p2?}", "..")]
[InlineData("{p1}.abc.{p2?}", ".abc.")] [InlineData("{p1}.abc.{p2?}", ".abc.")]
[InlineData("{p1}{p2?}", "p1")] [InlineData("{p1}{p2?}", "{p1}")]
public void Parse_ComplexSegment_OptionalParametersSeperatedByPeriod_Invalid(string template, string parameter) public void Parse_ComplexSegment_OptionalParametersSeperatedByPeriod_Invalid(string template, string parameter)
{ {
// Act and Assert // Act and Assert
ExceptionAssert.Throws<ArgumentException>( ExceptionAssert.Throws<ArgumentException>(
() => TemplateParser.Parse(template), () => TemplateParser.Parse(template),
"In the complex segment '"+ template +"', the optional parameter 'p2' is preceded by an invalid " + "In the segment '"+ template +"', the optional parameter 'p2' is preceded by an invalid " +
"segment '" + parameter +"'. Only valid literal to precede an optional parameter is a period (.)." + "segment '" + parameter +"'. Only a period (.) can precede an optional parameter." +
Environment.NewLine + "Parameter name: routeTemplate"); Environment.NewLine + "Parameter name: routeTemplate");
} }