-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule Rock.Struct.Cluster do | ||
defstruct points: [], size: 0 | ||
|
||
alias Rock.Struct.Cluster | ||
alias Rock.Struct.Point | ||
|
||
def new(points) when is_list(points) do | ||
size = points |> Enum.count | ||
|
||
%Cluster{points: points, size: size} | ||
end | ||
|
||
def add_point(%Cluster{points: points, size: size}, %Point{} = point) do | ||
new_points = points ++ [point] | ||
|
||
%Cluster{points: new_points, size: size + 1} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Rock.Struct.ClusterTest do | ||
use ExUnit.Case | ||
alias Rock.Struct.Cluster | ||
alias Rock.Test.TestFactory | ||
|
||
test "adds a point to a cluster" do | ||
point = TestFactory.from_string(:point, ["6"]) | ||
cluster = TestFactory.from_string(:cluster, | ||
[ | ||
["1", "2", "3"], | ||
["5"] | ||
]) | ||
|
||
%Cluster{points: points, size: size} = | ||
cluster | ||
|> Cluster.add_point(point) | ||
|
||
^size = 3 | ||
assert Enum.any?(points, fn(p) -> | ||
p == point | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule Rock.Test.TestFactory do | ||
alias Rock.Struct.Point | ||
alias Rock.Struct.Cluster | ||
|
||
def from_string(:cluster, string_points) do | ||
string_points | ||
|> Enum.map(fn(string_point) -> | ||
from_string(:point, string_point) | ||
end) | ||
|> Cluster.new | ||
end | ||
|
||
def from_string(:point, string_attributes) do | ||
string_attributes |> Point.new | ||
end | ||
end |