-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Text-based matching #61
Comments
The crux of this is somehow integrating with the text-match operator I think that One option would be to allow a Here's a quick worked example. The goal is to test that "some error message" occurs in the logs. # initial state
auto_assert capture_log(fn -> Logger.error("some error message") end)
# after first run
auto_assert "\e[31m\n12:50:27.941 [error] some error message\n\e[0m" <-
capture_log(fn -> Logger.error("some error message") end)
# manually edit to use <~ and match less of the message
auto_assert "[error] some error message" <~
capture_log(fn -> Logger.error("some error message") end) The biggest issue with this is that it's not obvious what to do if the value no longer matches. Possibly the best we can do is replace it with the entire captured log again and require you to rewrite it. # message changed
auto_assert "[error] some error message" <~
capture_log(fn -> Logger.error("some error MESSAGE") end)
# after running
auto_assert "\e[31m\n12:50:27.941 [error] some error MESSAGE\n\e[0m" <~
capture_log(fn -> Logger.error("some error MESSAGE") end)
# still have to manually edit to remove timestamp and only assert what you care about
auto_assert "[error] some error MESSAGE" <~
capture_log(fn -> Logger.error("some error MESSAGE") end) There are some heuristics that could be used to guess which part of the string you care about and suggest a more intelligent alternative. I might be able to leverage Given that, perhaps any string value that starts and ends with likely-ignorable formatting content could result in a pattern suggestion using defp example do
"\e[31m\n12:50:27.941 [error] some error MESSAGE\n\e[0m"
end
test "example/0" do
auto_assert example()
# running yields the following two suggestions
auto_assert "[error] some error MESSAGE" <~ example()
auto_assert "\e[31m\n12:50:27.941 [error] some error MESSAGE\n\e[0m" <- example()
end |
@tcoopman I have a somewhat minimal version of this implemented in the # dep
{:mneme, github: "zachallaun/mneme", ref: "text-match-operator"} |
I'll try to look at it tomorrow, I'll keep you posted |
Some feedback:
So that was weird / unexpected for me. For the rest it feels nice, maybe adding regexes could be useful, but on the other hand I'm not sure it's worth the extra value. |
I agree that the difference between the two operators is subtle. An alternative that, after some reflection, I think I like more is to introduce "matchers" that can go on the left-hand side of auto_assert text("bar") <- "foo bar baz" This is also consistent with how I'm planning to handle file snapshots (#72).
I think the issue here is that the auto_assert """
multiple workshops found for\
"""
<~ """
[error] multiple workshops found for .....
"""
Regexes do currently work, but you have to add them yourself and Mneme doesn't generate them. I don't plan to add regex generation -- that seems like a can of worms that I don't want to open. |
is it intentional that the |
Yes, that's intentional (for now). The idea is that if you're doing an exact match, you want to know if anything in value changes. If Mneme generated The current "rules" for when
At least that last one is likely to change because there's nothing that fundamentally prevents multi-line In these cases, what you'd likely want to do instead is split the captured log on newlines and then do regular assertions, like: logged = capture_log(...) |> String.split("\n")
assert Enum.any?(logged, &(&1 =~ "this is a warning")) |
Though, if you really want to stay in Mneme-land, we could theoretically introduce a new auto_assert contains(text("this is a warning")) <- capture_log(...) |> String.split("\n") |
What about: to be clear, removing the |
I could get behind I don't know about auto_assert exact_match("foo") <- "foo"
auto_assert "foo" <- "foo" Agreed that it should be documented! Right now there are some docs about generated patterns, but they're not comprehensive. I should write up a guide about how and why patterns are generated/changed that I can link to from various places. |
Wrote a new guide on pattern generation and selection to replace the small section it had in the overview. This will be a good place to add documentation for this feature. |
basically this with auto_assert:
assert capture_log(fn -> Logger.error(msg) end) =~ msg
The text was updated successfully, but these errors were encountered: