< Summary - Code Coverage

Information
Class: Plainquire.Filter.ValueFilterExpressions.StringFilterExpression
Assembly: Plainquire.Filter
File(s): /home/runner/work/plainquire/plainquire/Plainquire.Filter/Plainquire.Filter/ValueFilterExpressions/StringFilterExpression.cs
Tag: 64_13932151703
Line coverage
94%
Covered lines: 53
Uncovered lines: 3
Coverable lines: 56
Total lines: 159
Line coverage: 94.6%
Branch coverage
70%
Covered branches: 17
Total branches: 24
Branch coverage: 70.8%
Method coverage
100%
Covered methods: 9
Total methods: 9
Method coverage: 100%

Metrics

File(s)

/home/runner/work/plainquire/plainquire/Plainquire.Filter/Plainquire.Filter/ValueFilterExpressions/StringFilterExpression.cs

#LineLine coverage
 1using Plainquire.Filter.Abstractions;
 2using System;
 3using System.Collections.Generic;
 4using System.Diagnostics.CodeAnalysis;
 5using System.Globalization;
 6using System.Linq.Expressions;
 7
 8namespace Plainquire.Filter.ValueFilterExpressions;
 9
 10/// <inheritdoc cref="IStringFilterExpression"/>
 11[SuppressMessage("ReSharper", "MemberCanBePrivate.Global", Justification = "Provided as library, can be used from outsid
 12public class StringFilterExpression : DefaultFilterExpression, IStringFilterExpression
 13{
 14    /// <inheritdoc />
 15    public override ICollection<FilterOperator> SupportedFilterOperators
 16        =>
 106417        [
 106418            FilterOperator.Default,
 106419            FilterOperator.Contains,
 106420            FilterOperator.StartsWith,
 106421            FilterOperator.EndsWith,
 106422            FilterOperator.EqualCaseSensitive,
 106423            FilterOperator.EqualCaseInsensitive,
 106424            FilterOperator.NotEqual,
 106425            FilterOperator.IsNull,
 106426            FilterOperator.NotNull
 106427        ];
 28
 29    /// <inheritdoc />
 30    public override bool CanCreateExpressionFor(Type type)
 3504031        => type.GetUnderlyingType() == typeof(string);
 32
 33    /// <inheritdoc />
 34    protected internal override Expression CreateExpressionForValue<TEntity, TProperty>(Expression<Func<TEntity, TProper
 35    {
 336836        var strFilter = value?.Trim();
 37        switch (filterOperator)
 38        {
 39            case FilterOperator.Default:
 40            case FilterOperator.Contains:
 54441                return CreateStringContainsExpression(propertySelector, strFilter);
 42            case FilterOperator.StartsWith:
 46243                return CreateStringStartsWithExpression(propertySelector, strFilter);
 44            case FilterOperator.EndsWith:
 45645                return CreateStringEndsWithExpression(propertySelector, strFilter);
 46            case FilterOperator.EqualCaseSensitive:
 29447                return CreateStringCaseSensitiveEqualExpression(propertySelector, strFilter);
 48            case FilterOperator.EqualCaseInsensitive:
 151649                return CreateStringCaseInsensitiveEqualExpression(propertySelector, strFilter);
 50            case FilterOperator.NotEqual:
 9651                return CreateStringNotContainsExpression(propertySelector, strFilter);
 52            default:
 053                throw CreateFilterExpressionCreationException($"Filter operator '{filterOperator}' not allowed for prope
 54        }
 55    }
 56
 57    /// <summary>
 58    /// Creates a string contains expression.
 59    /// </summary>
 60    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 61    /// <typeparam name="TProperty">The type of the property.</typeparam>
 62    /// <param name="propertySelector">A property selector.</param>
 63    /// <param name="value">The value.</param>
 64    public static Expression CreateStringContainsExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> pro
 65    {
 64066        var valueToUpper = Expression.Constant(value?.ToUpper(CultureInfo.InvariantCulture), typeof(TProperty));
 64067        var propertyToUpper = propertySelector.Body.StringToUpper();
 64068        var propertyContainsValue = propertyToUpper.StringContains(valueToUpper);
 64069        var propertyIsNotNull = propertySelector.IsNotNull();
 64070        var propertyIsNotNullAndContainsValue = Expression.AndAlso(propertyIsNotNull, propertyContainsValue);
 64071        return propertyIsNotNullAndContainsValue;
 72    }
 73
 74    /// <summary>
 75    /// Creates a string case-sensitive equal expression.
 76    /// </summary>
 77    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 78    /// <typeparam name="TProperty">The type of the property.</typeparam>
 79    /// <param name="propertySelector">The property selector.</param>
 80    /// <param name="value">The value.</param>
 81    public static Expression CreateStringCaseSensitiveEqualExpression<TEntity, TProperty>(Expression<Func<TEntity, TProp
 82    {
 29483        if (value == null)
 084            return propertySelector.IsNull();
 29485        if (value == string.Empty)
 5486            return propertySelector.StringIsEmpty();
 87
 24088        var valueExpression = Expression.Constant(value, typeof(TProperty));
 24089        var propertyEqualsValue = Expression.Equal(propertySelector.Body, valueExpression);
 24090        return propertyEqualsValue;
 91    }
 92
 93    /// <summary>
 94    /// Creates a string case-insensitive equal expression.
 95    /// </summary>
 96    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 97    /// <typeparam name="TProperty">The type of the property.</typeparam>
 98    /// <param name="propertySelector">The property selector.</param>
 99    /// <param name="value">The value.</param>
 100    public static Expression CreateStringCaseInsensitiveEqualExpression<TEntity, TProperty>(Expression<Func<TEntity, TPr
 101    {
 1522102        if (value == null)
 0103            return propertySelector.IsNull();
 1522104        if (value == string.Empty)
 192105            return propertySelector.StringIsEmpty();
 106
 1330107        var valueToUpper = Expression.Constant(value.ToUpper(CultureInfo.InvariantCulture), typeof(TProperty));
 1330108        var propertyToUpper = propertySelector.Body.StringToUpper();
 1330109        var propertyEqualsValue = Expression.Equal(propertyToUpper, valueToUpper);
 1330110        var propertyIsNotNull = propertySelector.IsNotNull();
 1330111        var propertyIsNotNullAndEqualsValue = Expression.AndAlso(propertyIsNotNull, propertyEqualsValue);
 1330112        return propertyIsNotNullAndEqualsValue;
 113    }
 114
 115    /// <summary>
 116    /// Creates a string starts with expression.
 117    /// </summary>
 118    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 119    /// <typeparam name="TProperty">The type of the property.</typeparam>
 120    /// <param name="propertySelector">The property selector.</param>
 121    /// <param name="value">The value.</param>
 122    public static Expression CreateStringStartsWithExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> p
 123    {
 462124        var valueToUpper = Expression.Constant(value?.ToUpper(CultureInfo.InvariantCulture), typeof(TProperty));
 462125        var propertyToUpper = propertySelector.Body.StringToUpper();
 462126        var propertyStartsWithValue = propertyToUpper.StringStartsWith(valueToUpper);
 462127        var propertyIsNotNull = propertySelector.IsNotNull();
 462128        var propertyIsNotNullAndStartsWithValue = Expression.AndAlso(propertyIsNotNull, propertyStartsWithValue);
 462129        return propertyIsNotNullAndStartsWithValue;
 130    }
 131
 132    /// <summary>
 133    /// Creates a string ends with expression.
 134    /// </summary>
 135    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 136    /// <typeparam name="TProperty">The type of the property.</typeparam>
 137    /// <param name="propertySelector">The property selector.</param>
 138    /// <param name="value">The value.</param>
 139    public static Expression CreateStringEndsWithExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> pro
 140    {
 456141        var valueToUpper = Expression.Constant(value?.ToUpper(CultureInfo.InvariantCulture), typeof(TProperty));
 456142        var propertyToUpper = propertySelector.Body.StringToUpper();
 456143        var propertyEndsWithValue = propertyToUpper.StringEndsWith(valueToUpper);
 456144        var propertyIsNotNull = propertySelector.IsNotNull();
 456145        var propertyIsNotNullAndEndsWithValue = Expression.AndAlso(propertyIsNotNull, propertyEndsWithValue);
 456146        return propertyIsNotNullAndEndsWithValue;
 147    }
 148
 149    /// <summary>
 150    /// Creates a negated string contains expression.
 151    /// </summary>
 152    /// <typeparam name="TEntity">The type of the t entity.</typeparam>
 153    /// <typeparam name="TProperty">The type of the t property.</typeparam>
 154    /// <param name="propertySelector">The property selector.</param>
 155    /// <param name="value">The value.</param>
 156    /// <autogeneratedoc />
 157    public static Expression CreateStringNotContainsExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> 
 96158        => Expression.Not(CreateStringContainsExpression(propertySelector, value));
 159}