Skip to content

Commit

Permalink
Can format MapSets
Browse files Browse the repository at this point in the history
  • Loading branch information
BjRo committed Feb 27, 2016
1 parent 37289ff commit 97a4e4e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/apex/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,14 @@ defimpl Apex.Format, for: HashDict do
end

defimpl Apex.Format, for: HashSet do
import Apex.Format.Utils
def format(data, options \\ []) do
Apex.Format.Seq.format_set("HashSet", data, options)
end
end

defimpl Apex.Format, for: MapSet do
def format(data, options \\ []) do
Apex.Format.Seq.format(
Set.to_list(data),
options,
start_token: "HashSet<[",
end_token: "]>",
numbers: false) |> colorize(data, options)
Apex.Format.Seq.format_set("MapSet", data, options)
end
end

Expand Down
9 changes: 9 additions & 0 deletions lib/apex/format/seq.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ defmodule Apex.Format.Seq do
import Apex.Format.Utils
@list []

def format_set(name, data, options \\ []) do
format(
Set.to_list(data) |> Enum.sort,
options,
start_token: "#{name}<[",
end_token: "]>",
numbers: false) |> colorize(data, options)
end

def format(data, options, config \\ []) do
pre = start_token(config) <> new_line
post = indent(options) <> end_token(config) <> new_line
Expand Down
10 changes: 10 additions & 0 deletions test/format_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,18 @@ defmodule Apex.Format.Test do
set = HashSet.new |> HashSet.put(:foo) |> HashSet.put(:baz)
assert format(set, color: false) == """
HashSet<[
baz
foo
]>
"""
end

test "Can format MapSets" do
set = MapSet.new |> MapSet.put(:foo) |> MapSet.put(:baz)
assert format(set, color: false) == """
MapSet<[
baz
foo
]>
"""
end
Expand Down

0 comments on commit 97a4e4e

Please sign in to comment.