Skip to main content

Char

Describes the possible expectations for char values.

ExpectationNegatedSummary
IsEqualToIsNotEqualToequal to the expected character
IsOneOfIsNotOneOfequal to one of the expected characters
IsAnAsciiLetterIsNotAnAsciiLetteran ASCII letter
IsAnAsciiDigitIsNotAnAsciiDigitan ASCII digit
IsAnAsciiHexDigitIsNotAnAsciiHexDigitan ASCII hexadecimal digit
IsALetterIsNotALettera Unicode letter
IsADigitIsNotADigita decimal digit
IsANumberIsNotANumbera Unicode number
IsUpperCasedIsNotUpperCasedan upper-case letter
IsLowerCasedIsNotLowerCaseda lower-case letter
IsAControlCharacterIsNotAControlCharactera control character
IsWhiteSpaceIsNotWhiteSpacewhitespace
.NET 8 or later

On .NET 8 or later char is an INumber<char>, so the ordering expectations, IsBetween, IsPositive and IsNegative of the number expectations also accept a char. Below .NET 8 they are not available for char.

Equality​

You can verify that the char is equal to another one or not:

char initial = 'a';

await That(initial).IsEqualTo('a');
await That(initial).IsNotEqualTo('b');
await That(initial).IsEqualTo('A').IgnoringCase();

One of​

You can verify that the char is one of many alternatives:

char initial = 'a';

await That(initial).IsOneOf('a', 'b', 'c');
await That(initial).IsNotOneOf('x', 'y', 'z');
await That(initial).IsOneOf('A', 'B', 'C').IgnoringCase();

Categories​

You can verify the category of the char, or that it does not belong to it:

await That('a').IsAnAsciiLetter();
await That('乐').IsALetter();
await That('3').IsADigit();
await That('½').IsANumber().And.IsNotADigit();
await That('A').IsUpperCased();
await That('\t').IsWhiteSpace().And.IsAControlCharacter();
await That(' ').IsNotAControlCharacter();

Each expectation follows the corresponding method of char:

ExpectationMethod
IsAnAsciiLetterchar.IsAsciiLetter
IsAnAsciiDigitchar.IsAsciiDigit
IsAnAsciiHexDigitchar.IsAsciiHexDigit
IsALetterchar.IsLetter
IsADigitchar.IsDigit
IsANumberchar.IsNumber
IsUpperCasedchar.IsUpper
IsLowerCasedchar.IsLower
IsAControlCharacterchar.IsControl
IsWhiteSpacechar.IsWhiteSpace

IsADigit only accepts decimal digits, while IsANumber also accepts characters like '½'.

Casing of a char vs. a string

IsUpperCased and IsLowerCased differ from the string casing expectations, which only look at cased letters: '1' is neither upper-cased nor lower-cased, while "1" is both.

IgnoringCase() compares the characters the same way as strings with IgnoringCase(), i.e. with StringComparison.OrdinalIgnoreCase.