Strings#
String assertion helpers for comparison and checks.
Provides utilities for comparing string equality, checking length bounds, and testing for None/empty/whitespace values.
Example#
from punit import strings
assert strings.are_same('hello', 'hello')
assert not strings.are_same('hello', 'world')
assert strings.is_none_or_empty(None)
assert strings.has_length('hello', min=3, max=10)
- are_same(a, b)#
Check if two strings contain the same characters in the same order.
- Return type:
bool
- Args:
a: The first string b: The second string
- Returns:
True if the strings contain the same characters in the same order, False otherwise
- has_length(actual, min=None, max=None)#
Check if actual value’s length falls within the inclusive range [min, max].
- Return type:
bool
- Args:
actual: The string to check. min: Inclusive lower bound on length (
len(actual) >= min). max: Inclusive upper bound on length (len(actual) <= max).- Returns:
True if the length satisfies the bounds, False otherwise. When actual is None, returns True only when both bounds are effectively zero.
Example#
from punit import strings a = 'hello' assert strings.has_length(a, min=5) assert strings.has_length(a, max=5) assert strings.has_length(a, min=3, max=7) assert not strings.has_length(a, min=6)
- is_none_or_empty(string)#
Check if a string is None or empty.
- Return type:
bool
- Args:
string: The string to check
- Returns:
True if the string is None or empty, False otherwise
- is_none_or_whitespace(string)#
Check if a string is None or whitespace.
- Return type:
bool
- Args:
string: The string to check.
- Returns:
True if the string is None or whitespace, False otherwise.
Example#
from punit import strings assert not strings.is_none_or_whitespace('hello') assert strings.is_none_or_whitespace(' ') assert strings.is_none_or_whitespace(None)
- areSame(a, b)#
Check if two strings contain the same characters in the same order.
- Return type:
bool
- Args:
a: The first string b: The second string
- Returns:
True if the strings contain the same characters in the same order, False otherwise
- hasLength(actual, min=None, max=None)#
Check if actual value’s length falls within the inclusive range [min, max].
- Return type:
bool
- Args:
actual: The string to check. min: Inclusive lower bound on length (
len(actual) >= min). max: Inclusive upper bound on length (len(actual) <= max).- Returns:
True if the length satisfies the bounds, False otherwise. When actual is None, returns True only when both bounds are effectively zero.
Example#
from punit import strings a = 'hello' assert strings.has_length(a, min=5) assert strings.has_length(a, max=5) assert strings.has_length(a, min=3, max=7) assert not strings.has_length(a, min=6)
- isNoneOrEmpty(string)#
Check if a string is None or empty.
- Return type:
bool
- Args:
string: The string to check
- Returns:
True if the string is None or empty, False otherwise
- isNoneOrWhitespace(string)#
Check if a string is None or whitespace.
- Return type:
bool
- Args:
string: The string to check.
- Returns:
True if the string is None or whitespace, False otherwise.
Example#
from punit import strings assert not strings.is_none_or_whitespace('hello') assert strings.is_none_or_whitespace(' ') assert strings.is_none_or_whitespace(None)