Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring #26

Merged
merged 8 commits into from
Dec 11, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
checkpoint
  • Loading branch information
dogweather committed Dec 11, 2023
commit 7d23291c3ae24cb7c9e8e53b99f61a270f2ae8cd
26 changes: 17 additions & 9 deletions test/news/date_modified_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ defmodule News.DateModifiedTest do
doctest News.DateModified


@doc """
A helper that optimistically returns the value from an `{:ok, x}` tuple.
"""
def from_ok({:ok, x}), do: x


@yoast_schema_org """
{
"@context": "https://schema.org",
Expand Down Expand Up @@ -146,26 +152,28 @@ end
%{
html: "<html><script type='application/ld+json'>{\"dateBorn\": \"2020-01-01\"}</script></html>",
date: nil,
}
},
%{
html: "<html><script type='application/ld+json'>{\"datePublished\": \"2020-01-01\"}</script></html>",
date: "2020-01-01",
},
]

Enum.each(@test_cases, fn %{html: html, date: date} ->
test "finds the date in #{html}" do
{:ok, document} = unquote(html) |> Floki.parse_document
date = unquote(date)

assert News.DateModified.parse(document) == unquote(date)
if is_nil(date) do
assert News.DateModified.parse(document) == nil
else
assert News.DateModified.parse(document) == from_ok(Date.from_iso8601(date))
end
end
end)



test "parse/1 returns the date when there's just a datePublished" do
{:ok, document} = Floki.parse_document("<html><script type='application/ld+json'>{\"datePublished\": \"2020-01-01\"}</script></html>")

assert News.DateModified.parse(document) == ~D[2020-01-01]
end


test "parse/1 prefers dateModified over datePublished" do
{:ok, document} = Floki.parse_document("<html><script type='application/ld+json'>{\"datePublished\": \"2020-01-01\", \"dateModified\": \"2020-01-02\"}</script></html>")

Expand Down