Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use regex wildcard in assertion?

I am trying to do a general Assert.AreEqual call on some details in a table header, however I am struggling figuring out how to successfully format the expected results. The return value on the GetTableHeader call is as follows:

"× •••\r\nAcme Health Fund\r\nBalance Date: 9/27/2017"

I ONLY want to assert that the Acme Health Fund text is present. My current call is this:

Assert.AreEqual("/.*Acme Health Fund.*/" , GetTableHeader() );

How can I format my first parameter in the AreEqual call to ONLY expect "Acme Health Fund"?

like image 388
JOberloh Avatar asked Sep 27 '17 14:09

JOberloh


People also ask

How do you use wildcards in regex?

In regular expressions, the period ( . , also called "dot") is the wildcard pattern which matches any single character. Combined with the asterisk operator . * it will match any number of any characters.

Is wildcard same as regex?

Wildcards are different from the regular expressions used in grep (although they may look similar at times). Wildcards apply to all commands including grep and are used in place of or in combination with operands. Regular Expressions only apply to grep and a few other UNIX commands.


1 Answers

NUnit 3 has a much more powerful constraint syntax, I would recommend you use that instead. New features are added to the constraint syntax, not to the old Assert.AreEqual style.

Regex is overkill for what you want, all you need to do is assert that the string Does.Contain the name.

Assert.That(GetTableHeader(), Does.Contain("Acme Health Fund"));

If you really need to use a regex, first you don't need to surround it in the slashes and you use the Does.Match syntax.

Assert.That(GetTableHeader(), Does.Match(".*Acme Health Fund.*"));

Note the fix in your regex.

like image 68
Rob Prouse Avatar answered Oct 03 '22 02:10

Rob Prouse