public void Test(string value, ulong?size, Unit unit, int errorCount) { MaxReportSize maxReportSize = _parser.Parse(value); Assert.That(maxReportSize.Value, Is.EqualTo(size)); Assert.That(maxReportSize.Unit, Is.EqualTo(unit)); Assert.That(maxReportSize.ErrorCount, Is.EqualTo(errorCount)); }
public MaxReportSize Parse(string value) { if (value != null) { Match match = _regex.Match(value); if (match.Success) { string sizeToken = match.Groups["size"].Value.ToLower(); string unitToken = match.Groups["unit"].Value; ulong candidateSize; ulong?size = ulong.TryParse(sizeToken, out candidateSize) ? candidateSize : (ulong?)null; Unit unit; if (!Enum.TryParse(unitToken, true, out unit)) { unit = Unit.B; //byte is default if no unit specified. } MaxReportSize maxReportSize = new MaxReportSize(size, unit); if (!size.HasValue) { string maxSizeErrorMessage = string.Format(DmarcParserResource.InvalidValueErrorMessage, "max size", sizeToken); maxReportSize.AddError(new Error(ErrorType.Error, maxSizeErrorMessage)); } if (!string.IsNullOrEmpty(unitToken) && unit == Unit.Unknown) { string unitErrorMessage = string.Format(DmarcParserResource.InvalidValueErrorMessage, "unit", unitToken); maxReportSize.AddError(new Error(ErrorType.Error, unitErrorMessage)); } return(maxReportSize); } } MaxReportSize invalidMaxReportSize = new MaxReportSize(null, Unit.Unknown); string maxReportSizeErrorMessage = string.Format(DmarcParserResource.InvalidValueErrorMessage, "max report size", value); invalidMaxReportSize.AddError(new Error(ErrorType.Error, maxReportSizeErrorMessage)); return(invalidMaxReportSize); }
public UriTag Parse(string uriString) { string[] tokens = uriString?.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries).Select(_ => _.Trim()).ToArray() ?? new string[0]; DmarcUri dmarcUri = _dmarcUriParser.Parse(tokens.ElementAtOrDefault(0)); MaxReportSize maxReportSize = tokens.Length > 1 ? _maxReportSizeParser.Parse(tokens[1]) : null; UriTag uriTag = new UriTag(uriString, dmarcUri, maxReportSize); if (tokens.Length > 2) { string unexpectedValues = string.Join(",", tokens.Skip(2)); string unexpectedValuesErrorMessage = string.Format(DmarcParserResource.UnexpectedValueErrorMessage, unexpectedValues, "uri", unexpectedValues); uriTag.AddError(new Error(ErrorType.Error, unexpectedValuesErrorMessage)); } return(uriTag); }
public void ExtraTokensFoundUriReturnedWithError() { string value = $"{UriString}!{SizeString}!{SizeString}"; DmarcUri dmarcUri = new DmarcUri(new Uri(UriString)); A.CallTo(() => _dmarcUriParser.Parse(UriString)).Returns(dmarcUri); MaxReportSize maxReportSize = new MaxReportSize(50, Unit.M); A.CallTo(() => _maxReportSizeParser.Parse(SizeString)).Returns(maxReportSize); UriTag uriTag = _parser.Parse(value); Assert.That(uriTag.Uri, Is.SameAs(dmarcUri)); Assert.That(uriTag.MaxReportSize, Is.SameAs(maxReportSize)); Assert.That(uriTag.ErrorCount, Is.EqualTo(1)); A.CallTo(() => _dmarcUriParser.Parse(UriString)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => _maxReportSizeParser.Parse(SizeString)).MustHaveHappened(Repeated.Exactly.Once); }
public void UriAndMaxReportSizeTokensPresentFullyFormedUriTagReturned() { string value = $"{UriString}!{SizeString}"; DmarcUri dmarcUri = new DmarcUri(new Uri(UriString)); A.CallTo(() => _dmarcUriParser.Parse(UriString)).Returns(dmarcUri); MaxReportSize maxReportSize = new MaxReportSize(50, Unit.M); A.CallTo(() => _maxReportSizeParser.Parse(SizeString)).Returns(maxReportSize); UriTag uriTag = _parser.Parse(value); Assert.That(uriTag.Uri, Is.SameAs(dmarcUri)); Assert.That(uriTag.MaxReportSize, Is.SameAs(maxReportSize)); Assert.That(uriTag.ErrorCount, Is.Zero); A.CallTo(() => _dmarcUriParser.Parse(UriString)).MustHaveHappened(Repeated.Exactly.Once); A.CallTo(() => _maxReportSizeParser.Parse(SizeString)).MustHaveHappened(Repeated.Exactly.Once); }