Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/ex_format.ex
Expand Up @@ -47,6 +47,11 @@ defmodule ExFormat do
def process(file_name) do
file_name
|> File.read!
|> process_string
end

def process_string(string) do
string
|> prepare_data
|> preprocess
|> format
Expand Down
38 changes: 38 additions & 0 deletions test/cases/kw_list.exs
@@ -0,0 +1,38 @@
#=- CASE: Multiple function heads
#=- BEFORE:
def func1(a1,b1) do
func3(a,b)
1+2
end
def func1(a1,b1) do
func3(a,b)
end
def func1(a1,b1) do
func3(a,b)
end


#=- AFTER:
def func1(a1, b1) do
func3(a, b)
1 + 2
end
def func1(a1, b1) do
func3(a, b)
end
def func1(a1, b1) do
func3(a, b)
end
#=-
#=- CASE: Preserve keyword list syntax
#=- BEFORE:
#=-
def func2(a2,b2), do: func3(a,b)
def func2(a2,b2), do: func3(a,b)
def func2(a2, b2), do: func3(a,b)


#=- AFTER:
def func2(a2, b2), do: func3(a, b)
def func2(a2, b2), do: func3(a, b)
def func2(a2, b2), do: func3(a, b)
18 changes: 18 additions & 0 deletions test/cases/prefix_comments.exs
@@ -0,0 +1,18 @@
#=-
#=- CASE: Preserve comments
#=- BEFORE:
#=-
# prefix comment preserved
def hello(name) do
# prefix comment preserved
"hello " <> name
end
#=-
#=- AFTER:
#=-
# prefix comment preserved
def hello(name) do
# prefix comment preserved
"hello " <> name
end
#=-
24 changes: 24 additions & 0 deletions test/comparison_test.exs
@@ -0,0 +1,24 @@
defmodule ComparisonTest do
use ExUnit.Case, async: true

@case_dir "test/cases"
@case_split "#=- CASE:"
@case_spacer "#=-\n"
@case_extractor ~r/(?<name>.*)#=- BEFORE:\n(?<before_string>.*)\n#=- AFTER:\n(?<after_string>.*)/s

Enum.map(File.ls!(@case_dir), fn file ->
File.read!("#{@case_dir}/#{file}")
|> String.replace(@case_spacer, "")
|> String.split(@case_split, trim: true)
|> 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})", [])
Copy link

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.

Copy link
Author

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

def unquote(function_name)(_) do
formatted = ExFormat.process_string(unquote(before_string))
expected = unquote(after_string)
assert formatted == expected
end
end)
end)
end