accent/test/test_helper.exs
Simon Prévost ae0df57dae
Add basic support for XLIFF 1.2 file format (#79)
![image](https://user-images.githubusercontent.com/464900/55203015-35c17980-51a0-11e9-8647-d91209c7b6de.png)

## Issue
📚 https://github.com/mirego/accent/issues/21

## Feature
This is a basic implementation of the XLIFF 1.2 format. This format is used heavily in a lot of translations related tool (and in XCode) so I may have missed some of the implementation details. But it’s a good start for someone who want to contribute to Accent 😉 

## Refactor
This format is a bit trickier than other since it required the master language to export the targets. We needed to refactor some module to use the master language in the serialization process. Also, not needed but cleaner, we wrap the document’s key `top_of_the_file_comment` and `header` inside a new struct `Language.Document`. This will become handy if we ever need to add an attribute to the document OR if we add a different attribute to the serialization input.

## Next steps
This format includes 2 new dependencies to handle XML encode and decode. _Why not use the same XML library used for the XML Android format?_ Because… \*drum roll\* The `<source>` XML tag when encoded by the `mochiweb_html` module is a self closing tag (per the HTML spec) 🥇 

Since those 2 new deps are required for the XLIFF format and can pretty print XML, we should use them _instead of `mochiweb`_ 🎉
2019-03-31 16:16:20 -04:00

54 lines
1.4 KiB
Elixir

defmodule Accent.FormatterTestHelper do
def test_parse(variant, parser) do
context =
%Langue.Formatter.SerializerResult{
render: variant.render,
document: %Langue.Document{
path: "project-a",
master_language: "en",
top_of_the_file_comment: variant.top_of_the_file_comment,
header: variant.header
}
}
|> parser.parse()
{variant.entries, context.entries}
end
def test_serialize(variant, serializer, language \\ %Langue.Language{slug: "fr"}) do
context =
%Langue.Formatter.ParserResult{
entries: variant.entries,
language: language,
document: %Langue.Document{
path: "project-a",
master_language: "en",
top_of_the_file_comment: variant.top_of_the_file_comment,
header: variant.header
}
}
|> serializer.serialize()
{variant.render, context.render}
end
end
defmodule Langue.Expectation.Case do
defmacro __using__(_) do
quote do
def top_of_the_file_comment, do: ""
def header, do: ""
@callback header() :: binary()
@callback top_of_the_file_comment() :: binary()
@callback render() :: binary()
@callback entries() :: [Language.Entry.t()]
defoverridable top_of_the_file_comment: 0, header: 0
end
end
end
ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(Accent.Repo, :manual)