Current section
Files
Jump to
Current section
Files
src/dream_test@context.erl
-module(dream_test@context).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/dream_test/context.gleam").
-export([new/0, failures/1, add_failure/2]).
-export_type([test_context/0]).
-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(
" Per-test context.\n"
"\n"
" This module provides a small record (`TestContext`) for storing\n"
" `types.AssertionFailure` values. Most test code won’t interact with it\n"
" directly—matchers already turn failures into `types.AssertionResult`.\n"
"\n"
" You may find it useful if you’re building custom integrations where you want\n"
" to accumulate multiple failures during a single test run.\n"
).
-type test_context() :: {test_context,
list(dream_test@types:assertion_failure())}.
-file("src/dream_test/context.gleam", 113).
?DOC(
" Create a new, empty `TestContext`.\n"
"\n"
" ## Returns\n"
"\n"
" A `TestContext` with no recorded failures.\n"
"\n"
" ## Parameters\n"
"\n"
" None.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import dream_test/context\n"
" import dream_test/matchers.{be_equal, or_fail_with, should}\n"
" import dream_test/types.{AssertionFailure}\n"
" import dream_test/unit.{describe, it}\n"
" import gleam/option.{None}\n"
"\n"
" pub fn tests() {\n"
" describe(\"dream_test/context\", [\n"
" it(\"new has no failures\", fn() {\n"
" context.new()\n"
" |> context.failures()\n"
" |> should\n"
" |> be_equal([])\n"
" |> or_fail_with(\"expected new context to have no failures\")\n"
" }),\n"
"\n"
" it(\"add_failure stores failures newest-first\", fn() {\n"
" let first_failure =\n"
" AssertionFailure(operator: \"op1\", message: \"m1\", payload: None)\n"
" let second_failure =\n"
" AssertionFailure(operator: \"op2\", message: \"m2\", payload: None)\n"
"\n"
" context.new()\n"
" |> context.add_failure(first_failure)\n"
" |> context.add_failure(second_failure)\n"
" |> context.failures()\n"
" |> should\n"
" |> be_equal([second_failure, first_failure])\n"
" |> or_fail_with(\"expected newest-first failure ordering\")\n"
" }),\n"
" ])\n"
" }\n"
" ```\n"
).
-spec new() -> test_context().
new() ->
{test_context, []}.
-file("src/dream_test/context.gleam", 165).
?DOC(
" Get all failures recorded in a `TestContext`.\n"
"\n"
" Failures are stored newest-first.\n"
"\n"
" ## Parameters\n"
"\n"
" - `context`: the `TestContext` to inspect\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import dream_test/context\n"
" import dream_test/matchers.{be_equal, or_fail_with, should}\n"
" import dream_test/types.{AssertionFailure}\n"
" import dream_test/unit.{describe, it}\n"
" import gleam/option.{None}\n"
"\n"
" pub fn tests() {\n"
" describe(\"dream_test/context\", [\n"
" it(\"new has no failures\", fn() {\n"
" context.new()\n"
" |> context.failures()\n"
" |> should\n"
" |> be_equal([])\n"
" |> or_fail_with(\"expected new context to have no failures\")\n"
" }),\n"
"\n"
" it(\"add_failure stores failures newest-first\", fn() {\n"
" let first_failure =\n"
" AssertionFailure(operator: \"op1\", message: \"m1\", payload: None)\n"
" let second_failure =\n"
" AssertionFailure(operator: \"op2\", message: \"m2\", payload: None)\n"
"\n"
" context.new()\n"
" |> context.add_failure(first_failure)\n"
" |> context.add_failure(second_failure)\n"
" |> context.failures()\n"
" |> should\n"
" |> be_equal([second_failure, first_failure])\n"
" |> or_fail_with(\"expected newest-first failure ordering\")\n"
" }),\n"
" ])\n"
" }\n"
" ```\n"
"\n"
" ## Returns\n"
"\n"
" A list of `AssertionFailure` values (newest-first).\n"
).
-spec failures(test_context()) -> list(dream_test@types:assertion_failure()).
failures(Context) ->
erlang:element(2, Context).
-file("src/dream_test/context.gleam", 222).
?DOC(
" Record an `AssertionFailure` in a `TestContext`.\n"
"\n"
" Dream Test represents assertion failures as structured values\n"
" (`types.AssertionFailure`). This helper lets internal code accumulate those\n"
" failures while a test runs.\n"
"\n"
" Failures are stored **newest-first**, so adding a failure is \\(O(1)\\).\n"
"\n"
" ## Parameters\n"
"\n"
" - `context`: the current `TestContext`\n"
" - `failure`: the failure to record\n"
"\n"
" ## Returns\n"
"\n"
" A new `TestContext` containing the added failure.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import dream_test/context\n"
" import dream_test/matchers.{be_equal, or_fail_with, should}\n"
" import dream_test/types.{AssertionFailure}\n"
" import dream_test/unit.{describe, it}\n"
" import gleam/option.{None}\n"
"\n"
" pub fn tests() {\n"
" describe(\"dream_test/context\", [\n"
" it(\"new has no failures\", fn() {\n"
" context.new()\n"
" |> context.failures()\n"
" |> should\n"
" |> be_equal([])\n"
" |> or_fail_with(\"expected new context to have no failures\")\n"
" }),\n"
"\n"
" it(\"add_failure stores failures newest-first\", fn() {\n"
" let first_failure =\n"
" AssertionFailure(operator: \"op1\", message: \"m1\", payload: None)\n"
" let second_failure =\n"
" AssertionFailure(operator: \"op2\", message: \"m2\", payload: None)\n"
"\n"
" context.new()\n"
" |> context.add_failure(first_failure)\n"
" |> context.add_failure(second_failure)\n"
" |> context.failures()\n"
" |> should\n"
" |> be_equal([second_failure, first_failure])\n"
" |> or_fail_with(\"expected newest-first failure ordering\")\n"
" }),\n"
" ])\n"
" }\n"
" ```\n"
).
-spec add_failure(test_context(), dream_test@types:assertion_failure()) -> test_context().
add_failure(Context, Failure) ->
{test_context, [Failure | erlang:element(2, Context)]}.