Exemple #1
0
        public void NotEqual__String()
        {
            //Arrange
            string value  = "hello world";
            string single = "hello";

            bool expected = false;

            //Act
            RangeValidation <string> range = new RangeValidation <string>(single);
            bool result = range.IsRange(value);

            //Assert
            Assert.AreEqual(expected, result);
        }
Exemple #2
0
        public void Equal__Int32()
        {
            //Arrange
            int single = 10;
            int value  = 10;

            bool expected = true;

            //Act
            RangeValidation <Int32> range = new RangeValidation <int>(single);
            bool result = range.IsRange(value);

            //Assert
            Assert.AreEqual(expected, result);
        }
Exemple #3
0
        public void Range__DateTime()
        {
            //Arrange
            DateTime min   = DateTime.Parse("2016/01/01");
            DateTime max   = DateTime.Parse("2016/06/01");
            DateTime value = DateTime.Parse("2016/05/31");

            bool expected = true;

            //Act
            RangeValidation <DateTime> range = new RangeValidation <DateTime>(min, max);
            bool result = range.IsRange(value);

            //Assert
            Assert.AreEqual(expected, result);
        }
Exemple #4
0
        public void Range__Int32()
        {
            //Arrange
            int min   = 0;
            int max   = 10;
            int value = 5;

            bool expected = true;

            //Act
            RangeValidation <Int32> range = new RangeValidation <int>(min, max);
            bool result = range.IsRange(value);

            //Assert
            Assert.AreEqual(expected, result);
        }
Exemple #5
0
 /// <summary>
 /// 依照資料型態,驗證資料值範圍
 /// </summary>
 /// <typeparam name="T">資料型態</typeparam>
 /// <param name="value">資料</param>
 /// <param name="min">最小值</param>
 /// <param name="max">最大值</param>
 /// <param name="ignoreEmpty">是否忽略空白資料</param>
 /// <param name="field">顯示欄位</param>
 /// <param name="messageFormat">錯誤訊息字串格式, {0} 為顯示欄位, {1} 為最小值, {2} 為最大值</param>
 public void RangeValidate <T>(string value, T min, T max, bool ignoreEmpty, string field, string messageFormat) where T : IComparable
 {
     if (!ignoreEmpty || !String.IsNullOrWhiteSpace(value))
     {
         T convertValue            = ConvertUtility.ConvertType <T>(value, true);
         RangeValidation <T> range = new RangeValidation <T>(min, max);
         if (range.IsRange(convertValue) == false)
         {
             if (min.CompareTo(max) == 0)
             {
                 this.errorMessageList.Add(String.Format(messageFormat, field, min));
             }
             else
             {
                 this.errorMessageList.Add(String.Format(messageFormat, field, min, max));
             }
         }
     }
 }