public void Combine_RightOverridesReflectedAttributeRouteModel( AttributeRouteModel left, AttributeRouteModel right) { // Arrange var expectedTemplate = AttributeRouteModel.CombineTemplates(null, right.Template); // Act var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right); // Assert Assert.NotNull(combined); Assert.Equal(expectedTemplate, combined.Template); Assert.Equal(combined.Order, right.Order); }
public void ReplaceTokens_ValidValues(string template, object values, string expected) { // Arrange var valuesDictionary = values as IDictionary <string, object>; if (valuesDictionary == null) { valuesDictionary = new RouteValueDictionary(values); } // Act var result = AttributeRouteModel.ReplaceTokens(template, valuesDictionary); // Assert Assert.Equal(expected, result); }
public void CopyConstructor_CopiesAllProperties() { // Arrange var route = new AttributeRouteModel(new HttpGetAttribute("/api/Products")); route.Name = "products"; route.Order = 5; // Act var route2 = new AttributeRouteModel(route); // Assert foreach (var property in typeof(AttributeRouteModel).GetProperties()) { var value1 = property.GetValue(route); var value2 = property.GetValue(route2); if (typeof(IEnumerable <object>).IsAssignableFrom(property.PropertyType)) { Assert.Equal <object>((IEnumerable <object>)value1, (IEnumerable <object>)value2); // Ensure non-default value Assert.NotEmpty((IEnumerable <object>)value1); } else if (property.PropertyType.IsValueType || Nullable.GetUnderlyingType(property.PropertyType) != null) { Assert.Equal(value1, value2); // Ensure non-default value Assert.NotEqual(value1, Activator.CreateInstance(property.PropertyType)); } else { Assert.Same(value1, value2); // Ensure non-default value Assert.NotNull(value1); } } }
public void CopyConstructor_DoesDeepCopyOfOtherModels() { // Arrange var action = new ActionModel(typeof(TestController).GetMethod("Edit")); var parameter = new ParameterModel(action.ActionMethod.GetParameters()[0]); parameter.Action = action; action.Parameters.Add(parameter); var route = new AttributeRouteModel(new HttpGetAttribute("api/Products")); action.AttributeRouteModel = route; // Act var action2 = new ActionModel(action); // Assert Assert.NotSame(action, action2.Parameters[0]); Assert.NotSame(route, action2.AttributeRouteModel); }
public void ReplaceTokens_InvalidFormat(string template, object values, string reason) { // Arrange var valuesDictionary = values as IDictionary <string, object>; if (valuesDictionary == null) { valuesDictionary = new RouteValueDictionary(values); } var expected = string.Format( "The route template '{0}' has invalid syntax. {1}", template, reason); // Act var ex = Assert.Throws <InvalidOperationException>( () => { AttributeRouteModel.ReplaceTokens(template, valuesDictionary); }); // Assert Assert.Equal(expected, ex.Message); }
public void ReplaceTokens_UnknownValue() { // Arrange var template = "[area]/[controller]/[action2]"; var values = new RouteValueDictionary() { { "area", "Help" }, { "controller", "Admin" }, { "action", "SeeUsers" }, }; var expected = "While processing template '[area]/[controller]/[action2]', " + "a replacement value for the token 'action2' could not be found. " + "Available tokens: 'area, controller, action'."; // Act var ex = Assert.Throws <InvalidOperationException>( () => { AttributeRouteModel.ReplaceTokens(template, values); }); // Assert Assert.Equal(expected, ex.Message); }