< Summary

Information
Class: MyNet.UI.ViewModels.List.Filtering.Filters.StringFilterViewModel<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/StringFilterViewModel.cs
Tag: 323_28699572109
Line coverage
8%
Covered lines: 4
Uncovered lines: 45
Coverable lines: 49
Total lines: 118
Line coverage: 8.1%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
set_Operator(...)100%11100%
set_Value(...)100%210%
set_CaseSensitive(...)100%11100%
get_IsEmpty()100%210%
BuildExpressionCore()0%110100%
Reset()100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/StringFilterViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="StringFilterViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Linq.Expressions;
 9using MyNet.Primitives;
 10
 11namespace MyNet.UI.ViewModels.List.Filtering.Filters;
 12
 13public class StringFilterViewModel<T>(
 14    string propertyName,
 15    Expression<Func<T, string?>> property,
 16    StringOperator filterMode = StringOperator.Contains,
 17    bool caseSensitive = false)
 618    : FilterConditionViewModel<T>(propertyName)
 19{
 20    /// <summary>
 21    /// Gets the expression representing the string property to filter on.
 22    /// </summary>
 623    public Expression<Func<T, string?>> Property { get; } = property;
 24
 25    /// <summary>
 26    /// Gets or sets the string comparison operator used for matching.
 27    /// </summary>
 628    public StringOperator Operator { get; set => SetProperty(ref field, value); } = filterMode;
 29
 30    /// <summary>
 31    /// Gets or sets the search value to match against.
 32    /// When null or empty, the filter matches all items (empty filter).
 33    /// </summary>
 034    public string? Value { get; set => SetProperty(ref field, value); }
 35
 36    /// <summary>
 37    /// Gets or sets a value indicating whether the string comparison should be case-sensitive.
 38    /// When false (default), comparisons are case-insensitive.
 39    /// </summary>
 640    public bool CaseSensitive { get; set => SetProperty(ref field, value); } = caseSensitive;
 41
 42    /// <summary>
 43    /// Gets a value indicating whether this filter is in an empty state.
 44    /// A string filter is empty when <see cref="Value"/> is null or empty.
 45    /// </summary>
 046    public override bool IsEmpty => string.IsNullOrEmpty(Value);
 47
 48    /// <summary>
 49    /// Builds the expression representing this string filter condition based on the specified operator, value, and case
 50    /// </summary>
 51    /// <returns>An expression representing the filter condition.</returns>
 52    /// <exception cref="NotSupportedException">Thrown when the specified operator is not supported.</exception>
 53    protected override Expression<Func<T, bool>> BuildExpressionCore()
 54    {
 055        var parameter = Property.Parameters[0];
 056        var property = Property.Body;
 57
 58        // Ensure it's string
 059        var propertyAsString = property.Type == typeof(string)
 060            ? property
 061            : Expression.Call(property, nameof(ToString), Type.EmptyTypes);
 62
 063        var value = Expression.Constant(Value, typeof(string));
 64
 065        var left = propertyAsString;
 066        Expression right = value;
 67
 68        // Handle case sensitivity
 069        if (!CaseSensitive)
 70        {
 071            var toUpper = typeof(string).GetMethod(nameof(string.ToUpper), Type.EmptyTypes)!;
 72
 073            left = Expression.Condition(
 074                Expression.Equal(propertyAsString, Expression.Constant(null, typeof(string))),
 075                Expression.Constant(null, typeof(string)),
 076                Expression.Call(propertyAsString, toUpper));
 77
 078            right = Expression.Call(value, toUpper);
 79        }
 80
 81        // Handle null property
 082        var notNull = Expression.NotEqual(propertyAsString, Expression.Constant(null, typeof(string)));
 83
 084        Expression body = Operator switch
 085        {
 086            StringOperator.Contains =>
 087                Expression.AndAlso(
 088                    notNull,
 089                    Expression.Call(left, nameof(string.Contains), Type.EmptyTypes, right)),
 090
 091            StringOperator.StartsWith =>
 092                Expression.AndAlso(
 093                    notNull,
 094                    Expression.Call(left, nameof(string.StartsWith), Type.EmptyTypes, right)),
 095
 096            StringOperator.EndsWith =>
 097                Expression.AndAlso(
 098                    notNull,
 099                    Expression.Call(left, nameof(string.EndsWith), Type.EmptyTypes, right)),
 0100
 0101            StringOperator.Is =>
 0102                Expression.Equal(left, right),
 0103
 0104            StringOperator.IsNot =>
 0105                Expression.NotEqual(left, right),
 0106
 0107            _ => throw new NotSupportedException($"Operator {Operator} is not supported")
 0108        };
 109
 0110        return Expression.Lambda<Func<T, bool>>(body, parameter);
 111    }
 112
 113    /// <summary>
 114    /// Resets the filter to its default state by clearing the value.
 115    /// </summary>
 0116    public override void Reset() => Value = null;
 117}
 118