Skip to content

Commit

Permalink
Merge pull request #9 from nicholasbair/generate-tests
Browse files Browse the repository at this point in the history
WIP generate tests
  • Loading branch information
nicholasbair authored Sep 4, 2024
2 parents ad8e7b0 + 28667ab commit 04625c4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
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

0 comments on commit 04625c4

Please sign in to comment.