< Summary

Information
Class: ValidateLib.Metadata.Properties.NaturalLanguageProperty
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\Properties\NaturalLanguageProperty.cs
Line coverage
60%
Covered lines: 12
Uncovered lines: 8
Coverable lines: 20
Total lines: 59
Line coverage: 60%
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\Metadata\Properties\NaturalLanguageProperty.cs

#LineLine coverage
 1using Newtonsoft.Json.Linq;
 2using ValidateLib.ErrorsAndWarnings.Warnings;
 3using ValidateLib.Metadata.Descriptors;
 4using ValidateLib.Metadata.Descriptors.Interfaces;
 5
 6namespace ValidateLib.Metadata.Properties
 7{
 8    public class NaturalLanguageProperty : Property<Dictionary<string, string[]>>, INormalize
 9    {
 010        public static readonly string _default = "";
 111        public NaturalLanguageProperty(Dictionary<string, string[]> value) : base(value) { }
 12        public void AddNewLanguage(string language, string[] expressions)
 13        {
 014            _value![language] = expressions;
 015        }
 16
 17        static string[] GetExpressionsFromToken(JToken token)
 18        {
 019            if (token.Type == JTokenType.String)
 20            {
 021                return new string[] { token.ToString() };
 22            }
 023            else if (token.Type == JTokenType.Array)
 24            {
 025                return ((JArray)token).Select(token => (string)token!)?.ToArray()!;
 26            }
 27            else
 28            {
 029                throw new ArgumentException();
 30            }
 31        }
 32
 33        public static List<Warning> Normalize(JToken token, Context context, JProperty? prop = null)
 34        {
 135            List<Warning> errors = new List<Warning>();
 136            if (token.Type == JTokenType.Object)
 37            {
 138                foreach (JProperty property in token)
 39                {
 140                    if (property.Value.Type == JTokenType.String)
 41                    {
 142                        property.Value = new JArray(property.Value);
 43                    }
 44                }
 45            }
 146            else if (token.Type == JTokenType.String)
 47            {
 148                token.Replace(new JObject(new JProperty(context?.language?._value!, new JArray(token.ToString()))));
 49            }
 50
 151            else if (token.Type == JTokenType.Array)
 52            {
 153                token.Replace(new JObject(new JProperty(context?.language?._value!, token)));
 54            }
 155            else errors.Add(WarningFactory.GetNaturalLanguageWrongValue(token, prop?.Name!));
 156            return errors;
 57        }
 58    }
 59}