Skip to content

Commit

Permalink
Implement assert_matches with expanded expressions support in match…
Browse files Browse the repository at this point in the history
… assertions (#5024)

* An experiment in more advanced predicates in pattern matches

* More friendly pin expressions in comparison report

* Use the new assert in a real test

* Remove synthetic failing test

* Add moduledoc

* Rename `exact` to `exactly`

* Make standard match bindings not throw unused warnings

* Add basic support for ^strict_map() operator

* Improve strict map checking to support nesting and better error report

* Add checks for more common data types, including serialized time formats

* Refactor the macro for a bit better legibility

* Reduce code nesting further

* Fix documentation definition
  • Loading branch information
zoldar authored Jan 31, 2025
1 parent 1ae350a commit 62c6cb5
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"{config,lib,test,extra}/**/*.{heex,ex,exs}",
"priv/*/seeds.exs",
"storybook/**/*.exs"
]
],
locals_without_parens: [assert_matches: 1]
]
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,17 @@ defmodule PlausibleWeb.Api.Internal.SegmentsControllerTest do
})
|> json_response(200)

assert %{
"name" => "Some segment",
"type" => "#{unquote(type)}",
"segment_data" => %{"filters" => [["is", "visit:entry_page", ["/blog"]]]},
"owner_id" => user.id
} == Map.drop(response, ["id", "inserted_at", "updated_at"])
assert_matches ^strict_map(%{
"id" => ^any(:pos_integer),
"name" => "Some segment",
"type" => ^"#{unquote(type)}",
"segment_data" =>
^strict_map(%{"filters" => [["is", "visit:entry_page", ["/blog"]]]}),
"owner_id" => ^user.id,
"inserted_at" => ^any(:iso8601_naive_datetime),
"updated_at" => ^any(:iso8601_naive_datetime)
}) = response

assert is_integer(response["id"])
assert is_binary(response["inserted_at"])
assert is_binary(response["updated_at"])
assert response["inserted_at"] == response["updated_at"]

verify_segment_in_db(%Plausible.Segments.Segment{
Expand Down
Loading

0 comments on commit 62c6cb5

Please sign in to comment.