/// <summary> /// Adds a constraint instance for the given key. /// </summary> /// <param name="key">The key.</param> /// <param name="value"> /// The constraint instance. Must either be a string or an instance of <see cref="IDispatcherValueConstraint"/>. /// </param> /// <remarks> /// If the <paramref name="value"/> is a string, it will be converted to a <see cref="RegexDispatcherValueConstraint"/>. /// /// For example, the string <code>Product[0-9]+</code> will be converted to the regular expression /// <code>^(Product[0-9]+)</code>. See <see cref="System.Text.RegularExpressions.Regex"/> for more details. /// </remarks> public void AddConstraint(string key, object value) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (value == null) { throw new ArgumentNullException(nameof(value)); } var constraint = value as IDispatcherValueConstraint; if (constraint == null) { var regexPattern = value as string; if (regexPattern == null) { throw new InvalidOperationException( Resources.FormatDispatcherValueConstraintBuilder_ValidationMustBeStringOrCustomConstraint( key, value, _rawText, typeof(IDispatcherValueConstraint))); } var constraintsRegEx = "^(" + regexPattern + ")$"; constraint = new RegexDispatcherValueConstraint(constraintsRegEx); } Add(key, constraint); }
public void RegexConstraintFailsIfKeyIsNotFoundInRouteValues() { // Arrange var constraint = new RegexDispatcherValueConstraint(new Regex("^abc$")); var values = new DispatcherValueCollection(new { action = "abc" }); // Act var match = TestConstraint(constraint, values, "controller"); // Assert Assert.False(match); }
public void RegexConstraintConstructedWithRegex_SimpleFailedMatch() { // Arrange var constraint = new RegexDispatcherValueConstraint(new Regex("^abc$")); var values = new DispatcherValueCollection(new { controller = "Abc" }); // Act var match = TestConstraint(constraint, values, "controller"); // Assert Assert.False(match); }
public void RegexConstraint_TakesRegexAsInput_SimpleMatch() { // Arrange var constraint = new RegexDispatcherValueConstraint(new Regex("^abc$")); var values = new DispatcherValueCollection(new { controller = "abc" }); // Act var match = TestConstraint(constraint, values, "controller"); // Assert Assert.True(match); }
[InlineData(@"*****@*****.**", @"^\w+[\w\.]*\@\w+((-\w+)|(\w*))\.[a-z]{2,3}$", true)] // email public void RegexConstraintBuildRegexVerbatimFromInput( string routeValue, string constraintValue, bool shouldMatch) { // Arrange var constraint = new RegexDispatcherValueConstraint(constraintValue); var values = new DispatcherValueCollection(new { controller = routeValue }); // Act var match = TestConstraint(constraint, values, "controller"); // Assert Assert.Equal(shouldMatch, match); }
public void RegexConstraintIsCultureInsensitiveWhenConstructedWithString(string culture) { if (TestPlatformHelper.IsMono) { // The Regex in Mono returns true when matching the Turkish I for the a-z range which causes the test // to fail. Tracked via #100. return; } // Arrange var constraint = new RegexDispatcherValueConstraint("^([a-z]+)$"); var values = new DispatcherValueCollection(new { controller = "\u0130" }); // Turkish upper-case dotted I using (new CultureReplacer(culture)) { // Act var match = TestConstraint(constraint, values, "controller"); // Assert Assert.False(match); } }