Ejemplo n.º 1
0
        /// <summary>
        /// Creates a mock of the supplied model type and populates it with any data from the pattern directory.
        /// </summary>
        /// <param name="patternData"></param>
        /// <returns></returns>
        private static T Create <T>(JObject patternData) where T : class, IPublishedContent
        {
            // get default constructor
            var constructor = typeof(T).GetConstructor(new[] { typeof(IPublishedContent) });

            if (constructor == null)
            {
                return(default(T));
            }

            // need to pass in IPublishedContent mock into constructor
            var mockPublishedContent = new MockedPublishedContent(patternData);

            var mock = constructor.Invoke(new object[] { mockPublishedContent }) as T;

            return(mock);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets property value in specified type, using JSON data.
        /// </summary>
        /// <param name="alias"></param>
        /// <param name="patternData"></param>
        /// <returns></returns>
        private T GetPropertyValue <T>(string alias, JObject patternData)
        {
            if (string.IsNullOrEmpty(alias))
            {
                return(default(T));
            }

            // try to find property value in JSON data
            if (!patternData.TryGetValue(alias, StringComparison.InvariantCultureIgnoreCase, out var value))
            {
                return(default(T));
            }

            object propertyValue = null;

            if (value is JArray array)
            {
                // return array items
                propertyValue = array.Values <JObject>().Select(x => new MockedPublishedContent(x).ToTypedModel()).ToList();
            }
            else if (value is JValue)
            {
                var stringValue = value.Value <string>();

                // check if it's a specific type
                if (DateTime.TryParse(stringValue, out var date))
                {
                    // datetime
                    propertyValue = date;
                }
                else if (int.TryParse(stringValue, out var integer))
                {
                    // integer
                    propertyValue = integer;
                }
                else if (stringValue.StartsWith("<p") || stringValue.StartsWith("<div") ||
                         stringValue.StartsWith("<span"))
                {
                    // HTML (from RTE)
                    propertyValue = new HtmlString(stringValue);
                }
                else
                {
                    // return value as string
                    propertyValue = stringValue;
                }
            }
            else
            {
                // value is an object, assume it's IPublishedContent
                var obj = value.Value <JObject>();

                propertyValue = new MockedPublishedContent(obj).ToTypedModel();
            }

            if (typeof(T) == typeof(object))
            {
                return((T)((object)propertyValue));
            }
            else
            {
                return((T)Convert.ChangeType(propertyValue, typeof(T)));
            }
        }