Exemplo n.º 1
0
        public void HtmlAttributePropertyHelperReturnsValueCorrectlyForValueTypes()
        {
            // Arrange
            var          anonymous = new { foo = 32 };
            PropertyInfo property  = anonymous.GetType().GetProperties().First();

            // Act
            HtmlAttributePropertyHelper helper = new HtmlAttributePropertyHelper(property);

            // Assert
            Assert.Equal("foo", helper.Name);
            Assert.Equal(32, helper.GetValue(anonymous));
        }
Exemplo n.º 2
0
        public void HtmlAttributePropertyHelperRenamesPropertyNames()
        {
            // Arrange
            var          anonymous = new { bar_baz = "foo" };
            PropertyInfo property  = anonymous.GetType().GetProperties().First();

            // Act
            HtmlAttributePropertyHelper helper = new HtmlAttributePropertyHelper(property);

            // Assert
            Assert.Equal("bar_baz", property.Name);
            Assert.Equal("bar-baz", helper.Name);
        }
Exemplo n.º 3
0
        public void HtmlAttributePropertyHelperReturnsNameCorrectly()
        {
            // Arrange
            var          anonymous = new { foo = "bar" };
            PropertyInfo property  = anonymous.GetType().GetProperties().First();

            // Act
            HtmlAttributePropertyHelper helper = new HtmlAttributePropertyHelper(property);

            // Assert
            Assert.Equal("foo", property.Name);
            Assert.Equal("foo", helper.Name);
        }
Exemplo n.º 4
0
    public void HtmlAttributePropertyHelper_ReturnsValueCorrectly()
    {
        // Arrange
        var anonymous = new { bar = "baz" };
        var property  = anonymous.GetType().GetTypeInfo().DeclaredProperties.First();

        // Act
        var helper = new HtmlAttributePropertyHelper(property);

        // Assert
        Assert.Equal("bar", helper.Name);
        Assert.Equal("baz", helper.GetValue(anonymous));
    }
        public void HtmlAttributePropertyHelperReturnsValueCorrectlyForValueTypes()
        {
            // Arrange
            var anonymous = new { foo = 32 };
            PropertyInfo property = anonymous.GetType().GetProperties().First();

            // Act
            HtmlAttributePropertyHelper helper = new HtmlAttributePropertyHelper(property);

            // Assert
            Assert.Equal("foo", helper.Name);
            Assert.Equal(32, helper.GetValue(anonymous));
        }
        public void HtmlAttributePropertyHelperReturnsNameCorrectly()
        {
            // Arrange
            var anonymous = new { foo = "bar" };
            PropertyInfo property = anonymous.GetType().GetProperties().First();

            // Act
            HtmlAttributePropertyHelper helper = new HtmlAttributePropertyHelper(property);

            // Assert
            Assert.Equal("foo", property.Name);
            Assert.Equal("foo", helper.Name);
        }
        public void HtmlAttributePropertyHelperRenamesPropertyNames()
        {
            // Arrange
            var anonymous = new { bar_baz = "foo" };
            PropertyInfo property = anonymous.GetType().GetProperties().First();

            // Act
            HtmlAttributePropertyHelper helper = new HtmlAttributePropertyHelper(property);

            // Assert
            Assert.Equal("bar_baz", property.Name);
            Assert.Equal("bar-baz", helper.Name);
        }
Exemplo n.º 8
0
        public void HtmlAttributePropertyHelperReturnsCachedPropertyHelper()
        {
            // Arrange
            var anonymous = new { foo = "bar" };

            // Act
            PropertyHelper[] helpers1 = HtmlAttributePropertyHelper.GetProperties(anonymous);
            PropertyHelper[] helpers2 = HtmlAttributePropertyHelper.GetProperties(anonymous);

            // Assert
            Assert.Single(helpers1);
            Assert.ReferenceEquals(helpers1, helpers2);
            Assert.ReferenceEquals(helpers1[0], helpers2[0]);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creates a dictionary of HTML attributes from the input object,
        /// translating underscores to dashes.
        /// </summary>
        /// <example>
        /// new <c>{ data_name="value" }</c> will translate to the entry <c>{ "data-name" , "value" }</c>
        /// in the resulting dictionary.
        /// </example>
        /// <param name="htmlAttributes">Anonymous object describing HTML attributes.</param>
        /// <returns>A dictionary that represents HTML attributes.</returns>
        public static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes)
        {
            RouteValueDictionary result = new RouteValueDictionary();

            if (htmlAttributes != null)
            {
                foreach (PropertyHelper property in HtmlAttributePropertyHelper.GetProperties(htmlAttributes))
                {
                    result.Add(property.Name, property.GetValue(htmlAttributes));
                }
            }

            return(result);
        }
Exemplo n.º 10
0
    public void HtmlAttributePropertyHelper_ReturnsCachedPropertyHelper()
    {
        // Arrange
        var anonymous = new { foo = "bar" };

        // Act
        var helpers1 = HtmlAttributePropertyHelper.GetProperties(anonymous.GetType());
        var helpers2 = HtmlAttributePropertyHelper.GetProperties(anonymous.GetType());

        // Assert
        Assert.Single(helpers1);
        Assert.Same(helpers1, helpers2);
        Assert.Same(helpers1[0], helpers2[0]);
    }
Exemplo n.º 11
0
    public static HtmlAttributePropertyHelper[] GetProperties(Type type)
    {
        if (!ReflectionCache.TryGetValue(type, out var result))
        {
            var propertyHelpers = PropertyHelper.GetProperties(type, cache: null);
            result = new HtmlAttributePropertyHelper[propertyHelpers.Length];
            for (var i = 0; i < propertyHelpers.Length; i++)
            {
                result[i] = new(propertyHelpers[i]);
            }

            ReflectionCache[type] = result;
        }

        return(result);
    }
Exemplo n.º 12
0
        /// <summary>
        /// Creates a dictionary of HTML attributes from the input object,
        /// translating underscores to dashes in each public instance property.
        ///
        /// If the object is already an <see cref="IDictionary{string, object}"/> instance, then it is
        /// returned as-is.
        /// <example>
        /// <c>new { data_name="value" }</c> will translate to the entry <c>{ "data-name", "value" }</c>
        /// in the resulting dictionary.
        /// </example>
        /// </summary>
        /// <param name="htmlAttributes">Anonymous object describing HTML attributes.</param>
        /// <returns>A dictionary that represents HTML attributes.</returns>
        public static IDictionary <string, object> AnonymousObjectToHtmlAttributes(object htmlAttributes)
        {
            var dictionary = htmlAttributes as IDictionary <string, object>;

            if (dictionary != null)
            {
                return(new Dictionary <string, object>(dictionary, StringComparer.OrdinalIgnoreCase));
            }

            dictionary = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            if (htmlAttributes != null)
            {
                foreach (var helper in HtmlAttributePropertyHelper.GetProperties(htmlAttributes))
                {
                    dictionary[helper.Name] = helper.GetValue(htmlAttributes);
                }
            }

            return(dictionary);
        }
Exemplo n.º 13
0
        public void HtmlAttributeDoesNotShareCacheWithPropertyHelper()
        {
            // Arrange
            var anonymous = new { bar_baz1 = "foo" };

            // Act
            PropertyHelper[] helpers1 = HtmlAttributePropertyHelper.GetProperties(anonymous);
            PropertyHelper[] helpers2 = PropertyHelper.GetProperties(anonymous);

            // Assert
            PropertyHelper helper1 = Assert.Single(helpers1);
            PropertyHelper helper2 = Assert.Single(helpers2);

            Assert.NotEqual(helpers1, helpers2);
            Assert.NotEqual(helper1, helper2);

            Assert.IsType <HtmlAttributePropertyHelper>(helper1);
            Assert.IsNotType <HtmlAttributePropertyHelper>(helper2);

            Assert.Equal("bar-baz1", helper1.Name);
            Assert.Equal("bar_baz1", helper2.Name);
        }
Exemplo n.º 14
0
    public void HtmlAttributePropertyHelper_DoesNotShareCacheWithPropertyHelper()
    {
        // Arrange
        var anonymous = new { bar_baz1 = "foo" };

        // Act
        var helpers1 = HtmlAttributePropertyHelper.GetProperties(anonymous.GetType());
        var helpers2 = PropertyHelper.GetProperties(anonymous.GetType());

        // Assert
        Assert.Single(helpers1);
        Assert.Single(helpers2);

        Assert.NotEqual <PropertyHelper[]>(helpers1, helpers2);
        Assert.NotEqual <PropertyHelper>(helpers1[0], helpers2[0]);

        Assert.IsType <HtmlAttributePropertyHelper>(helpers1[0]);
        Assert.IsNotType <HtmlAttributePropertyHelper>(helpers2[0]);

        Assert.Equal("bar-baz1", helpers1[0].Name);
        Assert.Equal("bar_baz1", helpers2[0].Name);
    }