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
Introduce a dynamic test mechanism #16
Conversation
Hi @binaryseed! Thanks a lot for your contribution. Your idea sounds really neat, since having a file per test case could be cumbersome. Just wondering, does it work with comments as well? Some test cases have comments so it'd be great if you can do a quick sanity test for that as well. :) @whatyouhide what do you think of this idea? |
I added an example that shows that comments are preserved. Also, I tweaked the formatting so the delimiters are more obvious, there's an explicit begin & end, and you can add optional "spacers" that will get ignored |
I am not sure about this as it, in my mind, complicates things a bit. Can I ask why we don't test this as: before = """
def ... do :ok end
"""
after = """
def ... do
:ok
end
"""
assert format(before) == after ? |
The method that whatyouhide is what I'm doing and I find it to be very easy to work with. |
|> Enum.map(fn test_case -> | ||
%{"name" => name, "before_string" => before_string, "after_string" => after_string} = | ||
Regex.named_captures(@case_extractor, test_case) | ||
function_name = ExUnit.Case.register_test(__ENV__, :test, "#{String.trim(name)} (#{file})", []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, I didn't know you could do this. Pretty cool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is simply what the test
macro boils down to: https://github.com/elixir-lang/elixir/blob/master/lib/ex_unit/lib/ex_unit/case.ex#L277
You could certainly do that, but I think there are a few benefits to defining the cases outside of the tests:
|
https://github.com/lpil/exfmt/blob/master/test/exfmt/integration/def_test.exs Seems about the same amount of boilerplate to me. :) |
Cool, didn't know about your project @lpil ... I'm not seeing a 'before' / 'after' case in those tests, is it just asserting that the input equals the output when run through the formatter? Do you have test cases that run "bad" input through and assert that it comes out correct? I don't know about you but I really rely on syntax highlighting to quickly read code, and I have a hard time reading code inside raw strings... |
Ahh, this is interesting: https://github.com/lpil/exfmt/blob/master/test/exfmt/integration/comments_test.exs#L84-L98 Looks like you did the That's quite nice and simple, but the highlighting issue still remains test "@spec" do
"""
@spec run(String.t) :: atom | String.t | :hello
""" ~> """
@spec run(String.t)
:: atom | String.t | :hello
"""
end vs: #=- CASE: @spec
#=- BEFORE:
#=-
@spec run(String.t) :: atom | String.t | :hello
#=-
#=- AFTER:
#=-
@spec run(String.t)
:: atom | String.t | :hello |
After writing a few tests I decided that the before/after comparison is rarely useful as almost all formatting information is discarded by the parser. |
Oh interesting! If that's the case, it seems like you could literally just have a series of files that have properly formatted code, run them through the formatter and expect no changes. |
That would work, there's a few trade offs though:
On the flip side, less boilerplate and you get syntax highlighting. :) |
Consider that the string comparison approach is simpler to implement. I would say that that's the most important feature at this stage of the project, so personally I would start with that. |
Cool, well I'll go ahead and close this for now then.. |
Thanks for the fruitful discussion, everyone. I think both approaches have their pros and cons though. Dynamic testing (test case as string) is useful for small examples but lacks syntax highlighting. Static testing (test case as file) is useful for integration tests but you have to tag the test cases from another place. To be fair, @binaryseed's version of dynamic testing takes the best of two worlds without the disadvantages. The only downside is the need for careful delimiter handling inside the test file and a more complex implementation. But there are always tradeoffs. @binaryseed, if the code is still present on your machine, could you please push your |
Yeah, I put the branch back up for the future! |
Awesome @binaryseed, thanks a lot! ❤️ |
Hi! This is a sweet project!
Here's an idea for a simple way of defining test cases as mentioned in #12
With this PR, you just add more "Cases" to any file in
test/cases
and they will be compared in their owntest
.The test will be named like
"test Preserve keyword list syntax (kw_list.exs)"
, so it would be easy to tell where the failure comes from...Right now the "format" looks like this:
Open to suggestions!