dream_test/matchers/string

String matchers for dream_test.

These matchers work with String values and are re-exported through dream_test/matchers.

Use them to assert string structure (prefix/suffix/substring) while preserving the original string for further chaining.

Example

"hello world"
|> should
|> start_with("hello")
|> or_fail_with("expected string to start with \"hello\"")

Values

pub fn contain_string(
  value_or_result value_or_result: types.MatchResult(String),
  substring substring: String,
) -> types.MatchResult(String)

Assert that a string contains a substring.

Parameters

  • value_or_result: the MatchResult(String) produced by should (or a previous matcher)
  • substring: required substring that must be present

Returns

A MatchResult(String) preserving the string for further chaining.

Example

"hello world"
|> should
|> contain_string("world")
|> or_fail_with("expected substring match")
pub fn end_with(
  value_or_result value_or_result: types.MatchResult(String),
  suffix suffix: String,
) -> types.MatchResult(String)

Assert that a string ends with a suffix.

Parameters

  • value_or_result: the MatchResult(String) produced by should (or a previous matcher)
  • suffix: required ending substring

Returns

A MatchResult(String) preserving the string for further chaining.

Example

"hello.gleam"
|> should
|> end_with(".gleam")
|> or_fail_with("expected .gleam suffix")
pub fn match_regex(
  value_or_result value_or_result: types.MatchResult(String),
  pattern pattern: String,
) -> types.MatchResult(String)

Assert that a string matches a regular expression.

The regex pattern is compiled using gleam/regexp.from_string.

The assertion passes if the pattern matches anywhere within the string (it is not implicitly anchored). Use ^...$ if you want to require a full string match.

If the pattern is invalid, the matcher fails (with an error message, and no structured payload).

Parameters

  • value_or_result: the MatchResult(String) produced by should (or a previous matcher)
  • pattern: the regular expression pattern string

Returns

A MatchResult(String) preserving the string for further chaining.

Example

import dream_test/matchers.{match_regex, or_fail_with, should}

"user-123"
|> should
|> match_regex("^user-\\d+$")
|> or_fail_with("expected an id like user-123")
pub fn start_with(
  value_or_result value_or_result: types.MatchResult(String),
  prefix prefix: String,
) -> types.MatchResult(String)

Assert that a string starts with a prefix.

Parameters

  • value_or_result: the MatchResult(String) produced by should (or a previous matcher)
  • prefix: required starting substring

Returns

A MatchResult(String) preserving the string for further chaining.

Example

"hello world"
|> should
|> start_with("hello")
|> or_fail_with("expected string to start with \"hello\"")
Search Document