From 322c233e18b2868c1fdc4ac14c3ededdd5c458f0 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 19 Jul 2020 10:50:15 -0400 Subject: [PATCH] Add basic tests --- lib/gemini.ex | 5 +++- lib/gemini/response.ex | 9 +++++- test/gemini/response_test.exs | 14 +++++++++ test/gemini_test.exs | 53 +++++++++++++++++++++++++++++++++-- 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 test/gemini/response_test.exs diff --git a/lib/gemini.ex b/lib/gemini.ex index de09398..4960efa 100644 --- a/lib/gemini.ex +++ b/lib/gemini.ex @@ -60,6 +60,9 @@ defmodule Gemini do @spec parse(String.t()) :: [line()] + @doc """ + Parses a `text/gemini` document into its lines. + """ def parse(doc) do {lines, _} = doc @@ -73,7 +76,7 @@ defmodule Gemini do preformatting_toggle && !in_preformatting -> "```" <> alt = line - alt = if length(alt) == 0, do: nil, else: alt + alt = if alt == "", do: nil, else: alt {[{:preformatting_start, alt} | lines], true} in_preformatting -> diff --git a/lib/gemini/response.ex b/lib/gemini/response.ex index ac5a9a9..62067a5 100644 --- a/lib/gemini/response.ex +++ b/lib/gemini/response.ex @@ -43,12 +43,19 @@ defmodule Gemini.Response do defp parse_meta(data, acc \\ [], length \\ 0) defp parse_meta(<<"\r\n", rest::binary>>, acc, _length) do + body = + if rest == "" do + nil + else + rest + end + { :ok, acc |> Enum.reverse() |> :erlang.list_to_binary(), - rest + body } end diff --git a/test/gemini/response_test.exs b/test/gemini/response_test.exs new file mode 100644 index 0000000..e153813 --- /dev/null +++ b/test/gemini/response_test.exs @@ -0,0 +1,14 @@ +defmodule Gemini.ResponseTest do + use ExUnit.Case + alias Gemini.Response + doctest Response + + test "parses a response with a body" do + assert Response.parse("20 text/gemini\r\nsome body") == + {:ok, %Response{status: 20, meta: "text/gemini", body: "some body"}} + end + + test "parses a response without a body" do + assert Response.parse("31 /foo\r\n") == {:ok, %Response{status: 31, meta: "/foo", body: nil}} + end +end diff --git a/test/gemini_test.exs b/test/gemini_test.exs index 6cfe934..0053e7f 100644 --- a/test/gemini_test.exs +++ b/test/gemini_test.exs @@ -2,7 +2,56 @@ defmodule GeminiTest do use ExUnit.Case doctest Gemini - test "greets the world" do - assert Gemini.hello() == :world + test "parse text lines" do + assert Gemini.parse("test") == [{:text, "test"}] + end + + test "parse link lines" do + assert Gemini.parse("=>gemini://example.com") == [ + {:link, URI.parse("gemini://example.com"), nil} + ] + + assert Gemini.parse("=> \t gemini://example.com") == [ + {:link, URI.parse("gemini://example.com"), nil} + ] + + assert Gemini.parse("=>gemini://example.com some text") == [ + {:link, URI.parse("gemini://example.com"), "some text"} + ] + + assert Gemini.parse("=> \t gemini://example.com \t\t\tother text") == [ + {:link, URI.parse("gemini://example.com"), "other text"} + ] + end + + test "parse preformatted block" do + assert Gemini.parse("```foo\nbar\n```") == [ + {:preformatting_start, "foo"}, + {:preformatted, "bar"}, + :preformatting_end + ] + + assert Gemini.parse("```foo\nbar\n```baz") == [ + {:preformatting_start, "foo"}, + {:preformatted, "bar"}, + :preformatting_end + ] + end + + test "parse headings" do + assert Gemini.parse("#one") == [{:heading, "one", 1}] + assert Gemini.parse("# one") == [{:heading, "one", 1}] + assert Gemini.parse("## two") == [{:heading, "two", 2}] + assert Gemini.parse("### three") == [{:heading, "three", 3}] + end + + test "parse list items" do + assert Gemini.parse("* one") == [{:list_item, "one"}] + assert Gemini.parse("*one") == [{:text, "*one"}] + end + + test "parse block quotes" do + assert Gemini.parse(">quoted") == [{:quoted, "quoted"}] + assert Gemini.parse("> \t quoted") == [{:quoted, "quoted"}] end end