Skip to content

Commit

Permalink
extracted common methods to macro
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkaspa committed Mar 13, 2016
1 parent 8a09c30 commit e4bdd1e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 70 deletions.
4 changes: 4 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"}
Expand Down
12 changes: 1 addition & 11 deletions test/exstreme/graph_creator_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Exstreme.GraphCreatorTest do
use ExUnit.Case
use Exstreme.Common
alias Exstreme.GraphCreator
alias Exstreme.Graph
doctest Exstreme.GraphCreator
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions test/exstreme/graph_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Exstreme.GraphTest do
use ExUnit.Case
use Exstreme.Common
alias Exstreme.Graph
doctest Exstreme.Graph

end
61 changes: 2 additions & 59 deletions test/exstreme/graph_validator_test.exs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
69 changes: 69 additions & 0 deletions test/support/common.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e4bdd1e

Please sign in to comment.