Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Created January 15, 2015 18:28
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 pmarreck/a5eba67b30d07910c113 to your computer and use it in GitHub Desktop.
Save pmarreck/a5eba67b30d07910c113 to your computer and use it in GitHub Desktop.
How to unit-test a small Elixir library inline, without any project management cruft (at least in the early stages)
defmodule Sparkline do
def draw(str) do
values = str |> String.split(~r/[, ]+/)
|> Enum.map(&(elem(Float.parse(&1), 0)))
{min, max} = {Enum.min(values), Enum.max(values)}
Enum.map(values, &(round((&1 - min) / (max - min) * 7 + 0x2581)))
end
end
# run this inline suite with "elixir #{__ENV__.file} test"
if System.argv |> List.first == "test" do
ExUnit.start
defmodule SparklineTest do
use ExUnit.Case, async: true
test "sparkline" do
assert Sparkline.draw("1 2.5 3 5, 3 1") == '▁▄▅█▅▁'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment