public void Max_MaximumIsIntegerAndTheValueIsGreaterThan_Fails() { int value = 2; var sut = new MaxAttribute(1); Assert.IsFalse(sut.IsValid(value)); }
public void Max_MaximumIsIntegerAndTheValueIsLessThan_Validates() { int value = 0; var sut = new MaxAttribute(1); Assert.IsTrue(sut.IsValid(value)); }
public async Task <IActionResult> Edit(int id, [Bind("ID,ObjectName,AttributeName,Description,CreatedDate")] MaxAttribute maxAttr) { if (id != maxAttr.ID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(maxAttr); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MaxAttrExists(maxAttr.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(maxAttr)); }
public void Max_MaximumIsDoubleAndTheValueIsLessThan_Validates() { double value = 0; var sut = new MaxAttribute(1.1); Assert.IsTrue(sut.IsValid(value)); }
public void Max_MaximumIsSpecifiedTypeAndTheValueIsGreaterThan_Fails() { long value = 2; var sut = new MaxAttribute(typeof(long), "1"); Assert.IsFalse(sut.IsValid(value)); }
private static Attribute ConvertToMax(XmlNhvmRuleConverterArgs rule) { NhvmMax maxRule = (NhvmMax)rule.schemaRule; long value = long.MaxValue; if (maxRule.valueSpecified) { value = maxRule.value; } #if NETFX log.Info(string.Format("Converting to Max attribute with value {0}", value)); #else Log.Info("Converting to Max attribute with value {0}", value); #endif MaxAttribute thisAttribute = new MaxAttribute(); thisAttribute.Value = value; if (maxRule.message != null) { thisAttribute.Message = maxRule.message; } AssignTagsFromString(thisAttribute, maxRule.tags); return(thisAttribute); }
public void Max_MaximumIsDoubleAndTheValueIsGreaterThan_Fails() { double value = 2.2; var sut = new MaxAttribute(1.1); Assert.IsFalse(sut.IsValid(value)); }
public void Max_MaximumIsSpecifiedTypeAndTheValueIsEqualTo_Validates() { long value = 1; var sut = new MaxAttribute(typeof(long), "1"); Assert.IsTrue(sut.IsValid(value)); }
public void Max_FormatErrorMessage_ReturnsFormattedErrorMessage() { var sut = new MaxAttribute(1); var error = sut.FormatErrorMessage("property"); Assert.IsTrue(!string.IsNullOrEmpty(error)); }
public void Max_FormatErrorMessageWithErrorMessage_ReturnsFormattedErrorMessage() { var sut = new MaxAttribute(1); sut.ErrorMessage = "My error."; var error = sut.FormatErrorMessage("property"); Assert.IsTrue(error == "My error."); }
public void Max_FormatErrorMessageWithErrorResource_ReturnsFormattedErrorMessage() { var sut = new MaxAttribute(1); sut.ErrorMessageResourceName = "MaxAttribute_Invalid"; sut.ErrorMessageResourceType = typeof(DataAnnotationsResources); var error = sut.FormatErrorMessage("property"); Assert.IsTrue(error == string.Format(DataAnnotationsResources.MaxAttribute_Invalid, "property", 1)); }
public async Task <IActionResult> Create([Bind("ID,Description,ObjectName,AttributeName,Type,Size,Purpose,CreatedDate")] MaxAttribute maxAttr) { if (ModelState.IsValid) { _context.Add(maxAttr); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(maxAttr)); }
public void ErrorMessageTest() { var attribute = new MaxAttribute(1); attribute.ErrorMessage = "SampleErrorMessage"; const string invalidValue = "a"; var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null)); Assert.AreEqual("SampleErrorMessage", result.ErrorMessage); }
public void ErrorResourcesTest() { var attribute = new MaxAttribute(1); attribute.ErrorMessageResourceName = "ErrorMessage"; attribute.ErrorMessageResourceType = typeof(ErrorResources); const string invalidValue = "a"; var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null)); Assert.AreEqual("error message", result.ErrorMessage); }
public void GlobalizedErrorResourcesTest() { System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-MX"); var attribute = new MaxAttribute(1); attribute.ErrorMessageResourceName = "ErrorMessage"; attribute.ErrorMessageResourceType = typeof(ErrorResources); const string invalidValue = "a"; var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null)); Assert.AreEqual("mensaje de error", result.ErrorMessage); }
public void Validate(Config c) { var typ = c.GetType(); foreach (var field in typ.GetProperties()) { MinAttribute min = field.Get <MinAttribute>(); MaxAttribute max = field.Get <MaxAttribute>(); if (min == null && max == null) { continue; } object value = field.GetValue(c, index: null); var opt = field.Get <OptAttribute>(); if (min != null) { var coercedMinVal = (IComparable)opt.Coerce(min.Value, field.PropertyType); if (coercedMinVal.CompareTo(value) == 1) { throw new Exception(string.Format("invalid {0} ! {1} < {2}", opt.Name, value, coercedMinVal)); } } if (max != null) { var coercedMaxVal = (IComparable)opt.Coerce(max.Value, field.PropertyType); if (coercedMaxVal.CompareTo(value) == -1) { throw new Exception(string.Format("invalid {0} ! {1} > {2}", opt.Name, value, coercedMaxVal)); } } } if (c.HeartbeatInterval > c.ReadTimeout) { throw new Exception(string.Format("HeartbeatInterval {0} must be less than ReadTimeout {1}", c.HeartbeatInterval, c.ReadTimeout)); } // TODO: PR go-nsq: this check was removed, seems still valid since the value can be set through code // https://github.com/bitly/go-nsq/commit/dd8e5fc4ad80922d884ece51f5574af3fa4f14d3#diff-b4bda758a2aef091432646c354b4dc59L376 if (c.BackoffStrategy == null) { throw new Exception(string.Format("BackoffStrategy cannot be null")); } }
public void IsValid() { var v = new MaxAttribute(); Assert.IsTrue(v.IsValid(0, null)); v = new MaxAttribute(1000); Assert.IsTrue(v.IsValid(3, null)); Assert.IsTrue(v.IsValid(200.0m, null)); Assert.IsTrue(v.IsValid(1000, null)); Assert.IsTrue(v.IsValid(null, null)); Assert.IsTrue(v.IsValid("5", null)); Assert.IsFalse(v.IsValid(1001, null)); Assert.IsFalse(v.IsValid("aaa", null)); Assert.IsFalse(v.IsValid(new object(), null)); Assert.IsFalse(v.IsValid(long.MaxValue, null)); }
public void IsValidDoubleTests() { const double max = 3.50f; var attribute = new MaxAttribute(max); Assert.IsTrue(attribute.IsValid(null)); // Optional values are always valid Assert.IsTrue(attribute.IsValid(3)); Assert.IsTrue(attribute.IsValid("3.498")); Assert.IsTrue(attribute.IsValid("-5")); Assert.IsFalse(attribute.IsValid(3.51)); Assert.IsFalse(attribute.IsValid("4")); Assert.IsFalse(attribute.IsValid("4.5")); Assert.IsFalse(attribute.IsValid(100)); Assert.IsFalse(attribute.IsValid("100.42")); Assert.IsFalse(attribute.IsValid("INVALID STRING")); }
public void IsValidIntegerTests() { const int max = 42; var attribute = new MaxAttribute(max); Assert.IsTrue(attribute.IsValid(null)); // Optional values are always valid Assert.IsTrue(attribute.IsValid("41")); Assert.IsTrue(attribute.IsValid("10")); Assert.IsTrue(attribute.IsValid(0)); Assert.IsTrue(attribute.IsValid("-1")); Assert.IsTrue(attribute.IsValid(-50)); Assert.IsTrue(attribute.IsValid("42")); Assert.IsFalse(attribute.IsValid(42.5)); Assert.IsFalse(attribute.IsValid(43)); Assert.IsFalse(attribute.IsValid("50")); Assert.IsFalse(attribute.IsValid(100)); Assert.IsFalse(attribute.IsValid("10000000000")); Assert.IsFalse(attribute.IsValid("fifty")); }
public void Extreme() { var v = new MaxAttribute(10000); Assert.IsTrue(v.IsValid(10000, null)); Assert.IsTrue(v.IsValid(10000L, null)); Assert.IsTrue(v.IsValid(123UL, null)); Assert.IsTrue(v.IsValid(123U, null)); Assert.IsTrue(v.IsValid((ushort)5, null)); Assert.IsTrue(v.IsValid((short)5, null)); Assert.IsTrue(v.IsValid(true, null)); Assert.IsTrue(v.IsValid((byte)100, null)); Assert.IsTrue(v.IsValid((sbyte)100, null)); Assert.IsTrue(v.IsValid(AEnum.A, null)); Assert.IsTrue(v.IsValid(CarOptions.Spoiler | CarOptions.FogLights, null)); Assert.IsTrue(v.IsValid('A', null)); Assert.IsTrue(v.IsValid(9999.99999f, null)); Assert.IsTrue(v.IsValid(9999.9999999999999999999999999d, null)); Assert.IsFalse(v.IsValid(decimal.MaxValue, null)); Assert.IsFalse(v.IsValid(decimal.MaxValue.ToString(), null)); Assert.IsFalse(v.IsValid(double.MaxValue, null)); Assert.IsFalse(v.IsValid("1" + double.MaxValue, null)); }
private static string WriteExtenders(PropertyInfo p, out bool isValidatable) { isValidatable = false; StringBuilder sb = new StringBuilder(); sb.Append(".extend({ editState : false, disableValidation : false, empty : true })"); RequiredAttribute requiredAttribute = p.GetCustomAttribute <RequiredAttribute>(); if (requiredAttribute != null) { sb.Append($".extend({{ required: {{ message: \"{requiredAttribute.FormatErrorMessage(p.Name)}\", onlyIf: function() {{ return ko.utils.isPropertyValidatable(self, \"{ p.Name}\"); }} }} }})"); isValidatable = true; } MinLengthAttribute minLengthAttribute = p.GetCustomAttribute <MinLengthAttribute>(); if (minLengthAttribute != null) { sb.Append(string.Format(".extend({ minLength: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", minLengthAttribute.FormatErrorMessage(p.Name), minLengthAttribute.Length, p.Name)); isValidatable = true; } MaxLengthAttribute maxLengthAttribute = p.GetCustomAttribute <MaxLengthAttribute>(); if (maxLengthAttribute != null) { sb.Append(string.Format(".extend({ maxLength: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", maxLengthAttribute.FormatErrorMessage(p.Name), maxLengthAttribute.Length, p.Name)); isValidatable = true; } RegularExpressionAttribute regularExpressionAttribute = p.GetCustomAttribute <RegularExpressionAttribute>(); if (regularExpressionAttribute != null) { sb.Append(string.Format(".extend({ pattern: { message: \"{0}\", params: /{1}/, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", regularExpressionAttribute.FormatErrorMessage(p.Name), regularExpressionAttribute.Pattern, p.Name)); isValidatable = true; } UrlAttribute urlAttribute = p.GetCustomAttribute <UrlAttribute>(); if (urlAttribute != null) { sb.Append(string.Format(".extend({ pattern: { message: \"{0}\", params: /{1}/, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", urlAttribute.FormatErrorMessage(p.Name), urlAttribute, p.Name)); isValidatable = true; } DateAttribute dateAttribute = p.GetCustomAttribute <DateAttribute>(); if (dateAttribute != null) { sb.Append(string.Format(".extend({ date: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } })", dateAttribute.FormatErrorMessage(p.Name), p.Name)); isValidatable = true; } NumericAttribute numericAttribute = p.GetCustomAttribute <NumericAttribute>(); if (numericAttribute != null) { sb.Append( string.Format(".extend({ number: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } }).extend({ numeric: {2} })", numericAttribute.FormatErrorMessage(p.Name), p.Name, numericAttribute.Precision)); isValidatable = true; } EmailAttribute emailAttribute = p.GetCustomAttribute <EmailAttribute>(); if (emailAttribute != null) { sb.Append(string.Format(".extend({ email: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } })", emailAttribute.FormatErrorMessage(p.Name), p.Name)); isValidatable = true; } DigitsAttribute digitsAttribute = p.GetCustomAttribute <DigitsAttribute>(); if (digitsAttribute != null) { sb.Append(string.Format(".extend({ digit: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } }).extend({ numeric: 0 })", digitsAttribute.FormatErrorMessage(p.Name), p.Name)); isValidatable = true; } MinAttribute minAttribute = p.GetCustomAttribute <MinAttribute>(); if (minAttribute != null) { sb.Append(string.Format(".extend({ min: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", minAttribute.FormatErrorMessage(p.Name), minAttribute.Min, p.Name)); isValidatable = true; } MaxAttribute maxAttribute = p.GetCustomAttribute <MaxAttribute>(); if (maxAttribute != null) { sb.Append(string.Format(".extend({ max: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", maxAttribute.FormatErrorMessage(p.Name), maxAttribute.Max, p.Name)); isValidatable = true; } EqualToAttribute equalToAttribute = p.GetCustomAttribute <EqualToAttribute>(); if (equalToAttribute != null) { sb.Append(string.Format(".extend({ equal: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", equalToAttribute.FormatErrorMessage(p.Name), equalToAttribute.OtherProperty, p.Name)); isValidatable = true; } CompareAttribute compareAttribute = p.GetCustomAttribute <CompareAttribute>(); if (compareAttribute != null) { sb.Append(string.Format(".extend({ equal: { message: \"{0}\", params: \"{1}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", compareAttribute.FormatErrorMessage(p.Name), compareAttribute.OtherProperty, p.Name)); isValidatable = true; } FormatterAttribute formatterAttribute = p.GetCustomAttribute <FormatterAttribute>(); if (formatterAttribute != null) { sb.Append(string.Format(".formatted({0}, {1})", formatterAttribute.Formatter, JsonConvert.SerializeObject(formatterAttribute.Arguments))); } return(sb.ToString()); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { MaxAttribute max = (MaxAttribute)attribute; if (fieldInfo.FieldType == typeof(int)) { if (property.intValue > max.intValue) { property.intValue = max.intValue; } } else if (fieldInfo.FieldType == typeof(long)) { switch (max.valueType) { case MaxAttribute.ValueType.Int: if (property.longValue > max.intValue) { property.longValue = max.intValue; } break; case MaxAttribute.ValueType.Long: if (property.longValue > max.longValue) { property.longValue = max.longValue; } break; } } else if (fieldInfo.FieldType == typeof(float)) { switch (max.valueType) { case MaxAttribute.ValueType.Int: if (property.floatValue > max.intValue) { property.floatValue = max.intValue; } break; case MaxAttribute.ValueType.Long: if (property.floatValue > max.longValue) { property.floatValue = max.longValue; } break; case MaxAttribute.ValueType.Float: if (property.floatValue > max.floatValue) { property.floatValue = max.floatValue; } break; } } else if (fieldInfo.FieldType == typeof(double)) { switch (max.valueType) { case MaxAttribute.ValueType.Int: if (property.doubleValue > max.intValue) { property.doubleValue = max.intValue; } break; case MaxAttribute.ValueType.Long: if (property.doubleValue > max.longValue) { property.doubleValue = max.longValue; } break; case MaxAttribute.ValueType.Float: if (property.doubleValue > max.floatValue) { property.doubleValue = max.floatValue; } break; case MaxAttribute.ValueType.Double: if (property.doubleValue > max.doubleValue) { property.doubleValue = max.doubleValue; } break; default: break; } } }
public void KnownRulesConvertAssing() { NhvMapping map = XmlMappingLoader.GetXmlMappingFor(typeof(WellKnownRules)); NhvmClass cm = map.@class[0]; XmlClassMapping rm = new XmlClassMapping(cm); MemberInfo mi; List <Attribute> attributes; mi = typeof(WellKnownRules).GetField("AP"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); Assert.AreEqual("A string value", ((ACustomAttribute)attributes[0]).Value1); Assert.AreEqual(123, ((ACustomAttribute)attributes[0]).Value2); Assert.AreEqual("custom message", ((ACustomAttribute)attributes[0]).Message); mi = typeof(WellKnownRules).GetField("StrProp"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); NotEmptyAttribute nea = FindAttribute <NotEmptyAttribute>(attributes); Assert.AreEqual("not-empty message", nea.Message); NotNullAttribute nna = FindAttribute <NotNullAttribute>(attributes); Assert.AreEqual("not-null message", nna.Message); NotNullNotEmptyAttribute nnea = FindAttribute <NotNullNotEmptyAttribute>(attributes); Assert.AreEqual("notnullnotempty message", nnea.Message); LengthAttribute la = FindAttribute <LengthAttribute>(attributes); Assert.AreEqual("length message", la.Message); Assert.AreEqual(1, la.Min); Assert.AreEqual(10, la.Max); PatternAttribute pa = FindAttribute <PatternAttribute>(attributes); Assert.AreEqual("pattern message", pa.Message); Assert.AreEqual("[0-9]+", pa.Regex); Assert.AreEqual(RegexOptions.Compiled, pa.Flags); EmailAttribute ea = FindAttribute <EmailAttribute>(attributes); Assert.AreEqual("email message", ea.Message); IPAddressAttribute ipa = FindAttribute <IPAddressAttribute>(attributes); Assert.AreEqual("ipAddress message", ipa.Message); EANAttribute enaa = FindAttribute <EANAttribute>(attributes); Assert.AreEqual("ean message", enaa.Message); CreditCardNumberAttribute ccna = FindAttribute <CreditCardNumberAttribute>(attributes); Assert.AreEqual("creditcardnumber message", ccna.Message); IBANAttribute iban = FindAttribute <IBANAttribute>(attributes); Assert.AreEqual("iban message", iban.Message); mi = typeof(WellKnownRules).GetField("DtProp"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); FutureAttribute fa = FindAttribute <FutureAttribute>(attributes); Assert.AreEqual("future message", fa.Message); PastAttribute psa = FindAttribute <PastAttribute>(attributes); Assert.AreEqual("past message", psa.Message); mi = typeof(WellKnownRules).GetField("DecProp"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); DigitsAttribute dga = FindAttribute <DigitsAttribute>(attributes); Assert.AreEqual("digits message", dga.Message); Assert.AreEqual(5, dga.IntegerDigits); Assert.AreEqual(2, dga.FractionalDigits); MinAttribute mina = FindAttribute <MinAttribute>(attributes); Assert.AreEqual("min message", mina.Message); Assert.AreEqual(100, mina.Value); MaxAttribute maxa = FindAttribute <MaxAttribute>(attributes); Assert.AreEqual("max message", maxa.Message); Assert.AreEqual(200, maxa.Value); DecimalMaxAttribute decimalmaxa = FindAttribute <DecimalMaxAttribute>(attributes); Assert.AreEqual("decimal max message", decimalmaxa.Message); Assert.AreEqual(200.1m, decimalmaxa.Value); DecimalMinAttribute decimalmina = FindAttribute <DecimalMinAttribute>(attributes); Assert.AreEqual("decimal min message", decimalmina.Message); Assert.AreEqual(99.9m, decimalmina.Value); mi = typeof(WellKnownRules).GetField("BProp"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); AssertTrueAttribute ata = FindAttribute <AssertTrueAttribute>(attributes); Assert.AreEqual("asserttrue message", ata.Message); AssertFalseAttribute afa = FindAttribute <AssertFalseAttribute>(attributes); Assert.AreEqual("assertfalse message", afa.Message); mi = typeof(WellKnownRules).GetField("ArrProp"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); SizeAttribute sa = FindAttribute <SizeAttribute>(attributes); Assert.AreEqual("size message", sa.Message); Assert.AreEqual(2, sa.Min); Assert.AreEqual(9, sa.Max); mi = typeof(WellKnownRules).GetField("Pattern"); attributes = new List <Attribute>(rm.GetMemberAttributes(mi)); PatternAttribute spa = FindAttribute <PatternAttribute>(attributes); Assert.AreEqual("{validator.pattern}", spa.Message); Assert.AreEqual(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", spa.Regex); Assert.AreEqual(RegexOptions.CultureInvariant | RegexOptions.IgnoreCase, spa.Flags); }
/*************************** ******* METHODS ******* **************************/ // Make your own IMGUI based GUI for the property public override void OnGUI(Rect _position, SerializedProperty _property, GUIContent _label) { MaxAttribute _attribute = (MaxAttribute)attribute; EditorGUIEnhanced.MaxField(_position, _property, _label, _attribute.MaxValue); }
public void Max_NullValue_Validates() { var sut = new MaxAttribute(1); Assert.IsTrue(sut.IsValid(null)); }
public void Max_EmptyValue_Validates() { var sut = new MaxAttribute(1); Assert.IsTrue(sut.IsValid(string.Empty)); }