Current section
Files
Jump to
Current section
Files
src/dream_test@matchers@string.erl
-module(dream_test@matchers@string).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/dream_test/matchers/string.gleam").
-export([start_with/2, end_with/2, contain_string/2, match_regex/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" String matchers for dream_test.\n"
"\n"
" These matchers work with `String` values and are re-exported through\n"
" `dream_test/matchers`.\n"
"\n"
" Use them to assert string structure (prefix/suffix/substring) while\n"
" preserving the original string for further chaining.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" \"hello world\"\n"
" |> should\n"
" |> start_with(\"hello\")\n"
" |> or_fail_with(\"expected string to start with \\\"hello\\\"\")\n"
" ```\n"
).
-file("src/dream_test/matchers/string.gleam", 55).
-spec check_starts_with(binary(), binary()) -> dream_test@types:match_result(binary()).
check_starts_with(Actual, Prefix) ->
case gleam_stdlib:string_starts_with(Actual, Prefix) of
true ->
{match_ok, Actual};
false ->
Payload = {string_match_failure,
Actual,
Prefix,
<<"start_with"/utf8>>},
{match_failed,
{assertion_failure,
<<"start_with"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/string.gleam", 45).
?DOC(
" Assert that a string starts with a prefix.\n"
"\n"
" ## Parameters\n"
"\n"
" - `value_or_result`: the `MatchResult(String)` produced by `should` (or a previous matcher)\n"
" - `prefix`: required starting substring\n"
"\n"
" ## Returns\n"
"\n"
" A `MatchResult(String)` preserving the string for further chaining.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" \"hello world\"\n"
" |> should\n"
" |> start_with(\"hello\")\n"
" |> or_fail_with(\"expected string to start with \\\"hello\\\"\")\n"
" ```\n"
).
-spec start_with(dream_test@types:match_result(binary()), binary()) -> dream_test@types:match_result(binary()).
start_with(Value_or_result, Prefix) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_starts_with(Actual, Prefix)
end.
-file("src/dream_test/matchers/string.gleam", 105).
-spec check_ends_with(binary(), binary()) -> dream_test@types:match_result(binary()).
check_ends_with(Actual, Suffix) ->
case gleam_stdlib:string_ends_with(Actual, Suffix) of
true ->
{match_ok, Actual};
false ->
Payload = {string_match_failure,
Actual,
Suffix,
<<"end_with"/utf8>>},
{match_failed,
{assertion_failure,
<<"end_with"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/string.gleam", 95).
?DOC(
" Assert that a string ends with a suffix.\n"
"\n"
" ## Parameters\n"
"\n"
" - `value_or_result`: the `MatchResult(String)` produced by `should` (or a previous matcher)\n"
" - `suffix`: required ending substring\n"
"\n"
" ## Returns\n"
"\n"
" A `MatchResult(String)` preserving the string for further chaining.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" \"hello.gleam\"\n"
" |> should\n"
" |> end_with(\".gleam\")\n"
" |> or_fail_with(\"expected .gleam suffix\")\n"
" ```\n"
).
-spec end_with(dream_test@types:match_result(binary()), binary()) -> dream_test@types:match_result(binary()).
end_with(Value_or_result, Suffix) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_ends_with(Actual, Suffix)
end.
-file("src/dream_test/matchers/string.gleam", 155).
-spec check_contains_string(binary(), binary()) -> dream_test@types:match_result(binary()).
check_contains_string(Actual, Substring) ->
case gleam_stdlib:contains_string(Actual, Substring) of
true ->
{match_ok, Actual};
false ->
Payload = {string_match_failure,
Actual,
Substring,
<<"contain_string"/utf8>>},
{match_failed,
{assertion_failure,
<<"contain_string"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/string.gleam", 145).
?DOC(
" Assert that a string contains a substring.\n"
"\n"
" ## Parameters\n"
"\n"
" - `value_or_result`: the `MatchResult(String)` produced by `should` (or a previous matcher)\n"
" - `substring`: required substring that must be present\n"
"\n"
" ## Returns\n"
"\n"
" A `MatchResult(String)` preserving the string for further chaining.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" \"hello world\"\n"
" |> should\n"
" |> contain_string(\"world\")\n"
" |> or_fail_with(\"expected substring match\")\n"
" ```\n"
).
-spec contain_string(dream_test@types:match_result(binary()), binary()) -> dream_test@types:match_result(binary()).
contain_string(Value_or_result, Substring) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_contains_string(Actual, Substring)
end.
-file("src/dream_test/matchers/string.gleam", 238).
-spec regex_no_match_failure(binary(), binary()) -> dream_test@types:match_result(binary()).
regex_no_match_failure(Actual, Pattern) ->
Payload = {string_match_failure, Actual, Pattern, <<"match_regex"/utf8>>},
{match_failed,
{assertion_failure,
<<"match_regex"/utf8>>,
<<""/utf8>>,
{some, Payload}}}.
-file("src/dream_test/matchers/string.gleam", 227).
-spec check_matches_compiled_regex(binary(), binary(), gleam@regexp:regexp()) -> dream_test@types:match_result(binary()).
check_matches_compiled_regex(Actual, Pattern, Compiled_regexp) ->
case gleam@regexp:check(Compiled_regexp, Actual) of
true ->
{match_ok, Actual};
false ->
regex_no_match_failure(Actual, Pattern)
end.
-file("src/dream_test/matchers/string.gleam", 256).
-spec invalid_regex_pattern_failure(binary(), gleam@regexp:compile_error()) -> dream_test@types:match_result(binary()).
invalid_regex_pattern_failure(Pattern, Compile_error) ->
{match_failed,
{assertion_failure,
<<"match_regex"/utf8>>,
<<<<<<"invalid regex pattern: "/utf8,
(gleam@string:inspect(Pattern))/binary>>/binary,
" "/utf8>>/binary,
(gleam@string:inspect(Compile_error))/binary>>,
none}}.
-file("src/dream_test/matchers/string.gleam", 218).
-spec check_matches_regex(binary(), binary()) -> dream_test@types:match_result(binary()).
check_matches_regex(Actual, Pattern) ->
case gleam@regexp:from_string(Pattern) of
{ok, Compiled_regexp} ->
check_matches_compiled_regex(Actual, Pattern, Compiled_regexp);
{error, Compile_error} ->
invalid_regex_pattern_failure(Pattern, Compile_error)
end.
-file("src/dream_test/matchers/string.gleam", 208).
?DOC(
" Assert that a string matches a regular expression.\n"
"\n"
" The regex pattern is compiled using `gleam/regexp.from_string`.\n"
"\n"
" The assertion passes if the pattern matches **anywhere** within the string\n"
" (it is not implicitly anchored). Use `^...$` if you want to require a full\n"
" string match.\n"
"\n"
" If the pattern is invalid, the matcher fails (with an error message, and no\n"
" structured payload).\n"
"\n"
" ## Parameters\n"
"\n"
" - `value_or_result`: the `MatchResult(String)` produced by `should` (or a previous matcher)\n"
" - `pattern`: the regular expression pattern string\n"
"\n"
" ## Returns\n"
"\n"
" A `MatchResult(String)` preserving the string for further chaining.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import dream_test/matchers.{match_regex, or_fail_with, should}\n"
"\n"
" \"user-123\"\n"
" |> should\n"
" |> match_regex(\"^user-\\\\d+$\")\n"
" |> or_fail_with(\"expected an id like user-123\")\n"
" ```\n"
).
-spec match_regex(dream_test@types:match_result(binary()), binary()) -> dream_test@types:match_result(binary()).
match_regex(Value_or_result, Pattern) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_matches_regex(Actual, Pattern)
end.