Skip to content

Commit

Permalink
Fix compose output of extracted comments on plural messages (#133)
Browse files Browse the repository at this point in the history
Closes #132.
  • Loading branch information
mthiems committed Mar 26, 2024
1 parent dbcc3e2 commit 4d168bd
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/expo/po/composer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule Expo.PO.Composer do

[
dump_comments(t.comments, line_prefix),
dump_comments(t.extracted_comments, line_prefix),
dump_extracted_comments(t.extracted_comments, line_prefix),
dump_references(t.references, line_prefix),
dump_flags(t.flags, line_prefix),
dump_previous_messages(t.previous_messages, line_prefix),
Expand Down
23 changes: 23 additions & 0 deletions test/expo/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,29 @@ defmodule Expo.ParserTest do
msgstr[0] "bar"
""")
end

test "are associated with plural messages" do
assert [
%Message.Plural{
msgid: ["foo"],
msgid_plural: ["foos"],
msgstr: %{0 => ["bar"], 1 => ["bars"]},
comments: [" This is a message", " Ah, another comment!"],
extracted_comments: [" An extracted comment"],
references: [[{"lib/foo.ex", 32}]]
}
] =
parse("""
# This is a message
#: lib/foo.ex:32
# Ah, another comment!
#. An extracted comment
msgid "foo"
msgid_plural "foos"
msgstr[0] "bar"
msgstr[1] "bars"
""")
end
end

defp parse(string) do
Expand Down
127 changes: 127 additions & 0 deletions test/expo/po_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,110 @@ defmodule Expo.POTest do
"""
end

test "message with both comments and extracted comments" do
messages = %Messages{
headers: [],
messages: [
%Message.Singular{
msgid: ["foo"],
msgstr: ["bar"],
extracted_comments: [" some comment", " another extracted comment"],
comments: [" comment", " another comment"]
}
]
}

assert IO.iodata_to_binary(PO.compose(messages)) == ~S"""
# comment
# another comment
#. some comment
#. another extracted comment
msgid "foo"
msgstr "bar"
"""
end

test "plural message with comments" do
messages = %Messages{
headers: [],
messages: [
%Message.Plural{
msgid: ["one foo"],
msgid_plural: ["%{count} foos"],
msgstr: %{
0 => ["one bar"],
1 => ["%{count} bars"]
},
comments: [" comment", " another comment"]
}
]
}

assert IO.iodata_to_binary(PO.compose(messages)) == ~S"""
# comment
# another comment
msgid "one foo"
msgid_plural "%{count} foos"
msgstr[0] "one bar"
msgstr[1] "%{count} bars"
"""
end

test "plural message with extracted comments" do
messages = %Messages{
headers: [],
messages: [
%Message.Plural{
msgid: ["one foo"],
msgid_plural: ["%{count} foos"],
msgstr: %{
0 => ["one bar"],
1 => ["%{count} bars"]
},
extracted_comments: [" some comment", " another comment"]
}
]
}

assert IO.iodata_to_binary(PO.compose(messages)) == ~S"""
#. some comment
#. another comment
msgid "one foo"
msgid_plural "%{count} foos"
msgstr[0] "one bar"
msgstr[1] "%{count} bars"
"""
end

test "plural message with both comments and extracted comments" do
messages = %Messages{
headers: [],
messages: [
%Message.Plural{
msgid: ["one foo"],
msgid_plural: ["%{count} foos"],
msgstr: %{
0 => ["one bar"],
1 => ["%{count} bars"]
},
extracted_comments: [" some comment", " another extracted comment"],
comments: [" comment", " another comment"]
}
]
}

assert IO.iodata_to_binary(PO.compose(messages)) == ~S"""
# comment
# another comment
#. some comment
#. another extracted comment
msgid "one foo"
msgid_plural "%{count} foos"
msgstr[0] "one bar"
msgstr[1] "%{count} bars"
"""
end

test "references" do
messages = %Messages{
messages: [
Expand Down Expand Up @@ -622,6 +726,29 @@ defmodule Expo.POTest do
]
end

test "extracted comments are extracted into the :extracted_comments field of a plural message" do
assert {:ok, %Messages{messages: [%Message.Plural{} = message]}} =
PO.parse_string("""
#. Extracted comment
# Not an extracted comment
#.Another extracted comment
msgid "foo"
msgid_plural "foos"
msgstr[0] "bar"
msgstr[1] "bars"
""")

assert message.extracted_comments == [
" Extracted comment",
"Another extracted comment"
]

# All the reference comments are removed.
assert message.comments == [
" Not an extracted comment"
]
end

test "flags are extracted in to the :flags field of a message" do
assert {:ok, %Messages{messages: [%Message.Singular{} = message]}} =
PO.parse_string("""
Expand Down

0 comments on commit 4d168bd

Please sign in to comment.