Ejemplo n.º 1
0
        /// <summary>
        /// Returns the value needed by the search query value.
        /// </summary>
        /// <typeparam name="T">The type of <see cref="IndexableEntity"/> implementation to use.</typeparam>
        /// <param name="searchQueryRule">The specifed search query rule</param>
        /// <returns>A typed value</returns>
        public object GetValueForSearchQueryRule <T>(SearchQueryRule <T> searchQueryRule) where T : IndexableEntity, new()
        {
            Assert.ArgumentNotNull(searchQueryRule, "searchQueryRule");

            object retVal;

            var value = GetRawDefaultOrDynamicValueProvidedByParameter(searchQueryRule);

            if (string.IsNullOrWhiteSpace(value))
            {
                return(null);
            }

            var propertyType = ExpressionUtils.GetPropertyTypeFromPropertySelector(searchQueryRule.PropertySelector);

            Tuple <string, string> rangeValueParts;

            if (SearchQueryValueConversionService.TryConvertToRangeValueParts(value, out rangeValueParts))
            {
                var lowerValue = SearchQueryValueConversionService.ToTypedValue(propertyType, rangeValueParts.Item1);
                var upperValue = SearchQueryValueConversionService.ToTypedValue(propertyType, rangeValueParts.Item2);

                retVal = new RangeValue(lowerValue, upperValue);
            }
            else
            {
                retVal = SearchQueryValueConversionService.ToTypedValue(propertyType, value);
            }

            return(retVal);
        }
        public void ToTypedValue_ValueCanBeConvertedIntoValueType_ReturnsTypedValue
            (Type valueType, string rawValue, object expectedTypedValue)
        {
            // Act
            var actual = SearchQueryValueConversionService.ToTypedValue(valueType, rawValue);

            // Assert
            actual.ShouldBeEquivalentTo(expectedTypedValue);
        }
        public void ToTypedValue_ValueCannotBeConvertedIntoValueType_ReturnsNull()
        {
            // Arrange
            Type   valueType = typeof(int);
            string rawValue  = "Foo";

            // Acts
            var actual = SearchQueryValueConversionService.ToTypedValue(valueType, rawValue);

            // Assert
            actual.Should().BeNull();
        }
        public void ToTypedValue_ValueTypeIsNull_ThrowsException()
        {
            // Arrange
            Type   valueType = null;
            string rawValue  = "1";

            // Acts
            Action act = () => SearchQueryValueConversionService.ToTypedValue(valueType, rawValue);

            // Assert
            act.ShouldThrow <ArgumentNullException>();
        }
        public void ToTypedValue_ValueCanBeConvertedIntoEnumerableGenericValueType_ReturnsEnumericGenericTypedValue()
        {
            // Arrange
            Type   valueType = typeof(IEnumerable <int>);
            string rawValue  = "1";
            int    expected  = 1;

            // Act
            var actual = SearchQueryValueConversionService.ToTypedValue(valueType, rawValue);

            // Assert
            actual.ShouldBeEquivalentTo(expected);
        }
        public void ToTypedValue_ValueCanBeConvertedIntoSitecoreIDValueType_ReturnsTypedSitecoreIDValue()
        {
            // Arrange
            Type   valueType = typeof(ID);
            string rawValue  = "{818A0CA7-1388-48BC-9E07-851314C288CB}";
            ID     expected  = new ID(rawValue);

            // Act
            var actual = SearchQueryValueConversionService.ToTypedValue(valueType, rawValue);

            // Assert
            actual.ShouldBeEquivalentTo(expected);
        }