< Summary

Class:ApplicationCore.Model.CommentForm
Assembly:ApplicationCore
File(s):C:\Users\Teknikaali\Source\Repos\jekyll-blog-comments\ApplicationCore\Model\CommentForm.cs
Covered lines:62
Uncovered lines:0
Coverable lines:62
Total lines:121
Line coverage:100% (62 of 62)
Covered branches:24
Total branches:24
Branch coverage:100% (24 of 24)

Coverage History

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.cctor()10100%100%
.ctor(...)80100%100%
TryCreateComment()40100%100%
TryConvertFormFieldValue(...)80100%100%
IsEmailValid()20100%100%

File(s)

C:\Users\Teknikaali\Source\Repos\jekyll-blog-comments\ApplicationCore\Model\CommentForm.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Globalization;
 5using System.Linq;
 6using System.Reflection;
 7using System.Text.RegularExpressions;
 8using Microsoft.AspNetCore.Http;
 9
 10namespace ApplicationCore.Model
 11{
 12    public class CommentForm : ICommentForm
 13    {
 14        /// <summary>
 15        /// Simplest form of email validation
 16        /// </summary>
 117        private static readonly Regex _validEmail = new Regex(@"^[^@\s]+@[^@\s]+\.[^@\s]+$");
 18
 19        private readonly Dictionary<string, FormField> _fields;
 20        private readonly ConstructorInfo _constructor;
 21
 2622        public IEnumerable<string> Errors { get; }
 23
 1024        public bool HasErrors => Errors.Any();
 25
 1526        public CommentForm(IFormCollection form)
 1527        {
 1528            _constructor = typeof(Comment).GetConstructors()[0];
 29
 1530            _fields = _constructor.GetParameters().ToDictionary(
 12031                p => p.Name!,
 12032                p => TryConvertFormFieldValue(form[p.Name], p));
 33
 1534            var errors = _fields
 12035                .Where(p => p.Value.HasError)
 3036                .Select(p => p.Value.Error)
 1537                .ToList();
 38
 1539            if (!IsEmailValid())
 140            {
 141                errors.Add("email is not in the correct format");
 142            }
 43
 1544            Errors = errors;
 1545        }
 46
 47        /// <summary>
 48        /// Try to create a Comment from the form.  Each Comment constructor argument will be name-matched
 49        /// against values in the form. Each non-optional arguments (those that don't have a default value)
 50        /// not supplied will cause an error in the list of errors and prevent the Comment from being created.
 51        /// </summary>
 52        public CommentResult TryCreateComment()
 253        {
 254            if (!Errors.Any())
 155            {
 856                return new CommentResult((Comment)_constructor.Invoke(_fields.Values.Select(x => x.Value).ToArray()));
 57            }
 58            else
 159            {
 160                return new CommentResult(new Comment(string.Empty, string.Empty, string.Empty), Errors);
 61            }
 262        }
 63
 64        private static FormField TryConvertFormFieldValue(string fieldValue, ParameterInfo parameterInfo)
 10565        {
 10566            if (IsRequiredField())
 3167            {
 3168                return new FormField(TypeDescriptor.GetConverter(parameterInfo.ParameterType).ConvertFrom(fieldValue));
 69            }
 7470            else if (IsOptionalField())
 6071            {
 6072                if (!string.IsNullOrEmpty(fieldValue))
 1373                {
 1374                    var converter = TypeDescriptor.GetConverter(parameterInfo.ParameterType);
 75
 1376                    if (converter.IsValid(fieldValue))
 1277                    {
 1278                        var convertedValue = converter.ConvertFrom(fieldValue);
 1279                        return new FormField(convertedValue);
 80                    }
 81                    else
 182                    {
 183                        return new FormField(string.Format(
 184                            CultureInfo.InvariantCulture,
 185                            CommentResources.InvalidTypeConversionErrorMessage,
 186                            fieldValue,
 187                            parameterInfo.ParameterType.ToString()));
 88                    }
 89                }
 90                else
 4791                {
 4792                    return new FormField(parameterInfo.DefaultValue);
 93                }
 94            }
 95            else
 1496            {
 97                // Field is required but missing a value
 1498                return new FormField(string.Format(
 1499                    CultureInfo.InvariantCulture,
 14100                    CommentResources.MissingRequiredValueErrorMessage,
 14101                    parameterInfo.Name));
 102            }
 103
 105104            bool IsRequiredField() => !string.IsNullOrWhiteSpace(fieldValue) && !parameterInfo.HasDefaultValue;
 74105            bool IsOptionalField() => parameterInfo.HasDefaultValue;
 105106        }
 107
 108        // TODO: Remove magic string "email"
 109        private bool IsEmailValid()
 15110        {
 15111            var email = _fields["email"].Value as string;
 112
 15113            if (string.IsNullOrEmpty(email))
 10114            {
 10115                return true;
 116            }
 117
 5118            return _validEmail.IsMatch(email);
 15119        }
 120    }
 121}