From e4bdd1e3ec367cec7f74b0238a81da490d9893fd Mon Sep 17 00:00:00 2001 From: mrkaspa Date: Sun, 13 Mar 2016 13:44:26 -0500 Subject: [PATCH] extracted common methods to macro --- mix.exs | 4 ++ test/exstreme/graph_creator_test.exs | 12 +---- test/exstreme/graph_test.exs | 7 +++ test/exstreme/graph_validator_test.exs | 61 +---------------------- test/support/common.ex | 69 ++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 70 deletions(-) create mode 100644 test/exstreme/graph_test.exs create mode 100644 test/support/common.ex diff --git a/mix.exs b/mix.exs index 0d8adf8..9ad9637 100644 --- a/mix.exs +++ b/mix.exs @@ -5,6 +5,7 @@ defmodule Exstreme.Mixfile do [app: :exstreme, version: "0.0.1", elixir: "~> 1.2", + elixirc_paths: elixirc_paths(Mix.env), build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps] @@ -17,6 +18,9 @@ defmodule Exstreme.Mixfile do [applications: [:logger]] end + defp elixirc_paths(:test), do: ["lib", "test/support"] + defp elixirc_paths(_), do: ["lib"] + # Dependencies can be Hex packages: # # {:mydep, "~> 0.3.0"} diff --git a/test/exstreme/graph_creator_test.exs b/test/exstreme/graph_creator_test.exs index 44e80bb..c126957 100644 --- a/test/exstreme/graph_creator_test.exs +++ b/test/exstreme/graph_creator_test.exs @@ -1,5 +1,6 @@ defmodule Exstreme.GraphCreatorTest do use ExUnit.Case + use Exstreme.Common alias Exstreme.GraphCreator alias Exstreme.Graph doctest Exstreme.GraphCreator @@ -87,15 +88,4 @@ defmodule Exstreme.GraphCreatorTest do assert new_graph == compare_graph end - - # private - - defp params, do: [] - - defp create_graph do - graph = GraphCreator.create_graph(params) - {graph, n1} = GraphCreator.create_node(graph, params) - {graph, n2} = GraphCreator.create_node(graph, params) - GraphCreator.add_connection(graph, n1, n2) - end end diff --git a/test/exstreme/graph_test.exs b/test/exstreme/graph_test.exs new file mode 100644 index 0000000..7b47a5d --- /dev/null +++ b/test/exstreme/graph_test.exs @@ -0,0 +1,7 @@ +defmodule Exstreme.GraphTest do + use ExUnit.Case + use Exstreme.Common + alias Exstreme.Graph + doctest Exstreme.Graph + +end diff --git a/test/exstreme/graph_validator_test.exs b/test/exstreme/graph_validator_test.exs index e46be28..cc9a3f1 100644 --- a/test/exstreme/graph_validator_test.exs +++ b/test/exstreme/graph_validator_test.exs @@ -1,6 +1,7 @@ defmodule Exstreme.GraphValidatorTest do use ExUnit.Case - alias Exstreme.{GraphCreator, GraphValidator} + use Exstreme.Common + alias Exstreme.GraphValidator doctest Exstreme.GraphValidator test "the graph with many nodes must be valid" do @@ -23,62 +24,4 @@ defmodule Exstreme.GraphValidatorTest do test "all the nodes in the graph has to be connected" do assert {:error, _} = GraphValidator.validate(graph_unconnected_nodes) end - - # private - - # valid graphs - - defp graph_many_nodes do - graph = GraphCreator.create_graph(params) - {graph, n1} = GraphCreator.create_node(graph, params) - {graph, n2} = GraphCreator.create_node(graph, params) - {graph, b1} = GraphCreator.create_broadcast(graph, params) - {graph, n3} = GraphCreator.create_node(graph, params) - {graph, n4} = GraphCreator.create_node(graph, params) - {graph, f1} = GraphCreator.create_funnel(graph, params) - {graph, n5} = GraphCreator.create_node(graph, params) - - graph - |> GraphCreator.add_connection(n1, n2) - |> GraphCreator.add_connection(n2, b1) - |> GraphCreator.add_connection(b1, n3) - |> GraphCreator.add_connection(b1, n4) - |> GraphCreator.add_connection(n3, f1) - |> GraphCreator.add_connection(n4, f1) - |> GraphCreator.add_connection(f1, n5) - end - - # invalid graphs - - defp graph_one_node_no_connections do - graph = GraphCreator.create_graph(params) - {graph, _n1} = GraphCreator.create_node(graph, params) - graph - end - - defp graph_no_connections, do: GraphCreator.create_graph(params) - - defp graph_start_with_broadcast do - graph = GraphCreator.create_graph(params) - {graph, b1} = GraphCreator.create_broadcast(graph, params) - {graph, n1} = GraphCreator.create_node(graph, params) - GraphCreator.add_connection(graph, b1, n1) - end - - defp graph_start_with_funnnel do - graph = GraphCreator.create_graph(params) - {graph, f1} = GraphCreator.create_funnel(graph, params) - {graph, n1} = GraphCreator.create_node(graph, params) - GraphCreator.add_connection(graph, f1, n1) - end - - defp graph_unconnected_nodes do - graph = GraphCreator.create_graph(params) - {graph, n1} = GraphCreator.create_node(graph, params) - {graph, n2} = GraphCreator.create_node(graph, params) - {graph, _n3} = GraphCreator.create_node(graph, params) - GraphCreator.add_connection(graph, n1, n2) - end - - defp params, do: [] end diff --git a/test/support/common.ex b/test/support/common.ex new file mode 100644 index 0000000..49d6488 --- /dev/null +++ b/test/support/common.ex @@ -0,0 +1,69 @@ +defmodule Exstreme.Common do + alias Exstreme.GraphCreator + use ExUnit.CaseTemplate + + using do + quote do + def graph_many_nodes do + graph = GraphCreator.create_graph(params) + {graph, n1} = GraphCreator.create_node(graph, params) + {graph, n2} = GraphCreator.create_node(graph, params) + {graph, b1} = GraphCreator.create_broadcast(graph, params) + {graph, n3} = GraphCreator.create_node(graph, params) + {graph, n4} = GraphCreator.create_node(graph, params) + {graph, f1} = GraphCreator.create_funnel(graph, params) + {graph, n5} = GraphCreator.create_node(graph, params) + + graph + |> GraphCreator.add_connection(n1, n2) + |> GraphCreator.add_connection(n2, b1) + |> GraphCreator.add_connection(b1, n3) + |> GraphCreator.add_connection(b1, n4) + |> GraphCreator.add_connection(n3, f1) + |> GraphCreator.add_connection(n4, f1) + |> GraphCreator.add_connection(f1, n5) + end + + # invalid graphs + + def graph_one_node_no_connections do + graph = GraphCreator.create_graph(params) + {graph, _n1} = GraphCreator.create_node(graph, params) + graph + end + + def graph_no_connections, do: GraphCreator.create_graph(params) + + def graph_start_with_broadcast do + graph = GraphCreator.create_graph(params) + {graph, b1} = GraphCreator.create_broadcast(graph, params) + {graph, n1} = GraphCreator.create_node(graph, params) + GraphCreator.add_connection(graph, b1, n1) + end + + def graph_start_with_funnnel do + graph = GraphCreator.create_graph(params) + {graph, f1} = GraphCreator.create_funnel(graph, params) + {graph, n1} = GraphCreator.create_node(graph, params) + GraphCreator.add_connection(graph, f1, n1) + end + + def graph_unconnected_nodes do + graph = GraphCreator.create_graph(params) + {graph, n1} = GraphCreator.create_node(graph, params) + {graph, n2} = GraphCreator.create_node(graph, params) + {graph, _n3} = GraphCreator.create_node(graph, params) + GraphCreator.add_connection(graph, n1, n2) + end + + defp create_graph do + graph = GraphCreator.create_graph(params) + {graph, n1} = GraphCreator.create_node(graph, params) + {graph, n2} = GraphCreator.create_node(graph, params) + GraphCreator.add_connection(graph, n1, n2) + end + + def params, do: [] + end + end +end