Created
January 15, 2015 18:28
-
-
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)
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
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