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

Generate tests #9

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/code_generator.ex → lib/code_gen/code_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ defmodule ExOanda.CodeGenerator do
end
end

@doc false
def format_module_name(module_name) do
@doc false
def format_module_name(module_name) do
module_name
|> Atom.to_string()
|> String.replace("Elixir.", "")
|> String.to_atom()
end

defp generate_module_name(module_name), do: String.to_atom("Elixir.ExOanda.#{module_name}")
defp generate_module_name(module_name), do: Module.concat([ExOanda, module_name])

defp format_args(args) do
for a <- args, do: {String.to_atom(a), [], nil}
Expand Down
6 changes: 4 additions & 2 deletions lib/config.ex → lib/code_gen/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ExOanda.Config do
use TypedEctoSchema
import Ecto.Changeset

@yaml_file "./config.yml"
@config_file "./config.yml"

@primary_key false

Expand All @@ -29,15 +29,17 @@ defmodule ExOanda.Config do
end
end

@doc false
def changeset(config, params \\ %{}) do
config
|> cast(params, [:module_name, :description])
|> validate_required([:module_name, :description])
|> cast_embed(:functions, with: &functions_changeset/2)
end

@doc false
def load_config do
@yaml_file
@config_file
|> YamlElixir.read_all_from_file!()
|> List.first()
|> Map.fetch!("interfaces")
Expand Down
36 changes: 36 additions & 0 deletions lib/code_gen/test_generator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule ExOanda.TestGenerator do
@moduledoc false

use ExUnit.Case, async: true

def generate_tests(configs) do
Enum.each(configs, fn %{module_name: module_name, functions: functions} ->
module = Module.concat([ExOandaTest, module_name, "Generated"])

test_functions =
Enum.map(functions, fn function ->
function_name = function.function_name
target_module = Module.concat([ExOanda, module_name])
arity = length(function.arguments) + 1 # Connection struct + configured arguments (less optional params)

quote do
test "#{unquote(module_name)}.#{unquote(function_name)} is generated." do
assert function_exported?(unquote(target_module), unquote(String.to_atom(function_name)), unquote(arity))
end

test "#{unquote(module_name)}.#{unquote(function_name)}! is generated." do
assert function_exported?(unquote(target_module), unquote(String.to_atom("#{function_name}!")), unquote(arity))
end
end
end)

module_body =
quote do
use ExUnit.Case, async: true
unquote_splicing(test_functions)
end

Module.create(module, module_body, __ENV__)
end)
end
end
5 changes: 5 additions & 0 deletions test/generated_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule ExOandaTest.GeneratedFunctions do
use ExUnit.Case, async: true

ExOanda.TestGenerator.generate_tests(ExOanda.Config.load_config())
end
Loading