From 13cac4e8d5890e11a3b14d9e898af7811a5e5ccd Mon Sep 17 00:00:00 2001 From: nickbair Date: Mon, 2 Sep 2024 12:50:31 -0600 Subject: [PATCH 1/3] WIP generate tests --- lib/{ => code_gen}/code_generator.ex | 6 ++--- lib/{ => code_gen}/config.ex | 0 lib/code_gen/test_generator.ex | 34 ++++++++++++++++++++++++++++ test/generated_test.exs | 5 ++++ 4 files changed, 42 insertions(+), 3 deletions(-) rename lib/{ => code_gen}/code_generator.ex (97%) rename lib/{ => code_gen}/config.ex (100%) create mode 100644 lib/code_gen/test_generator.ex create mode 100644 test/generated_test.exs diff --git a/lib/code_generator.ex b/lib/code_gen/code_generator.ex similarity index 97% rename from lib/code_generator.ex rename to lib/code_gen/code_generator.ex index 91a0370..6ec26fa 100644 --- a/lib/code_generator.ex +++ b/lib/code_gen/code_generator.ex @@ -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} diff --git a/lib/config.ex b/lib/code_gen/config.ex similarity index 100% rename from lib/config.ex rename to lib/code_gen/config.ex diff --git a/lib/code_gen/test_generator.ex b/lib/code_gen/test_generator.ex new file mode 100644 index 0000000..30fdfe1 --- /dev/null +++ b/lib/code_gen/test_generator.ex @@ -0,0 +1,34 @@ +defmodule ExOanda.TestGenerator do + 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 diff --git a/test/generated_test.exs b/test/generated_test.exs new file mode 100644 index 0000000..400c5fa --- /dev/null +++ b/test/generated_test.exs @@ -0,0 +1,5 @@ +defmodule ExOandaTest.GeneratedFunctions do + use ExUnit.Case, async: true + + ExOanda.TestGenerator.generate_tests(ExOanda.Config.load_config()) +end From 90d993ed85c426c01f81b1637a57702614c5e41e Mon Sep 17 00:00:00 2001 From: nickbair Date: Mon, 2 Sep 2024 19:30:22 -0600 Subject: [PATCH 2/3] Add moduledoc tag --- lib/code_gen/test_generator.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/code_gen/test_generator.ex b/lib/code_gen/test_generator.ex index 30fdfe1..2d9460a 100644 --- a/lib/code_gen/test_generator.ex +++ b/lib/code_gen/test_generator.ex @@ -1,4 +1,6 @@ defmodule ExOanda.TestGenerator do + @moduledoc false + use ExUnit.Case, async: true def generate_tests(configs) do From 28667abf3b70c55da7a5d3eb663733025bc2e56a Mon Sep 17 00:00:00 2001 From: nickbair Date: Wed, 4 Sep 2024 16:45:08 -0600 Subject: [PATCH 3/3] Clean up --- lib/code_gen/config.ex | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/code_gen/config.ex b/lib/code_gen/config.ex index dceebea..205366b 100644 --- a/lib/code_gen/config.ex +++ b/lib/code_gen/config.ex @@ -4,7 +4,7 @@ defmodule ExOanda.Config do use TypedEctoSchema import Ecto.Changeset - @yaml_file "./config.yml" + @config_file "./config.yml" @primary_key false @@ -29,6 +29,7 @@ defmodule ExOanda.Config do end end + @doc false def changeset(config, params \\ %{}) do config |> cast(params, [:module_name, :description]) @@ -36,8 +37,9 @@ defmodule ExOanda.Config do |> 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")