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

Improve i18n newlines test output #23218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
26 changes: 14 additions & 12 deletions spec/shared/i18n/newlines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ def add_string_chevrons(input)

shared_examples :newlines do |dir|
it "translations honor newlines" do
boundary_errors = {}
string_ends = {}
string_starts = {}
overall_errors = {}

Pathname.glob(File.join(dir, "**", "*.po")).each do |po_file|
Expand All @@ -17,12 +18,12 @@ def add_string_chevrons(input)

# Check newlines at string ends
if (translation[-1] == "\n" && original[-1] != "\n") || (translation[-1] != "\n" && original[-1] == "\n")
boundary_errors.store_path(po_file.to_s, add_string_chevrons(original), add_string_chevrons(translation))
string_ends.store_path(po_file.to_s, add_string_chevrons(original), add_string_chevrons(translation))
end

# Check newlines at string starts
if (translation[0] == "\n" && original[0] != "\n") || (translation[0] != "\n" && original[0] == "\n")
boundary_errors.store_path(po_file.to_s, add_string_chevrons(original), add_string_chevrons(translation))
string_starts.store_path(po_file.to_s, add_string_chevrons(original), add_string_chevrons(translation))
end

# Check that overall amount of newlines in original and translation matches
Expand All @@ -32,21 +33,22 @@ def add_string_chevrons(input)
end
end

[['newlines at string boundaries', boundary_errors], ['overall number of newlines', overall_errors]].each do |rule, err|
[
['no newlines at string ends', string_ends],
['no newlines at string start', string_starts],
['equal number of overall newlines', overall_errors]
].each do |rule, err|
next if err.empty?

puts "The following translation entries do not honor #{rule}:"
err_msg = ""
err.each do |file, file_errors|
puts ">> File: #{file}\n", "----------"
err_msg << "File: #{file}\n"
file_errors.each do |original, translation|
puts original, translation, "----------"
err_msg << " English:\n #{original.inspect}\n"
err_msg << " Translation:\n #{translation.inspect}\n"
end
puts
end
expect(err).to be_empty, "Rule: #{rule} was violated in:\n #{err_msg}"
end

expect(boundary_errors).to be_empty
# We intentionaly do not expect overall_errors to be empty, since in certain cases it may be desirable
# for newlines in translated string not to match its original. We'll still be logging the mismatches above.
end
end
Loading