< Summary

Information
Class: ValidateLib.TabularData.Datatypes.DurationDatatypes.DurationDT
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Datatypes\DurationDatatypes\DurationDT.cs
Line coverage
64%
Covered lines: 27
Uncovered lines: 15
Coverable lines: 42
Total lines: 87
Line coverage: 64.2%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Datatypes\DurationDatatypes\DurationDT.cs

#LineLine coverage
 1using System.Text.RegularExpressions;
 2using System.Xml;
 3using ValidateLib.ErrorsAndWarnings.Errors;
 4using ValidateLib.Metadata.Descriptors;
 5
 6namespace ValidateLib.TabularData.Datatypes.DurationDatatypes
 7{
 8
 9    public class DurationDT : BaseDT, IValue
 10    {
 111        public TimeSpan Value { get; set; }
 112        public DurationDT() { }
 113        public DurationDT(string stringValue) : base(stringValue)
 14        {
 115            ValidateValue(stringValue);
 16            try
 17            {
 118                Value = XmlConvert.ToTimeSpan(stringValue);
 119            }
 120            catch
 21            {
 122                ErrorFactory.ThrowDatatypeValidationError(stringValue);
 023            }
 124        }
 25
 126        public DurationDT(string stringValue, FormatDescriptor format) : base(stringValue)
 27        {
 128            ValidateValue(stringValue);
 129            ValidateFormat(format.pattern!);
 30
 131            if (!Regex.IsMatch(stringValue, format.pattern!))
 132                ErrorFactory.ThrowDatatypeValidationError(stringValue);
 33            try
 34            {
 135                Value = XmlConvert.ToTimeSpan(stringValue);
 36
 137            }
 038            catch
 39            {
 040                ErrorFactory.ThrowDatatypeValidationError(stringValue);
 041            }
 142        }
 43
 44        void ValidateValue(string stringValue)
 45        {
 146            string pattern = @"-?P((([0-9]+Y([0-9]+M)?([0-9]+D)?)|([0-9]+M)([0-9]+D)?|([0-9]+D))(T((([0-9]+H)([0-9]+M)?(
 47
 148            if (!Regex.IsMatch(stringValue, pattern))
 149                ErrorFactory.ThrowDatatypeValidationError(stringValue);
 150        }
 51
 52        void ValidateFormat(string stringValue)
 53        {
 54            try
 55            {
 156                Regex regex = new Regex(stringValue);
 157            }
 058            catch (Exception)
 59            {
 060                ErrorFactory.ThrowInvalidFormatError(stringValue);
 061            }
 162        }
 63        override public int CompareTo(BaseDT? other)
 64        {
 065            if (other is null)
 066                return 1;
 067            if (other is not DurationDT)
 068                throw new ArgumentException();
 069            var typedOther = (DurationDT)other;
 070            return Value.CompareTo(typedOther.Value);
 71
 72        }
 73
 74        public override bool IsPatternValid(FormatDescriptor format)
 75        {
 76            try
 77            {
 178                Regex regex = new Regex(format.pattern!);
 179                return true;
 80            }
 081            catch (Exception)
 82            {
 083                return false;
 84            }
 185        }
 86    }
 87}