Add basic tests

This commit is contained in:
Shadowfacts 2020-07-19 10:50:15 -04:00
parent b97479c58d
commit 322c233e18
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 77 additions and 4 deletions

View File

@ -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 ->

View File

@ -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

View File

@ -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

View File

@ -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