Skip to content

LikeComparison is a library that allows you to compare a string expression to a pattern in an "SQL LIKE" expression.

License

Notifications You must be signed in to change notification settings

cagrin/LikeComparison

Repository files navigation

NuGet Nuget Coverage Status

LikeComparison

LikeComparison is a library that allows you to compare a string expression to a pattern in an "SQL LIKE" expression.

It supports many LIKE operator syntax:

  • Visual Basic,
  • Transact-SQL (with ESCAPE),
  • PostgreSQL (both LIKE and ILIKE).

Main use:

  • Like method on strings
  • IsLike method on MSTest assertions
  • ShouldBeLike method on Shouldly assertions

Using Like method on strings from LikeComparison package

You can simply use extension method on String class:

using LikeComparison.VisualBasic;
string? matchExpression = "abcdef";

bool isLike = matchExpression.Like(pattern: "a*");

or

using LikeComparison.TransactSql;
string? matchExpression = "abcdef";

// common use
bool isLike = matchExpression.Like(pattern: "a%");

// or with escape character
bool isLike = matchExpression.Like(pattern: "/a%", escapeCharacter: "/");

or

using LikeComparison.PostgreSql;
string? matchExpression = "abcdef";

// case-insensitive
bool isLike = matchExpression.ILike(pattern: "A%");

// or case-sensitive
bool isLike = matchExpression.Like(pattern: "a%");

Using IsLike method on MSTest assertions from LikeComparison.MSTest package

You can simply use extension method on Assert class:

using LikeComparison.VisualBasic;
string? matchExpression = "abcdef";

Assert.That.IsLike(matchExpression, pattern: "a*");

or

using LikeComparison.TransactSql;
string? matchExpression = "abcdef";

// common use
Assert.That.IsLike(matchExpression, pattern: "a%");

// or with escape character
Assert.That.IsLike(matchExpression, pattern: "/a%", escape: "/");

or

using LikeComparison.PostgreSql;
string? matchExpression = "abcdef";

// case-insensitive
Assert.That.IsILike(matchExpression, pattern: "A%");

// or case-sensitive
Assert.That.IsLike(matchExpression, pattern: "a%");

Using ShouldBeLike method on Shouldly assertions from LikeComparison.Shouldly package

You can simply use extension method on String class:

using LikeComparison.VisualBasic;
string? matchExpression = "abcdef";

matchExpression.ShouldBeLike(pattern: "a*");

or

using LikeComparison.TransactSql;
string? matchExpression = "abcdef";

// common use
matchExpression.ShouldBeLike(pattern: "a%");

// or with escape character
matchExpression.ShouldBeLike(pattern: "/a%", escape: "/");

or

using LikeComparison.PostgreSql;
string? matchExpression = "abcdef";

// case-insensitive
matchExpression.ShouldBeILike(pattern: "A%");

// or case-sensitive
matchExpression.ShouldBeLike(pattern: "a%");

Supported syntax

Visual Basic

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/like-operator

* ? [ ] ^ #

Transact-SQL

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql

% _ [ ] !

PostgreSQL

https://www.postgresql.org/docs/current/functions-matching.html

% _

Supported targets

.NET 8.0

  • Recommended.
  • Default target for code coverage.

.NET 6.0

  • Supported.

.NET Standard 2.0

  • Experimentally supported with C# 8.

About

LikeComparison is a library that allows you to compare a string expression to a pattern in an "SQL LIKE" expression.

Resources

License

Stars

Watchers

Forks