-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
307 additions
and
67 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Smee.Publish.FrontendUtils do | ||
|
||
@default_options [format: :saml, lang: "en", id_type: :hash, to: "published", labels: false] | ||
@allowed_options Keyword.keys(@default_options) ++ [:valid_until, :filename] | ||
|
||
@spec formats() :: list(atom()) | ||
def formats() do | ||
[ | ||
:csv, | ||
:disco, | ||
:index, | ||
:markdown, | ||
:saml, | ||
:thiss, | ||
:udest, | ||
:udisco | ||
] | ||
end | ||
|
||
def prepare_options(options) do | ||
Keyword.merge(@default_options, options) | ||
|> Keyword.take(@allowed_options) | ||
end | ||
|
||
def select_backend(options) do | ||
case options[:format] do | ||
:csv -> Smee.Publish.Csv | ||
:disco -> Smee.Publish.Disco | ||
:index -> Smee.Publish.Index | ||
:markdown -> Smee.Publish.Markdown | ||
:metadata -> Smee.Publish.SamlXml | ||
:saml -> Smee.Publish.SamlXml | ||
:thiss -> Smee.Publish.Thiss | ||
:udest -> Smee.Publish.Udest | ||
:udisco -> Smee.Publish.Udisco | ||
:default -> Smee.Publish.SamlXml | ||
nil -> Smee.Publish.SamlXml | ||
_ -> raise "Unknown publishing format ':#{options[:format]}' - known formats include #{Enum.join(formats(), ", :")}" | ||
end | ||
end | ||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
defmodule SmeePublishFrontendUtilsTest do | ||
use ExUnit.Case | ||
|
||
alias Smee.Publish.FrontendUtils | ||
alias Smee.Source | ||
#alias Smee.Metadata | ||
#alias Smee.Lint | ||
#alias Smee.XmlMunger | ||
|
||
@valid_metadata Source.new("test/support/static/aggregate.xml") | ||
|> Smee.fetch!() | ||
|
||
describe "formats/0" do | ||
|
||
test "returns a list of supported publishing formats" do | ||
assert [:csv, :disco, :index, :markdown, :saml, :thiss, :udest, :udisco] = FrontendUtils.formats() | ||
end | ||
|
||
test "does not return a list containing private formats" do | ||
refute Enum.member?(FrontendUtils.formats(), [:progress]) | ||
refute Enum.member?(FrontendUtils.formats(), [:string]) | ||
refute Enum.member?(FrontendUtils.formats(), [:null]) | ||
end | ||
|
||
end | ||
|
||
describe "prepare_options/1" do | ||
|
||
test "user options have a format of :saml by default" do | ||
assert :saml = Keyword.get(FrontendUtils.prepare_options([]), :format) | ||
end | ||
|
||
test "user options have a lang of 'en' by default" do | ||
assert "en" = Keyword.get(FrontendUtils.prepare_options([]), :lang) | ||
end | ||
|
||
test "user options have an id_type of :hash by default" do | ||
assert :hash = Keyword.get(FrontendUtils.prepare_options([]), :id_type) | ||
end | ||
|
||
test "user options have a default output path of './published' by default" do | ||
assert "published" = Keyword.get(FrontendUtils.prepare_options([]), :to) | ||
end | ||
|
||
test "user options have index labels turned off by default" do | ||
refute Keyword.get(FrontendUtils.prepare_options([]), :labels) | ||
end | ||
|
||
test "unknown option keys do not pass through" do | ||
refute Keyword.get(FrontendUtils.prepare_options([banana: "icecream"]), :banana) | ||
end | ||
|
||
end | ||
|
||
describe "select_backend/1" do | ||
|
||
test "known, supported format types return a Publish module name" do | ||
assert Smee.Publish.Csv = FrontendUtils.select_backend([format: :csv]) | ||
assert Smee.Publish.Disco = FrontendUtils.select_backend([format: :disco]) | ||
assert Smee.Publish.Index = FrontendUtils.select_backend([format: :index]) | ||
assert Smee.Publish.Markdown = FrontendUtils.select_backend([format: :markdown]) | ||
assert Smee.Publish.SamlXml = FrontendUtils.select_backend([format: :saml]) | ||
assert Smee.Publish.Thiss = FrontendUtils.select_backend([format: :thiss]) | ||
assert Smee.Publish.Udest = FrontendUtils.select_backend([format: :udest]) | ||
assert Smee.Publish.Udisco = FrontendUtils.select_backend([format: :udisco]) | ||
end | ||
|
||
test "there are various legacy aliases for the default format, SAML" do | ||
assert Smee.Publish.SamlXml = FrontendUtils.select_backend([format: :metadata]) | ||
assert Smee.Publish.SamlXml = FrontendUtils.select_backend([format: :nil]) | ||
assert Smee.Publish.SamlXml = FrontendUtils.select_backend([format: :default]) | ||
|
||
end | ||
|
||
test "Unknown format types cause an exception" do | ||
assert_raise RuntimeError, fn -> FrontendUtils.select_backend([format: :msword]) end | ||
end | ||
|
||
end | ||
|
||
end |
Oops, something went wrong.