Skip to content

Instantly share code, notes, and snippets.

@vanstee
Last active December 30, 2015 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanstee/7893019 to your computer and use it in GitHub Desktop.
Save vanstee/7893019 to your computer and use it in GitHub Desktop.
Ugh gross. Needs some refactoring love.
def evaluate_filters(filters, tags) do
results = Enum.flat_map tags, fn tag ->
Enum.map filters, fn filter ->
{ { elem(filter, 0), elem(tag, 0) }, evaluate_filter(filter, tag) }
end
end
results = Enum.reduce results, HashDict.new, fn { key, evaluation }, dict ->
Dict.update dict, key, evaluation, &(evaluation || &1)
end
mismatch = Enum.find Dict.to_list(results), &match?({ _, false }, &1)
case mismatch do
{ { _, tag }, _ } -> { :error, tag }
_ -> :ok
end
end
def evaluate_filter({ tag, value, :include }, { tag, value }), do: true
def evaluate_filter({ tag, value, :exclude }, { tag, value }), do: false
def evaluate_filter({ tag, _, :include }, { tag, _ }), do: false
def evaluate_filter({ tag, _, :exclude }, { tag, _ }), do: true
def evaluate_filter(_, _), do: tru
test "filters with matching tags are ored and non-matching tags are anded" do
filters = [ { :os, :unix, :include },
{ :os, :win32, :include },
{ :type, :unit, :include } ]
assert ExUnit.Runner.fancy_filter_match(filters, [])
assert ExUnit.Runner.fancy_filter_match(filters, [os: :unix])
assert ExUnit.Runner.fancy_filter_match(filters, [os: :unix, type: :unit])
refute ExUnit.Runner.fancy_filter_match(filters, [os: :unix, type: :integration])
assert ExUnit.Runner.fancy_filter_match(filters, [os: :win32])
assert ExUnit.Runner.fancy_filter_match(filters, [os: :win32, type: :unit])
refute ExUnit.Runner.fancy_filter_match(filters, [os: :win32, type: :integration])
assert ExUnit.Runner.fancy_filter_match(filters, [os: :unix, os: :win32])
assert ExUnit.Runner.fancy_filter_match(filters, [type: :unit, type: :integration])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment