Files
pinchflat/test/pinchflat/downloading/output_path/parser_test.exs
T
Kieran 933daf8c78 [Housekeeping] Improve test stability (#232)
* Ensured all tests use at least the DataCase module

* addressed one flakey test

* More flakey tests

* Consolidated Mox usage
2024-05-08 09:57:36 -07:00

49 lines
1.7 KiB
Elixir

defmodule Pinchflat.Downloading.OutputPath.ParserTest do
use Pinchflat.DataCase
alias Pinchflat.Downloading.OutputPath.Parser
describe "parse/3" do
test "it returns the rendered string when the string is valid" do
assert {:ok, "bar"} = Parser.parse("{{ foo }}", %{"foo" => "bar"})
end
test "it works with filepath-like strings" do
assert {:ok, "bar/baz"} =
Parser.parse("{{ foo }}/{{ bar }}", %{"foo" => "bar", "bar" => "baz"})
end
test "it works when mixing text and variables" do
assert {:ok, "bar text baz"} =
Parser.parse("{{ foo }} text {{ bar }}", %{"foo" => "bar", "bar" => "baz"})
end
test "it removes the placeholder but doesn't blow up when the variable isn't provided" do
assert {:ok, ""} = Parser.parse("{{ foo }}", %{})
end
test "it accepts any number of spaces between open and closing tags" do
assert {:ok, "bar"} = Parser.parse("{{foo}}", %{"foo" => "bar"})
assert {:ok, "bar"} = Parser.parse("{{ foo}}", %{"foo" => "bar"})
assert {:ok, "bar"} = Parser.parse("{{foo }}", %{"foo" => "bar"})
assert {:ok, "bar"} = Parser.parse("{{ foo }}", %{"foo" => "bar"})
end
test "it doesn't interpret single braces as variables" do
assert {:ok, "{foo}"} = Parser.parse("{foo}", %{})
end
test "it returns an error when the string is invalid" do
assert {:error, "expected end of string"} = Parser.parse("{{ 1-1 }", %{})
end
test "it supports a custom fetcher function" do
custom_fetcher = fn _, _ ->
"quux"
end
assert {:ok, "quux"} = Parser.parse("{{ foo }}", %{}, custom_fetcher)
end
end
end